-
Notifications
You must be signed in to change notification settings - Fork 70
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: add preparse hook * fix: update this.argv after preparse hook
- Loading branch information
1 parent
f2b7f51
commit 80745c4
Showing
8 changed files
with
379 additions
and
2 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
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
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,14 @@ | ||
{ | ||
"name": "preparse-plugin", | ||
"private": true, | ||
"files": [], | ||
"engines": { | ||
"node": ">=18.0.0" | ||
}, | ||
"oclif": { | ||
"commands": "./lib/commands", | ||
"hooks": { | ||
"preparse": "./lib/hooks/preparse.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,64 @@ | ||
import {Command, Flags, Interfaces} from '../../../../../../src' | ||
import {BooleanFlag} from '../../../../../../src/interfaces' | ||
|
||
type GroupAliasOption = { | ||
flag: string | ||
option?: string | ||
} | ||
|
||
function groupAliasFlag<T = boolean>( | ||
options: Partial<BooleanFlag<T> & {groupAlias: GroupAliasOption[]}> = {}, | ||
): BooleanFlag<T> { | ||
return { | ||
parse: async (b, _) => b, | ||
...options, | ||
allowNo: Boolean(options.allowNo), | ||
type: 'boolean', | ||
} as BooleanFlag<T> | ||
} | ||
|
||
export default class Test extends Command { | ||
static args = {} | ||
|
||
static flags = { | ||
burger: Flags.string({ | ||
char: 'b', | ||
default: async () => 'double', | ||
}), | ||
combo: groupAliasFlag({ | ||
char: 'c', | ||
groupAlias: [ | ||
{flag: 'burger'}, | ||
{flag: 'fries'}, | ||
{ | ||
flag: 'shake', | ||
option: 'strawberry', | ||
}, | ||
], | ||
}), | ||
shake: Flags.option({ | ||
options: ['chocolate', 'vanilla', 'strawberry'], | ||
char: 's', | ||
})(), | ||
fries: Flags.boolean({ | ||
allowNo: true, | ||
char: 'f', | ||
}), | ||
'flags-dir': Flags.directory(), | ||
sauce: Flags.string({ | ||
multiple: true, | ||
default: ['ketchup'], | ||
}), | ||
} | ||
|
||
async run(): Promise<{ | ||
args: Interfaces.InferredArgs<typeof Test.args> | ||
flags: Interfaces.InferredFlags<typeof Test.flags> | ||
}> { | ||
const {args, flags} = await this.parse(Test) | ||
return { | ||
args, | ||
flags, | ||
} | ||
} | ||
} |
81 changes: 81 additions & 0 deletions
81
test/parser/fixtures/preparse-plugin/src/hooks/preparse.ts
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 {readFile, readdir} from 'node:fs/promises' | ||
import {join, parse} from 'node:path' | ||
|
||
import {Hook} from '../../../../../../src' | ||
|
||
const hook: Hook<'preparse'> = async function ({argv, options}) { | ||
const flagsToIgnore = new Set( | ||
Object.entries(options.flags ?? {}) | ||
.filter( | ||
([_, flagOptions]) => | ||
// don't ignore if flag can take multiple values | ||
(flagOptions.type === 'option' && flagOptions.multiple !== true) || flagOptions.type === 'boolean', | ||
) | ||
.filter( | ||
([flagName, flagOptions]) => | ||
// ignore if short char flag is present | ||
argv.includes(`-${flagOptions.char}`) || | ||
// ignore if long flag is present | ||
argv.includes(`--${flagName}`) || | ||
// ignore if --no- flag is present | ||
(flagOptions.type === 'boolean' && flagOptions.allowNo && argv.includes(`--no-${flagName}`)), | ||
) | ||
.map(([flagName]) => flagName), | ||
) | ||
|
||
const groupAliasFlags = Object.fromEntries( | ||
Object.entries(options.flags ?? {}).filter( | ||
([_, flagOptions]) => | ||
// @ts-expect-error because the type isn't aware of the custom flag we made | ||
flagOptions.groupAlias, | ||
), | ||
) | ||
|
||
for (const [flagName, flagOptions] of Object.entries(groupAliasFlags)) { | ||
const groupAliasFlagPresent = argv.includes(`--${flagName}`) || argv.includes(`-${flagOptions.char}`) | ||
|
||
if (groupAliasFlagPresent) { | ||
// @ts-expect-error because the type isn't aware of the custom flag we made | ||
for (const groupAliasOption of flagOptions.groupAlias) { | ||
if (flagsToIgnore.has(groupAliasOption.flag)) continue | ||
argv.push(`--${groupAliasOption.flag}`) | ||
if (groupAliasOption.option) argv.push(groupAliasOption.option) | ||
if (typeof options.flags?.[groupAliasOption.flag].default === 'function') { | ||
// eslint-disable-next-line no-await-in-loop | ||
argv.push(await options.flags?.[groupAliasOption.flag].default()) | ||
continue | ||
} | ||
|
||
if (options.flags?.[groupAliasOption.flag].default) { | ||
argv.push(options.flags?.[groupAliasOption.flag].default) | ||
} | ||
} | ||
} | ||
} | ||
|
||
if (argv.includes('--flags-dir')) { | ||
const flagsDir = argv[argv.indexOf('--flags-dir') + 1] | ||
const filesInDir = await readdir(flagsDir) | ||
const flagsToInsert = await Promise.all( | ||
filesInDir | ||
// ignore files that were provided as flags | ||
.filter((f) => !flagsToIgnore.has(f)) | ||
.map(async (file) => { | ||
const contents = await readFile(join(flagsDir, file), 'utf8') | ||
const values = contents?.split('\n') | ||
return [parse(file).name, values] | ||
}), | ||
) | ||
|
||
for (const [flag, values] of flagsToInsert) { | ||
for (const value of values) { | ||
argv.push(`--${flag}`) | ||
if (value) argv.push(value) | ||
} | ||
} | ||
} | ||
|
||
return argv | ||
} | ||
|
||
export default hook |
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,7 @@ | ||
{ | ||
"compilerOptions": { | ||
"outDir": "./lib", | ||
"rootDirs": ["./src"] | ||
}, | ||
"include": ["./src/**/*"] | ||
} |
Oops, something went wrong.