-
Notifications
You must be signed in to change notification settings - Fork 2
/
typedoc-runner.js
242 lines (214 loc) · 6.81 KB
/
typedoc-runner.js
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
const { join } = require('upath');
const typedocModule = require('typedoc');
const semver = require('semver');
const { default: git } = require('git-command-helper');
const { mkdirSync, existsSync, writeFileSync, readdirSync, statSync } = require('fs');
const typedocOptions = require('./typedoc');
const pkgjson = require('./package.json');
const { EOL } = require('os');
const { spawnAsync } = require('git-command-helper/dist/spawn');
const axios = require('axios');
const { writeFile } = require('fs/promises');
const fs = require('fs');
const path = require('path');
// required : upath semver typedoc git-command-helper gulp cross-spawn
// update : curl -L https://github.com/dimaslanjaka/nodejs-package-types/raw/main/typedoc-runner.js > typedoc-runner.js
// repo : https://github.com/dimaslanjaka/nodejs-package-types/blob/main/typedoc-runner.js
// usages
// - git clone https://github.com/dimaslanjaka/docs.git
// - node typedoc-runner.js
const REPO_URL = 'https://github.com/dimaslanjaka/docs.git';
let compiled = 0;
/**
* Compile typedocs
* @param {import('typedoc').TypeDocOptions} options
* @param {(...args: any[]) => any} callback
*/
const compile = async function (options = {}, callback = null) {
const outDir = join(__dirname, 'docs');
const projectDocsDir = join(outDir, pkgjson.name);
if (options) setTypedocOptions(options);
if (!existsSync(outDir)) {
await spawnAsync('git', ['clone', REPO_URL, 'docs'], { cwd: __dirname });
}
if (!existsSync(projectDocsDir)) mkdirSync(projectDocsDir, { recursive: true });
// disable delete dir while running twice
if (compiled > 0) setTypedocOptions({ cleanOutputDir: false });
compiled++;
const app = new typedocModule.Application();
if (semver.gte(typedocModule.Application.VERSION, '0.16.1')) {
app.options.addReader(new typedocModule.TSConfigReader());
app.options.addReader(new typedocModule.TypeDocReader());
}
//console.log(options);
//delete options.run;
app.bootstrap(getTypedocOptions());
const project = app.convert();
if (typeof project !== 'undefined') {
await app.generateDocs(project, projectDocsDir);
await app.generateJson(project, join(projectDocsDir, 'info.json'));
} else {
console.error('[error]', 'project undefined');
}
// call API callback
if (typeof callback === 'function') await callback.apply(app);
// call standalone callback
const callback_script = join(__dirname, 'typedoc-callback.js');
if (existsSync(callback_script)) {
await spawnAsync('node', [callback_script], { cwd: __dirname, stdio: 'inherit' });
}
await createIndex();
};
/**
* Compile and publish to github pages
* @param {import('typedoc').TypeDocOptions} options
* @param {(...args: any[]) => any} callback
*/
const publish = async function (options = {}, callback = null) {
console.log('publishing docs');
const outDir = join(__dirname, 'docs');
if (!existsSync(join(outDir))) {
console.log('cloning', REPO_URL);
await new git(__dirname)
.spawn('git', ['clone', REPO_URL, 'docs', '-b', 'master', '--single-branch'], { cwd: __dirname })
.catch(() => console.log('fail clone to /docs'));
}
const github = new git(outDir);
//await github.reset('master');
await github
.pull(['-X', 'theirs'])
.then(() => console.log('success pull'))
.catch(() => console.log('fail pull'));
await github
.spawn('git', 'config core.eol lf'.split(' '))
.then(() => console.log('success set EOL LF'))
.catch(() => console.log('fail set EOL LF'));
for (let i = 0; i < 2; i++) {
await compile(options, callback).catch(() => console.log('publish fail to compile'));
}
const response = await axios.default.get(
'https://raw.githubusercontent.com/dimaslanjaka/nodejs-package-types/main/.gitattributes',
{
responseType: 'blob'
}
);
writeFile(join(outDir, '.gitattributes'), response.data, (err) => {
if (err) throw err;
console.log('.gitattributes has been saved!');
});
try {
const commit = await new git(__dirname).latestCommit().catch(noop);
const remote = (await new git(__dirname).getremote().catch(noop)).push.url.replace(/.git$/, '').trim();
if (remote.length > 0) {
console.log('current git project', remote);
await github.add(pkgjson.name).catch(noop);
await spawnAsync('git', [
'commit',
'-m',
`update ${pkgjson.name} docs [${remote}/commit/${commit}]`,
'-m',
`at ${new Date()}`
]);
const isCanPush = await github.canPush().catch(noop);
if (isCanPush) {
await github.push().catch(noop);
}
}
} catch {
//
}
};
function noop(..._) {
return;
}
let opt = typedocOptions;
/**
* Get typedoc options
* @returns {typeof import('./typedoc')}
*/
function getTypedocOptions() {
return opt;
}
/**
* Set typedoc options
* @param {typeof import('./typedoc')} newOpt
*/
function setTypedocOptions(newOpt) {
opt = Object.assign(opt, newOpt || {});
writefile(join(__dirname, 'tmp/typedocs/options.json'), JSON.stringify(opt));
return opt;
}
if (require.main === module) {
(async () => {
const argv = process.argv;
// node typedoc-runner.js --publish
if (argv.includes('--publish')) {
console.log('[publish] start');
await publish();
console.log('[publish] finish');
} else {
console.log('[compile] start');
await compile();
console.log('[compile] finish');
}
})();
} else {
//console.log('required as a module');
}
/**
* create docs/readme.md
*/
async function createIndex() {
let body =
`
<h1 id="headline">Monorepo Documentation Site</h1>
`.trim() + EOL;
readdirSync(join(__dirname, 'docs')).forEach((filename) => {
const path = join(__dirname, 'docs', filename);
const stat = statSync(path);
if (stat.isDirectory() && filename !== '.git') {
body +=
`
<li><a href="./${filename}">${filename}</a></li>
`.trim() + EOL;
}
});
writeFileSync(join(__dirname, 'docs/index.html'), body.trim());
}
/**
* read file with validation
* @param {string} str
* @param {import('fs').EncodingOption} encoding
* @returns
*/
// eslint-disable-next-line @typescript-eslint/no-unused-vars
function readfile(str, encoding = 'utf-8') {
if (fs.existsSync(str)) {
if (fs.statSync(str).isFile()) {
return fs.readFileSync(str, encoding);
} else {
throw str + ' is directory';
}
} else {
throw str + ' not found';
}
}
/**
* write to file recursively
* @param {string} dest
* @param {any} data
*/
function writefile(dest, data) {
if (!fs.existsSync(path.dirname(dest))) fs.mkdirSync(path.dirname(dest), { recursive: true });
if (fs.existsSync(dest)) {
if (fs.statSync(dest).isDirectory()) throw dest + ' is directory';
}
fs.writeFileSync(dest, data);
}
module.exports = {
compile,
compileDocs: compile,
publish,
getTypedocOptions,
setTypedocOptions
};