-
-
Notifications
You must be signed in to change notification settings - Fork 8.8k
/
Copy pathcopy-examples.js
executable file
·232 lines (219 loc) · 6.7 KB
/
copy-examples.js
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
#!/usr/bin/env node
/**
* Copyright (c) 2017-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
const chalk = require('chalk');
const commander = require('commander');
const fs = require('fs-extra');
const glob = require('glob');
const path = require('path');
const CWD = process.cwd();
let feature;
commander
.arguments('[feature]')
.action(feat => {
feature = feat;
})
.parse(process.argv);
// add scripts to package.json file
if (fs.existsSync(CWD + '/package.json')) {
const packageContent = JSON.parse(
fs.readFileSync(CWD + '/package.json', 'utf8')
);
if (!packageContent.scripts) {
packageContent.scripts = {};
}
packageContent.scripts['start'] = 'docusaurus-start';
packageContent.scripts['build'] = 'docusaurus-build';
packageContent.scripts['publish-gh-pages'] = 'docusaurus-publish';
packageContent.scripts['examples'] = 'docusaurus-examples';
packageContent.scripts['write-translations'] =
'docusaurus-write-translations';
packageContent.scripts['version'] = 'docusaurus-version';
packageContent.scripts['rename-version'] = 'docusaurus-rename-version';
fs.writeFileSync(
CWD + '/package.json',
JSON.stringify(packageContent, null, 2) + '\n'
);
console.log(
`${chalk.green('Wrote docusaurus scripts to package.json file.')}\n`
);
}
const outerFolder = path.basename(path.dirname(CWD));
let docsCreated = false;
let blogCreated = false;
let exampleSiteCreated = false;
// handles cases where feature is "translations", "versions" or neither/not present
if (feature === 'translations') {
// copy files for translations
const folder = path.join(__dirname, '..', 'examples', 'translations');
if (fs.existsSync(CWD + '/../crowdin.yaml')) {
console.log(
`${chalk.yellow('crowdin.yaml already exists')} in ${chalk.yellow(
outerFolder + '/'
)}. Rename or remove the file to regenerate an example version.\n`
);
} else {
fs.copySync(folder + '/crowdin.yaml', CWD + '/../crowdin.yaml');
exampleSiteCreated = true;
}
let files = glob.sync(folder + '/**/*');
files.forEach(file => {
if (fs.lstatSync(file).isDirectory()) {
return;
}
if (path.basename(file) === 'crowdin.yaml') {
return;
}
const filePath = path.resolve(file).split(path.resolve(folder))[1];
try {
fs.copySync(file, CWD + filePath, {
overwrite: false,
errorOnExist: true,
});
exampleSiteCreated = true;
} catch (e) {
console.log(
`${chalk.yellow(
path.basename(filePath) + ' already exists'
)} in ${chalk.yellow(
'website' + filePath.split(path.basename(filePath))[0]
)}. Rename or remove the file to regenerate an example version.\n`
);
}
});
} else if (feature === 'versions') {
// copy files for versions
const folder = path.join(__dirname, '..', 'examples', 'versions');
let files = glob.sync(folder + '/**/*');
files.forEach(file => {
if (fs.lstatSync(file).isDirectory()) {
return;
}
const filePath = path.resolve(file).split(path.resolve(folder))[1];
try {
fs.copySync(file, CWD + filePath, {
overwrite: false,
errorOnExist: true,
});
exampleSiteCreated = true;
} catch (e) {
console.log(
`${chalk.yellow(
path.basename(filePath) + ' already exists'
)} in ${chalk.yellow(
'website' + filePath.split(path.basename(filePath))[0]
)}. Rename or remove the file to regenerate an example version.\n`
);
}
});
} else {
const folder = path.join(__dirname, '..', 'examples', 'basics');
// copy docs examples
if (fs.existsSync(CWD + '/../docs-examples-from-docusaurus')) {
console.log(
`${chalk.yellow(
'Example docs already exist!'
)} Rename or remove ${chalk.yellow(
outerFolder + '/docs-examples-from-docusaurus'
)} to regenerate example docs.\n`
);
} else {
fs.copySync(
folder + '/docs-examples-from-docusaurus',
CWD + '/../docs-examples-from-docusaurus'
);
exampleSiteCreated = true;
docsCreated = true;
}
// copy blog examples
if (fs.existsSync(CWD + '/blog-examples-from-docusaurus')) {
console.log(
`${chalk.yellow(
'Example blog posts already exist!'
)} Rename or remove ${chalk.yellow(
outerFolder + '/website/blog-examples-from-docusaurus'
)} to regenerate example blog posts.\n`
);
} else {
fs.copySync(
path.join(folder, 'blog-examples-from-docusaurus'),
path.join(CWD, 'blog-examples-from-docusaurus')
);
exampleSiteCreated = true;
blogCreated = true;
}
// copy .gitignore file
let gitignoreName = '.gitignore';
if (fs.existsSync(CWD + '/../.gitignore')) {
gitignoreName = '.gitignore-example-from-docusaurus';
console.log(
`${chalk.yellow('.gitignore already exists')} in ${chalk.yellow(
CWD
)}. Creating an example gitignore file for you to copy from if desired.\n`
);
}
fs.copySync(
path.join(folder, 'gitignore'),
path.join(CWD, '/../' + gitignoreName)
);
// copy other files
let files = glob.sync(folder + '/**/*');
files.forEach(file => {
if (fs.lstatSync(file).isDirectory()) {
return;
}
const containingFolder = path.basename(path.dirname(file));
if (
path.basename(file) === 'gitignore' ||
containingFolder === 'blog-examples-from-docusaurus' ||
containingFolder === 'docs-examples-from-docusaurus'
) {
return;
}
const filePath = path.resolve(file).split(path.resolve(folder))[1];
try {
fs.copySync(file, CWD + filePath, {
overwrite: false,
errorOnExist: true,
});
exampleSiteCreated = true;
} catch (e) {
console.log(
`${chalk.yellow(
path.basename(filePath) + ' already exists'
)} in ${chalk.yellow(
'website' + filePath.split(path.basename(filePath))[0]
)}. Rename or remove the file to regenerate an example version.\n`
);
}
});
if (exampleSiteCreated) {
const tree = require('tree-node-cli');
const dirString = tree(path.join(CWD, '..'), {
exclude: [/node_modules/],
});
console.log(dirString);
}
}
if (docsCreated) {
console.log(
`Rename ${chalk.yellow(
outerFolder + '/docs-examples-from-docusaurus'
)} to ${chalk.yellow(
outerFolder + '/docs'
)} to see the example docs on your site.\n`
);
}
if (blogCreated) {
console.log(
`Rename ${chalk.yellow(
outerFolder + '/website/blog-examples-from-docusaurus'
)} to ${chalk.yellow(
outerFolder + '/website/blog'
)} to see the example blog posts on your site.\n`
);
}