-
-
Notifications
You must be signed in to change notification settings - Fork 236
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for Deno shortcuts and wildcards (#508)
- Loading branch information
Showing
10 changed files
with
505 additions
and
294 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 was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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,41 @@ | ||
import { CommandInfo } from '../command'; | ||
import { ExpandShortcut } from './expand-shortcut'; | ||
|
||
const parser = new ExpandShortcut(); | ||
|
||
const createCommandInfo = (command: string, name = ''): CommandInfo => ({ | ||
name, | ||
command, | ||
}); | ||
|
||
it('returns same command if no prefix is present', () => { | ||
const commandInfo = createCommandInfo('echo foo'); | ||
expect(parser.parse(commandInfo)).toBe(commandInfo); | ||
}); | ||
|
||
describe.each([ | ||
['npm', 'npm run'], | ||
['yarn', 'yarn run'], | ||
['pnpm', 'pnpm run'], | ||
['bun', 'bun run'], | ||
['node', 'node --run'], | ||
['deno', 'deno task'], | ||
])(`with '%s:' prefix`, (prefix, command) => { | ||
it(`expands to "${command} <script> <args>"`, () => { | ||
const commandInfo = createCommandInfo(`${prefix}:foo -- bar`, 'echo'); | ||
expect(parser.parse(commandInfo)).toEqual({ | ||
...commandInfo, | ||
name: 'echo', | ||
command: `${command} foo -- bar`, | ||
}); | ||
}); | ||
|
||
it('sets name to script name if none', () => { | ||
const commandInfo = createCommandInfo(`${prefix}:foo -- bar`); | ||
expect(parser.parse(commandInfo)).toEqual({ | ||
...commandInfo, | ||
name: 'foo', | ||
command: `${command} foo -- bar`, | ||
}); | ||
}); | ||
}); |
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,39 @@ | ||
import { CommandInfo } from '../command'; | ||
import { CommandParser } from './command-parser'; | ||
|
||
/** | ||
* Expands shortcuts according to the following table: | ||
* | ||
* | Syntax | Expands to | | ||
* | --------------- | --------------------- | | ||
* | `npm:<script>` | `npm run <script>` | | ||
* | `pnpm:<script>` | `pnpm run <script>` | | ||
* | `yarn:<script>` | `yarn run <script>` | | ||
* | `bun:<script>` | `bun run <script>` | | ||
* | `node:<script>` | `node --run <script>` | | ||
* | `deno:<script>` | `deno task <script>` | | ||
*/ | ||
export class ExpandShortcut implements CommandParser { | ||
parse(commandInfo: CommandInfo) { | ||
const [, prefix, script, args] = | ||
/^(npm|yarn|pnpm|bun|node|deno):(\S+)(.*)/.exec(commandInfo.command) || []; | ||
if (!script) { | ||
return commandInfo; | ||
} | ||
|
||
let command: string; | ||
if (prefix === 'node') { | ||
command = 'node --run'; | ||
} else if (prefix === 'deno') { | ||
command = 'deno task'; | ||
} else { | ||
command = `${prefix} run`; | ||
} | ||
|
||
return { | ||
...commandInfo, | ||
name: commandInfo.name || script, | ||
command: `${command} ${script}${args}`, | ||
}; | ||
} | ||
} |
Oops, something went wrong.