-
-
Notifications
You must be signed in to change notification settings - Fork 506
/
Copy pathupdate-version-links
executable file
·99 lines (86 loc) · 2.94 KB
/
update-version-links
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
96
97
98
99
#!/usr/bin/env node
/**
* Search & Replace previous release numbers
* such as `3.15`, `3.15.0`, `v3.15.0` or `release`
* to the provided release number for guides and api URLs
*
* Running tests:
* ./update-version-links --tests
*
* How to use:
* ./update-version-links <guides-path> <release-number> <options...>
*
* Examples:
* ./update-version-links ../guides/v3.21.0 3.21
* ./update-version-links ../guides/v3.21.0 3.21 --dry-run
* ./update-version-links ../guides/v3.21.0 3.21 --silent
* ./update-version-links ../guides/v3.21.0 3.21 --verbose
*
* Will replace the following:
* https://api.emberjs.com/ember/release/ -> https://api.emberjs.com/ember/3.21/
* https://api.emberjs.com/ember/3.15/ -> https://api.emberjs.com/ember/3.21/
* https://api.emberjs.com/ember-data/release/ -> https://api.emberjs.com/ember-data/3.21/
* https://api.emberjs.com/ember-data/3.15/ -> https://api.emberjs.com/ember-data/3.21/
* https://guides.emberjs.com/release/getting-started/ -> https://guides.emberjs.com/v3.21/getting-started/
*/
const fs = require('fs');
const replaceURLVersions = require('./helpers/replace-url-versions');
const { Command } = require('commander');
const program = new Command();
program.option('--dry-run')
.option('--verbose')
.option('--silent')
.argument('<currentFolder>')
.argument('<newEmberVersionNumber>')
.argument('<newEmberDataVersionNumber>');
program.parse(process.argv);
const options = program.opts();
const currentFolder = fs.realpathSync(program.args[0]);
const newEmberVersionNumber = program.args[1];
const newEmberDataVersionNumber = program.args[2];
let dryRun = options.dryRun;
let verbose = options.verbose;
let silent = options.silent;
let filesFound = [];
let filesUpdated = [];
const recursion = (path) => {
const files = fs.readdirSync(path);
files.forEach((fileName) => {
const filePath = `${path}/${fileName}`;
const stat = fs.statSync(filePath);
if (stat.isDirectory()) {
recursion(filePath);
} else if (fileName.endsWith('.md')) {
const currentOutput = fs.readFileSync(filePath).toString();
const newOutput = replaceURLVersions(
currentOutput,
newEmberVersionNumber,
newEmberDataVersionNumber
);
if (currentOutput !== newOutput) {
if (!dryRun) {
fs.writeFileSync(filePath, newOutput);
}
filesUpdated.push(filePath);
}
filesFound.push(filePath);
}
});
}
if (silent) {
recursion(currentFolder);
} else {
console.log('Welcome to the automated process of updating the Guides version links');
console.log();
if (dryRun) {
console.log('❗ Running on --dry-run. Files will NOT be updated.');
console.log();
}
recursion(currentFolder);
console.log(`Found: ${filesFound.length} files`);
console.log(`${dryRun ? 'To be updated:' : 'Updated:'} ${filesUpdated.length} files`);
if (verbose) {
console.log();
console.log(filesUpdated);
}
}