@xec-sh/cli
Version:
Xec: The Universal Shell for TypeScript
199 lines • 5.21 kB
JavaScript
import os from 'os';
import path from 'path';
import chalk from 'chalk';
import which from 'which';
import fs from 'fs-extra';
import { glob } from 'glob';
import fetch from 'node-fetch';
import * as clack from '@clack/prompts';
import { $ as xecDollar } from '@xec-sh/core';
export const $ = xecDollar;
let currentDir = process.cwd();
export function cd(dir) {
if (dir === undefined) {
return currentDir;
}
const newDir = path.resolve(currentDir, dir);
if (!fs.existsSync(newDir)) {
throw new Error(`Directory not found: ${newDir}`);
}
if (!fs.statSync(newDir).isDirectory()) {
throw new Error(`Not a directory: ${newDir}`);
}
currentDir = newDir;
process.chdir(newDir);
return currentDir;
}
export function pwd() {
return currentDir;
}
export const echo = Object.assign(function echo(...args) {
console.log(...args);
}, {
info: (message) => clack.log.info(message),
success: (message) => clack.log.success(message),
warning: (message) => clack.log.warn(message),
error: (message) => clack.log.error(message),
debug: (message) => console.log(chalk.gray(`[DEBUG] ${message}`)),
step: (message) => clack.log.step(message),
});
export function spinner(options) {
const message = typeof options === 'string' ? options : options?.text;
const s = clack.spinner();
if (message) {
s.start(message);
}
return s;
}
export const question = clack.text;
export const confirm = clack.confirm;
export const select = clack.select;
export const multiselect = clack.multiselect;
export const password = clack.password;
export { os, path };
export { fs, glob };
export { fetch };
export function exit(code = 0) {
process.exit(code);
}
export function env(key, defaultValue) {
return process.env[key] || defaultValue;
}
export function setEnv(key, value) {
process.env[key] = value;
}
export async function retry(fn, options = {}) {
const { retries = 3, delay = 1000, backoff = 2, onRetry = () => { }, } = options;
let lastError;
for (let i = 0; i <= retries; i++) {
try {
return await fn();
}
catch (error) {
lastError = error;
if (i < retries) {
onRetry(lastError, i + 1);
await sleep(delay * Math.pow(backoff, i));
}
}
}
throw lastError;
}
export function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
export function template(strings, ...values) {
return strings.reduce((result, str, i) => {
const value = values[i - 1];
return result + (value !== undefined ? value : '') + str;
});
}
export { which };
export function quote(arg) {
if (!/[\s"'$`\\]/.test(arg)) {
return arg;
}
return "'" + arg.replace(/'/g, "'\"'\"'") + "'";
}
export function tmpdir() {
return os.tmpdir();
}
export function tmpfile(prefix = 'xec-', suffix = '') {
const random = Math.random().toString(36).substring(2, 15);
return path.join(os.tmpdir(), `${prefix}${random}${suffix}`);
}
export async function yaml() {
const YAML = await import('js-yaml');
return {
parse: YAML.load,
stringify: YAML.dump,
};
}
export async function csv() {
const parse = await import('csv-parse/sync');
const stringify = await import('csv-stringify/sync');
return { parse: parse.parse, stringify: stringify.stringify };
}
export async function diff(a, b, options) {
const { diffLines } = await import('diff');
return diffLines(a, b, options);
}
export async function parseArgs(args) {
const { default: minimist } = await import('minimist');
return minimist(args);
}
export async function loadEnv(envPath) {
const { config } = await import('dotenv');
return config({ path: envPath });
}
export function kill(pid, signal = 'SIGTERM') {
process.kill(pid, signal);
}
export async function ps() {
const { default: pslist } = await import('ps-list');
return pslist();
}
export async function within(options, fn) {
const originalCwd = process.cwd();
const originalEnv = { ...process.env };
try {
if (options.cwd) {
cd(options.cwd);
}
if (options.env) {
Object.assign(process.env, options.env);
}
return await fn();
}
finally {
cd(originalCwd);
process.env = originalEnv;
}
}
export const log = {
info: (message) => clack.log.info(message),
success: (message) => clack.log.success(message),
warning: (message) => clack.log.warn(message),
error: (message) => clack.log.error(message),
step: (message) => clack.log.step(message),
};
export { chalk };
const scriptUtils = {
$,
cd,
pwd,
echo,
spinner,
question,
confirm,
select,
multiselect,
password,
fs,
glob,
path,
os,
fetch,
exit,
env,
setEnv,
retry,
sleep,
template,
which,
quote,
tmpdir,
tmpfile,
yaml,
csv,
diff,
parseArgs,
loadEnv,
kill,
ps,
within,
log,
chalk,
};
export default scriptUtils;
//# sourceMappingURL=script-utils.js.map