Skip to content

Commit b1b0bbb

Browse files
authored
feat: Add CLI option to run package install command (#8)
* chore: Configure Biome linter to enforce unused import and variable rules * chore: Format * feat: Add CLI option to run package install command * docs: Update readme * refactor: Consolidate pinDependencies function into package-manager module * feat: Add runInstall function to execute package manager install command * fix: Pass package manager to runInstall function
1 parent b0718e7 commit b1b0bbb

File tree

6 files changed

+66
-40
lines changed

6 files changed

+66
-40
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ Pin dependency versions in package.json
4545
Options:
4646
-V, --version output the version number
4747
-l, --lockfile <lockfile> lockfile to use
48+
-i, --install run install command
4849
-h, --help display help for command
4950
```
5051

biome.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,11 @@
1919
"linter": {
2020
"enabled": true,
2121
"rules": {
22-
"recommended": true
22+
"recommended": true,
23+
"correctness": {
24+
"noUnusedImports": "error",
25+
"noUnusedVariables": "error"
26+
}
2327
}
2428
},
2529
"javascript": {

src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ program
99
.version(version)
1010
.description(description)
1111
.option("-l, --lockfile <lockfile>", "lockfile to use")
12+
.option("-i, --install", "run install command")
1213
.parse(process.argv);
1314

1415
main(program.opts<Options>()).catch((error) => {

src/lib/package-manager.ts

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import * as fs from "node:fs";
22
import * as path from "node:path";
3+
import PackageJson from "@npmcli/package-json";
4+
import { sh } from "./sh";
35

46
export type PackageManager = "npm" | "pnpm" | "bun";
57

@@ -66,3 +68,49 @@ export const determinePackageManager = async (
6668
return "bun";
6769
}
6870
};
71+
72+
export const pinDependencies = async (
73+
allDependencies: Record<string, string>,
74+
) => {
75+
// load package.json
76+
const packageJson = new PackageJson();
77+
await packageJson.load(process.cwd());
78+
79+
// pin dependencies
80+
if (packageJson.content.dependencies) {
81+
const dependencies: Record<string, string> = Object.fromEntries(
82+
Object.keys(packageJson.content.dependencies).map((name) => [
83+
name,
84+
allDependencies[name],
85+
]),
86+
);
87+
88+
packageJson.update({ dependencies });
89+
}
90+
91+
// pin dev dependencies
92+
if (packageJson.content.devDependencies) {
93+
const devDependencies: Record<string, string> = Object.fromEntries(
94+
Object.keys(packageJson.content.devDependencies).map((name) => [
95+
name,
96+
allDependencies[name],
97+
]),
98+
);
99+
100+
packageJson.update({ devDependencies });
101+
}
102+
103+
// save package.json
104+
await packageJson.save();
105+
};
106+
107+
export const runInstall = async (packageManager: PackageManager) => {
108+
switch (packageManager) {
109+
case "npm":
110+
return sh("npm", ["install"]);
111+
case "pnpm":
112+
return sh("pnpm", ["install"]);
113+
case "bun":
114+
return sh("bun", ["install"]);
115+
}
116+
};

src/lib/pin.ts

Lines changed: 0 additions & 37 deletions
This file was deleted.

src/main.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,16 @@ import * as fs from "node:fs";
22
import * as path from "node:path";
33
import { listBunDependencies } from "./lib/bun";
44
import { listNpmDependencies } from "./lib/npm";
5-
import { determinePackageManager } from "./lib/package-manager";
6-
import { pinDependencies } from "./lib/pin";
5+
import {
6+
determinePackageManager,
7+
pinDependencies,
8+
runInstall,
9+
} from "./lib/package-manager";
710
import { listPnpmDependencies } from "./lib/pnpm";
811

912
export type Options = {
1013
lockfile?: string;
14+
install?: boolean;
1115
};
1216

1317
export const main = async (options: Options) => {
@@ -34,4 +38,9 @@ export const main = async (options: Options) => {
3438

3539
// pin dependencies
3640
await pinDependencies(allDependencies);
41+
42+
// run install command
43+
if (options.install) {
44+
await runInstall(packageManager);
45+
}
3746
};

0 commit comments

Comments
 (0)