From 80ab1be1bb6cf37d5f72b750c9bfc20acc144732 Mon Sep 17 00:00:00 2001 From: Lukas Stracke Date: Thu, 31 Mar 2022 11:08:52 +0200 Subject: [PATCH 1/8] ref(build): convert `postbuild.sh` to node script add `postbuild.ts` node script which can be run with ts-node to replace the bash script `postbuild.sh`. 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 make more adjustments that are harder to code/read in bash. * clone behaviour of `postbuild.sh` * add removal of `volta` and `scripts` properties in copied package.json * adjust calls to postbuild.sh to node script * adjust reference in comment to node script --- packages/browser/.npmignore | 2 +- packages/browser/package.json | 2 +- scripts/postbuild.sh | 40 ------------------------------ scripts/postbuild.ts | 46 +++++++++++++++++++++++++++++++++++ 4 files changed, 48 insertions(+), 42 deletions(-) delete mode 100644 scripts/postbuild.sh create mode 100644 scripts/postbuild.ts diff --git a/packages/browser/.npmignore b/packages/browser/.npmignore index 5c6d6b607beb..752f1102979e 100644 --- a/packages/browser/.npmignore +++ b/packages/browser/.npmignore @@ -1,6 +1,6 @@ # Info: the paths in this file are adjusted to match once this # file is copied to `./build`. This is done by a postbuild script -# located in sentry-javascript/scripts/postbuild.sh +# located in sentry-javascript/scripts/postbuild.ts * diff --git a/packages/browser/package.json b/packages/browser/package.json index da5acad0ed2c..d9d2b7691625 100644 --- a/packages/browser/package.json +++ b/packages/browser/package.json @@ -44,7 +44,7 @@ "webpack": "^4.30.0" }, "scripts": { - "build": "run-p build:cjs build:esm build:bundle build:types && bash ../../scripts/postbuild.sh", + "build": "run-p build:cjs build:esm build:bundle build:types && ts-node ../../scripts/postbuild.ts", "build:bundle": "rollup --config", "build:cjs": "tsc -p tsconfig.cjs.json", "build:dev": "run-p build:cjs build:esm build:types", diff --git a/scripts/postbuild.sh b/scripts/postbuild.sh deleted file mode 100644 index 423f36c076d8..000000000000 --- a/scripts/postbuild.sh +++ /dev/null @@ -1,40 +0,0 @@ -#!/bin/sh - -# This script prepares the `build` directory for NPM package creation. -# It first copies all non-code files into the `build` directory, including `package.json` which -# is edited via a few `sed` commands. These edits include corrections of paths (e.g. entry points) -# in the copied package.json so that they align with the directory structure inside `build`. - -BUILD_DIR=build - -ASSETS="README.md -LICENSE -package.json -.npmignore" - -# check if build dir exists -if [ ! -d $BUILD_DIR ]; then - echo "Directory ${BUILD_DIR}/ DOES NOT exist." - echo "This script should only be executed after you've run \`yarn build\`." - exit 1 -fi - -# copy non-code assets to build dir -for f in $ASSETS; do - cp $f $BUILD_DIR/ -done - -# package.json modifications - -# sed command to modify package.json entry points in build dir -# remove `BUILD_DIR` from `main`, `module` and `type` entry point paths -entryPointsCommand="/\"(main|module|types)\": .*,/s/$BUILD_DIR\///" - -# use empty backup file extension for sed in-place editing on MacOS and no backup for other platforms -if [[ $(uname) == "Darwin" ]]; then - sed -i "" -E "$entryPointsCommand" $BUILD_DIR/package.json -else - sed -i -E "$entryPointsCommand#" $BUILD_DIR/package.json -fi - -echo "Successfully finished postbuild commands" diff --git a/scripts/postbuild.ts b/scripts/postbuild.ts new file mode 100644 index 000000000000..cb6e54c5b6a8 --- /dev/null +++ b/scripts/postbuild.ts @@ -0,0 +1,46 @@ +/* + This script prepares the central `build` directory for NPM package creation. + It first copies all non-code files into the `build` directory, including `package.json` which + is edited to adjust entry point paths. These corrections are performed so that they align with + the directory structure inside `build`. +*/ + +import * as fs from 'fs'; + +const BUILD_DIR = 'build'; +const ASSETS = ['README.md', 'LICENSE', 'package.json', '.npmignore']; +const ENTRY_POINTS = ['main', 'module', 'types']; + +// check if build dirs exists +if (!fs.existsSync(BUILD_DIR)) { + console.error(`Directory ${BUILD_DIR} DOES NOT exist`); + console.error("This script should only be executed after you've run `yarn build`."); + process.exit(1); +} + +// copy non-code assets to build dir +ASSETS.forEach(asset => { + if (!fs.existsSync(asset)) { + console.error(`Asset ${asset} does not exist.`); + process.exit(1); + } + fs.copyFileSync(asset, `${BUILD_DIR}/${asset}`); +}); + +// package.json modifications +const packageJsonPath = `${process.cwd()}/${BUILD_DIR}/package.json`; +const pkg: { [key: string]: string } = require(packageJsonPath); + +// modify entry points to point to correct paths (i.e. delete the build directory) +ENTRY_POINTS.filter(entryPoint => !!pkg[entryPoint]).forEach(entryPoint => { + pkg[entryPoint] = pkg[entryPoint].replace(`${BUILD_DIR}/`, ''); +}); + +// TODO decide if we want this: +delete pkg.scripts; +delete pkg.volta; + +// write modified package.json to file +fs.writeFileSync(packageJsonPath, JSON.stringify(pkg, null, 2)); + +console.log('Successfully finishied postbuild commands'); From f020d56072938152dd1ba611d5bcb451209d635c Mon Sep 17 00:00:00 2001 From: Lukas Stracke Date: Thu, 31 Mar 2022 14:59:50 +0200 Subject: [PATCH 2/8] use `path.join()` instead of template strings --- scripts/postbuild.ts | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/scripts/postbuild.ts b/scripts/postbuild.ts index cb6e54c5b6a8..b8a59926a358 100644 --- a/scripts/postbuild.ts +++ b/scripts/postbuild.ts @@ -7,6 +7,8 @@ import * as fs from 'fs'; +import * as path from 'path'; + const BUILD_DIR = 'build'; const ASSETS = ['README.md', 'LICENSE', 'package.json', '.npmignore']; const ENTRY_POINTS = ['main', 'module', 'types']; @@ -24,11 +26,11 @@ ASSETS.forEach(asset => { console.error(`Asset ${asset} does not exist.`); process.exit(1); } - fs.copyFileSync(asset, `${BUILD_DIR}/${asset}`); + fs.copyFileSync(asset, path.join(BUILD_DIR, asset)); }); // package.json modifications -const packageJsonPath = `${process.cwd()}/${BUILD_DIR}/package.json`; +const packageJsonPath = path.join(process.cwd(), BUILD_DIR, 'package.json'); const pkg: { [key: string]: string } = require(packageJsonPath); // modify entry points to point to correct paths (i.e. delete the build directory) From 34b038adfad9f2805b919e836f918c09b8ac4ab7 Mon Sep 17 00:00:00 2001 From: Lukas Stracke Date: Thu, 31 Mar 2022 15:04:15 +0200 Subject: [PATCH 3/8] fix typo --- scripts/postbuild.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/postbuild.ts b/scripts/postbuild.ts index b8a59926a358..e82d935141b7 100644 --- a/scripts/postbuild.ts +++ b/scripts/postbuild.ts @@ -45,4 +45,4 @@ delete pkg.volta; // write modified package.json to file fs.writeFileSync(packageJsonPath, JSON.stringify(pkg, null, 2)); -console.log('Successfully finishied postbuild commands'); +console.log('Successfully finished postbuild commands'); From 6aa4ff6b992b1d43340a066b93ae63c8a79b26a6 Mon Sep 17 00:00:00 2001 From: Lukas Stracke Date: Fri, 1 Apr 2022 09:21:42 +0200 Subject: [PATCH 4/8] apply suggestions from code review * comment/spelling corrections * console output Co-authored-by: Katie Byers --- scripts/postbuild.ts | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/scripts/postbuild.ts b/scripts/postbuild.ts index e82d935141b7..e318c1099b73 100644 --- a/scripts/postbuild.ts +++ b/scripts/postbuild.ts @@ -1,7 +1,7 @@ /* This script prepares the central `build` directory for NPM package creation. - It first copies all non-code files into the `build` directory, including `package.json` which - is edited to adjust entry point paths. These corrections are performed so that they align with + It first copies all non-code files into the `build` directory, including `package.json`, which + is edited to adjust entry point paths. These corrections are performed so that the paths align with the directory structure inside `build`. */ @@ -13,7 +13,7 @@ const BUILD_DIR = 'build'; const ASSETS = ['README.md', 'LICENSE', 'package.json', '.npmignore']; const ENTRY_POINTS = ['main', 'module', 'types']; -// check if build dirs exists +// check if build dir exists if (!fs.existsSync(BUILD_DIR)) { console.error(`Directory ${BUILD_DIR} DOES NOT exist`); console.error("This script should only be executed after you've run `yarn build`."); @@ -33,7 +33,7 @@ ASSETS.forEach(asset => { const packageJsonPath = path.join(process.cwd(), BUILD_DIR, 'package.json'); const pkg: { [key: string]: string } = require(packageJsonPath); -// modify entry points to point to correct paths (i.e. delete the build directory) +// modify entry points to point to correct paths (i.e. strip out the build directory) ENTRY_POINTS.filter(entryPoint => !!pkg[entryPoint]).forEach(entryPoint => { pkg[entryPoint] = pkg[entryPoint].replace(`${BUILD_DIR}/`, ''); }); @@ -45,4 +45,4 @@ delete pkg.volta; // write modified package.json to file fs.writeFileSync(packageJsonPath, JSON.stringify(pkg, null, 2)); -console.log('Successfully finished postbuild commands'); +console.log(`\nSuccessfully finished postbuild commands for ${pkg.name}`); From d66c3d4da4e899154e9c88b9e3706565b647d893 Mon Sep 17 00:00:00 2001 From: Lukas Stracke Date: Fri, 1 Apr 2022 09:55:26 +0200 Subject: [PATCH 5/8] clarify `.npmignore` comment, improve variable naming --- packages/browser/.npmignore | 6 +++--- scripts/postbuild.ts | 16 ++++++++-------- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/packages/browser/.npmignore b/packages/browser/.npmignore index 752f1102979e..643a96e2e5f4 100644 --- a/packages/browser/.npmignore +++ b/packages/browser/.npmignore @@ -1,6 +1,6 @@ -# Info: the paths in this file are adjusted to match once this -# file is copied to `./build`. This is done by a postbuild script -# located in sentry-javascript/scripts/postbuild.ts +# Info: the paths in this file are specified so that they align with the file +# structure in `./build` where this file is copied to. This is done by the +# postbuild script `sentry-javascript/scripts/postbuild.ts`. * diff --git a/scripts/postbuild.ts b/scripts/postbuild.ts index e318c1099b73..c34ef384ec13 100644 --- a/scripts/postbuild.ts +++ b/scripts/postbuild.ts @@ -31,18 +31,18 @@ ASSETS.forEach(asset => { // package.json modifications const packageJsonPath = path.join(process.cwd(), BUILD_DIR, 'package.json'); -const pkg: { [key: string]: string } = require(packageJsonPath); +const pkgJson: { [key: string]: string } = require(packageJsonPath); // modify entry points to point to correct paths (i.e. strip out the build directory) -ENTRY_POINTS.filter(entryPoint => !!pkg[entryPoint]).forEach(entryPoint => { - pkg[entryPoint] = pkg[entryPoint].replace(`${BUILD_DIR}/`, ''); +ENTRY_POINTS.filter(entryPoint => pkgJson[entryPoint]).forEach(entryPoint => { + pkgJson[entryPoint] = pkgJson[entryPoint].replace(`${BUILD_DIR}/`, ''); }); // TODO decide if we want this: -delete pkg.scripts; -delete pkg.volta; +delete pkgJson.scripts; +delete pkgJson.volta; -// write modified package.json to file -fs.writeFileSync(packageJsonPath, JSON.stringify(pkg, null, 2)); +// write modified package.json to file (pretty-printed with 2 spaces) +fs.writeFileSync(packageJsonPath, JSON.stringify(pkgJson, null, 2)); -console.log(`\nSuccessfully finished postbuild commands for ${pkg.name}`); +console.log(`\nSuccessfully finished postbuild commands for ${pkgJson.name}`); From 312036d6a4a768a9164b245aaef383e0731739b3 Mon Sep 17 00:00:00 2001 From: Lukas Stracke Date: Fri, 1 Apr 2022 10:04:00 +0200 Subject: [PATCH 6/8] use `path.resolve()` instead of `path.join()` --- scripts/postbuild.ts | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/scripts/postbuild.ts b/scripts/postbuild.ts index c34ef384ec13..3c8ef8601974 100644 --- a/scripts/postbuild.ts +++ b/scripts/postbuild.ts @@ -14,7 +14,7 @@ const ASSETS = ['README.md', 'LICENSE', 'package.json', '.npmignore']; const ENTRY_POINTS = ['main', 'module', 'types']; // check if build dir exists -if (!fs.existsSync(BUILD_DIR)) { +if (!fs.existsSync(path.resolve(BUILD_DIR))) { console.error(`Directory ${BUILD_DIR} DOES NOT exist`); console.error("This script should only be executed after you've run `yarn build`."); process.exit(1); @@ -22,15 +22,16 @@ if (!fs.existsSync(BUILD_DIR)) { // copy non-code assets to build dir ASSETS.forEach(asset => { - if (!fs.existsSync(asset)) { + const assetPath = path.resolve(asset); + if (!fs.existsSync(assetPath)) { console.error(`Asset ${asset} does not exist.`); process.exit(1); } - fs.copyFileSync(asset, path.join(BUILD_DIR, asset)); + fs.copyFileSync(assetPath, path.resolve(BUILD_DIR, asset)); }); // package.json modifications -const packageJsonPath = path.join(process.cwd(), BUILD_DIR, 'package.json'); +const packageJsonPath = path.resolve(BUILD_DIR, 'package.json'); const pkgJson: { [key: string]: string } = require(packageJsonPath); // modify entry points to point to correct paths (i.e. strip out the build directory) From 38969e21d28f6abf8fefab00d76c65a1c0400afa Mon Sep 17 00:00:00 2001 From: Lukas Stracke Date: Mon, 4 Apr 2022 10:01:39 +0200 Subject: [PATCH 7/8] add try/catch blocks to fs operations as suggested by @mrbaguvix --- scripts/postbuild.ts | 30 +++++++++++++++++++++--------- 1 file changed, 21 insertions(+), 9 deletions(-) diff --git a/scripts/postbuild.ts b/scripts/postbuild.ts index 3c8ef8601974..bd93712d1ebc 100644 --- a/scripts/postbuild.ts +++ b/scripts/postbuild.ts @@ -14,20 +14,28 @@ const ASSETS = ['README.md', 'LICENSE', 'package.json', '.npmignore']; const ENTRY_POINTS = ['main', 'module', 'types']; // check if build dir exists -if (!fs.existsSync(path.resolve(BUILD_DIR))) { - console.error(`Directory ${BUILD_DIR} DOES NOT exist`); - console.error("This script should only be executed after you've run `yarn build`."); - process.exit(1); +try { + if (!fs.existsSync(path.resolve(BUILD_DIR))) { + console.error(`Directory ${BUILD_DIR} DOES NOT exist`); + console.error("This script should only be executed after you've run `yarn build`."); + process.exit(1); + } +} catch (error) { + console.error(`Error while looking up directory ${BUILD_DIR}`); } // copy non-code assets to build dir ASSETS.forEach(asset => { const assetPath = path.resolve(asset); - if (!fs.existsSync(assetPath)) { - console.error(`Asset ${asset} does not exist.`); - process.exit(1); + try { + if (!fs.existsSync(assetPath)) { + console.error(`Asset ${asset} does not exist.`); + process.exit(1); + } + fs.copyFileSync(assetPath, path.resolve(BUILD_DIR, asset)); + } catch (error) { + console.error(`Error while copying ${asset} to ${BUILD_DIR}`); } - fs.copyFileSync(assetPath, path.resolve(BUILD_DIR, asset)); }); // package.json modifications @@ -44,6 +52,10 @@ delete pkgJson.scripts; delete pkgJson.volta; // write modified package.json to file (pretty-printed with 2 spaces) -fs.writeFileSync(packageJsonPath, JSON.stringify(pkgJson, null, 2)); +try { + fs.writeFileSync(packageJsonPath, JSON.stringify(pkgJson, null, 2)); +} catch (error) { + console.error(`Error while writing package.json to disk`); +} console.log(`\nSuccessfully finished postbuild commands for ${pkgJson.name}`); From 686644a46ba8dfd2b2e08a3850f3247cc4d1ab94 Mon Sep 17 00:00:00 2001 From: Lukas Stracke Date: Mon, 4 Apr 2022 18:12:14 +0200 Subject: [PATCH 8/8] apply suggestions from code review --- scripts/postbuild.ts | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/scripts/postbuild.ts b/scripts/postbuild.ts index bd93712d1ebc..959569bec034 100644 --- a/scripts/postbuild.ts +++ b/scripts/postbuild.ts @@ -22,6 +22,7 @@ try { } } catch (error) { console.error(`Error while looking up directory ${BUILD_DIR}`); + process.exit(1); } // copy non-code assets to build dir @@ -35,19 +36,19 @@ ASSETS.forEach(asset => { fs.copyFileSync(assetPath, path.resolve(BUILD_DIR, asset)); } catch (error) { console.error(`Error while copying ${asset} to ${BUILD_DIR}`); + process.exit(1); } }); // package.json modifications const packageJsonPath = path.resolve(BUILD_DIR, 'package.json'); -const pkgJson: { [key: string]: string } = require(packageJsonPath); +const pkgJson: { [key: string]: unknown } = require(packageJsonPath); // modify entry points to point to correct paths (i.e. strip out the build directory) ENTRY_POINTS.filter(entryPoint => pkgJson[entryPoint]).forEach(entryPoint => { - pkgJson[entryPoint] = pkgJson[entryPoint].replace(`${BUILD_DIR}/`, ''); + pkgJson[entryPoint] = (pkgJson[entryPoint] as string).replace(`${BUILD_DIR}/`, ''); }); -// TODO decide if we want this: delete pkgJson.scripts; delete pkgJson.volta; @@ -56,6 +57,7 @@ try { fs.writeFileSync(packageJsonPath, JSON.stringify(pkgJson, null, 2)); } catch (error) { console.error(`Error while writing package.json to disk`); + process.exit(1); } console.log(`\nSuccessfully finished postbuild commands for ${pkgJson.name}`);