Skip to content

Commit 64b7e2a

Browse files
authored
Add support for Deno shortcuts and wildcards (#508)
1 parent 3bcc9c9 commit 64b7e2a

10 files changed

+505
-294
lines changed

docs/cli/shortcuts.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Command Shortcuts
22

3-
Package managers that execute scripts from a `package.json` file can be shortened when in concurrently.<br/>
3+
Package managers that execute scripts from a `package.json` or `deno.json` file can be shortened when in concurrently.<br/>
44
The following are supported:
55

66
| Syntax | Expands to |
@@ -10,6 +10,7 @@ The following are supported:
1010
| `yarn:<script>` | `yarn run <script>` |
1111
| `bun:<script>` | `bun run <script>` |
1212
| `node:<script>` | `node --run <script>` |
13+
| `deno:<script>` | `deno task <script>` |
1314

1415
> [!NOTE]
1516
>

src/command-parser/expand-npm-shortcut.spec.ts

Lines changed: 0 additions & 40 deletions
This file was deleted.

src/command-parser/expand-npm-shortcut.ts

Lines changed: 0 additions & 22 deletions
This file was deleted.

src/command-parser/expand-npm-wildcard.spec.ts

Lines changed: 0 additions & 152 deletions
This file was deleted.

src/command-parser/expand-npm-wildcard.ts

Lines changed: 0 additions & 75 deletions
This file was deleted.
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import { CommandInfo } from '../command';
2+
import { ExpandShortcut } from './expand-shortcut';
3+
4+
const parser = new ExpandShortcut();
5+
6+
const createCommandInfo = (command: string, name = ''): CommandInfo => ({
7+
name,
8+
command,
9+
});
10+
11+
it('returns same command if no prefix is present', () => {
12+
const commandInfo = createCommandInfo('echo foo');
13+
expect(parser.parse(commandInfo)).toBe(commandInfo);
14+
});
15+
16+
describe.each([
17+
['npm', 'npm run'],
18+
['yarn', 'yarn run'],
19+
['pnpm', 'pnpm run'],
20+
['bun', 'bun run'],
21+
['node', 'node --run'],
22+
['deno', 'deno task'],
23+
])(`with '%s:' prefix`, (prefix, command) => {
24+
it(`expands to "${command} <script> <args>"`, () => {
25+
const commandInfo = createCommandInfo(`${prefix}:foo -- bar`, 'echo');
26+
expect(parser.parse(commandInfo)).toEqual({
27+
...commandInfo,
28+
name: 'echo',
29+
command: `${command} foo -- bar`,
30+
});
31+
});
32+
33+
it('sets name to script name if none', () => {
34+
const commandInfo = createCommandInfo(`${prefix}:foo -- bar`);
35+
expect(parser.parse(commandInfo)).toEqual({
36+
...commandInfo,
37+
name: 'foo',
38+
command: `${command} foo -- bar`,
39+
});
40+
});
41+
});

src/command-parser/expand-shortcut.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import { CommandInfo } from '../command';
2+
import { CommandParser } from './command-parser';
3+
4+
/**
5+
* Expands shortcuts according to the following table:
6+
*
7+
* | Syntax | Expands to |
8+
* | --------------- | --------------------- |
9+
* | `npm:<script>` | `npm run <script>` |
10+
* | `pnpm:<script>` | `pnpm run <script>` |
11+
* | `yarn:<script>` | `yarn run <script>` |
12+
* | `bun:<script>` | `bun run <script>` |
13+
* | `node:<script>` | `node --run <script>` |
14+
* | `deno:<script>` | `deno task <script>` |
15+
*/
16+
export class ExpandShortcut implements CommandParser {
17+
parse(commandInfo: CommandInfo) {
18+
const [, prefix, script, args] =
19+
/^(npm|yarn|pnpm|bun|node|deno):(\S+)(.*)/.exec(commandInfo.command) || [];
20+
if (!script) {
21+
return commandInfo;
22+
}
23+
24+
let command: string;
25+
if (prefix === 'node') {
26+
command = 'node --run';
27+
} else if (prefix === 'deno') {
28+
command = 'deno task';
29+
} else {
30+
command = `${prefix} run`;
31+
}
32+
33+
return {
34+
...commandInfo,
35+
name: commandInfo.name || script,
36+
command: `${command} ${script}${args}`,
37+
};
38+
}
39+
}

0 commit comments

Comments
 (0)