initial commit - far from runnable

This commit is contained in:
Anika Raemer 2025-09-21 12:39:54 +02:00
commit db057ce342
8614 changed files with 1032171 additions and 0 deletions

View file

@ -0,0 +1,19 @@
import { CommandInfo } from '../command';
/**
* A command parser encapsulates a specific logic for mapping `CommandInfo` objects
* into another `CommandInfo`.
*
* A prime example is turning an abstract `npm:foo` into `npm run foo`, but it could also turn
* the prefix color of a command brighter, or maybe even prefixing each command with `time(1)`.
*/
export interface CommandParser {
/**
* Parses `commandInfo` and returns one or more `CommandInfo`s.
*
* Returning multiple `CommandInfo` is used when there are multiple possibilities of commands to
* run given the original input.
* An example of this is when the command contains a wildcard and it must be expanded into all
* viable options so that the consumer can decide which ones to run.
*/
parse(commandInfo: CommandInfo): CommandInfo | CommandInfo[];
}

View file

@ -0,0 +1,2 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });

View file

@ -0,0 +1,18 @@
import { CommandInfo } from '../command';
import { CommandParser } from './command-parser';
/**
* Replace placeholders with additional arguments.
*/
export declare class ExpandArguments implements CommandParser {
private readonly additionalArguments;
constructor(additionalArguments: string[]);
parse(commandInfo: CommandInfo): {
command: string;
name: string;
env?: Record<string, unknown>;
cwd?: string;
prefixColor?: string;
ipc?: number;
raw?: boolean;
};
}

View file

@ -0,0 +1,41 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ExpandArguments = void 0;
const shell_quote_1 = require("shell-quote");
/**
* Replace placeholders with additional arguments.
*/
class ExpandArguments {
additionalArguments;
constructor(additionalArguments) {
this.additionalArguments = additionalArguments;
}
parse(commandInfo) {
const command = commandInfo.command.replace(/\\?\{([@*]|[1-9][0-9]*)\}/g, (match, placeholderTarget) => {
// Don't replace the placeholder if it is escaped by a backslash.
if (match.startsWith('\\')) {
return match.slice(1);
}
if (this.additionalArguments.length > 0) {
// Replace numeric placeholder if value exists in additional arguments.
if (!isNaN(placeholderTarget) &&
placeholderTarget <= this.additionalArguments.length) {
return (0, shell_quote_1.quote)([this.additionalArguments[placeholderTarget - 1]]);
}
// Replace all arguments placeholder.
if (placeholderTarget === '@') {
return (0, shell_quote_1.quote)(this.additionalArguments);
}
// Replace combined arguments placeholder.
if (placeholderTarget === '*') {
return (0, shell_quote_1.quote)([this.additionalArguments.join(' ')]);
}
}
// Replace placeholder with empty string
// if value doesn't exist in additional arguments.
return '';
});
return { ...commandInfo, command };
}
}
exports.ExpandArguments = ExpandArguments;

View file

@ -0,0 +1,17 @@
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 declare class ExpandShortcut implements CommandParser {
parse(commandInfo: CommandInfo): CommandInfo;
}

View file

