Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: improve documents synchronize #262

Merged
merged 2 commits into from
Mar 16, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 51 additions & 13 deletions sync-docs.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ console.log("Start sync-docs.js");

const childProcess = require("child_process");
const fs = require("fs");
const path = require("path");

// NOTE: disable "apisix-docker" "apisix-helm-chart" currently
const projects = ["apisix-ingress-controller", "apisix", "apisix-dashboard"];
Expand All @@ -20,12 +21,7 @@ const projectPaths = projects.map((project) => {
});

const isFileExisted = (path) => {
try {
fs.accessSync(path);
return true;
} catch {
return false;
}
return fs.existsSync(path);
};

const replaceMDElements = (project, path) => {
Expand Down Expand Up @@ -80,6 +76,41 @@ const replaceMDElements = (project, path) => {
}
};

const removeFolder = (tarDir) => {
if (!fs.existsSync(tarDir)) return;

let files = fs.readdirSync(tarDir);
files.forEach((file) => {
const tarPath = path.join(tarDir, file);
let stats = fs.statSync(tarPath);
if (stats.isDirectory()) {
removeFolder(tarPath);
} else {
fs.unlinkSync(tarPath);
}
});

fs.rmdirSync(tarDir);
}

const copyFolder = (srcDir, tarDir) => {
let files = fs.readdirSync(srcDir);
files.forEach(file => {
let srcPath = path.join(srcDir, file);
let tarPath = path.join(tarDir, file);

let stats = fs.statSync(srcPath);
if (stats.isDirectory()) {
if (!fs.existsSync(tarPath)) {
fs.mkdirSync(tarPath);
}
copyFolder(srcPath, tarPath);
} else {
fs.copyFileSync(srcPath, tarPath);
}
});
}

const copyDocs = (source, target, projectName, locale) => {
if (isFileExisted(`${source}/${locale}/latest`) === false) {
console.log(`[${projectName}] can not find ${locale} latest folder, skip.`);
Expand All @@ -95,7 +126,7 @@ const copyDocs = (source, target, projectName, locale) => {
fs.unlinkSync(`${source}/${locale}/latest/config.json`);

console.log(`[${projectName}] copy latest ${locale} docs to ${target}`);
childProcess.execSync(`cp -rf ${source}/${locale}/latest/* ${target}`);
copyFolder(`${source}/${locale}/latest/`, target)

console.log(`[${projectName}] write sidebar.json`);
const sidebar = {
Expand All @@ -107,7 +138,9 @@ const copyDocs = (source, target, projectName, locale) => {
const main = () => {
console.log("Install dependencies");
childProcess.execSync("npm i --save replace-in-file");
childProcess.execSync("mkdir tmp");

removeFolder("tmp");
fs.mkdirSync("tmp");

console.log("Clone repos");
const gitCommand =
Expand All @@ -116,7 +149,7 @@ const main = () => {
(project) =>
`git clone --depth=1 https://github.com/apache/${project}.git`
)
.join(" & ") + " & wait";
.join(" & ");
childProcess.execSync(gitCommand, { cwd: "./tmp" });

console.log("Replace elements inside MD files");
Expand All @@ -141,10 +174,15 @@ const main = () => {
});

console.log("Delete tmp folder");
childProcess.execSync("rm -rf tmp");

console.log("Delete node_modules");
childProcess.execSync("rm -rf package.json package-lock.json node_modules");
removeFolder("tmp");

console.log("Delete npm related files");
removeFolder("node_modules");
["package.json", "package-lock.json"].forEach((file) => {
if (fs.existsSync(file)){
fs.unlinkSync(file);
}
})
};

main();