-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Expand file tree
/
Copy pathupdate-dependency.ts
More file actions
51 lines (45 loc) · 1.45 KB
/
Copy pathupdate-dependency.ts
File metadata and controls
51 lines (45 loc) · 1.45 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
/**
* Update a dependency to a version in all relevant samples
*/
import * as child_process from 'child_process';
import * as fs from 'fs';
import * as path from 'path';
import { Sample, samples } from './samples';
function setVersion(
dependencyName: string,
version: string,
sample: Sample
) {
const packageJsonPath = path.join(sample.path, 'package.json');
const packageJsonContents = fs.readFileSync(packageJsonPath).toString();
const packageJson = JSON.parse(packageJsonContents);
let changed = false;
if (packageJson.dependencies?.[dependencyName]) {
packageJson.dependencies[dependencyName] = version;
changed = true;
}
if (packageJson.devDependencies?.[dependencyName]) {
packageJson.devDependencies[dependencyName] = version;
changed = true;
}
if (!changed) {
return;
}
console.log(`Updated ${dependencyName} in ${packageJson.name}`);
const space = packageJsonContents.includes('\t') ? '\t' : ' ';
fs.writeFileSync(packageJsonPath, JSON.stringify(packageJson, undefined, space) + '\n');
if (fs.existsSync(path.join(sample.path, 'package-lock.json'))) {
console.log(` npm install in ${packageJson.name}`);
child_process.execSync(`npm install`, {
cwd: sample.path,
stdio: 'inherit'
});
}
}
const [, , dependency, version] = process.argv;
if (!dependency || !version) {
console.log(`Usage: ${process.argv[0]} ${process.argv[1]} <depndency> <version>`);
}
for (const sample of samples) {
setVersion(dependency, version, sample);
}