@ -0,0 +1,39 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ExpandShortcut = void 0;
/**
* 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>` |
*/
class ExpandShortcut {
parse(commandInfo) {
const [, prefix, script, args] = /^(npm|yarn|pnpm|bun|node|deno):(\S+)(.*)/.exec(commandInfo.command) || [];
if (!script) {
return commandInfo;
}
let command;
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}`,
};
}
}
exports.ExpandShortcut = ExpandShortcut;

View file

@ -0,0 +1,18 @@
import { CommandInfo } from '../command';
import { CommandParser } from './command-parser';
/**
* Finds wildcards in 'npm/yarn/pnpm/bun run', 'node --run' and 'deno task'
* commands and replaces them with all matching scripts in the NodeJS and Deno
* configuration files of the current directory.
*/
export declare class ExpandWildcard implements CommandParser {
private readonly readDeno;
private readonly readPackage;
static readDeno(): any;
static readPackage(): any;
private packageScripts?;
private denoTasks?;
constructor(readDeno?: typeof ExpandWildcard.readDeno, readPackage?: typeof ExpandWildcard.readPackage);
private relevantScripts;
parse(commandInfo: CommandInfo): CommandInfo | CommandInfo[];
}

View file

@ -0,0 +1,108 @@
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ExpandWildcard = void 0;
const fs_1 = __importDefault(require("fs"));
const jsonc_1 = __importDefault(require("../jsonc"));
const utils_1 = require("../utils");
// Matches a negative filter surrounded by '(!' and ')'.
const OMISSION = /\(!([^)]+)\)/;
/**
* Finds wildcards in 'npm/yarn/pnpm/bun run', 'node --run' and 'deno task'
* commands and replaces them with all matching scripts in the NodeJS and Deno
* configuration files of the current directory.
*/
class ExpandWildcard {
readDeno;
readPackage;
static readDeno() {
try {
let json = '{}';
if (fs_1.default.existsSync('deno.json')) {
json = fs_1.default.readFileSync('deno.json', { encoding: 'utf-8' });
}
else if (fs_1.default.existsSync('deno.jsonc')) {
json = fs_1.default.readFileSync('deno.jsonc', { encoding: 'utf-8' });
}
return jsonc_1.default.parse(json);
}
catch {
return {};
}
}
static readPackage() {
try {
const json = fs_1.default.readFileSync('package.json', { encoding: 'utf-8' });
return JSON.parse(json);
}
catch {
return {};
}
}
packageScripts;
denoTasks;
constructor(readDeno = ExpandWildcard.readDeno, readPackage = ExpandWildcard.readPackage) {
this.readDeno = readDeno;
this.readPackage = readPackage;
}
relevantScripts(command) {
if (!this.packageScripts) {
this.packageScripts = Object.keys(this.readPackage().scripts || {});
}
if (command === 'deno task') {
if (!this.denoTasks) {
// If Deno tries to run a task that doesn't exist,
// it can fall back to running a script with the same name.
// Therefore, the actual list of tasks is the union of the tasks and scripts.
this.denoTasks = [
...Object.keys(this.readDeno().tasks || {}),
...this.packageScripts,
];
}
return this.denoTasks;
}
return this.packageScripts;
}
parse(commandInfo) {
// We expect one of the following patterns:
// - <npm|yarn|pnpm|bun> run <script> [args]
// - node --run <script> [args]
// - deno task <script> [args]
const [, command, scriptGlob, args] = /((?:npm|yarn|pnpm|bun) (?:run)|node --run|deno task) (\S+)([^&]*)/.exec(commandInfo.command) || [];
const wildcardPosition = (scriptGlob || '').indexOf('*');
// If the regex didn't match an npm script, or it has no wildcard,
// then we have nothing to do here
if (wildcardPosition === -1) {
return commandInfo;
}
const [, omission] = OMISSION.exec(scriptGlob) || [];
const scriptGlobSansOmission = scriptGlob.replace(OMISSION, '');
const preWildcard = (0, utils_1.escapeRegExp)(scriptGlobSansOmission.slice(0, wildcardPosition));
const postWildcard = (0, utils_1.escapeRegExp)(scriptGlobSansOmission.slice(wildcardPosition + 1));
const wildcardRegex = new RegExp(`^${preWildcard}(.*?)${postWildcard}$`);
// If 'commandInfo.name' doesn't match 'scriptGlob', this means a custom name
// has been specified and thus becomes the prefix (as described in the README).
const prefix = commandInfo.name !== scriptGlob ? commandInfo.name : '';
return this.relevantScripts(command)
.map((script) => {
if (omission && RegExp(omission).test(script)) {
return;
}
const result = wildcardRegex.exec(script);
const match = result?.[1];
if (match !== undefined) {
return {
...commandInfo,
command: `${command} ${script}${args}`,
// Will use an empty command name if no prefix has been specified and
// the wildcard match is empty, e.g. if `npm:watch-*` matches `npm run watch-`.
name: prefix + match,
};
}
})
.filter((commandInfo) => !!commandInfo);
}
}
exports.ExpandWildcard = ExpandWildcard;

View file

@ -0,0 +1,16 @@
import { CommandInfo } from '../command';
import { CommandParser } from './command-parser';
/**
* Strips quotes around commands so that they can run on the current shell.
*/
export declare class StripQuotes implements CommandParser {
parse(commandInfo: CommandInfo): {
command: string;
name: string;
env?: Record<string, unknown>;
cwd?: string;
prefixColor?: string;
ipc?: number;
raw?: boolean;
};
}

View file

@ -0,0 +1,17 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.StripQuotes = void 0;
/**
* Strips quotes around commands so that they can run on the current shell.
*/
class StripQuotes {
parse(commandInfo) {
let { command } = commandInfo;
// Removes the quotes surrounding a command.
if (/^"(.+?)"$/.test(command) || /^'(.+?)'$/.test(command)) {
command = command.slice(1, command.length - 1);
}
return { ...commandInfo, command };
}
}
exports.StripQuotes = StripQuotes;