-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.mjs
88 lines (75 loc) · 2.34 KB
/
index.mjs
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
import { existsSync } from 'node:fs';
import fs from 'node:fs/promises';
import path from 'node:path';
import { fileURLToPath } from 'node:url';
import core from '@actions/core';
import { exec } from '@actions/exec';
const entry = core.getInput('entry') || 'src/index.ts';
const name = core.getInput('name');
const treatWarningsAsErrors = core.getInput('treatWarningsAsErrors');
const defaultTsconfig = `{
"compilerOptions": {
"lib": ["DOM", "ESNext"]
}
}
`;
try {
const packageJson = await getPackageJson();
const tsVersion = getTsVersion(packageJson);
const { hasTsConfig, tsConfigPath } = await ensureTsConfig();
if (!existsSync('node_modules')) {
core.warning('node_modules is not present. Running `npm install`...');
await exec('npm', ['install']);
}
await writeTypedocJson(name || packageJson.name, tsConfigPath);
if (tsVersion) {
// Install the same version of TypeScript as the project.
await exec('npm', ['install', `typescript@${tsVersion}`]);
}
const args = [
fileURLToPath(new URL('node_modules/typedoc/bin/typedoc', import.meta.url)),
];
await exec('node', args, {
cwd: fileURLToPath(new URL('.', import.meta.url)),
});
if (!hasTsConfig) {
await fs.unlink(tsConfigPath);
}
} catch (error) {
core.setFailed(error);
}
async function writeTypedocJson(name, tsConfigPath) {
/**
* @type {import('typedoc').TypeDocOptions}
*/
const options = {
out: path.resolve('docs'),
name,
excludePrivate: true,
hideGenerator: true,
tsconfig: path.resolve(tsConfigPath),
entryPoints: entry.split(/ +/).map((entry) => path.resolve(entry)),
treatWarningsAsErrors: treatWarningsAsErrors === 'true',
};
await fs.writeFile(
new URL('typedoc.json', import.meta.url),
JSON.stringify(options, null, 2),
);
}
async function getPackageJson() {
return JSON.parse(await fs.readFile('package.json', 'utf-8'));
}
async function ensureTsConfig() {
let has = true;
if (!existsSync('tsconfig.json')) {
has = false;
await fs.writeFile('tsconfig.json', defaultTsconfig);
}
return { hasTsConfig: has, tsConfigPath: path.resolve('tsconfig.json') };
}
function getTsVersion(packageJson) {
const deps = packageJson.dependencies || {};
const devDeps = packageJson.devDependencies || {};
const tsVersion = deps.typescript || devDeps.typescript;
return tsVersion || null;
}