Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bump to v1.0.5, strict type checks, generate types #20

Merged
merged 1 commit into from
Jan 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"env" : {
"env": {
"node": true,
"es2023" : true
"es2023": true
},
"parserOptions": {
"ecmaVersion": "latest",
Expand All @@ -25,7 +25,7 @@
]
}
],
"rules" : {
"rules": {
"@stylistic/js/comma-dangle": [
"error", "never"
],
Expand All @@ -50,5 +50,12 @@
"no-console": [
"error", { "allow": [ "warn", "error" ]}
]
},
"settings": {
"jsdoc": {
"preferredTypes": {
"Object": "object"
}
}
}
}
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,6 @@ node_modules/
out/
pnpm-debug.log
tmp/
types/
*.log
*.tgz
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ Running the wrapper will generate the local `file://` URL to the generated
`index.html` file, e.g.:

```text
file:///path/to/jsdoc/output/jsdoc-cli-wrapper/1.0.4/index.html
file:///path/to/jsdoc/output/jsdoc-cli-wrapper/1.0.5/index.html
```

You can click on or copy this link to open it in your browser. You can also open
Expand Down Expand Up @@ -99,10 +99,10 @@ This wrapper resolves both of these minor annoyances.
```sh
$ pnpm jsdoc

> jsdoc-cli-wrapper@1.0.4 jsdoc /path/to/jsdoc-cli-wrapper
> jsdoc-cli-wrapper@1.0.5 jsdoc /path/to/jsdoc-cli-wrapper
> node index.js -c jsdoc.json .

file:///path/to/jsdoc-cli-wrapper/jsdoc/jsdoc-cli-wrapper/1.0.4/index.html
file:///path/to/jsdoc-cli-wrapper/jsdoc/jsdoc-cli-wrapper/1.0.5/index.html
```

