summaryrefslogtreecommitdiff
path: root/src/web/docs/api/gulpfile.ts
blob: 908280453cfd6c88f78118e7b77e49495667ceeb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/**
 * 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';

import config from './../../../conf';

import generateVars from '../vars';

const commonVars = generateVars();

const langs = ['ja', 'en'];

const kebab = string => string.replace(/([a-z])([A-Z])/g, '$1-$2').replace(/\s+/g, '-').toLowerCase();

const parseParam = param => {
	const id = param.type.match(/^id\((.+?)\)|^id/);
	const entity = param.type.match(/^entity\((.+?)\)/);
	const isObject = /^object/.test(param.type);
	const isDate = /^date/.test(param.type);
	const isArray = /\[\]$/.test(param.type);
	if (id) {
		param.kind = 'id';
		param.type = 'string';
		param.entity = id[1];
		if (isArray) {
			param.type += '[]';
		}
	}
	if (entity) {
		param.kind = 'entity';
		param.type = 'object';
		param.entity = entity[1];
		if (isArray) {
			param.type += '[]';
		}
	}
	if (isObject) {
		param.kind = 'object';
	}
	if (isDate) {
		param.kind = 'date';
		param.type = 'string';
		if (isArray) {
			param.type += '[]';
		}
	}

	return param;
};

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;
};

const extractDefs = params => {
	let defs = [];

	params.forEach(param => {
		if (param.def) {
			defs.push({
				name: param.defName,
				params: sortParams(param.def.map(p => parseParam(p)))
			});

			const childDefs = extractDefs(param.def);

			defs = defs.concat(childDefs);
		}
	});

	return sortParams(defs);
};

gulp.task('doc:api', [
	'doc:api:endpoints',
	'doc:api:entities'
]);

gulp.task('doc:api:endpoints', () => {
	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,
				params: sortParams(ep.params.map(p => parseParam(p))),
				paramDefs: extractDefs(ep.params),
				res: ep.res ? sortParams(ep.res.map(p => parseParam(p))) : null,
				resDefs: ep.res ? extractDefs(ep.res) : null,
			};
			langs.forEach(lang => {
				pug.renderFile('./src/web/docs/api/endpoints/view.pug', Object.assign({}, vars, {
					lang,
					title: ep.endpoint,
					kebab,
					common: commonVars
				}), (renderErr, html) => {
					if (renderErr) {
						console.error(renderErr);
						return;
					}
					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');
					});
				});
			});
		});
	});
});

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),
			};
			langs.forEach(lang => {
				pug.renderFile('./src/web/docs/api/entities/view.pug', Object.assign({}, vars, {
					lang,
					title: entity.name,
					kebab,
					common: commonVars
				}), (renderErr, html) => {
					if (renderErr) {
						console.error(renderErr);
						return;
					}
					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');
					});
				});
			});
		});
	});
});