/* eslint-disable no-console */ import fs from 'fs-extra'; import promiseFs from 'fs/promises'; import path from 'path'; import shelljs from 'shelljs'; // populate global scope import '../src/index'; const outFile = path.join(__dirname, '..', 'src', 'generated', 'exports.ts'); const globalsFolder = path.join(__dirname, '..', 'src', 'global'); const removeDefault = (input: string) => input !== 'default'; const fileToPackage = (filename) => `@storybook/${filename.replace(/.ts$/, '')}`; /** * Iterate over the files in /src/global, and save off the names of their exports into a `exports.ts` file. */ const run = async () => { console.log('Generating exports.ts...'); const exports = {}; const globalFiles = (await promiseFs.readdir(globalsFolder, { withFileTypes: true })) .filter((item) => !item.isDirectory()) .map((item) => item.name); const globalPackageNames = globalFiles.map(fileToPackage); await Promise.all( globalFiles.map(async (name) => { const filename = path.join(globalsFolder, name); return import(filename).then((mod) => { exports[fileToPackage(name)] = Object.keys(mod).filter(removeDefault); }); }) ); await fs.ensureFile(outFile); await fs.writeFile( outFile, ` // this file is generated by generate-exports-file.ts // this is done to prevent runtime dependencies from making it's way into the build/start script of the manager // the preview builder needs to know which dependencies are 'globalized' in the ui export type GlobalPackages = ${globalPackageNames.map((name) => JSON.stringify(name)).join(' | ')} export default ${JSON.stringify(exports, null, 2)} as const;` ); console.log('Linting...'); shelljs.exec(`yarn lint:js:cmd --fix ${outFile}`, { cwd: path.join(__dirname, '..', '..', '..'), }); console.log('Done!'); }; run().catch((e) => { console.error(e); process.exitCode = 1; });