Skip to content

Commit

Permalink
chore: add debug script
Browse files Browse the repository at this point in the history
  • Loading branch information
targos committed Jan 16, 2025
1 parent a6ab798 commit ff635b6
Show file tree
Hide file tree
Showing 6 changed files with 113 additions and 90 deletions.
18 changes: 18 additions & 0 deletions action.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import core from '@actions/core';
import {exec} from '@actions/exec';

import {executeAction} from './src/index.js';

const entry = core.getInput('entry') || 'src/index.ts';
const name = core.getInput('name');
const treatWarningsAsErrors = core.getInput('treatWarningsAsErrors') === 'true';

function onWarn(message) {
core.warning(message)
}

try {
await executeAction({entry, name, treatWarningsAsErrors, onWarn, exec});
} catch (error) {
core.setFailed(error);
}
2 changes: 1 addition & 1 deletion action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ inputs:
description: 'Name of the package'
runs:
using: 'node20'
main: 'index.js'
main: 'action.js'
13 changes: 13 additions & 0 deletions debug.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import {execFileSync} from 'node:child_process';

import {executeAction} from './src/index.js';

function exec(command, args, options) {
return execFileSync(command, args, {stdio:'inherit', ...options});
}

function onWarn(message) {
core.warning(message)
}

await executeAction({entry: 'src/index.ts', name: 'test', treatWarningsAsErrors: false, onWarn, exec});
88 changes: 0 additions & 88 deletions index.js

This file was deleted.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
"version": "2.9.1",
"description": "Build documentation using TypeDoc",
"type": "module",
"main": "index.js",
"repository": {
"type": "git",
"url": "git+https://github.com/zakodium/typedoc-action.git"
Expand Down
81 changes: 81 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import {existsSync, readFileSync, unlinkSync, writeFileSync} from 'node:fs';
import path from 'node:path';

const defaultTsconfig = `{
"compilerOptions": {
"lib": ["DOM", "ESNext"]
}
}
`;

const actionDir = path.join(import.meta.dirname, '..');

export async function executeAction({entry, name, treatWarningsAsErrors, onWarn, exec}) {
const packageJson = getPackageJson();
const tsVersion = getTsVersion(packageJson);
const { hasTsConfig, tsConfigPath } = ensureTsConfig();

if (!existsSync('node_modules')) {
onWarn('node_modules is not present. Running `npm install`...');
await exec('npm', ['install']);
}

writeTypedocJson(entry, name || packageJson.name, tsConfigPath, treatWarningsAsErrors);

if (tsVersion) {
// Install the same version of TypeScript as the project.
await exec('npm', ['install', `typescript@${tsVersion}`], {
cwd: actionDir,
});
}

const args = [
path.join(actionDir, 'node_modules/typedoc/bin/typedoc'),
];

await exec('node', args, {
cwd: actionDir,
});
if (!hasTsConfig) {
unlinkSync(tsConfigPath);
}
}

function writeTypedocJson(entry, name, tsConfigPath, treatWarningsAsErrors) {
/**
* @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,
};
writeFileSync(
path.join(actionDir, 'typedoc.json'),
JSON.stringify(options, null, 2),
);
}

function getPackageJson() {
return JSON.parse(readFileSync('package.json', 'utf-8'));
}

function ensureTsConfig() {
let has = true;
if (!existsSync('tsconfig.json')) {
has = false;
writeFileSync('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;
}

0 comments on commit ff635b6

Please sign in to comment.