Skip to content

Commit 38969e2

Browse files
committed
add try/catch blocks to fs operations
as suggested by @mrbaguvix
1 parent 312036d commit 38969e2

File tree

1 file changed

+21
-9
lines changed

1 file changed

+21
-9
lines changed

scripts/postbuild.ts

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,28 @@ const ASSETS = ['README.md', 'LICENSE', 'package.json', '.npmignore'];
1414
const ENTRY_POINTS = ['main', 'module', 'types'];
1515

1616
// check if build dir exists
17-
if (!fs.existsSync(path.resolve(BUILD_DIR))) {
18-
console.error(`Directory ${BUILD_DIR} DOES NOT exist`);
19-
console.error("This script should only be executed after you've run `yarn build`.");
20-
process.exit(1);
17+
try {
18+
if (!fs.existsSync(path.resolve(BUILD_DIR))) {
19+
console.error(`Directory ${BUILD_DIR} DOES NOT exist`);
20+
console.error("This script should only be executed after you've run `yarn build`.");
21+
process.exit(1);
22+
}
23+
} catch (error) {
24+
console.error(`Error while looking up directory ${BUILD_DIR}`);
2125
}
2226

2327
// copy non-code assets to build dir
2428
ASSETS.forEach(asset => {
2529
const assetPath = path.resolve(asset);
26-
if (!fs.existsSync(assetPath)) {
27-
console.error(`Asset ${asset} does not exist.`);
28-
process.exit(1);
30+
try {
31+
if (!fs.existsSync(assetPath)) {
32+
console.error(`Asset ${asset} does not exist.`);
33+
process.exit(1);
34+
}
35+
fs.copyFileSync(assetPath, path.resolve(BUILD_DIR, asset));
36+
} catch (error) {
37+
console.error(`Error while copying ${asset} to ${BUILD_DIR}`);
2938
}
30-
fs.copyFileSync(assetPath, path.resolve(BUILD_DIR, asset));
3139
});
3240

3341
// package.json modifications
@@ -44,6 +52,10 @@ delete pkgJson.scripts;
4452
delete pkgJson.volta;
4553

4654
// write modified package.json to file (pretty-printed with 2 spaces)
47-
fs.writeFileSync(packageJsonPath, JSON.stringify(pkgJson, null, 2));
55+
try {
56+
fs.writeFileSync(packageJsonPath, JSON.stringify(pkgJson, null, 2));
57+
} catch (error) {
58+
console.error(`Error while writing package.json to disk`);
59+
}
4860

4961
console.log(`\nSuccessfully finished postbuild commands for ${pkgJson.name}`);

0 commit comments

Comments
 (0)