Skip to content

Commit

Permalink
feat: 增加内网api文档同步工具
Browse files Browse the repository at this point in the history
  • Loading branch information
ostli authored and zoswing committed Dec 27, 2021
1 parent b4494d4 commit 0191ffb
Show file tree
Hide file tree
Showing 8 changed files with 274 additions and 94 deletions.
21 changes: 12 additions & 9 deletions docs/.vuepress/config.js
@@ -1,11 +1,12 @@
const { convertSummary } = require('./summary-sidebar');

// 注意:需要在dev前初始化内网api文档
const apiConfig = require('../develop/api/config');
const nodesdkConfig = require('../develop/nodesdk/config');
const pythonsdkConfig = require('../develop/pythonsdk/config');
const commonConfig = require('./common');
// openapi 外部文档隐藏的接口,注意不要携带.md后缀
// 废弃,请使用 summary-public 来约束可以展示的内容
const hiddenApi = [];

const base = '/wiki/';

module.exports = ctx => ({
base,
configureWebpack: (config, isServer) => {
Expand Down Expand Up @@ -108,10 +109,11 @@ module.exports = ctx => ({
text: '介绍',
link: '/',
},
{
text: 'API文档',
link: '/develop/api/',
},
// {
// text: 'API文档',
// link: '/develop/api/',
// },
apiConfig.nav,
{
text: 'SDK文档',
items: [
Expand All @@ -138,7 +140,8 @@ module.exports = ctx => ({
docsDir: 'docs',
docsBranch: 'main',
sidebar: {
'/develop/api/': convertSummary('./docs/develop/api/SUMMARY-PUBLIC.md', hiddenApi, 1, true),
// '/develop/api/': convertSummary('./docs/develop/api/SUMMARY-PUBLIC.md', hiddenApi, 1, true),
...apiConfig.sidebar,
...nodesdkConfig.sidebar,
...pythonsdkConfig.sidebar,
'/': [''],
Expand Down
79 changes: 0 additions & 79 deletions docs/.vuepress/summary-sidebar.js

This file was deleted.

16 changes: 10 additions & 6 deletions package.json
Expand Up @@ -12,8 +12,10 @@
"@vuepress/plugin-back-to-top": "^1.8.2",
"@vuepress/plugin-pwa": "^1.8.3",
"@vuepress/plugin-search": "^1.8.2",
"chalk": "^4.1.2",
"cross-env": "^7.0.3",
"eslint": "^8.5.0",
"fs-extra": "^10.0.0",
"husky": "^7.0.4",
"lint-staged": "^12.1.3",
"lodash.throttle": "^4.1.1",
Expand All @@ -22,13 +24,15 @@
"remark-lint": "^9.1.1",
"remark-preset-lint-consistent": "^5.1.1",
"remark-preset-lint-recommended": "^6.1.2",
"shelljs": "^0.8.4",
"textlint": "^12.1.0",
"textlint-filter-rule-comments": "^1.2.2",
"textlint-rule-apostrophe": "^2.0.0",
"textlint-rule-common-misspellings": "^1.0.1",
"textlint-rule-diacritics": "^1.0.0",
"textlint-rule-stop-words": "^2.0.9",
"textlint-rule-write-good": "^2.0.0",
"umi-request": "^1.4.0",
"vuepress": "^1.8.2",
"vuepress-plugin-baidu-autopush": "^1.0.1",
"vuepress-plugin-fulltext-search": "^2.2.1",
Expand All @@ -39,17 +43,17 @@
"vuepress-plugin-zooming": "^1.1.8"
},
"scripts": {
"dev": "vuepress dev docs --temp .temp",
"build": "vuepress build docs --temp .temp --dest dist",
"build-test": "cross-env BUILD_ENV=test vuepress build docs --temp .temp --dest dist",
"dev": "npm run sync-api && vuepress dev docs --temp .temp",
"build": "npm run sync-api && vuepress build docs --temp .temp --dest dist",
"build-test": "npm run sync-api && cross-env BUILD_ENV=test vuepress build docs --temp .temp --dest dist",
"lint-md": "npm run lint-md:style && npm run lint-md:wording",
"lint-md:style": "remark --quiet --frail .",
"lint-md:wording": "textlint './docs/**/*.md'",
"view-info": "vuepress view-info docs --temp .temp"
"view-info": "vuepress view-info docs --temp .temp",
"sync-api": "node scripts/sync-inner-docs/index.js"
},
"dependencies": {
"async-validator": "1.11.5",
"element-ui": "^2.15.6",
"umi-request": "^1.4.0"
"element-ui": "^2.15.6"
}
}
12 changes: 12 additions & 0 deletions scripts/sync-inner-docs/default-docs/README.md
@@ -0,0 +1,12 @@
# API 文档

::: warning

目前 api 文档在内网维护,外部用户暂不开放直接维护。

如需共建:

- 内网用户:[工蜂](https://git.woa.com/bbteam_projects/group_pro/docs)
- 外部用户:[Issues](https://github.com/tencent-connect/bot-docs/issues/new)

:::
12 changes: 12 additions & 0 deletions scripts/sync-inner-docs/default-docs/config.js
@@ -0,0 +1,12 @@
// api文档默认模版
const linkConst = '/develop/api/';
module.exports = {
linkConst,
nav: {
text: 'API文档',
link: linkConst,
},
sidebar: {
[linkConst]: [''],
},
};
127 changes: 127 additions & 0 deletions scripts/sync-inner-docs/index.js
@@ -0,0 +1,127 @@
/**
* 内网api文档同步工具
* @author ostwindli
*/
const sh = require('shelljs');
const path = require('path');
const fs = require('fs-extra');
const chalk = require('chalk');
const utils = require('../utils.js');
const { convertSummary } = require('./summary-to-vuepress');
const apiDefaultConfig = require('./default-docs/config.js');

// api文档临时更新目录
const apiDocsTempPath = path.join(__dirname, '../../.temp_docs/');
// api文档临时更新目录-文档具体目录
const apiDocsTempPathBase = path.join(apiDocsTempPath, 'docs/');
// api文档要同步的位置
const apiDocsTargetPath = path.join(__dirname, '../../docs/develop/api/');
fs.ensureDirSync(apiDocsTargetPath);

// 默认api模板位置
const defaultApiTempPath = path.join(__dirname, 'default-docs/')

const apiGit = 'git@git.woa.com:bbteam_projects/group_pro/docs.git';

syncDocs();

async function syncDocs() {
// 先检测下是内网还是外网
const isAtTencent = await utils.isAtTencent();
if (isAtTencent) {
getApiDocs();
syncApiAssets();
} else {
// 外网下,区分下之前内网同步过,还是从没同步过
// 之前同步过,则不做处理,直接用之前同步过的.一般是内部开发者,则无需处理

console.log()
console.log(chalk.green('--------外网api文档同步 start--------'));
const apiDir = fs.readdirSync(apiDocsTargetPath)
if(Array.isArray(apiDir) && ['README.md', 'config.js', 'openapi', 'miniapp'].every(file => apiDir.includes(file))){
console.log(chalk.blue('检测到你之前内网同步过,外网暂不同步!'));
}else{
sh.rm('-rf', apiDocsTargetPath);
fs.copySync(defaultApiTempPath, apiDocsTargetPath)
console.log(chalk.blue('同步默认api模板!'));
}
console.log(chalk.green('--------外网api文档同步 start--------'));

// 从没同步过,则用默认模板,一般是外部用户使用,比如GitHub fork


}
}

// 从工蜂获取文档
function getApiDocs() {
// sh.mkdir(apiDocsTempPath)
if (fs.existsSync(path.join(apiDocsTempPathBase, 'SUMMARY-PUBLIC.md'))) {
console.log(chalk.green('--------拉取最新内网API文档 start--------'));
sh.cd(apiDocsTempPath);
sh.exec('git checkout master');
sh.exec('git pull', { silent: true });
console.log();
console.log(chalk.blue('最新提交记录参考:'));
sh.exec('git log --pretty=format:"%h %cd %an %s" -6');
console.log();
console.log(chalk.green('--------拉取最新内网API文档 end--------'));
console.log();
} else {
console.log(chalk.green('--------clone内网API文档 start--------'));
sh.rm('-rf', apiDocsTempPath);
sh.exec(`git clone -b master ${apiGit} ${apiDocsTempPath}`);
console.log(chalk.green('--------clone内网API文档 end--------'));
}
}

// 同步md和图片文件到目标目录 用于dev、build等
function syncApiAssets() {
console.log(chalk.green('--------同步内网API文档 start--------'));

// 从SUMMARY-PUBLIC.md转换为vuepress需要的格式
const summaryPath = path.join(apiDocsTempPathBase, 'SUMMARY-PUBLIC.md');
const apiLink = convertSummary(summaryPath, 1, true);

console.log(chalk.blue('1、组装需要的api配置文件内容,并写入config.js文件'));
apiDefaultConfig.sidebar[apiDefaultConfig.linkConst] = apiLink;
// 然后写入文件
fs.writeFileSync(
path.join(apiDocsTargetPath, 'config.js'),
`module.exports = ${JSON.stringify(apiDefaultConfig, null, 2)}`,
);

console.log(chalk.blue('2、同步README.md文件'));
fs.copySync(
path.join(apiDocsTempPathBase, 'README.md'),
path.join(apiDocsTargetPath, 'README.md'),
);
console.log(chalk.blue('3、同步其他markdown文件'));
apiLink.forEach(conf => {
if (conf && conf.children) {
conf.children.forEach(file => {
let targetFile = file.split('/');
targetFile.pop();
targetFile = targetFile.join('/');
targetFile = path.join(apiDocsTargetPath, targetFile);

fs.ensureDirSync(targetFile);
fs.copySync(
path.join(apiDocsTempPathBase, `${file}.md`),
path.join(apiDocsTargetPath, `${file}.md`),
);
});
}
});

console.log(chalk.blue('4、同步所有图片文件'));
const imgs = sh
.find(apiDocsTempPathBase)
.filter(file => !file.includes('docs/_book/') && file.match(/\.(png|jpg|gif|jpeg)$/));
imgs.forEach(img => {
fs.copySync(img, img.replace(apiDocsTempPathBase, apiDocsTargetPath));
});

// console.log(imgs);
console.log(chalk.green('--------同步内网API文档 end--------'));
}

0 comments on commit 0191ffb

Please sign in to comment.