paricafe/src/web/docs/api/gulpfile.ts

157 lines
3.5 KiB
TypeScript
Raw Normal View History

2017-12-14 01:24:41 -06: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-14 09:23:45 -06:00
import config from './../../../conf';
const kebab = string => string.replace(/([a-z])([A-Z])/g, '$1-$2').replace(/\s+/g, '-').toLowerCase();
2017-12-14 01:24:41 -06:00
const parseParam = param => {
2017-12-14 09:23:45 -06:00
const id = param.type.match(/^id\((.+?)\)|^id/);
2017-12-14 07:36:04 -06:00
const entity = param.type.match(/^entity\((.+?)\)/);
const isObject = /^object/.test(param.type);
2017-12-14 09:23:45 -06:00
const isDate = /^date/.test(param.type);
2017-12-14 01:24:41 -06: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 07:36:04 -06:00
if (entity) {
param.kind = 'entity';
2017-12-14 01:24:41 -06:00
param.type = 'object';
2017-12-14 07:36:04 -06:00
param.entity = entity[1];
2017-12-14 01:24:41 -06:00
if (isArray) {
param.type += '[]';
}
}
2017-12-14 07:36:04 -06:00
if (isObject) {
param.kind = 'object';
}
2017-12-14 09:23:45 -06:00
if (isDate) {
param.kind = 'date';
param.type = 'string';
if (isArray) {
param.type += '[]';
}
}
2017-12-14 01:24:41 -06:00
return param;
};
2017-12-14 09:23:45 -06: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 07:36:04 -06:00
const extractDefs = params => {
2017-12-14 09:23:45 -06:00
let defs = [];
2017-12-14 07:36:04 -06:00
params.forEach(param => {
if (param.def) {
defs.push({
name: param.defName,
2017-12-14 09:23:45 -06:00
params: sortParams(param.def.map(p => parseParam(p)))
2017-12-14 07:36:04 -06:00
});
const childDefs = extractDefs(param.def);
2017-12-14 09:23:45 -06:00
defs = defs.concat(childDefs);
2017-12-14 07:36:04 -06:00
}
});
2017-12-14 09:54:28 -06:00
return sortParams(defs);
2017-12-14 07:36:04 -06:00
};
2017-12-14 09:23:45 -06:00
gulp.task('doc:api', [
'doc:api:endpoints',
'doc:api:entities'
]);
gulp.task('doc:api:endpoints', () => {
2017-12-14 01:24:41 -06: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,
url: `${config.api_url}/${ep.endpoint}`,
desc: ep.desc,
2017-12-14 09:23:45 -06:00
params: sortParams(ep.params.map(p => parseParam(p))),
2017-12-14 07:36:04 -06:00
paramDefs: extractDefs(ep.params),
2017-12-14 09:23:45 -06:00
res: sortParams(ep.res.map(p => parseParam(p))),
resDefs: extractDefs(ep.res),
kebab
2017-12-14 01:24:41 -06:00
};
pug.renderFile('./src/web/docs/api/endpoints/view.pug', vars, (renderErr, html) => {
if (renderErr) {
console.error(renderErr);
return;
}
const htmlPath = `./built/web/docs/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 09:23:45 -06: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),
kebab
};
pug.renderFile('./src/web/docs/api/entities/view.pug', vars, (renderErr, html) => {
if (renderErr) {
console.error(renderErr);
return;
}
const htmlPath = `./built/web/docs/api/entities/${kebab(entity.name)}.html`;
mkdirp(path.dirname(htmlPath), (mkdirErr) => {
if (mkdirErr) {
console.error(mkdirErr);
return;
}
fs.writeFileSync(htmlPath, html, 'utf-8');
});
});
});
});
});