lazy-js-utils
Version:
A collection of lazy-loaded JavaScript utilities for efficient development
205 lines (188 loc) • 4.98 kB
JavaScript
import {
toAbsolutePath
} from "../chunk-OZ77VSFJ.mjs";
import {
jsShell,
useNodeWorker,
useProcressNodeWorker
} from "../chunk-YOLMLECK.mjs";
import {
isFile
} from "../chunk-LIKJRYBJ.mjs";
import {
isStr
} from "../chunk-4N54TZJJ.mjs";
// src/node/fileCopy.ts
function fileCopy(urls, destination) {
return jsShell(`cp -r {${urls.join(",")}} ${destination}`, "pipe");
}
// src/node/getExportBundle.ts
import fsp from "fs/promises";
import path from "path";
import process from "process";
async function getExportBundle(url) {
if (/[./]/.test(url))
throw new Error("module must be a npm module");
const pkg = path.resolve(process.cwd(), "node_modules", url, "package.json");
const { module, main } = JSON.parse(await fsp.readFile(pkg, "utf-8"));
const modulePath = path.resolve(`./node_modules/${url}`, module || main);
return fsp.readFile(modulePath, "utf-8");
}
// src/node/getPkg.ts
import fsp2 from "fs/promises";
// src/string/ensureSuffix.ts
function ensureSuffix(suffix, str) {
if (str.endsWith(suffix))
return str;
return str + suffix;
}
// src/node/getPkg.ts
async function getPkg(url = "./package.json") {
const resolvedPath = toAbsolutePath(ensureSuffix("/package.json", url));
if (!isFile(resolvedPath))
throw new Error(`${resolvedPath} is not a file`);
return JSON.parse(await fsp2.readFile(resolvedPath, "utf-8"));
}
// src/node/getPkgTool.ts
async function getPkgTool() {
const pkg = await getPkg() || {};
const { packageManager } = pkg;
if (packageManager) {
const manager = packageManager.split("@")[0].replace(/[~^]/, "");
if (packageManager)
return manager;
}
switch (true) {
case isFile(toAbsolutePath("./bun.lockb")):
return "bun";
case isFile(toAbsolutePath("./pnpm-lock.yaml")):
case isFile(toAbsolutePath("./pnpm-workspace.yaml")):
return "pnpm";
case isFile(toAbsolutePath("./yarn.lock")):
case isFile(toAbsolutePath("./lerna.json")):
return "yarn";
default:
return "npm";
}
}
// src/node/transformArgv.ts
import process2 from "process";
function transformArgv() {
var _a, _b;
return (_b = (_a = process2) == null ? void 0 : _a.argv) == null ? void 0 : _b.slice(2).reduce((result, arg) => {
const [key, value] = arg.split("=");
result[key.slice(2)] = value || true;
return result;
}, {});
}
// src/node/withTaskName.ts
function withTaskName(name, fn) {
return Object.assign(fn, { displayName: name });
}
// src/node/writeFile.ts
import fsp3 from "fs/promises";
function writeFile(paths, callback, encoding = "utf-8") {
if (isStr(paths))
paths = [paths];
paths.forEach(async (relativepath, i) => {
const content = await fsp3.readFile(relativepath, encoding);
const result = (callback == null ? void 0 : callback(content, i)) || content;
fsp3.writeFile(relativepath, result).catch((err) => {
throw err;
});
});
}
// src/node/hasPkg.ts
import path2 from "path";
async function hasPkg(rootPath) {
const url = path2.resolve(rootPath, "package.json");
const { result } = await jsShell(
`test -f "${url}" && echo "0"|| echo "1"`,
"pipe"
);
return result === "0";
}
// src/node/isInstallPkg.ts
async function isInstallPkg(pkg) {
const { status } = await jsShell(`if ! command -v ${pkg} &> /dev/null; then
exit 1
else
exit 0
fi`);
return status === 0;
}
// src/node/isExist.ts
import fs from "fs";
function isExist(url) {
try {
fs.accessSync(toAbsolutePath(url));
return true;
} catch (error) {
return false;
}
}
// src/node/isGo.ts
import path3 from "path";
import process3 from "process";
async function isGo(rootPath = process3.cwd()) {
const url = path3.resolve(rootPath, "go.mod");
const { result } = await jsShell(
`(test -f "main.go" || test -f "${url}") && echo "0"|| echo "1"`,
"pipe"
);
return result === "0";
}
// src/node/isRust.ts
import process4 from "process";
import path4 from "path";
async function isRust(rootPath = process4.cwd()) {
const url = path4.resolve(rootPath, "Cargo.toml");
const { result } = await jsShell(
`test -f "${url}" && echo "0"|| echo "1"`,
"pipe"
);
return result === "0";
}
// src/node/isWritable.ts
import fs2 from "fs";
function isWritable(filename) {
try {
fs2.accessSync(filename, fs2.constants.W_OK);
return true;
} catch {
return false;
}
}
// src/node/isPkg.ts
import process5 from "process";
import path5 from "path";
async function isPkg(rootPath = process5.cwd()) {
const url = path5.resolve(
rootPath.replace(/package.json$/, ""),
"package.json"
);
const { result } = await jsShell(
`test -f "${url}" && echo "0"|| echo "1"`,
"pipe"
);
return result === "0";
}
export {
fileCopy,
getExportBundle,
getPkg,
getPkgTool,
hasPkg,
isExist,
isGo,
isInstallPkg,
isPkg,
isRust,
isWritable,
jsShell,
transformArgv,
useNodeWorker,
useProcressNodeWorker,
withTaskName,
writeFile
};