forked from open-wc/open-wc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdate-dependency.js
68 lines (55 loc) · 2.04 KB
/
update-dependency.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
/* eslint-disable import/no-extraneous-dependencies, no-param-reassign, no-console */
const fs = require('fs-extra');
const path = require('path');
/**
* Script to update a single dependency on the monorepo. Example:
*
* yarn update-dependency rollup ^1.2.3
*/
const isDefined = _ => !!_;
const dependencyFields = ['dependencies', 'devDependencies', 'peerDependencies'];
const [, , pkg, version] = process.argv;
if (!pkg || !version) {
throw new Error(
'Package and version must be specified. For example: yarn update-dependency rollup ^1.2.3',
);
}
function forEachParallel(items, callback) {
return Promise.all(items.map(callback));
}
async function run() {
const basedir = path.resolve(__dirname, '..');
const dirs = await fs.readdir(path.join(basedir, 'packages'));
const rootPackageJsonPath = path.join(basedir, 'package.json');
const readRootPackageJsonTask = fs.readJSON(rootPackageJsonPath, 'utf-8');
// read all project package.json
let packageJsons = await Promise.all(
dirs.map(async dir => {
const packageJsonPath = path.join(basedir, 'packages', dir, 'package.json');
if (!(await fs.pathExists(packageJsonPath))) return null;
const content = await fs.readJSON(packageJsonPath, 'utf-8');
return { content, path: packageJsonPath };
}),
);
packageJsons = packageJsons.filter(isDefined);
// read root package json
const rootPackageJson = await readRootPackageJsonTask;
packageJsons.push({ content: rootPackageJson, path: rootPackageJsonPath });
// update package.json files
await forEachParallel(packageJsons, async ({ content: packageJson, path: packageJsonPath }) => {
let changed = false;
for (const field of dependencyFields) {
if (packageJson[field] && pkg in packageJson[field]) {
packageJson[field][pkg] = version;
changed = true;
}
}
if (changed) {
console.log('updating package.json in ', packageJsonPath);
await fs.writeJSON(packageJsonPath, packageJson, { spaces: 2 });
}
});
}
run().catch(e => {
console.error(e);
});