Of course, your own project would use `jsdoc-cli-wrapper` instead of `node
Expand Down
1 change: 1 addition & 0 deletions ci/vitest.config.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// @ts-nocheck
import { defineConfig, mergeConfig } from 'vitest/config'
import baseConfig from '../vitest.config'

Expand Down
16 changes: 16 additions & 0 deletions jsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"compilerOptions": {
"checkJs": true,
"lib": [
"ES2022"
],
"module": "node16",
"target": "es2020",
"strict": true
},
"exclude": [
"node_modules/**",
"coverage/**",
"jsdoc/**"
]
}
31 changes: 21 additions & 10 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,30 +8,32 @@ import { spawn } from 'node:child_process'
import { access, readdir, readFile, rm } from 'node:fs/promises'
import path from 'node:path'

export const INSTALL_HINT = 'Run \'pnpm add [-g|-D] jsdoc\' ' +
'to install JSDoc: https://jsdoc.app'

/**
* Result of the `jsdoc` execution
* @typedef {object} RunJsdocResults
* @property {number} exitCode - 0 on success, nonzero on failure
* @property {string} indexHtml - path to the generated index.html file
* @property {string} [indexHtml] - path to the generated index.html file
*/

/**
* Removes the existing JSDoc directory, runs `jsdoc`, and emits the result path
* @param {string[]} argv - JSDoc command line interface arguments
* @param {object} env - environment variables, presumably process.env
* @param {EnvVars} env - environment variables, presumably process.env
* @param {string} platform - the process.platform string
* @returns {Promise<RunJsdocResults>} result of `jsdoc` execution
* @throws if `jsdoc` isn't found or can't execute
*/
export async function runJsdoc(argv, env, platform) {
/** @type {string} */
let jsdocPath

try {
jsdocPath = await getPath('jsdoc', env, platform)
} catch {
return Promise.reject(
'Run \'pnpm add -g jsdoc\' to install JSDoc: https://jsdoc.app'
)
} catch (err) {
return Promise.reject(err instanceof Error ? err : INSTALL_HINT)
}

const {destination, willGenerate} = await analyzeArgv(argv)
Expand Down Expand Up @@ -62,22 +64,26 @@ export async function runJsdoc(argv, env, platform) {
*/
export const pathKey = platform => platform !== 'win32' ? 'PATH' : 'Path'

/** @typedef {Object<string,string|undefined>} EnvVars */

/**
* Returns the full path to the specified command
* @param {string} cmdName - command to find in env[pathKey(platform)]
* @param {object} env - environment variables, presumably process.env
* @param {EnvVars} env - environment variables, presumably process.env
* @param {string} platform - the process.platform string
* @returns {Promise<string>} path to the command
* @throws if `jsdoc` isn't found
*/
export async function getPath(cmdName, env, platform) {
const pk = pathKey(platform)
const pathVar = env[pk]
if (!pathVar) throw new Error(`"${pk}" environment variable not defined`)

// pnpm will install both the original script and versions ending with .CMD
// and .ps1. We'll just default to .CMD.
if (platform === 'win32') cmdName += '.CMD'

for (const p of env[pk].split(path.delimiter)) {
for (const p of pathVar.split(path.delimiter)) {
try {
const candidate = path.join(p, cmdName)
await access(candidate)
Expand All @@ -103,7 +109,11 @@ export async function getPath(cmdName, env, platform) {
* @returns {Promise<ArgvResults>} analysis results
*/
export async function analyzeArgv(argv) {
const validArg = nextArg => nextArg !== undefined && !nextArg.startsWith('-')
/**
* @param {(string|undefined)} nextArg - next argument after current one
* @returns {boolean} true if defined and not another flag, false otherwise
*/
function validArg(nextArg) { return !!nextArg && !nextArg.startsWith('-') }
let destination, cmdLineDest, willGenerate = true

for (let i = 0; i !== argv.length; ++i) {
Expand Down Expand Up @@ -178,7 +188,8 @@ export async function analyzeArgv(argv) {
* @returns {string} str with comments, trailing commas replaced by space
*/
export function stripJsonComments(str) {
let inString, escaped, comment, comma, result = []
let inString = false, escaped = false, comment = null, comma = null
let result = []

for (let i = 0; i !== str.length; ++i) {
let c = str[i]
Expand Down
30 changes: 20 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
{
"name": "jsdoc-cli-wrapper",
"version": "1.0.4",
"version": "1.0.5",
"description": "JSDoc command line interface wrapper",
"main": "index.js",
"bin": "./index.js",
"types": "./types/index.d.ts",
"scripts": {
"lint": "eslint --color --max-warnings 0 .",
"test": "vitest",
"test:ci": "eslint --color --max-warnings 0 . && vitest run -c ci/vitest.config.js",
"jsdoc": "node index.js -c jsdoc.json ."
"test:ci": "pnpm lint && pnpm typecheck && vitest run -c ci/vitest.config.js && pnpm jsdoc",
"typecheck": "npx -p typescript tsc -p jsconfig.json --noEmit --pretty",
"jsdoc": "node index.js -c jsdoc.json .",
"prepack": "npx -p typescript tsc ./index.js --allowJs --declaration --declarationMap --emitDeclarationOnly --outDir types"
},
"files": [ "lib/**" ],
"files": [
"lib/**",
"types/**"
],
"keywords": [
"jsdoc",
"JavaScript"
Expand All @@ -25,13 +31,17 @@
"repository": "https://github.com/mbland/jsdoc-cli-wrapper",
"bugs": "https://github.com/mbland/jsdoc-cli-wrapper/issues",
"devDependencies": {
"@stylistic/eslint-plugin-js": "^1.5.1",
"@vitest/coverage-istanbul": "^1.1.0",
"@vitest/coverage-v8": "^1.1.0",
"@vitest/ui": "^1.1.0",
"@stylistic/eslint-plugin-js": "^1.5.3",
"@types/chai": "^4.3.11",
"@types/node": "^20.10.7",
"@vitest/coverage-istanbul": "^1.1.3",
"@vitest/coverage-v8": "^1.1.3",
"@vitest/ui": "^1.1.3",
"eslint": "^8.56.0",
"eslint-plugin-jsdoc": "^46.9.1",
"eslint-plugin-jsdoc": "^46.10.1",
"eslint-plugin-vitest": "^0.3.20",
"vitest": "^1.1.0"
"jsdoc": "^4.0.2",
"typescript": "^5.3.3",
"vitest": "^1.1.3"
}
}
Loading