UNPKG

8.03 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 commander = require('commander');
12const fs = require('fs-extra');
13const glob = require('glob');
14const path = require('path');
15
16const CWD = process.cwd();
17
18const toHex = color => {
19 const hex = color.toString(16);
20 return hex.length === 1 ? `0${hex}` : hex;
21};
22
23const colorScheme = () => {
24 let primaryColor = '#';
25 let secondaryColor = '#';
26 for (let i = 0; i < 3; i++) {
27 // 175 is our ceiling to prevent the color from being too bright
28 const color = Math.floor(Math.random() * 176);
29 const darkColor = Math.floor(color * 0.7);
30 primaryColor += toHex(color);
31 secondaryColor += toHex(darkColor);
32 }
33 return {primaryColor, secondaryColor};
34};
35
36let feature;
37
38commander
39 .arguments('[feature]')
40 .action(feat => {
41 feature = feat;
42 })
43 .parse(process.argv);
44
45// add scripts to package.json file
46if (fs.existsSync(`${CWD}/package.json`)) {
47 const packageContent = JSON.parse(
48 fs.readFileSync(`${CWD}/package.json`, 'utf8'),
49 );
50 if (!packageContent.scripts) {
51 packageContent.scripts = {};
52 }
53 packageContent.scripts.start = 'docusaurus-start';
54 packageContent.scripts.build = 'docusaurus-build';
55 packageContent.scripts['publish-gh-pages'] = 'docusaurus-publish';
56 packageContent.scripts.examples = 'docusaurus-examples';
57 packageContent.scripts['write-translations'] =
58 'docusaurus-write-translations';
59 packageContent.scripts.version = 'docusaurus-version';
60 packageContent.scripts['rename-version'] = 'docusaurus-rename-version';
61 fs.writeFileSync(
62 `${CWD}/package.json`,
63 `${JSON.stringify(packageContent, null, 2)}\n`,
64 );
65 console.log(
66 `${chalk.green('Wrote docusaurus scripts to package.json file.')}\n`,
67 );
68}
69
70const outerFolder = path.basename(path.dirname(CWD));
71
72let docsCreatedInIntendedDirectory = true;
73
74// handles cases where feature is "translations", "versions" or neither/not present
75if (feature === 'translations') {
76 // copy files for translations
77 const folder = path.join(__dirname, '..', 'examples', 'translations');
78 if (fs.existsSync(`${CWD}/../crowdin.yaml`)) {
79 console.log(
80 `${chalk.yellow('crowdin.yaml already exists')} in ${chalk.yellow(
81 `${outerFolder}/`,
82 )}. Rename or remove the file to regenerate an example version.\n`,
83 );
84 } else {
85 fs.copySync(`${folder}/crowdin.yaml`, `${CWD}/../crowdin.yaml`);
86 }
87 const files = glob.sync(`${folder}/**/*`);
88 files.forEach(file => {
89 if (fs.lstatSync(file).isDirectory()) {
90 return;
91 }
92 if (path.basename(file) === 'crowdin.yaml') {
93 return;
94 }
95 const filePath = path.resolve(file).split(path.resolve(folder))[1];
96 try {
97 fs.copySync(file, CWD + filePath, {
98 overwrite: false,
99 errorOnExist: true,
100 });
101 } catch (e) {
102 console.log(
103 `${chalk.yellow(
104 `${path.basename(filePath)} already exists`,
105 )} in ${chalk.yellow(
106 `website${filePath.split(path.basename(filePath))[0]}`,
107 )}. Rename or remove the file to regenerate an example version.\n`,
108 );
109 }
110 });
111} else if (feature === 'versions') {
112 // copy files for versions
113 const folder = path.join(__dirname, '..', 'examples', 'versions');
114 const files = glob.sync(`${folder}/**/*`);
115 files.forEach(file => {
116 if (fs.lstatSync(file).isDirectory()) {
117 return;
118 }
119 const filePath = path.resolve(file).split(path.resolve(folder))[1];
120 try {
121 fs.copySync(file, CWD + filePath, {
122 overwrite: false,
123 errorOnExist: true,
124 });
125 } catch (e) {
126 console.log(
127 `${chalk.yellow(
128 `${path.basename(filePath)} already exists`,
129 )} in ${chalk.yellow(
130 `website${filePath.split(path.basename(filePath))[0]}`,
131 )}. Rename or remove the file to regenerate an example version.\n`,
132 );
133 }
134 });
135} else {
136 const folder = path.join(__dirname, '..', 'examples', 'basics');
137 // copy docs examples
138 let targetDocsDir = `${CWD}/../docs`;
139 if (fs.existsSync(targetDocsDir)) {
140 console.log(
141 `- ${chalk.green('docs')} already exists in ${chalk.blue(
142 outerFolder,
143 )}. Copying into ${CWD}/../docs-examples-from-docusaurus instead.`,
144 );
145 targetDocsDir = `${CWD}/../docs-examples-from-docusaurus`;
146 docsCreatedInIntendedDirectory = false;
147 }
148
149 fs.copySync(`${folder}/docs`, targetDocsDir);
150
151 // copy blog examples
152 if (fs.existsSync(`${CWD}/blog`)) {
153 console.log(
154 `- ${chalk.green('blog')} already exists in ${chalk.blue(
155 `${outerFolder}/website`,
156 )}.`,
157 );
158 } else {
159 fs.copySync(path.join(folder, 'blog'), path.join(CWD, 'blog'));
160 }
161
162 const copyFileToProjectFolder = (fileNameFrom, fileNameTo) => {
163 const copiedFileName = fileNameTo || fileNameFrom;
164 const src = path.join(folder, fileNameFrom);
165 const dest = path.join(CWD, '..', copiedFileName);
166 if (fs.existsSync(dest)) {
167 console.log(
168 `- ${chalk.green(copiedFileName)} already exists in ${chalk.blue(
169 outerFolder,
170 )}.`,
171 );
172 } else {
173 fs.copySync(src, dest);
174 }
175 };
176
177 // copy .gitignore file
178 copyFileToProjectFolder('gitignore', '.gitignore');
179
180 // copy Dockerfile file
181 copyFileToProjectFolder('Dockerfile');
182
183 // copy docker-compose.yml file
184 copyFileToProjectFolder('docker-compose.yml');
185
186 // copy .dockerignore file
187 copyFileToProjectFolder('dockerignore', '.dockerignore');
188
189 // copy other files
190 const files = glob.sync(`${folder}/**/*`);
191 const {primaryColor, secondaryColor} = colorScheme();
192 files.forEach(file => {
193 if (fs.lstatSync(file).isDirectory()) {
194 return;
195 }
196 const containingFolder = path.basename(path.dirname(file));
197 if (
198 path.basename(file) === 'gitignore' ||
199 path.basename(file) === 'Dockerfile' ||
200 path.basename(file) === 'docker-compose.yml' ||
201 path.basename(file) === 'dockerignore' ||
202 containingFolder === 'blog' ||
203 containingFolder === 'docs'
204 ) {
205 return;
206 }
207 const filePath = path.resolve(file).split(path.resolve(folder))[1];
208 if (
209 path.basename(file) === 'siteConfig.js' &&
210 !fs.existsSync(CWD + filePath)
211 ) {
212 const siteConfig = fs
213 .readFileSync(file, 'utf8')
214 .replace('{{primaryColor}}', primaryColor)
215 .replace('{{secondaryColor}}', secondaryColor);
216 fs.writeFileSync(CWD + filePath, siteConfig);
217 } else {
218 try {
219 fs.copySync(file, CWD + filePath, {
220 overwrite: false,
221 errorOnExist: true,
222 });
223 } catch (e) {
224 console.log(
225 `- ${chalk.green(
226 `${path.basename(filePath)}`,
227 )} already exists in ${chalk.blue(
228 `${outerFolder}/website${
229 filePath.split(path.basename(filePath))[0]
230 }`,
231 )}.`,
232 );
233 }
234 }
235 });
236
237 const svgs = glob.sync(`${CWD}/static/img/**/*.svg`);
238 svgs.forEach(file => {
239 // Replace primary colors of SVGs.
240 const newImage = fs
241 .readFileSync(file, 'utf8')
242 .replace(/{{primaryColor}}/g, primaryColor);
243 fs.writeFileSync(file, newImage);
244 });
245
246 try {
247 const tree = require('tree-node-cli');
248 const dirString = tree(path.join(CWD, '..'), {
249 exclude: [
250 /node_modules/, // npm
251 /vendor/, // composer
252 ],
253 });
254 console.log(dirString);
255 } catch (error) {
256 console.warn(`Error printing directory: ${error}`);
257 }
258}
259
260if (!docsCreatedInIntendedDirectory) {
261 console.log(
262 `The ${chalk.yellow(
263 `${outerFolder}/docs`,
264 )} directory was not created because it already exists. ` +
265 `Please manually convert the contents into a Docusaurus-compatible format ` +
266 `by referring to the examples from ${chalk.yellow(
267 `${outerFolder}/docs-examples-from-docusaurus`,
268 )}.\n`,
269 );
270}