Update example files and examples script

This commit is contained in:
Frank Li 2017-08-09 14:18:17 -07:00
parent ac3cc01830
commit c3a971f2eb
24 changed files with 319 additions and 112 deletions

View file

@ -13,34 +13,176 @@ const CWD = process.cwd();
const fs = require("fs-extra");
const path = require("path");
const glob = require("glob");
const chalk = require("chalk");
let examplesFolder = "examples";
if (process.argv.indexOf("translations") !== -1) {
examplesFolder = "examples-translations";
}
const files = glob.sync(path.join(__dirname, "..", examplesFolder, "**"));
files.forEach(file => {
if (!fs.lstatSync(file).isDirectory()) {
let target = CWD;
if (file.includes("-examples")) {
target = path.join(CWD, "..");
}
let fileName = file.substring(
file.indexOf(examplesFolder) + examplesFolder.length
let feature;
const program = require("commander");
program
.arguments("[feature]")
.action(feat => {
feature = feat;
})
.parse(process.argv);
const outerFolder = path.basename(path.dirname(CWD));
if (feature === "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");
console.log(
`${chalk.green("Example crowdin.yaml file created")} in ${chalk.yellow(
outerFolder + "/"
)}.\n`
);
}
let files = glob.sync(folder + "/**/*");
files.forEach(file => {
if (fs.lstatSync(file).isDirectory()) {
return;
}
if (path.basename(file) === "crowdin.yaml") {
return;
}
const filePath = file.split(folder)[1];
try {
fs.copySync(file, path.join(target, fileName), {
fs.copySync(file, CWD + filePath, {
overwrite: false,
errorOnExist: true
});
console.log(fileName + " created in " + path.basename(target));
console.log(
`${chalk.green(
"Example " + path.basename(filePath) + " file created"
)} in ${chalk.yellow(
"website" + filePath.split(path.basename(filePath))[0]
)}.\n`
);
} catch (e) {
console.log(
fileName +
" already exists in " +
path.basename(target) +
". Remove or rename the file to regenerate this example file."
`${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") {
const folder = path.join(__dirname, "..", "examples", "versions");
let files = glob.sync(folder + "/**/*");
files.forEach(file => {
if (fs.lstatSync(file).isDirectory()) {
return;
}
const filePath = file.split(folder)[1];
try {
fs.copySync(file, CWD + filePath, {
overwrite: false,
errorOnExist: true
});
console.log(
`${chalk.green(
"Example " + path.basename(filePath) + " file created"
)} in ${chalk.yellow(
"website" + filePath.split(path.basename(filePath))[0]
)}.\n`
);
} 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"
);
console.log(
`${chalk.green("Example docs created")} in ${chalk.yellow(
outerFolder + "/docs-examples-from-docusaurus"
)}! Rename the folder to ${chalk.yellow(
outerFolder + "/docs"
)} to see the example docs on your site.\n`
);
}
});
// 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")
);
console.log(
`${chalk.green("Example blog posts created")} in ${chalk.yellow(
outerFolder + "/website/blog-examples-from-docusaurus"
)}! Rename the folder to ${chalk.yellow(
outerFolder + "/website/blog"
)} to see the example blog posts on your site.\n`
);
}
// 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 (
containingFolder === "blog-examples-from-docusaurus" ||
containingFolder === "docs-examples-from-docusaurus"
) {
return;
}
const filePath = file.split(folder)[1];
try {
fs.copySync(file, CWD + filePath, {
overwrite: false,
errorOnExist: true
});
console.log(
`${chalk.green(
"Example " + path.basename(filePath) + " file created"
)} in ${chalk.yellow(
"website" + filePath.split(path.basename(filePath))[0]
)}.\n`
);
} 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`
);
}
});
}