-
-
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
Changes from 7 commits
80ab1be
f020d56
34b038a
6aa4ff6
d66c3d4
312036d
38969e2
686644a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/* | ||
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 the paths 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 dir exists | ||
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); | ||
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}`); | ||
} | ||
}); | ||
|
||
// package.json modifications | ||
const packageJsonPath = path.resolve(BUILD_DIR, 'package.json'); | ||
const pkgJson: { [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. strip out the build directory) | ||
ENTRY_POINTS.filter(entryPoint => pkgJson[entryPoint]).forEach(entryPoint => { | ||
pkgJson[entryPoint] = pkgJson[entryPoint].replace(`${BUILD_DIR}/`, ''); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: filter + ForEach can just be a single reduce call, also means we emit a new object instead of mutating the previous reference. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
I also think that doing it in two stages like this makes it easier to reason about compared to using There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
yup, but important to note that one is more likely to cause bugs from mutating There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for the feedback but I don't entirely understand the reasoning of changing this. I used the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Something like so (please double check I wrote this in GH) const newPkgJson = ENTRY_POINTS.reduce((acc, curr) => {
acc[curr] = acc[curr].replace(`${BUILD_DIR}/`, '');
return acc;
}, pkgJson) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
I find this significantly harder to understand as a reader, both in and of itself and as a way to communicate the intention here. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah interesting - I find it much easier to understand, it's just an array reducing to an object (a very typical FP operation) - I'm fine with leaving as is though. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ahh got it, thanks. This would essentially not mutate the original
Agreed, me too. Which is why I wanna go with the original version. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Go with your original, @Lms24. You have the power! (This is what Daniel always used to say to me in such situations.) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
@AbhiPrasad - perhaps that explains it. Because I haven't done a ton of FP, it doesn't hit my brain as, "Oh, that matches a pattern I know." Instead, my internal monologue is more like, "Okay, we're reducing, so we're combining stuff. How are we combining it? Oh, we're not, we're just fixing a string, but wait, is the string already there? Are we starting with something non-empty? vs "Okay, we're taking the entry points, oh, but only the ones which exist in package.json, and then we're changing their value. Okay, got it." |
||
}); | ||
|
||
// TODO decide if we want this: | ||
delete pkgJson.scripts; | ||
delete pkgJson.volta; | ||
Lms24 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
// write modified package.json to file (pretty-printed with 2 spaces) | ||
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}`); |
Uh oh!
There was an error while loading. Please reload this page.