summaryrefslogtreecommitdiff
path: root/src/client/docs/api/gulpfile.ts
blob: 31027c0be37e3d57b77b5e07adb322470cbe49c7 (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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/**
 * 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 locales from '../../../../locales';
import I18nReplacer from '../../../build/i18n';
import fa from '../../../build/fa';
import config from './../../../config';

import generateVars from '../vars';

const langs = Object.keys(locales);

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', async () => {
	const commonVars = await generateVars();
	glob('./src/client/docs/api/endpoints/**/*.yaml', (globErr, files) => {
		if (globErr) {
			console.error(globErr);
			return;
		}
		//console.log(files);
		files.forEach(file => {
			const ep: any = yaml.safeLoad(fs.readFileSync(file, 'utf-8'));
			const vars = {
				endpoint: ep.endpoint,
				url: {
					host: config.api_url,
					path: 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/client/docs/api/endpoints/view.pug', Object.assign({}, vars, {
					lang,
					title: ep.endpoint,
					src: `https://github.com/syuilo/misskey/tree/master/src/client/docs/api/endpoints/${ep.endpoint}.yaml`,
					kebab,
					common: commonVars
				}), (renderErr, html) => {
					if (renderErr) {
						console.error(renderErr);
						return;
					}
					const i18n = new I18nReplacer(lang);
					html = html.replace(i18n.pattern, i18n.replacement.bind(null, null));
					html = fa(html);
					const htmlPath = `./built/client/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', async () => {
	const commonVars = await generateVars();
	glob('./src/client/docs/api/entities/**/*.yaml', (globErr, files) => {
		if (globErr) {
			console.error(globErr);
			return;
		}
		files.forEach(file => {
			const entity = yaml.safeLoad(fs.readFileSync(file, 'utf-8')) as any;
			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/client/docs/api/entities/view.pug', Object.assign({}, vars, {
					lang,
					title: entity.name,
					src: `https://github.com/syuilo/misskey/tree/master/src/client/docs/api/entities/${kebab(entity.name)}.yaml`,
					kebab,
					common: commonVars
				}), (renderErr, html) => {
					if (renderErr) {
						console.error(renderErr);
						return;
					}
					const i18n = new I18nReplacer(lang);
					html = html.replace(i18n.pattern, i18n.replacement.bind(null, null));
					html = fa(html);
					const htmlPath = `./built/client/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');
					});
				});
			});
		});
	});
});