-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
113 additions
and
90 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,4 +14,4 @@ inputs: | |
description: 'Name of the package' | ||
runs: | ||
using: 'node20' | ||
main: 'index.js' | ||
main: 'action.js' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |