-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbuild.ts
158 lines (142 loc) · 4.01 KB
/
build.ts
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
import { execSync } from 'child_process';
import path from 'path';
import { copyFileSync, createReadStream, mkdirSync, readdirSync, writeFileSync } from 'fs';
import { commonjs } from '@hyrious/esbuild-plugin-commonjs';
import esbuild from 'esbuild';
import rimraf from 'rimraf';
import {
Extractor,
ExtractorConfig,
type ExtractorResult,
} from '@microsoft/api-extractor';
import { createInterface } from 'readline';
const buildCJS = async () => await esbuild.build({
entryPoints: ['src/index.ts'],
bundle: true,
outfile: 'dist/cjs/index.cjs',
minify: true,
sourcemap: true,
format: 'cjs',
});
const buildESMForBrowser = async () => await esbuild.build({
entryPoints: ['src/index.ts'],
bundle: true,
outfile: 'dist/es/height-app-api.mjs',
minify: true,
sourcemap: true,
target: [
'es2020',
'chrome60',
'firefox60',
'safari11',
'edge18',
],
plugins: [
commonjs(),
],
format: 'esm',
platform: 'browser',
});
const buildESMForNode = async () => esbuild.build({
entryPoints: ['src/index.ts'],
bundle: true,
outfile: 'dist/es/index.js',
minify: true,
sourcemap: true,
target: [
'es2020',
'node12',
],
format: 'esm',
plugins: [
commonjs(),
],
platform: 'node',
});
const buildDocs = async () => new Promise<void>((resolve, reject) => {
execSync('tsc src/*.ts --emitDeclarationOnly --outDir types --declaration --lib es2022,dom');
mkdirSync(path.resolve('./dist/docs'));
mkdirSync(path.resolve('./docs/docs/api'));
const extractorConfig: ExtractorConfig = ExtractorConfig.loadFileAndPrepare(
path.resolve('./api-extractor.json'),
);
const extractorResult: ExtractorResult = Extractor.invoke(extractorConfig, {
localBuild: true,
showVerboseMessages: true,
});
if (extractorResult.succeeded) {
execSync('api-documenter markdown -i ./dist/docs/temp -o ./dist/docs');
const docFiles = readdirSync(path.resolve('./dist/docs'));
for (const docFile of docFiles) {
try {
const { name: id, ext } = path.parse(docFile);
if (ext !== '.md') {
continue;
}
const docPath = path.join('./dist/docs', docFile);
const input = createReadStream(docPath);
const output: string[] = [];
const lines = createInterface({
input,
crlfDelay: Infinity,
});
let title = '';
lines.on('line', line => {
let skip = false;
if (title === '') {
const titleLine = line.match(/## (.*)/);
if (titleLine !== null) {
title = titleLine[1];
}
}
const homeLink = line.match(/\[Home\]\(.\/index\.md\) > (.*)/);
if (homeLink !== null) {
if (id !== 'height-app-api') {
output.push(homeLink[1]);
}
skip = true;
}
if (line.startsWith('|')) {
line = line.replace(/\\\|/g, '|');
}
if (!skip) {
output.push(line);
}
});
lines.once('close', () => {
input.close();
const header = [
'---',
`id: ${id}`,
`title: ${title}`,
'hide_title: true',
'displayed_sidebar: null',
'---',
];
writeFileSync(path.join('./docs/docs/api', docFile), header.concat(output).join('\n'));
});
} catch (err) {
console.error(`Could not process ${docFile}:`, err);
}
}
rimraf.sync('types');
console.log('API Extractor completed successfully');
resolve();
} else {
const error = new Error(`API Extractor completed with ${extractorResult.errorCount} errors` +
` and ${extractorResult.warningCount} warnings`);
console.error(error);
reject(error);
}
});
void (async () => {
await rimraf('dist');
await rimraf('docs/docs/api');
await Promise.all([
buildCJS(),
buildESMForBrowser(),
buildESMForNode(),
]);
copyFileSync('openapi.yaml', 'docs/openapi.yaml');
await buildDocs();
})();