forked from react-native-community/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdate-metro.js
66 lines (59 loc) · 1.89 KB
/
update-metro.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
#!/usr/bin/env nodes
/**
* Script to update metro monorepo to current latest version on npm
* This script wiil update all package.json
*/
const fs = require('fs');
const path = require('path');
const cp = require('child_process');
const glob = require('glob').sync;
const chalk = require('chalk');
/**
* Fetches the current latest version of specific package
*/
const getLatestVersion = (pkgName) => {
const latestVersion = cp.execSync(`npm show ${pkgName} version`, {
encoding: 'utf8',
});
return `^${latestVersion.trim()}`;
};
/**
* Updates metro version in specific object (dependencies)
*/
const updateDependencies = (depsObject) => {
const updatedJson = Object.keys(depsObject).reduce((result, packageName) => {
if (!packageName.includes('metro')) {
return depsObject;
}
const prevVersion = depsObject[packageName];
const latestVersion = getLatestVersion(packageName);
if (latestVersion !== prevVersion) {
console.log(
`Updated ${packageName} ${prevVersion} -> ${chalk.cyan(latestVersion)}`,
);
}
return Object.assign(depsObject, {[packageName]: latestVersion});
}, depsObject);
return updatedJson;
};
const start = new Date().getTime();
['./package.json', ...glob('./packages/*/package.json')].forEach((pkgPath) => {
const pkg = require(path.join(process.cwd(), pkgPath));
const updatedDependency = pkg.dependencies
? Object.assign(pkg, {
dependencies: updateDependencies(pkg.dependencies),
})
: pkg;
const updatedDevDependency = pkg.devDependencies
? Object.assign(updatedDependency, {
devDependencies: updateDependencies(pkg.devDependencies),
})
: updatedDependency;
fs.writeFileSync(
pkgPath,
`${JSON.stringify(updatedDevDependency, null, 2)}\n`,
);
});
const end = new Date().getTime();
const ellapsedTime = (end - start) / 1000;
console.log(`✨ Done in ${ellapsedTime}s.`);