1 | #!/usr/bin/env node
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 | const chalk = require('chalk');
|
11 | const program = require('commander');
|
12 | const escapeStringRegexp = require('escape-string-regexp');
|
13 | const fs = require('fs');
|
14 | const glob = require('glob');
|
15 | const path = require('path');
|
16 |
|
17 | const metadataUtils = require('./server/metadataUtils.js');
|
18 |
|
19 | const CWD = process.cwd();
|
20 |
|
21 |
|
22 | function makeHeader(metadata) {
|
23 | let header = '---\n';
|
24 | Object.keys(metadata).forEach(key => {
|
25 | header += `${key}: ${metadata[key]}\n`;
|
26 | });
|
27 | header += '---\n';
|
28 | return header;
|
29 | }
|
30 |
|
31 | let currentVersion;
|
32 | let newVersion;
|
33 |
|
34 | program
|
35 | .arguments('<version_name> <new_version_name>')
|
36 | .action((ver1, ver2) => {
|
37 | currentVersion = ver1;
|
38 | newVersion = ver2;
|
39 | })
|
40 | .parse(process.argv);
|
41 |
|
42 |
|
43 |
|
44 | if (
|
45 | typeof currentVersion === 'undefined' ||
|
46 | typeof newVersion === 'undefined'
|
47 | ) {
|
48 | console.error(
|
49 | `${chalk.yellow(
|
50 | 'Version numbers are not properly specified!',
|
51 | )}\nSpecify as command line arguments: the current version you wish to rename, then the version number you want to rename it to. `,
|
52 | );
|
53 | process.exit(1);
|
54 | }
|
55 |
|
56 |
|
57 | if (!fs.existsSync(`${CWD}/versions.json`)) {
|
58 | console.error(
|
59 | `${chalk.yellow(
|
60 | 'No versions found!',
|
61 | )}\nNo versions.json file currently exists. Use the \`versions\` script if you wish to create new versions.`,
|
62 | );
|
63 | process.exit(1);
|
64 | }
|
65 |
|
66 | const versions = JSON.parse(fs.readFileSync(`${CWD}/versions.json`, 'utf8'));
|
67 |
|
68 | const versionIndex = versions.indexOf(currentVersion);
|
69 |
|
70 | if (versionIndex < 0) {
|
71 | console.error(
|
72 | `${chalk.yellow(
|
73 | `Version ${currentVersion} does not currently exist!`,
|
74 | )}\n Version ${currentVersion} is not in the versions.json file. You can only rename existing versions.`,
|
75 | );
|
76 | process.exit(1);
|
77 | }
|
78 |
|
79 | versions[versionIndex] = newVersion;
|
80 | fs.writeFileSync(
|
81 | `${CWD}/versions.json`,
|
82 | `${JSON.stringify(versions, null, 2)}\n`,
|
83 | );
|
84 |
|
85 |
|
86 |
|
87 | if (fs.existsSync(`${CWD}/versioned_docs/version-${currentVersion}`)) {
|
88 | fs.renameSync(
|
89 | `${CWD}/versioned_docs/version-${currentVersion}`,
|
90 | `${CWD}/versioned_docs/version-${newVersion}`,
|
91 | );
|
92 |
|
93 | const files = glob.sync(`${CWD}/versioned_docs/version-${newVersion}/*`);
|
94 | files.forEach(file => {
|
95 | const extension = path.extname(file);
|
96 | if (extension !== '.md' && extension !== '.markdown') {
|
97 | return;
|
98 | }
|
99 | const res = metadataUtils.extractMetadata(fs.readFileSync(file, 'utf8'));
|
100 | const metadata = res.metadata;
|
101 | const rawContent = res.rawContent;
|
102 | if (!metadata.id) {
|
103 | return;
|
104 | }
|
105 | metadata.id = metadata.id.replace(
|
106 | `version-${currentVersion}-`,
|
107 | `version-${newVersion}-`,
|
108 | );
|
109 | fs.writeFileSync(file, makeHeader(metadata) + rawContent);
|
110 | });
|
111 | }
|
112 |
|
113 |
|
114 |
|
115 | const currentSidebarFile = `${CWD}/versioned_sidebars/version-${currentVersion}-sidebars.json`;
|
116 | const newSidebarFile = `${CWD}/versioned_sidebars/version-${newVersion}-sidebars.json`;
|
117 | if (fs.existsSync(currentSidebarFile)) {
|
118 | fs.renameSync(currentSidebarFile, newSidebarFile);
|
119 | let sidebarContent = fs.readFileSync(newSidebarFile, 'utf8');
|
120 | sidebarContent = sidebarContent.replace(
|
121 | new RegExp(`version-${escapeStringRegexp(currentVersion)}-`, 'g'),
|
122 | `version-${newVersion}-`,
|
123 | );
|
124 | fs.writeFileSync(newSidebarFile, sidebarContent);
|
125 | }
|
126 |
|
127 | console.log(
|
128 | `${chalk.green('Successfully renamed version ')}${chalk.yellow(
|
129 | currentVersion,
|
130 | )}${chalk.green(' to version ')}${chalk.yellow(newVersion)}\n`,
|
131 | );
|