Skip to content

Commit 86b924d

Browse files
committed
Refactor deployment helper script and warn if no dependencies sliced
1 parent 4e4a635 commit 86b924d

File tree

1 file changed

+38
-24
lines changed

1 file changed

+38
-24
lines changed

scripts/prepare-deploy.mjs

Lines changed: 38 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,7 @@
1010
*/
1111

1212
import { execSync } from 'node:child_process'
13-
import console from 'node:console'
14-
15-
console.time('Cleaning of devDependencies list')
13+
import console, { warn } from 'node:console'
1614

1715
/** The list of `devDependencies` not needed to build the app. */
1816
const USELESS_DEPS = [
@@ -30,37 +28,53 @@ const USELESS_DEPS = [
3028
'typescript',
3129
]
3230

33-
const depsArgs = USELESS_DEPS
34-
/**
35-
* Exclude invalid names, which reduces the risk of failure since we try
36-
* to remove all deps in one command: they’ll all be deleted or fail.
37-
*/
38-
.filter(hasValidName)
31+
const benchmarkLabel = 'Cleaning of devDependencies list'
3932

40-
// Wrap dep names with single quotes, which is needed for `@scope/name` deps.
41-
.map(dep => `'${dep}'`)
33+
console.time(benchmarkLabel)
4234

43-
// Create a space-separated list of `devDepencies.'(@scope/)name'`.
44-
.join(` devDependencies.`)
35+
sliceDevDependencies(USELESS_DEPS)
4536

46-
try {
47-
execSync(`npm pkg delete devDependencies.${depsArgs}`, {
48-
stdio: 'inherit',
49-
timeout: 5000,
50-
})
51-
} catch (error) {
52-
console.warn(`Slicing of devDepencencies failed: ${error.message}`);
53-
}
37+
console.timeEnd(benchmarkLabel);
5438

55-
console.timeEnd('Cleaning of devDependencies list');
39+
/**
40+
* Remove `devDependencies` from `package.json`.
41+
*
42+
* @param {string[]} devDeps A list of devDependencies names.
43+
*/
44+
function sliceDevDependencies(devDeps) {
45+
const depsArgs = devDeps
46+
/**
47+
* Exclude invalid names, which reduces the risk of failure since we try
48+
* to remove all deps in one command: they’ll all be deleted or fail.
49+
*/
50+
.filter(hasValidName)
5651

57-
/** Utils */
52+
// Wrap dep names with single quotes, which is needed for `@scope/name` deps.
53+
.map(dep => `'${dep}'`)
54+
55+
// Create a space-separated list of `devDepencies.'(@scope/)name'`.
56+
.join(` devDependencies.`)
57+
58+
if (depsArgs.length) {
59+
try {
60+
execSync(`npm pkg delete devDependencies.${depsArgs}`, {
61+
stdio: 'inherit',
62+
timeout: 5000,
63+
})
64+
} catch (error) {
65+
warn(`Slicing of devDepencencies failed: ${error.message}`);
66+
}
67+
} else {
68+
warn('No devDepencencies to slice.');
69+
}
70+
}
5871

72+
/** @param {string} name The name of the dependency. */
5973
function hasValidName (name) {
6074
const isValid = /^[a-zA-Z0-9@/_.-]+$/.test(name)
6175

6276
if (!isValid) {
63-
console.warn(`Invalid dependency name: ${name} will stay.`)
77+
warn(`Invalid dependency name: ${name} will stay.`)
6478
}
6579

6680
return isValid

0 commit comments

Comments
 (0)