Skip to content

Commit

Permalink
Add support for Deno shortcuts and wildcards (#508)
Browse files Browse the repository at this point in the history
  • Loading branch information
mahtaran authored Nov 4, 2024
1 parent 3bcc9c9 commit 64b7e2a
Show file tree
Hide file tree
Showing 10 changed files with 505 additions and 294 deletions.
3 changes: 2 additions & 1 deletion docs/cli/shortcuts.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Command Shortcuts

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

| Syntax | Expands to |
Expand All @@ -10,6 +10,7 @@ The following are supported:
| `yarn:<script>` | `yarn run <script>` |
| `bun:<script>` | `bun run <script>` |
| `node:<script>` | `node --run <script>` |
| `deno:<script>` | `deno task <script>` |

> [!NOTE]
>
Expand Down
40 changes: 0 additions & 40 deletions src/command-parser/expand-npm-shortcut.spec.ts

This file was deleted.

22 changes: 0 additions & 22 deletions src/command-parser/expand-npm-shortcut.ts

This file was deleted.

152 changes: 0 additions & 152 deletions src/command-parser/expand-npm-wildcard.spec.ts

This file was deleted.

75 changes: 0 additions & 75 deletions src/command-parser/expand-npm-wildcard.ts

This file was deleted.

41 changes: 41 additions & 0 deletions src/command-parser/expand-shortcut.spec.ts
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`,
});
});
});
39 changes: 39 additions & 0 deletions src/command-parser/expand-shortcut.ts
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}`,
};
}
}
Loading

0 comments on commit 64b7e2a

Please sign in to comment.