create-app: add a yarn.lock seed file

Signed-off-by: Patrik Oldsberg <poldsberg@gmail.com>
This commit is contained in:
Patrik Oldsberg
2024-02-02 11:50:54 +01:00
parent 6a5702f087
commit c420081a20
6 changed files with 171 additions and 1 deletions
+17
View File
@@ -30,6 +30,7 @@ import {
templatingTask,
tryInitGitRepository,
readGitConfig,
fetchYarnLockSeedTask,
} from './lib/tasks';
const DEFAULT_BRANCH = 'master';
@@ -110,6 +111,8 @@ export default async (opts: OptionValues): Promise<void> => {
await moveAppTask(tempDir, appDir, answers.name);
}
const fetchedYarnLockSeed = await fetchYarnLockSeedTask(appDir);
if (gitConfig) {
if (await tryInitGitRepository(appDir)) {
// Since we don't know whether we were able to init git before we
@@ -128,6 +131,20 @@ export default async (opts: OptionValues): Promise<void> => {
chalk.green(`🥇 Successfully created ${chalk.cyan(answers.name)}`),
);
Task.log();
if (!fetchedYarnLockSeed) {
Task.log(
chalk.yellow(
[
'Warning: Failed to fetch the yarn.lock seed file.',
' You may end up with incompatible dependencies that break the app.',
' If you run into any errors, please search the issues at',
' https://github.com/backstage/backstage/issues for potential solutions',
].join('\n'),
),
);
}
Task.section('All set! Now you might want to');
if (opts.skipInstall) {
Task.log(
+42
View File
@@ -304,3 +304,45 @@ export async function tryInitGitRepository(dir: string) {
return false;
}
}
/**
* This fetches the yarn.lock seed file at https://github.com/backstage/backstage/blob/master/packages/create-app/seed-yarn.lock
* Its purpose is to lock individual dependencies with broken releases to known working versions.
* This flow is decoupled from the release of the create-app package in order to avoid
* the need to re-publish the create-app package whenever we want to update the seed file.
*
* @returns true if the yarn.lock seed file was fetched successfully
*/
export async function fetchYarnLockSeedTask(dir: string) {
try {
await Task.forItem('fetching', 'yarn.lock seed', async () => {
const controller = new AbortController();
setTimeout(() => controller.abort(), 5000);
const res = await fetch(
'https://raw.githubusercontent.com/backstage/backstage/master/packages/create-app/seed-yarn.lock',
{
signal: controller.signal,
},
);
if (!res.ok) {
throw new Error(
`Request failed with status ${res.status} ${res.statusText}`,
);
}
const initialYarnLockContent = await res.text();
await fs.writeFile(
resolvePath(dir, 'yarn.lock'),
initialYarnLockContent
.split('\n')
.filter(l => !l.startsWith('//'))
.join('\n'),
'utf8',
);
});
return true;
} catch {
return false;
}
}