Skip to content

Commit

Permalink
Start support for binaries that may have command behind double-dash
Browse files Browse the repository at this point in the history
  • Loading branch information
webpro committed Oct 31, 2024
1 parent 51f4481 commit be09c99
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
8 changes: 6 additions & 2 deletions packages/knip/src/binaries/fallback.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,16 @@ import { toBinary, toDeferResolve, toEntry } from '../util/input.js';
// Binaries that spawn a child process for the binary at first positional arg (and don't have custom resolver already)
const spawningBinaries = ['cross-env', 'retry-cli'];

// Binaries that have a new script behind the double-dash/end-of-command (and don't have custom resolver already)
const endOfCommandBinaries = ['dotenvx'];

const positionals = new Set(['babel-node', 'esbuild', 'execa', 'vite-node', 'zx']);

export const resolve: BinaryResolver = (binary, args, { fromArgs }) => {
const parsed = parseArgs(args, { boolean: ['quiet', 'verbose'] });
const parsed = parseArgs(args, { boolean: ['quiet', 'verbose'], '--': endOfCommandBinaries.includes(binary) });
const bin = binary.startsWith('.') ? toEntry(binary) : toBinary(binary);
const shiftedArgs = spawningBinaries.includes(binary) ? fromArgs(args) : [];
const pos = positionals.has(binary) ? [toDeferResolve(parsed._[0])] : [];
return compact([bin, ...shiftedArgs, ...pos]);
const newCommand = parsed['--'] && parsed['--'].length > 0 ? fromArgs(parsed['--']) : [];
return compact([bin, ...shiftedArgs, ...pos, ...newCommand]);
};
4 changes: 4 additions & 0 deletions packages/knip/test/util/get-inputs-from-scripts.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,10 @@ test('getInputsFromScripts (nodemon)', () => {
t('nodemon script.js', [toBinary('nodemon'), toDeferResolveEntry('script.js')]);
});

test('getInputsFromScripts (double-dash)', () => {
t('dotenvx run --convention=nextjs -- tsx watch src/index.ts', [toBinary('dotenvx'), toBinary('tsx'), toDeferResolveEntry('src/index.ts')]);
});

test('getInputsFromScripts (bash expressions)', () => {
t('if test "$NODE_ENV" = "production" ; then make install ; fi ', [toBinary('make')]);
t('node -e "if (NODE_ENV === \'production\'){process.exit(1)} " || make install', [toBinary('node'), toBinary('make')]);
Expand Down

0 comments on commit be09c99

Please sign in to comment.