forked from loopbackio/loopback-next
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdate-template-deps.js
executable file
·71 lines (56 loc) · 1.95 KB
/
update-template-deps.js
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
#!/usr/bin/env node
// Copyright IBM Corp. 2017,2018. 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('path');
const fs = require('fs');
const Project = require('@lerna/project');
async function updateTemplateDeps() {
const project = new Project(process.cwd());
const packages = await project.getPackages();
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;
}
const rootPath = project.rootPath;
// Load dependencies from `packages/build/package.json`
const buildDeps = require(path.join(rootPath, 'packages/build/package.json'))
.dependencies;
// Load dependencies from `packages/cli/package.json`
const cliPackageJson = path.join(rootPath, 'packages/cli/package.json');
// Loading existing dependencies from `packages/cli/package.json`
const cliPkg = require(cliPackageJson);
cliPkg.config = cliPkg.config || {};
const currentDeps = cliPkg.config.templateDependencies || {};
// Merge all entries
const deps = Object.assign({}, currentDeps, buildDeps, lbModules);
cliPkg.config.templateDependencies = deps;
// Convert to JSON
const json = JSON.stringify(cliPkg, null, 2);
if (process.argv[2] === '-f') {
// Using `-f` to overwrite packages/cli/lib/dependencies.json
fs.writeFileSync(cliPackageJson, json + '\n', {encoding: 'utf-8'});
console.log('%s has been updated.', cliPackageJson);
} else {
// Otherwise write to console
console.log(json);
}
}
if (require.main === module) {
updateTemplateDeps().catch(err => {
console.error(err);
process.exit(1);
});
}