|
| 1 | +import express from 'express'; |
| 2 | +import fs from 'node:fs/promises'; |
| 3 | +import path from 'node:path'; |
| 4 | +import { |
| 5 | + CosmosCommand, |
| 6 | + createRendererUrl, |
| 7 | + pickRendererUrl, |
| 8 | + removeFixtureNameExtension, |
| 9 | + removeFixtureNameSuffix, |
| 10 | +} from 'react-cosmos-core'; |
| 11 | +import { CosmosConfig } from '../cosmosConfig/types.js'; |
| 12 | +import { CosmosServerPlugin } from '../cosmosPlugin/types.js'; |
| 13 | +import { findUserModulePaths } from '../userModules/findUserModulePaths.js'; |
| 14 | +import { importKeyPath } from '../userModules/shared.js'; |
| 15 | + |
| 16 | +export type CosmosFixtureJson = { |
| 17 | + filePath: string; |
| 18 | + cleanPath: string[]; |
| 19 | + rendererUrl: string; |
| 20 | +}; |
| 21 | + |
| 22 | +export type CosmosFixturesJson = { |
| 23 | + rendererUrl: string | null; |
| 24 | + fixtures: CosmosFixtureJson[]; |
| 25 | +}; |
| 26 | + |
| 27 | +export const fixturesJsonPlugin: CosmosServerPlugin = { |
| 28 | + name: 'fixturesJson', |
| 29 | + |
| 30 | + devServer({ cosmosConfig, expressApp }) { |
| 31 | + expressApp.get( |
| 32 | + '/cosmos.fixtures.json', |
| 33 | + (req: express.Request, res: express.Response) => { |
| 34 | + res.json(createFixtureItems(cosmosConfig, 'dev')); |
| 35 | + } |
| 36 | + ); |
| 37 | + }, |
| 38 | + |
| 39 | + async export({ cosmosConfig }) { |
| 40 | + const { exportPath } = cosmosConfig; |
| 41 | + const json = createFixtureItems(cosmosConfig, 'export'); |
| 42 | + await fs.writeFile( |
| 43 | + path.join(exportPath, 'cosmos.fixtures.json'), |
| 44 | + JSON.stringify(json, null, 2) |
| 45 | + ); |
| 46 | + }, |
| 47 | +}; |
| 48 | + |
| 49 | +function createFixtureItems( |
| 50 | + cosmosConfig: CosmosConfig, |
| 51 | + command: CosmosCommand |
| 52 | +): CosmosFixturesJson { |
| 53 | + const rendererUrl = pickRendererUrl(cosmosConfig.rendererUrl, command); |
| 54 | + if (!rendererUrl) { |
| 55 | + return { |
| 56 | + rendererUrl: null, |
| 57 | + fixtures: [], |
| 58 | + }; |
| 59 | + } |
| 60 | + |
| 61 | + const { fixturesDir, fixtureFileSuffix } = cosmosConfig; |
| 62 | + const { fixturePaths } = findUserModulePaths(cosmosConfig); |
| 63 | + |
| 64 | + return { |
| 65 | + rendererUrl, |
| 66 | + fixtures: fixturePaths.map(filePath => { |
| 67 | + const relPath = importKeyPath(filePath, cosmosConfig.rootDir); |
| 68 | + const fixtureId = { path: relPath }; |
| 69 | + return { |
| 70 | + filePath: relPath, |
| 71 | + cleanPath: cleanFixturePath(relPath, fixturesDir, fixtureFileSuffix), |
| 72 | + rendererUrl: createRendererUrl(rendererUrl, fixtureId, true), |
| 73 | + }; |
| 74 | + }), |
| 75 | + }; |
| 76 | +} |
| 77 | + |
| 78 | +function cleanFixturePath( |
| 79 | + filePath: string, |
| 80 | + fixturesDir: string, |
| 81 | + fixtureSuffix: string |
| 82 | +) { |
| 83 | + const paths = filePath.split('/').filter(p => p !== fixturesDir); |
| 84 | + return [ |
| 85 | + ...paths.slice(0, -1), |
| 86 | + removeFixtureNameSuffix( |
| 87 | + removeFixtureNameExtension(paths[paths.length - 1]), |
| 88 | + fixtureSuffix |
| 89 | + ), |
| 90 | + ]; |
| 91 | +} |
0 commit comments