From 55b9db7cea1ef720b4021480da1979a5544e746e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mathias=20K=C3=B6ppchen=20-=20Protronic=20GmbH?= Date: Wed, 1 Oct 2025 08:57:01 +0000 Subject: [PATCH 1/2] Refactor packWithNpm and publish functions to use packageSpec and improve pack options handling --- index.js | 8 ++++---- main.js | 31 +++++++++++++++++++++++++++++-- 2 files changed, 33 insertions(+), 6 deletions(-) diff --git a/index.js b/index.js index 60ca7ba..b91b221 100644 --- a/index.js +++ b/index.js @@ -41,8 +41,8 @@ function spawnNpmWithOutput(args, options) { }); } -async function packWithNpm({ sourceDir, targetDir, verbose }) { - const output = (await spawnNpmWithOutput(['pack', sourceDir], { +async function packWithNpm({ packageSpec, targetDir, verbose }) { + const output = (await spawnNpmWithOutput(['pack', packageSpec], { cwd: targetDir, verbose })).trim().split(/\n/); @@ -60,7 +60,7 @@ async function packWithNpm({ sourceDir, targetDir, verbose }) { } } -async function publish({tag, branch, version, push, packOptions}, pack = packWithNpm) { +async function publish({tag, branch, version, push, packageSpec, packOptions}, pack = packWithNpm) { if (!tag) { tag = `v${version}`; } @@ -86,7 +86,7 @@ async function publish({tag, branch, version, push, packOptions}, pack = packWit } await pack(Object.assign({ - sourceDir: process.cwd(), + packageSpec: packageSpec, targetDir: tmpDir, }, packOptions)); diff --git a/main.js b/main.js index 8b3aa38..c3397b6 100644 --- a/main.js +++ b/main.js @@ -12,15 +12,41 @@ const argv = yargs .describe('branch', "Branch name to append this new release to - none by default") .describe('push', 'Push update to the git remote (pass --no-push to disable)') .describe('force', 'Override any existing tag on the remote as well as locally (git tag -f, git push -f)') + .describe('--', 'All arguments after -- are passed to npm pack command') .boolean('push') .boolean('force') .default('push', 'true') .wrap(yargs.terminalWidth()) .argv; - + const path = require('path'); const packageJson = require(path.join(process.cwd(), '/package.json')); +// default pack options +let packOptions = { verbose: true }; + +// packageSpec is the first non-option token after `--`. +// Remaining forwarded tokens are forwarded as packOptions.args. +let packageSpec = process.cwd(); +const ddIndex = process.argv.indexOf('--'); + +if (ddIndex !== -1) { + const forwarded = process.argv.slice(ddIndex + 1); + if (forwarded.length) { + // find first token that is not an option (doesn't start with '-') + const firstNonOption = forwarded.findIndex(tok => !tok.startsWith('-')); + if (firstNonOption !== -1) { + packageSpec = forwarded[firstNonOption]; + // remove the packageSpec token from forwarded list + forwarded.splice(firstNonOption, 1); + } + if (forwarded.length) { + packOptions = Object.assign({}, packOptions, { args: forwarded }); + } + } +} +console.log('Pack options:', packOptions); + publish({ tag: argv.tag, branch: argv.branch, @@ -30,8 +56,9 @@ publish({ remote: argv.remote, force: argv.force, }, + packageSpec, packOptions: { - verbose: true + ...packOptions } }).catch(err => { if (err.cmd) { From c6e2805f5ba9ac4a9057ca3285c144ff4b6d7f35 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mathias=20K=C3=B6ppchen=20-=20Protronic=20GmbH?= Date: Wed, 1 Oct 2025 09:51:40 +0000 Subject: [PATCH 2/2] Forward as Object --- main.js | 36 +++++++++++------------------------- 1 file changed, 11 insertions(+), 25 deletions(-) diff --git a/main.js b/main.js index c3397b6..b92b6e3 100644 --- a/main.js +++ b/main.js @@ -12,41 +12,27 @@ const argv = yargs .describe('branch', "Branch name to append this new release to - none by default") .describe('push', 'Push update to the git remote (pass --no-push to disable)') .describe('force', 'Override any existing tag on the remote as well as locally (git tag -f, git push -f)') + .parserConfiguration({ 'populate--': true }) .describe('--', 'All arguments after -- are passed to npm pack command') .boolean('push') .boolean('force') .default('push', 'true') .wrap(yargs.terminalWidth()) .argv; - + const path = require('path'); const packageJson = require(path.join(process.cwd(), '/package.json')); -// default pack options -let packOptions = { verbose: true }; - // packageSpec is the first non-option token after `--`. -// Remaining forwarded tokens are forwarded as packOptions.args. -let packageSpec = process.cwd(); -const ddIndex = process.argv.indexOf('--'); +// Remaining forwarded tokens are forwarded. +const forwarded = yargs(argv['--'] || []) + .default('verbose', 'true') + .wrap(yargs.terminalWidth()) + .argv; + +const packageSpec = forwarded._.length > 0 ? forwarded._ : process.cwd(); +console.log('Using packageSpec:', packageSpec); -if (ddIndex !== -1) { - const forwarded = process.argv.slice(ddIndex + 1); - if (forwarded.length) { - // find first token that is not an option (doesn't start with '-') - const firstNonOption = forwarded.findIndex(tok => !tok.startsWith('-')); - if (firstNonOption !== -1) { - packageSpec = forwarded[firstNonOption]; - // remove the packageSpec token from forwarded list - forwarded.splice(firstNonOption, 1); - } - if (forwarded.length) { - packOptions = Object.assign({}, packOptions, { args: forwarded }); - } - } -} -console.log('Pack options:', packOptions); - publish({ tag: argv.tag, branch: argv.branch, @@ -58,7 +44,7 @@ publish({ }, packageSpec, packOptions: { - ...packOptions + ...forwarded } }).catch(err => { if (err.cmd) {