-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
Copy pathbuild.ts
executable file
·132 lines (106 loc) · 4 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
#! /usr/bin/env ts-node
import { parse, stringify } from '@iarna/toml';
import { exec as execCb } from 'child_process';
import { readFile, writeFile } from 'fs/promises';
import { chdir } from 'process';
import { promisify } from 'util';
import {
confirm,
customSemverCompare,
getCommandLineArguments,
type JsonVersionSchema,
LATEST_TAG,
log,
type TomlVersionSchema,
type VersionSchema
} from './utils';
const exec = promisify(execCb);
const RELEASES_TOML_FILE = './template/data/releases.toml';
const RELEASES_JSON_FILE = './template/static/versions.json';
const copyGeneratedDocsToDocsFolder = () => exec(`cp -R temp/. ../../docs/.`);
const removeTempDirectory = () => exec('rm -rf temp');
const installDependencies = () => exec('npm i --no-save --legacy-peer-deps typedoc@0.26.7');
const buildDocs = ({ tag }: VersionSchema) => {
const revision = tag === LATEST_TAG ? 'main' : `v${tag}.0`;
return exec(`npm run build:typedoc -- --gitRevision ${revision}`);
};
async function copyNewDocsToGeneratedSite({ tag }: VersionSchema) {
const outputDirectory = `./temp/${tag}`;
const pathToBuiltDocs = './build';
const command = `cp -R ${pathToBuiltDocs} ${outputDirectory}`;
return await exec(command);
}
async function updateSiteTemplateForNewVersion(
newVersion: VersionSchema,
tomlData: TomlVersionSchema,
jsonVersions: JsonVersionSchema[]
) {
const versionExists = jsonVersions.some(({ version }) => version === newVersion.tag);
if (versionExists) {
const existingVersionIndex = tomlData.versions.findIndex(({ tag }) => tag === newVersion.tag);
tomlData.versions[existingVersionIndex] = newVersion;
} else {
for (const version of tomlData.versions) {
// This new version is going to be the latest, we have to change the previous one to supported
if (version.status === 'latest') {
version.status = 'supported';
}
}
tomlData.versions.unshift(newVersion);
jsonVersions.unshift({ version: newVersion.tag });
}
tomlData.versions.sort((a, b) => customSemverCompare(a.tag, b.tag));
tomlData.current = tomlData.versions.find(
({ tag }) => tag.toLowerCase() !== LATEST_TAG.toLowerCase()
).version;
jsonVersions.sort((a, b) => customSemverCompare(a.version, b.version));
await writeFile(RELEASES_TOML_FILE, stringify(tomlData as any));
await writeFile(RELEASES_JSON_FILE, JSON.stringify(jsonVersions, null, 4));
// generate the site from the template
await exec(`hugo -s template -d ../temp -b "/node-mongodb-native"`);
}
async function main() {
try {
await exec('bash ./etc/check-remote.sh');
} catch (error) {
console.error(error.stdout);
process.exit(1);
}
chdir(__dirname);
const { tag, status, skipPrompts } = getCommandLineArguments();
const newVersion: VersionSchema = {
version: `${tag} Driver`,
status,
api: `./${tag}`,
usesMongoDBManual: true,
tag
};
if (!skipPrompts) {
await confirm(`
Generating docs for the following configuration.\n${JSON.stringify(newVersion, null, 2)}
Does this look right? [y/n] `);
}
log('Installing dependencies...');
await installDependencies();
log('Building docs for current branch');
await buildDocs(newVersion);
log('Generating new static site...');
const tomlVersions = parse(
await readFile(RELEASES_TOML_FILE, { encoding: 'utf8' })
) as unknown as TomlVersionSchema;
const jsonVersions = JSON.parse(
await readFile(RELEASES_JSON_FILE, { encoding: 'utf8' })
) as unknown as JsonVersionSchema[];
const versionAlreadyExists = jsonVersions.some(({ version }) => version === tag);
if (versionAlreadyExists && !skipPrompts) {
await confirm(
`Version ${tag} already exists. Do you want to override the existing docs? [y/n] `
);
}
await updateSiteTemplateForNewVersion(newVersion, tomlVersions, jsonVersions);
await copyNewDocsToGeneratedSite(newVersion);
await copyGeneratedDocsToDocsFolder();
await removeTempDirectory();
log('Successfully generated api documentation and updated the doc site.');
}
main();