Skip to content

Commit a064c7c

Browse files
Lms24lobsterkatie
andauthored
ref(build): Convert postbuild.sh to node script (#4828)
Replace the bash script `postbuild.sh` with the node script `postbuild.ts` which can be run with `ts-node`. This is done to conform with our decision to change all bash scripts to node s.t. they can be run on any platform (incl. Windows). Furthermore, it gives us the flexibility to add additional functionality in the future that is harder to code/read in bash. Co-authored-by: Katie Byers <[email protected]>
1 parent 334b097 commit a064c7c

File tree

4 files changed

+67
-44
lines changed

4 files changed

+67
-44
lines changed

packages/browser/.npmignore

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
# Info: the paths in this file are adjusted to match once this
2-
# file is copied to `./build`. This is done by a postbuild script
3-
# located in sentry-javascript/scripts/postbuild.sh
1+
# Info: the paths in this file are specified so that they align with the file
2+
# structure in `./build` where this file is copied to. This is done by the
3+
# postbuild script `sentry-javascript/scripts/postbuild.ts`.
44

55
*
66

packages/browser/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
"webpack": "^4.30.0"
4545
},
4646
"scripts": {
47-
"build": "run-p build:cjs build:esm build:bundle build:types && bash ../../scripts/postbuild.sh",
47+
"build": "run-p build:cjs build:esm build:bundle build:types && ts-node ../../scripts/postbuild.ts",
4848
"build:bundle": "rollup --config",
4949
"build:cjs": "tsc -p tsconfig.cjs.json",
5050
"build:dev": "run-p build:cjs build:esm build:types",

scripts/postbuild.sh

Lines changed: 0 additions & 40 deletions
This file was deleted.

scripts/postbuild.ts

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
This script prepares the central `build` directory for NPM package creation.
3+
It first copies all non-code files into the `build` directory, including `package.json`, which
4+
is edited to adjust entry point paths. These corrections are performed so that the paths align with
5+
the directory structure inside `build`.
6+
*/
7+
8+
import * as fs from 'fs';
9+
10+
import * as path from 'path';
11+
12+
const BUILD_DIR = 'build';
13+
const ASSETS = ['README.md', 'LICENSE', 'package.json', '.npmignore'];
14+
const ENTRY_POINTS = ['main', 'module', 'types'];
15+
16+
// check if build dir exists
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}`);
25+
process.exit(1);
26+
}
27+
28+
// copy non-code assets to build dir
29+
ASSETS.forEach(asset => {
30+
const assetPath = path.resolve(asset);
31+
try {
32+
if (!fs.existsSync(assetPath)) {
33+
console.error(`Asset ${asset} does not exist.`);
34+
process.exit(1);
35+
}
36+
fs.copyFileSync(assetPath, path.resolve(BUILD_DIR, asset));
37+
} catch (error) {
38+
console.error(`Error while copying ${asset} to ${BUILD_DIR}`);
39+
process.exit(1);
40+
}
41+
});
42+
43+
// package.json modifications
44+
const packageJsonPath = path.resolve(BUILD_DIR, 'package.json');
45+
const pkgJson: { [key: string]: unknown } = require(packageJsonPath);
46+
47+
// modify entry points to point to correct paths (i.e. strip out the build directory)
48+
ENTRY_POINTS.filter(entryPoint => pkgJson[entryPoint]).forEach(entryPoint => {
49+
pkgJson[entryPoint] = (pkgJson[entryPoint] as string).replace(`${BUILD_DIR}/`, '');
50+
});
51+
52+
delete pkgJson.scripts;
53+
delete pkgJson.volta;
54+
55+
// write modified package.json to file (pretty-printed with 2 spaces)
56+
try {
57+
fs.writeFileSync(packageJsonPath, JSON.stringify(pkgJson, null, 2));
58+
} catch (error) {
59+
console.error(`Error while writing package.json to disk`);
60+
process.exit(1);
61+
}
62+
63+
console.log(`\nSuccessfully finished postbuild commands for ${pkgJson.name}`);

0 commit comments

Comments
 (0)