zx-util.mjs
#!/usr/bin/env zx
/* eslint-disable max-len */
// #region ZX Util
import { stdin, fs, path, echo, chalk, question } from 'zx';
const join = path.join;
const resolve = path.resolve;
const filename = path.basename(__filename);
const cwd = () => process.cwd();
const exit = process.exit;
function exist(path: string) {
return fs.existsSync(path);
}
function isDir(path: string) {
return exist(path) && fs.lstatSync(path).isDirectory();
}
function isFile(path: string) {
return exist(path) && fs.lstatSync(path).isFile();
}
async function iterateDir(path: string, fn: (file: string) => Promise<void> | void) {
if (!isDir(path)) {
return;
}
for (const file of fs.readdirSync(path)) {
await fn(file);
}
}
function read(path: string) {
return fs.readFileSync(path, { encoding: 'utf8' });
}
// you should require when possible(optimized in js)
function readJsonSlow(path: string) {
return fs.readJSONSync(path);
}
function write(p: string, content: string) {
const dir = path.dirname(p);
if (!exist(dir)) {
fs.mkdirSync(dir, { recursive: true });
}
return fs.writeFileSync(p, content);
}
function writeJson(path: string, json: any) {
return write(path, JSON.stringify(json, null, 2));
}
function remove(path: string) {
if (!exist(path)) {
return;
}
if (fs.lstatSync(path).isDirectory()) {
return fs.rmSync(path, { force: true, recursive: true });
} else {
return fs.rmSync(path, { force: true });
}
}
function addLine(str, added, backward = false) {
if (backward) {
return added + '\n' + str;
} else {
return str + '\n' + added;
}
}
function addLineToFile(path: string, added: string, backward = false) {
return write(path, addLine(read(path), added, backward));
}
function print(...args: any[]) {
echo(chalk.blue(...args));
}
function printSuccess(...args: any[]) {
echo(chalk.bold.bgBlue(...args));
}
function printError(...args: any[]) {
echo(chalk.bold.bgRed(...args));
}
function asrt(condition: boolean | any, ...args: any[]) {
if (!condition) {
echo(chalk.bold.bgRed(...args));
exit(1);
}
}
async function input(message: string) {
if (message) {
return question(message + ': ');
} else {
return stdin();
}
}
// #endregion
async function main() {}
main();Last updated on