Skip to content

ref(build): Convert postbuild.sh to node script #4828

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Apr 5, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/browser/.npmignore
Original file line number Diff line number Diff line change
@@ -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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It won't let me do an official "suggestion", but:

# Info: the paths in this file are adjusted appropriately once this
# file is copied to `./build`. This is done by the postbuild script
# sentry-javascript/scripts/postbuild.ts.

Saves having to answer the question "match what?".

Copy link
Member Author

@Lms24 Lms24 Apr 1, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the suggestion - this made me realize that the comment is slightly misleading. My initial comment makes it appear as if postbuild actively modifies the paths in .npmignore, which is not the case. I'll clarify that postbuild only copies this file and the paths are specified as if the file was already located in build.


*

Expand Down
2 changes: 1 addition & 1 deletion packages/browser/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
40 changes: 0 additions & 40 deletions scripts/postbuild.sh

This file was deleted.

48 changes: 48 additions & 0 deletions scripts/postbuild.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
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';

import * as path from 'path';

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, path.join(BUILD_DIR, asset));
});

// package.json modifications
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)
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 finished postbuild commands');