-
Notifications
You must be signed in to change notification settings - Fork 95
Expand file tree
/
Copy pathpackage-lock-sync.mjs
More file actions
118 lines (95 loc) · 2.78 KB
/
Copy pathpackage-lock-sync.mjs
File metadata and controls
118 lines (95 loc) · 2.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import fs from 'node:fs';
import path from 'node:path';
import childProcess from 'node:child_process';
import { dirname } from 'node:path';
import { fileURLToPath } from 'node:url';
const __dirname = dirname(fileURLToPath(import.meta.url));
const shouldWrite = process.argv.includes('--write');
const shouldForceWrite = shouldWrite && process.argv.includes('--force');
const cwd = path.resolve(__dirname, '..');
const npmBin = resolveNpmBin(cwd);
const starters = fs.readdirSync(cwd, { withFileTypes: true });
// iterate over all starters and check if the `package-lock.json` is in sync with the `package.json`
let exitCode = 0;
for (const starter of starters) {
const dir = path.join(cwd, starter.name);
if (
!starter.isDirectory() ||
!fs.existsSync(path.join(dir, 'package.json'))
) {
// we're only interested in directories containing a `package.json`
continue;
}
try {
if (shouldWrite) {
await updatePackageLock(dir, starter.name, shouldForceWrite);
} else {
await checkPackageLockSync(dir, starter.name);
}
} catch (error) {
exitCode = 1;
console.error(error.message);
}
}
process.exit(exitCode);
function resolveNpmBin(cwd) {
const binPath = path.join(cwd, 'node_modules', '.bin', 'npm');
if (!fs.existsSync(binPath)) {
console.error('Could not find npm binary.');
process.exit(1);
}
return binPath;
}
function checkPackageLockSync(directory, name) {
return new Promise((resolve, reject) => {
if (!fs.existsSync(path.join(directory, 'package-lock.json'))) {
reject(
new Error(`No \`package-lock.json\` found for starter \`${name}\`.`)
);
return;
}
const child = childProcess.spawn(npmBin, ['ci'], {
cwd: directory,
});
child.on('exit', (code) => {
if (code === 0) {
console.log(`✔ ${name}`);
resolve();
} else {
reject(
new Error(
`The \`package-lock.json\` is not in sync with the \`package.json\` for starter \`${name}\`.`
)
);
}
});
});
}
function updatePackageLock(directory, name, force) {
return new Promise((resolve, reject) => {
// we only write the `package-lock.json` if it does not exist or if the `--force` flag is set
if (!force && fs.existsSync(path.join(directory, 'package-lock.json'))) {
resolve();
return;
}
const child = childProcess.spawn(
npmBin,
['install', '--package-lock-only', '--ignore-scripts'],
{
stdio: 'ignore',
cwd: directory,
}
);
child.on('exit', (code) => {
if (code === 0) {
resolve();
} else {
reject(
new Error(
`Failed to update the \`package-lock.json\` for starter \`${name}\`.`
)
);
}
});
});
}