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';
|
|
|
|
|
2017-12-17 14:35:30 +09:00
|
|
|
import locales from '../../../../locales';
|
|
|
|
import I18nReplacer from '../../../common/build/i18n';
|
|
|
|
import fa from '../../../common/build/fa';
|
2017-12-15 00:23:45 +09:00
|
|
|
import config from './../../../conf';
|
|
|
|
|
2017-12-15 06:41:57 +09:00
|
|
|
import generateVars from '../vars';
|
|
|
|
|
|
|
|
const commonVars = generateVars();
|
|
|
|
|
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'
|
|
|
|
]);
|
|
|
|
|
|
|
|
gulp.task('doc:api:endpoints', () => {
|
2017-12-14 16:24:41 +09:00
|
|
|
glob('./src/web/docs/api/endpoints/**/*.yaml', (globErr, files) => {
|
|
|
|
if (globErr) {
|
|
|
|
console.error(globErr);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
//console.log(files);
|
|
|
|
files.forEach(file => {
|
|
|
|
const ep = yaml.safeLoad(fs.readFileSync(file, 'utf-8'));
|
|
|
|
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 => {
|
|
|
|
pug.renderFile('./src/web/docs/api/endpoints/view.pug', Object.assign({}, vars, {
|
2017-12-16 00:19:10 +09:00
|
|
|
lang,
|
|
|
|
title: ep.endpoint,
|
2017-12-17 04:02:30 +09:00
|
|
|
src: `https://github.com/syuilo/misskey/tree/master/src/web/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);
|
2017-12-15 06:41:57 +09:00
|
|
|
const htmlPath = `./built/web/docs/${lang}/api/endpoints/${ep.endpoint}.html`;
|
|
|
|
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
|
|
|
|
|
|
|
gulp.task('doc:api:entities', () => {
|
|
|
|
glob('./src/web/docs/api/entities/**/*.yaml', (globErr, files) => {
|
|
|
|
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 => {
|
|
|
|
pug.renderFile('./src/web/docs/api/entities/view.pug', Object.assign({}, vars, {
|
2017-12-16 00:19:10 +09:00
|
|
|
lang,
|
|
|
|
title: entity.name,
|
2017-12-17 04:02:30 +09:00
|
|
|
src: `https://github.com/syuilo/misskey/tree/master/src/web/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);
|
2017-12-15 06:41:57 +09:00
|
|
|
const htmlPath = `./built/web/docs/${lang}/api/entities/${kebab(entity.name)}.html`;
|
|
|
|
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
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|