sharkey/src/client/docs/api/gulpfile.ts

189 lines
4.6 KiB
TypeScript
Raw Normal View History

2017-12-14 16:24:41 +09:00
/**
* Gulp tasks
*/
import * as fs from 'fs';
import * as path from 'path';
import * as glob from 'glob';
import * as gulp from 'gulp';
import * as pug from 'pug';
import * as yaml from 'js-yaml';
import * as mkdirp from 'mkdirp';
2018-03-29 20:32:18 +09:00
import locales from '../../../../locales';
import I18nReplacer from '../../../build/i18n';
import fa from '../../../build/fa';
2018-04-02 13:15:53 +09:00
import config from './../../../config';
2017-12-15 00:23:45 +09:00
2017-12-15 06:41:57 +09:00
import generateVars from '../vars';
2017-12-17 14:35:30 +09:00
const langs = Object.keys(locales);
2017-12-15 06:41:57 +09:00
2017-12-15 00:23:45 +09:00
const kebab = string => string.replace(/([a-z])([A-Z])/g, '$1-$2').replace(/\s+/g, '-').toLowerCase();
2017-12-14 16:24:41 +09:00
const parseParam = param => {
2017-12-15 00:23:45 +09:00
const id = param.type.match(/^id\((.+?)\)|^id/);
2017-12-14 22:36:04 +09:00
const entity = param.type.match(/^entity\((.+?)\)/);
const isObject = /^object/.test(param.type);
2017-12-15 00:23:45 +09:00
const isDate = /^date/.test(param.type);
2017-12-14 16:24:41 +09:00
const isArray = /\[\]$/.test(param.type);
if (id) {
param.kind = 'id';
param.type = 'string';
param.entity = id[1];
if (isArray) {
param.type += '[]';
}
}
2017-12-14 22:36:04 +09:00
if (entity) {
param.kind = 'entity';
2017-12-14 16:24:41 +09:00
param.type = 'object';
2017-12-14 22:36:04 +09:00
param.entity = entity[1];
2017-12-14 16:24:41 +09:00
if (isArray) {
param.type += '[]';
}
}
2017-12-14 22:36:04 +09:00
if (isObject) {
param.kind = 'object';
}
2017-12-15 00:23:45 +09:00
if (isDate) {
param.kind = 'date';
param.type = 'string';
if (isArray) {
param.type += '[]';
}
}
2017-12-14 16:24:41 +09:00
return param;
};
2017-12-15 00:23:45 +09:00
const sortParams = params => {
params.sort((a, b) => {
if (a.name < b.name)
return -1;
if (a.name > b.name)
return 1;
return 0;
});
return params;
};
2017-12-14 22:36:04 +09:00
const extractDefs = params => {
2017-12-15 00:23:45 +09:00
let defs = [];
2017-12-14 22:36:04 +09:00
params.forEach(param => {
if (param.def) {
defs.push({
name: param.defName,
2017-12-15 00:23:45 +09:00
params: sortParams(param.def.map(p => parseParam(p)))
2017-12-14 22:36:04 +09:00
});
const childDefs = extractDefs(param.def);
2017-12-15 00:23:45 +09:00
defs = defs.concat(childDefs);
2017-12-14 22:36:04 +09:00
}
});
2017-12-15 00:54:28 +09:00
return sortParams(defs);
2017-12-14 22:36:04 +09:00
};
2017-12-15 00:23:45 +09:00
gulp.task('doc:api', [
'doc:api:endpoints',
'doc:api:entities'
]);
2018-01-08 01:47:56 +09:00
gulp.task('doc:api:endpoints', async () => {
const commonVars = await generateVars();
2018-03-29 20:32:18 +09:00
glob('./src/client/docs/api/endpoints/**/*.yaml', (globErr, files) => {
2017-12-14 16:24:41 +09:00
if (globErr) {
console.error(globErr);
return;
}
//console.log(files);
files.forEach(file => {
2018-04-01 12:37:53 +09:00
const ep: any = yaml.safeLoad(fs.readFileSync(file, 'utf-8'));
2017-12-14 16:24:41 +09:00
const vars = {
endpoint: ep.endpoint,
2017-12-17 04:02:30 +09:00
url: {
host: config.api_url,
path: ep.endpoint
},
2017-12-14 16:24:41 +09:00
desc: ep.desc,
2017-12-15 00:23:45 +09:00
params: sortParams(ep.params.map(p => parseParam(p))),
2017-12-14 22:36:04 +09:00
paramDefs: extractDefs(ep.params),
2017-12-16 05:04:02 +09:00
res: ep.res ? sortParams(ep.res.map(p => parseParam(p))) : null,
resDefs: ep.res ? extractDefs(ep.res) : null,
2017-12-14 16:24:41 +09:00
};
2017-12-15 06:41:57 +09:00
langs.forEach(lang => {
2018-03-29 20:32:18 +09:00
pug.renderFile('./src/client/docs/api/endpoints/view.pug', Object.assign({}, vars, {
2017-12-16 00:19:10 +09:00
lang,
title: ep.endpoint,
2018-03-29 20:32:18 +09:00
src: `https://github.com/syuilo/misskey/tree/master/src/client/docs/api/endpoints/${ep.endpoint}.yaml`,
2017-12-16 00:19:10 +09:00
kebab,
common: commonVars
2017-12-15 06:41:57 +09:00
}), (renderErr, html) => {
if (renderErr) {
console.error(renderErr);
2017-12-14 16:24:41 +09:00
return;
}
2017-12-17 14:35:30 +09:00
const i18n = new I18nReplacer(lang);
html = html.replace(i18n.pattern, i18n.replacement);
html = fa(html);
2018-03-29 20:32:18 +09:00
const htmlPath = `./built/client/docs/${lang}/api/endpoints/${ep.endpoint}.html`;
2017-12-15 06:41:57 +09:00
mkdirp(path.dirname(htmlPath), (mkdirErr) => {
if (mkdirErr) {
console.error(mkdirErr);
return;
}
fs.writeFileSync(htmlPath, html, 'utf-8');
});
2017-12-14 16:24:41 +09:00
});
});
});
});
});
2017-12-15 00:23:45 +09:00
2018-01-08 01:47:56 +09:00
gulp.task('doc:api:entities', async () => {
const commonVars = await generateVars();
2018-03-29 20:32:18 +09:00
glob('./src/client/docs/api/entities/**/*.yaml', (globErr, files) => {
2017-12-15 00:23:45 +09:00
if (globErr) {
console.error(globErr);
return;
}
files.forEach(file => {
const entity = yaml.safeLoad(fs.readFileSync(file, 'utf-8'));
const vars = {
name: entity.name,
desc: entity.desc,
props: sortParams(entity.props.map(p => parseParam(p))),
propDefs: extractDefs(entity.props),
};
2017-12-15 06:41:57 +09:00
langs.forEach(lang => {
2018-03-29 20:32:18 +09:00
pug.renderFile('./src/client/docs/api/entities/view.pug', Object.assign({}, vars, {
2017-12-16 00:19:10 +09:00
lang,
title: entity.name,
2018-03-29 20:32:18 +09:00
src: `https://github.com/syuilo/misskey/tree/master/src/client/docs/api/entities/${kebab(entity.name)}.yaml`,
2017-12-16 00:19:10 +09:00
kebab,
common: commonVars
2017-12-15 06:41:57 +09:00
}), (renderErr, html) => {
if (renderErr) {
console.error(renderErr);
2017-12-15 00:23:45 +09:00
return;
}
2017-12-17 14:35:30 +09:00
const i18n = new I18nReplacer(lang);
html = html.replace(i18n.pattern, i18n.replacement);
html = fa(html);
2018-03-29 20:32:18 +09:00
const htmlPath = `./built/client/docs/${lang}/api/entities/${kebab(entity.name)}.html`;
2017-12-15 06:41:57 +09:00
mkdirp(path.dirname(htmlPath), (mkdirErr) => {
if (mkdirErr) {
console.error(mkdirErr);
return;
}
fs.writeFileSync(htmlPath, html, 'utf-8');
});
2017-12-15 00:23:45 +09:00
});
});
});
});
});