Skip to content

Commit

Permalink
Merge pull request #20018 from storybookjs/fix/exports-generator-inco…
Browse files Browse the repository at this point in the history
…nsistency

Build: Add a retry mechanism for exports file generation
  • Loading branch information
ndelangen authored Nov 30, 2022
2 parents ca21523 + 462a814 commit a7b54ca
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 16 deletions.
57 changes: 42 additions & 15 deletions code/ui/manager/scripts/generate-exports-file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,28 @@ import { dedent } from 'ts-dedent';
import { ESLint } from '../../../../scripts/node_modules/eslint';
import { values } from '../src/globals/runtime';

const removeDefault = (input: string) => input !== 'default';

const location = path.join(__dirname, '..', 'src', 'globals', 'exports.ts');
let attempts = 0;

function removeDefault(input: string) {
return input !== 'default';
}

const wait = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms));

async function generate(text: string) {
console.log('Linting...');

const eslint = new ESLint({
cwd: path.join(__dirname, '..'),
fix: true,
});
const output = await eslint.lintText(text, { filePath: location });

console.log('Writing...');

await fs.writeFile(location, output[0].output);
}

const run = async () => {
const data = Object.entries(values).reduce<Record<string, string[]>>((acc, [key, value]) => {
Expand All @@ -16,24 +35,32 @@ const run = async () => {
}, {});

console.log('Generating...');
const readyToWrite = fs.ensureFile(location);

const text = dedent`
// 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 manager builder needs to know which dependencies are 'globalized' in the ui
// 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 manager builder needs to know which dependencies are 'globalized' in the ui
export default ${JSON.stringify(data, null, 2)} as const;`;

export default ${JSON.stringify(data, null, 2)} as const;`;
await fs.ensureFile(location);

console.log('Linting...');
const tryGenerate = async () => {
attempts += 1;

const eslint = new ESLint({
cwd: path.join(__dirname, '..'),
fix: true,
});
const output = await eslint.lintText(text, { filePath: location });
await generate(text).catch(async (e) => {
if (attempts > 5) {
throw e;
}

await readyToWrite;
await fs.writeFile(location, output[0].output);
console.log('Retrying...');

await wait(1000);
await tryGenerate();
});
};

await tryGenerate();

console.log('Done!');
};
Expand Down
2 changes: 1 addition & 1 deletion code/ui/manager/src/globals/runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import type { Keys } from './types';

// Here we map the name of a module to their VALUE in the global scope.
export const values: Required<Record<keyof typeof Keys, any>> = {
react: REACT,
react: REACT as any,
'react-dom': REACTDOM,
'@storybook/components': STORYBOOKCOMPONENTS,
'@storybook/channels': STORYBOOKCHANNELS,
Expand Down

0 comments on commit a7b54ca

Please sign in to comment.