Skip to content
This repository has been archived by the owner on Nov 21, 2023. It is now read-only.

Conditionally run npm install in tmp directory #8

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from 5 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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
/node_modules/
/lib/
.DS_Store
21 changes: 21 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,27 @@ export default async function caxa({
);
const appDirectory = path.join(os.tmpdir(), "caxa", identifier);
await fs.copy(directory, appDirectory);

// if we did not copy a node modules folder, but there is a package or package-lock file, run npm install
if (!(await fs.pathExists(path.join(appDirectory, "node_modules")))) {
const packageLockPath = path.join(appDirectory, "package-lock.json");
const packageJsonPath = path.join(appDirectory, "package.json");
// prefer using package-lock, if available
try {
if (await fs.pathExists(packageLockPath)) {
await execa("npm", ["ci"], { cwd: appDirectory }).catch();
Copy link

Choose a reason for hiding this comment

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

Suggested change
await execa("npm", ["ci"], { cwd: appDirectory }).catch();
await execa("npm", ["ci"], { cwd: appDirectory, env: { NODE_ENV: "production" } }).catch();

Copy link

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?

Copy link
Author

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?

Copy link

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.

} else if (await fs.pathExists(packageJsonPath)) {
throw null;
}
} catch (err) {
try {
await execa("npm", ["i"], { cwd: appDirectory });
Copy link

Choose a reason for hiding this comment

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

Suggested change
await execa("npm", ["i"], { cwd: appDirectory });
await execa("npm", ["i"], { cwd: appDirectory, env: { NODE_ENV: "production" } });

Copy link
Author

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))

} catch (e) {
// pass, nothing we can do here
console.log(e);
}
}
}
await execa("npm", ["prune", "--production"], { cwd: appDirectory });
await execa("npm", ["dedupe"], { cwd: appDirectory });
await fs.ensureDir(path.join(appDirectory, "node_modules/.bin"));
Expand Down
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"include": ["src/**/*.ts"],
"compilerOptions": {
"rootDir": "src",
"outDir": "lib",
Expand Down