Skip to content

Commit 6b3ea05

Browse files
authored
ref(build): Improve logging in prepack scripts (#5071)
This improves logging in our prepack scripts, primarily by adding newlines and an `ERROR` prefix (to make errors easier to spot in a long stream of logs) and by logging the actual error when something goes wrong (rather than just logging that something is wrong without saying _what_ is wrong).
1 parent 7723d32 commit 6b3ea05

File tree

2 files changed

+36
-36
lines changed

2 files changed

+36
-36
lines changed

packages/gatsby/scripts/prepack.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,19 @@ export function prepack(buildDir: string): boolean {
1212
// copy package-specific assets to build dir
1313
return PACKAGE_ASSETS.every(asset => {
1414
const assetPath = path.resolve(asset);
15+
const destinationPath = path.resolve(buildDir, asset);
1516
try {
1617
if (!fs.existsSync(assetPath)) {
17-
console.error(`Asset ${asset} does not exist.`);
18+
console.error(`\nERROR: Asset '${asset}' does not exist.`);
1819
return false;
1920
}
20-
fs.copyFileSync(assetPath, path.resolve(buildDir, asset));
21+
console.log(`Copying ${path.basename(asset)} to ${path.relative('../..', destinationPath)}.`);
22+
fs.copyFileSync(assetPath, destinationPath);
2123
} catch (error) {
22-
console.error(`Error while copying ${asset} to ${buildDir}`);
24+
console.error(
25+
`\nERROR: Error while copying ${path.basename(asset)} to ${path.relative('../..', destinationPath)}:\n`,
26+
error,
27+
);
2328
return false;
2429
}
2530
return true;

scripts/prepack.ts

Lines changed: 28 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -19,54 +19,47 @@ const ENTRY_POINTS = ['main', 'module', 'types', 'browser'];
1919
const packageWithBundles = process.argv.includes('--bundles');
2020
const buildDir = packageWithBundles ? NPM_BUILD_DIR : BUILD_DIR;
2121

22+
// eslint-disable-next-line @typescript-eslint/no-var-requires
23+
const pkgJson: { [key: string]: unknown } = require(path.resolve('package.json'));
24+
2225
// check if build dir exists
23-
try {
24-
if (!fs.existsSync(path.resolve(buildDir))) {
25-
console.error(`Directory ${buildDir} DOES NOT exist`);
26-
console.error("This script should only be executed after you've run `yarn build`.");
27-
process.exit(1);
28-
}
29-
} catch (error) {
30-
console.error(`Error while looking up directory ${buildDir}`);
26+
if (!fs.existsSync(path.resolve(buildDir))) {
27+
console.error(`\nERROR: Directory '${buildDir}' does not exist in ${pkgJson.name}.`);
28+
console.error("This script should only be executed after you've run `yarn build`.");
3129
process.exit(1);
3230
}
3331

3432
// copy non-code assets to build dir
3533
ASSETS.forEach(asset => {
3634
const assetPath = path.resolve(asset);
37-
try {
38-
if (!fs.existsSync(assetPath)) {
39-
console.error(`Asset ${asset} does not exist.`);
40-
process.exit(1);
41-
}
42-
const destinationPath = path.resolve(buildDir, path.basename(asset));
43-
console.log(`Copying ${path.basename(asset)} to ${path.relative('../..', destinationPath)}.`);
44-
fs.copyFileSync(assetPath, destinationPath);
45-
} catch (error) {
46-
console.error(`Error while copying ${asset} to ${buildDir}`);
35+
if (!fs.existsSync(assetPath)) {
36+
console.error(`\nERROR: Asset '${asset}' does not exist.`);
4737
process.exit(1);
4838
}
39+
const destinationPath = path.resolve(buildDir, path.basename(asset));
40+
console.log(`Copying ${path.basename(asset)} to ${path.relative('../..', destinationPath)}.`);
41+
fs.copyFileSync(assetPath, destinationPath);
4942
});
5043

5144
// package.json modifications
52-
const packageJsonPath = path.resolve(buildDir, 'package.json');
45+
const newPackageJsonPath = path.resolve(buildDir, 'package.json');
5346
// eslint-disable-next-line @typescript-eslint/no-var-requires
54-
const pkgJson: { [key: string]: unknown } = require(packageJsonPath);
47+
const newPkgJson: { [key: string]: unknown } = require(newPackageJsonPath);
5548

5649
// modify entry points to point to correct paths (i.e. strip out the build directory)
57-
ENTRY_POINTS.filter(entryPoint => pkgJson[entryPoint]).forEach(entryPoint => {
58-
pkgJson[entryPoint] = (pkgJson[entryPoint] as string).replace(`${buildDir}/`, '');
50+
ENTRY_POINTS.filter(entryPoint => newPkgJson[entryPoint]).forEach(entryPoint => {
51+
newPkgJson[entryPoint] = (newPkgJson[entryPoint] as string).replace(`${buildDir}/`, '');
5952
});
6053

61-
delete pkgJson.scripts;
62-
delete pkgJson.volta;
63-
delete pkgJson.jest;
54+
delete newPkgJson.scripts;
55+
delete newPkgJson.volta;
56+
delete newPkgJson.jest;
6457

6558
// write modified package.json to file (pretty-printed with 2 spaces)
6659
try {
67-
fs.writeFileSync(packageJsonPath, JSON.stringify(pkgJson, null, 2));
60+
fs.writeFileSync(newPackageJsonPath, JSON.stringify(newPkgJson, null, 2));
6861
} catch (error) {
69-
console.error('Error while writing package.json to disk');
62+
console.error(`\nERROR: Error while writing modified 'package.json' to disk in ${pkgJson.name}:\n`, error);
7063
process.exit(1);
7164
}
7265

@@ -78,26 +71,28 @@ async function runPackagePrepack(packagePrepackPath: string): Promise<void> {
7871
process.exit(1);
7972
}
8073
} else {
81-
console.error(`Could not find a prepack function in ${packagePrepackPath}.`);
74+
console.error(`\nERROR: Could not find a \`prepack\` function in './scripts/prepack.ts' in ${pkgJson.name}.`);
8275
console.error(
83-
'Make sure, your package-specific prepack script exports `function prepack(buildDir: string): boolean`.',
76+
'Make sure your package-specific prepack script exports `function prepack(buildDir: string): boolean`.',
8477
);
8578
process.exit(1);
8679
}
8780
}
8881

8982
// execute package specific settings
90-
// 1. check if a package called `<package-root>/scripts/prepack.ts` exitsts
83+
// 1. check if a script called `<package-root>/scripts/prepack.ts` exists
9184
// if yes, 2.) execute that script for things that are package-specific
92-
void (async () => {
85+
async function runPackageSpecificScripts(): Promise<void> {
9386
const packagePrepackPath = path.resolve('scripts', 'prepack.ts');
9487
try {
9588
if (fs.existsSync(packagePrepackPath)) {
9689
await runPackagePrepack(packagePrepackPath);
9790
}
9891
} catch (error) {
99-
console.error(`Error while trying to access ${packagePrepackPath.toString()}`);
92+
console.error(`\nERROR: Error while trying to load and run ./scripts/prepack.ts in ${pkgJson.name}:\n`, error);
10093
process.exit(1);
10194
}
10295
console.log(`\nSuccessfully finished prepack commands for ${pkgJson.name}\n`);
103-
})();
96+
}
97+
98+
void runPackageSpecificScripts();

0 commit comments

Comments
 (0)