Repository navigation
Expand file tree
/
Copy pathupdate-template-deps.js
More file actions
executable file
·95 lines (79 loc) · 2.58 KB
/
Copy pathupdate-template-deps.js
File metadata and controls
executable file
·95 lines (79 loc) · 2.58 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
#!/usr/bin/env node
// Copyright IBM Corp. 2018,2020. All Rights Reserved.
// Node module: loopback-next
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT
/**
* This is an internal script to update module dependencies for CLI code
* generation templates of package.json.
*/
'use strict';
const path = require('node:path');
const pkgJson = require('@npmcli/package-json');
const mapWorkspaces = require('@npmcli/map-workspaces');
const {isDryRun, printJson, runMain} = require('./script-util');
/**
* Update `templateDependencies` in `packages/cli/package.json`
* @param {*} options - Options
*/
async function updateTemplateDeps(options) {
const rootPath = process.cwd();
const {content: rootPkg} = await pkgJson.load(rootPath);
const workspaces = await mapWorkspaces({cwd: rootPath, pkg: rootPkg});
const packages = await Promise.all(
Array.from(workspaces, async ([name, location]) => {
const {content: pkg} = await pkgJson.load(location);
return {
name,
location,
private: pkg.private ?? false,
version: pkg.version,
};
}),
);
const pkgs = packages
.filter(pkg => !pkg.private)
.map(pkg => ({
name: pkg.name,
version: pkg.version,
}));
const lbModules = {};
for (const p of pkgs) {
lbModules[p.name] = '^' + p.version;
}
// Load eslint related dependencies from `packages/eslint-config/package.json`
const {content: eslintPkg} = await pkgJson.load(
path.join(rootPath, 'packages/eslint-config'),
);
// Load dependencies from `packages/build/package.json`
const {content: buildPkg} = await pkgJson.load(
path.join(rootPath, 'packages/build'),
);
// Load dependencies from `packages/core/package.json` for `tslib`
const {content: corePkg} = await pkgJson.load(
path.join(rootPath, 'packages/core'),
);
const cliPath = path.join(rootPath, 'packages/cli');
const cliPkg = await pkgJson.load(cliPath);
const config = {
...cliPkg.content.config,
templateDependencies: {
tslib: corePkg.dependencies.tslib,
...cliPkg.content.config.templateDependencies,
...buildPkg.dependencies,
...eslintPkg.dependencies,
...lbModules,
},
};
if (isDryRun(options)) {
// Dry run
printJson(config);
} else {
cliPkg.update({config});
await cliPkg.save();
const fullPath = path.join(cliPath, 'package.json');
console.log('%s has been updated.', path.relative(process.cwd(), fullPath));
}
}
module.exports = updateTemplateDeps;
runMain(module, updateTemplateDeps);