UNPKG

4.05 kBJavaScriptView Raw
1#!/usr/bin/env node
2
3/**
4 * Copyright (c) 2017-present, Facebook, Inc.
5 *
6 * This source code is licensed under the MIT license found in the
7 * LICENSE file in the root directory of this source tree.
8 */
9
10const chalk = require('chalk');
11const program = require('commander');
12const escapeStringRegexp = require('escape-string-regexp');
13const fs = require('fs');
14const glob = require('glob');
15const path = require('path');
16
17const metadataUtils = require('./server/metadataUtils.js');
18
19const CWD = process.cwd();
20
21// generate a doc header from metadata
22function 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
31let currentVersion;
32let newVersion;
33
34program
35 .arguments('<version_name> <new_version_name>')
36 .action((ver1, ver2) => {
37 currentVersion = ver1;
38 newVersion = ver2;
39 })
40 .parse(process.argv);
41
42// require user to input two command line arguments, current version to be
43// renamed, and new version name
44if (
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// error if no versions currently exist
57if (!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
66const versions = JSON.parse(fs.readFileSync(`${CWD}/versions.json`, 'utf8'));
67
68const versionIndex = versions.indexOf(currentVersion);
69// error if current specified version does not exist
70if (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// replace old version with new version in versions.json file
79versions[versionIndex] = newVersion;
80fs.writeFileSync(
81 `${CWD}/versions.json`,
82 `${JSON.stringify(versions, null, 2)}\n`,
83);
84
85// if folder of docs for this version exists, rename folder and rewrite doc
86// headers to use new version
87if (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// if sidebar file exists for this version, rename sidebar file and rewrite
114// doc ids in the file
115const currentSidebarFile = `${CWD}/versioned_sidebars/version-${currentVersion}-sidebars.json`;
116const newSidebarFile = `${CWD}/versioned_sidebars/version-${newVersion}-sidebars.json`;
117if (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
127console.log(
128 `${chalk.green('Successfully renamed version ')}${chalk.yellow(
129 currentVersion,
130 )}${chalk.green(' to version ')}${chalk.yellow(newVersion)}\n`,
131);