-
Notifications
You must be signed in to change notification settings - Fork 34
Conditionally run npm install
in tmp directory
#8
Conversation
tsc was complaining that not all files were under `src`
src/index.ts
Outdated
// prefer using package-lock, if available | ||
try { | ||
if (await fs.pathExists(packageLockPath)) { | ||
await execa("npm", ["ci"], { cwd: appDirectory }).catch(); |
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.
await execa("npm", ["ci"], { cwd: appDirectory }).catch(); | |
await execa("npm", ["ci"], { cwd: appDirectory, env: { NODE_ENV: "production" } }).catch(); |
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.
Also, why did you add a .catch()
here but not on the npm i
below?
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.
Good catch on the catch (pun intended) - it was from a previous approach I had which didn't have the try blocks.
I'm actually intentionally NOT limiting it to production, because one of the example repos runs tsc
as part of its prepare
script, which fails without the dev dependency of typescript.
But we still run npm prune
to clear out the production dependencies before bundling, so this feels acceptable to me. Perhaps we add an optional flag to force production-only installation if you know you don't need the option of dev dependencies for this step?
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.
@kylebjordahl Ah, I see! Makes sense. I thought of this as an optimization, but you're right, there are situations in which installing it all is necessary. I'd say to just leave it the way you did.
} | ||
} catch (err) { | ||
try { | ||
await execa("npm", ["i"], { cwd: appDirectory }); |
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.
await execa("npm", ["i"], { cwd: appDirectory }); | |
await execa("npm", ["i"], { cwd: appDirectory, env: { NODE_ENV: "production" } }); |
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.
Same comment here as above (#8 (comment))
filter is a callback to choose whether to copy a file to the build dir
Just realized I forgot to put this pr on a branch, so its getting my continued updates. If that's ok, I'm fine with leaving it, but I'll gladly re-pull from a branch to keep the PR atomic, if that's preferred @papb |
I would personally prefer if the PR was kept atomic, but note that I am just a random person that happen to watch this repository and suggested some improvements, @leafac is the one who really decides those things :) That said, I would create a new branch to keep your add-filter-option commit, and fix master to stop on 6e05eca, as follows:
|
Thanks for your work, y’all. I’ll review this carefully and merge soon… Regarding atomic pull requests, I don’t care one way or the other 🤷♂️ |
@papb - makes sense, I just saw you were marked as a reviewer, didn't know if maybe @leafac delegated to you or something 😄 @leafac - Thanks for taking a look! If it's alright with you, I'd actually like to get my filter option feature in as well, it's only added on the module API and not in code, so it has minimal effect on functionality; I'm using it now in my own build and it's great, as our app generates some local cache files when run in the hot-reload server, and those need to be omitted when building. |
@kylebjordahl Haha, anyone on GitHub can review anyone's open PRs and GitHub will automatically add that person to the list of reviewers, it's out of control from repository owners. |
I’ve noticed a trend and I think I found a more general solution for a bunch of problems. The trend: You’re interested in running The solution I propose: Give you an option to run arbitrary commands on the build directory before it’s packaged (right here). These commands may be In terms of implementation, I think we should receive a new argument over here called What do you think? |
@leafac you're using You can solve the problem specified here if you only copy what's necessary and always exclude I think it's a good idea to think about what's easier for the average end-user to use in addition to being easier to maintain. |
@leafac - I'm a huge fan of going in the direction you propose, but I would suggest that it be handled differently between the CLI and the API; while it would be convenient to supply other I also think that @fcastilloec has a great point about exposing copy filter, especially useful in a large monorepo where you might need to cherry-pick things. I'm glad to rework this PR to add both a
|
Yep. Just sharing the sentiment, I am not a fan of the npm dedupe line, as it produces an "unstable" By the way, if any logic is added conditional on Awesome project! 👏 ❤️ |
Here's a different suggestion, I think simpler, instead of any of the above (no additional command line options, no filter/prepare callbacks, no include/exclude globs). The application bundled by
If the user's
where
|
Hi all, I’m just checking in to you know that I’m going to work on this soon. I maintain several open-source projects and go around spending some time on each; caxa is up next. I also started streaming my open-source work, which you may follow here: https://www.youtube.com/channel/UC_R-6HcHW5V9_FlZe30tnGA |
@leafac - I definitely understand the time constraint issue! As I said previously, I'm glad to rework this PR to cover a broader solution, just let me know which direction you'd prefer. Re: @pdcastro's suggestion of using a convention-based solution with an NPM script, I personally think that would be a great feature, but it does not supersede the need for something provided at the API level, as it adds significant limitations/hurdles to using |
Hi all, The changes we’ve all been wanting have landed in [email protected]. Thank you very much for this pull request and for all the investigative work that went into it. While I didn’t merge the pull request directly, we ended up with an approach very similar (and of course, inspired by) to the one y’all proposed here:
Let me know how you like it 😃 |
First off, let me say thank you for this elegant and clever solution! Having used both pkg and nexe for years, I always thought it could be as simple as this, so kudos for just getting it done. By the look of the README, you've clearly done your homework on this, and I'm excited to offer a bit of my own suggestion with this PR.
Why
I'm currently battling an issue with pkg involving some native dependencies, etc. (super common story, I'm sure). This code is part of a medium-sized Nx monorepo, which keeps things organized, but has the downside of having one large
node_modules
folder for multiple apps. Using their new build tool, it's possible to generate apackage.json
file as a build artifact which includes only the required dependencies for the particular app or library; while this is great, I don't love the idea of having to runnpm install
on a folder in our build artifacts tree before I bundle it withcaxa
.Solution
Just before running
npm prune
check for the existence of anode_modules
folder in the temp directory, and if one is not found, then check for either apackage.json
orpackage-lock.json
, runningnpm ci
for package-lock, and standardnpm i
for package.npm ci
is the preferred option, as it creates a (more) deterministic environment.Cheers, and thanks again for a great solution to this problem!