-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
Changes from 3 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
80ab1be
ref(build): convert `postbuild.sh` to node script
Lms24 f020d56
use `path.join()` instead of template strings
Lms24 34b038a
fix typo
Lms24 6aa4ff6
apply suggestions from code review
Lms24 d66c3d4
clarify `.npmignore` comment, improve variable naming
Lms24 312036d
use `path.resolve()` instead of `path.join()`
Lms24 38969e2
add try/catch blocks to fs operations
Lms24 686644a
apply suggestions from code review
Lms24 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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`. | ||
Lms24 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
*/ | ||
|
||
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 | ||
Lms24 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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); | ||
Lms24 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
// modify entry points to point to correct paths (i.e. delete the build directory) | ||
Lms24 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
ENTRY_POINTS.filter(entryPoint => !!pkg[entryPoint]).forEach(entryPoint => { | ||
Lms24 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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)); | ||
Lms24 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
console.log('Successfully finished postbuild commands'); | ||
Lms24 marked this conversation as resolved.
Show resolved
Hide resolved
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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:
Saves having to answer the question "match what?".
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
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 thatpostbuild
only copies this file and the paths are specified as if the file was already located inbuild
.