From 462a81490e17f96bd0e0b7cffae50ac09b42e806 Mon Sep 17 00:00:00 2001 From: Norbert de Langen Date: Tue, 29 Nov 2022 18:36:32 +0100 Subject: [PATCH] add a retry mechanism --- .../manager/scripts/generate-exports-file.ts | 57 ++++++++++++++----- code/ui/manager/src/globals/runtime.ts | 2 +- 2 files changed, 43 insertions(+), 16 deletions(-) diff --git a/code/ui/manager/scripts/generate-exports-file.ts b/code/ui/manager/scripts/generate-exports-file.ts index 2c38a0da6417..afdbea8ff6e6 100644 --- a/code/ui/manager/scripts/generate-exports-file.ts +++ b/code/ui/manager/scripts/generate-exports-file.ts @@ -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>((acc, [key, value]) => { @@ -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!'); }; diff --git a/code/ui/manager/src/globals/runtime.ts b/code/ui/manager/src/globals/runtime.ts index b09db0bdf2b7..10ab5452a976 100644 --- a/code/ui/manager/src/globals/runtime.ts +++ b/code/ui/manager/src/globals/runtime.ts @@ -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> = { - react: REACT, + react: REACT as any, 'react-dom': REACTDOM, '@storybook/components': STORYBOOKCOMPONENTS, '@storybook/channels': STORYBOOKCHANNELS,