Built and signed on GitHub ActionsBuilt and signed on GitHub Actions
Built and signed on GitHub Actions
latest
gnomejs/cmd-cliThe `cmd-cli` module provides a simple way to execute windows command line scripts or files.
It is unknown whether this package works with Cloudflare Workers, Node.js, Deno, Bun, Browsers
JSR Score
88%
Published
4 months ago (0.0.0)
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192import { ShellCommand, type ShellCommandOptions } from "jsr:@gnome/exec@^0.4.1"; import { pathFinder } from "jsr:/@gnome/exec@^0.4.1/path-finder"; import { WINDOWS } from "jsr:@gnome/os-constants@^0.0.0"; import { makeTempFileSync, writeTextFileSync } from "jsr:@gnome/fs@^0.1.0"; import { isAbsolute, resolve } from "jsr:@std/path@^0.225.1"; pathFinder.set("cmd", { name: "cmd", windows: [ "${SystemRoot}\\System32\\cmd.exe", ], }); /** * File extension for cmd scripts. */ export const CMD_EXT = ".cmd"; /** * Represents a windows cmd cli command executed using the `cmd` executable. */ export class CmdCliCommand extends ShellCommand { /** * Creates a new instance of the `cmdCommand` class. * @param script The cmd script to execute. * @param options The options for the cmdell command. */ constructor(script: string, options?: ShellCommandOptions) { super("cmd", script.trimEnd(), options); } static wslCheck = WINDOWS; /** * Gets the file extension associated with cmd scripts. */ get ext(): string { return CMD_EXT; } getScriptFile(): { file: string | undefined; generated: boolean } { let script = this.script.trimEnd(); if (script.match(/\n/) || !["cmd", "bat"].some((ext) => script.endsWith(`.${ext}`))) { script = ` @echo off ${script} `; const file = makeTempFileSync({ prefix: "cmd", suffix: this.ext, }); writeTextFileSync(file, script); return { file, generated: true }; } script = script.trimStart(); if (!isAbsolute(script)) { script = resolve(script); } return { file: script, generated: false }; } /** * Gets the cmd arguments for executing the cmd script. * @param script The cmd script to execute. * @param isFile Specifies whether the script is a file or a command. * @returns The cmd arguments for executing the script. */ // deno-lint-ignore no-unused-vars getShellArgs(script: string, isFile: boolean): string[] { const params = this.shellArgs ?? ["/D", "/E:ON", "/V:OFF", "/S", "/C"]; params.push(`CALL`, script); return params; } } /** * Executes a windows command line (.cmd, .bat) script using the CmdCliCommand class. * * @param script - The cmd script to execute. * @param options - Optional options for the cmd command. * @returns A new instance of the CmdCliCommand class. */ export function cmd(script: string, options?: ShellCommandOptions): CmdCliCommand { return new CmdCliCommand(script, options); }