Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Expose cosmos.fixtures.json with renderer URLs (#1629)
- Loading branch information
Showing
12 changed files
with
167 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
packages/react-cosmos-core/src/fixtureTree/fixtureUtils.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
export function removeFixtureNameExtension(fixtureName: string) { | ||
return fixtureName.replace(/\.(js|jsx|ts|tsx|md|mdx)$/, ''); | ||
} | ||
|
||
export function removeFixtureNameSuffix( | ||
fixtureNameWithoutExtension: string, | ||
suffix: string | ||
) { | ||
return fixtureNameWithoutExtension.replace(new RegExp(`\\.${suffix}$`), ''); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
91 changes: 91 additions & 0 deletions
91
packages/react-cosmos/src/corePlugins/fixturesJsonPlugin.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
import express from 'express'; | ||
import fs from 'node:fs/promises'; | ||
import path from 'node:path'; | ||
import { | ||
CosmosCommand, | ||
createRendererUrl, | ||
pickRendererUrl, | ||
removeFixtureNameExtension, | ||
removeFixtureNameSuffix, | ||
} from 'react-cosmos-core'; | ||
import { CosmosConfig } from '../cosmosConfig/types.js'; | ||
import { CosmosServerPlugin } from '../cosmosPlugin/types.js'; | ||
import { findUserModulePaths } from '../userModules/findUserModulePaths.js'; | ||
import { importKeyPath } from '../userModules/shared.js'; | ||
|
||
export type CosmosFixtureJson = { | ||
filePath: string; | ||
cleanPath: string[]; | ||
rendererUrl: string; | ||
}; | ||
|
||
export type CosmosFixturesJson = { | ||
rendererUrl: string | null; | ||
fixtures: CosmosFixtureJson[]; | ||
}; | ||
|
||
export const fixturesJsonPlugin: CosmosServerPlugin = { | ||
name: 'fixturesJson', | ||
|
||
devServer({ cosmosConfig, expressApp }) { | ||
expressApp.get( | ||
'/cosmos.fixtures.json', | ||
(req: express.Request, res: express.Response) => { | ||
res.json(createFixtureItems(cosmosConfig, 'dev')); | ||
} | ||
); | ||
}, | ||
|
||
async export({ cosmosConfig }) { | ||
const { exportPath } = cosmosConfig; | ||
const json = createFixtureItems(cosmosConfig, 'export'); | ||
await fs.writeFile( | ||
path.join(exportPath, 'cosmos.fixtures.json'), | ||
JSON.stringify(json, null, 2) | ||
); | ||
}, | ||
}; | ||
|
||
function createFixtureItems( | ||
cosmosConfig: CosmosConfig, | ||
command: CosmosCommand | ||
): CosmosFixturesJson { | ||
const rendererUrl = pickRendererUrl(cosmosConfig.rendererUrl, command); | ||
if (!rendererUrl) { | ||
return { | ||
rendererUrl: null, | ||
fixtures: [], | ||
}; | ||
} | ||
|
||
const { fixturesDir, fixtureFileSuffix } = cosmosConfig; | ||
const { fixturePaths } = findUserModulePaths(cosmosConfig); | ||
|
||
return { | ||
rendererUrl, | ||
fixtures: fixturePaths.map(filePath => { | ||
const relPath = importKeyPath(filePath, cosmosConfig.rootDir); | ||
const fixtureId = { path: relPath }; | ||
return { | ||
filePath: relPath, | ||
cleanPath: cleanFixturePath(relPath, fixturesDir, fixtureFileSuffix), | ||
rendererUrl: createRendererUrl(rendererUrl, fixtureId, true), | ||
}; | ||
}), | ||
}; | ||
} | ||
|
||
function cleanFixturePath( | ||
filePath: string, | ||
fixturesDir: string, | ||
fixtureSuffix: string | ||
) { | ||
const paths = filePath.split('/').filter(p => p !== fixturesDir); | ||
return [ | ||
...paths.slice(0, -1), | ||
removeFixtureNameSuffix( | ||
removeFixtureNameExtension(paths[paths.length - 1]), | ||
fixtureSuffix | ||
), | ||
]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters