Skip to content

Commit

Permalink
Get 'pnpm jsdoc' working on Windows
Browse files Browse the repository at this point in the history
For one thing, Windows can't interpret the `#!/usr/bin/env node` header,
nor can it grok the `bin/jsdoc-cli-wrapper.js` path directly.

Prefixing the script with `node` fixes the second problem. Updating
getPath() to detect the 'win32' platform and adding the .CMD extension
to the candidate path fixes the first. This is because 'pnpm install'
will create a CMD.EXE wrapper for the installed script on Windows.
  • Loading branch information
mbland committed Dec 26, 2023
1 parent d95cb52 commit 046fb5c
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 7 deletions.
19 changes: 13 additions & 6 deletions strcalc/src/main/frontend/bin/jsdoc-cli-wrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@ import path from 'node:path'
import { exit, stdout } from 'node:process'

try {
const jsdocArgs = process.argv.slice(2)
const {exitCode, indexHtml} = await runJsdoc(jsdocArgs, process.env)
const {exitCode, indexHtml} = await runJsdoc(
process.argv.slice(2), process.env, process.platform
)
if (indexHtml !== undefined) stdout.write(`${indexHtml}\n`)
exit(exitCode)

Expand All @@ -40,13 +41,14 @@ try {
* 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 {string} platform - the process.platform string
* @returns {Promise<RunJsdocResults>} - result of `jsdoc` execution
*/
async function runJsdoc(argv, env) {
async function runJsdoc(argv, env, platform) {
let jsdocPath

try {
jsdocPath = await getPath('jsdoc', env)
jsdocPath = await getPath('jsdoc', env, platform)
} catch {
return Promise.reject(
'Run \'pnpm add -g jsdoc\' to install JSDoc: https://jsdoc.app\n'
Expand Down Expand Up @@ -78,11 +80,16 @@ async function runJsdoc(argv, env) {
* @param {string} cmdName - command to find in env.PATH
* @param {object} env - environment variables, presumably process.env
* @param {string} env.PATH - the PATH environment variable
* @param {string} platform - the process.platform string
* @returns {Promise<string>} - path to the command
*/
async function getPath(cmdName, env) {
async function getPath(cmdName, env, platform) {
for (const p of env.PATH.split(path.delimiter)) {
const candidate = path.join(p, cmdName)
// pnpm will install both the original script and versions ending with .CMD
// and .ps1. We'll just default to .CMD.
const extension = (platform === 'win32') ? '.CMD' : ''
const candidate = path.join(p, cmdName) + extension

try {
await access(candidate)
return candidate
Expand Down
2 changes: 1 addition & 1 deletion strcalc/src/main/frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
"test-ui": "vitest --ui --coverage",
"test-ci": "eslint --color --max-warnings 0 . && vitest run --config ci/vitest.config.js && vitest run --config ci/vitest.config.browser.js",
"coverage": "vitest run --coverage",
"jsdoc": "bin/jsdoc-cli-wrapper.js -c ./jsdoc.json ."
"jsdoc": "node bin/jsdoc-cli-wrapper.js -c ./jsdoc.json ."
},
"devDependencies": {
"@rollup/pluginutils": "^5.1.0",
Expand Down

0 comments on commit 046fb5c

Please sign in to comment.