lazy-js-utils
Version:
A collection of lazy-loaded JavaScript utilities for efficient development
205 lines (150 loc) • 6.21 kB
JavaScript
Object.defineProperty(exports, "__esModule", {value: true}); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
var _chunk6GT6VOS7js = require('../chunk-6GT6VOS7.js');
var _chunk7C7ZRD6Hjs = require('../chunk-7C7ZRD6H.js');
var _chunk53V2MMRCjs = require('../chunk-53V2MMRC.js');
var _chunk3KT5TQ4Gjs = require('../chunk-3KT5TQ4G.js');
// src/node/fileCopy.ts
function fileCopy(urls, destination) {
return _chunk7C7ZRD6Hjs.jsShell.call(void 0, `cp -r {${urls.join(",")}} ${destination}`, "pipe");
}
// src/node/getExportBundle.ts
var _promises = require('fs/promises'); var _promises2 = _interopRequireDefault(_promises);
var _path = require('path'); var _path2 = _interopRequireDefault(_path);
var _process = require('process'); var _process2 = _interopRequireDefault(_process);
async function getExportBundle(url) {
if (/[./]/.test(url))
throw new Error("module must be a npm module");
const pkg = _path2.default.resolve(_process2.default.cwd(), "node_modules", url, "package.json");
const { module, main } = JSON.parse(await _promises2.default.readFile(pkg, "utf-8"));
const modulePath = _path2.default.resolve(`./node_modules/${url}`, module || main);
return _promises2.default.readFile(modulePath, "utf-8");
}
// src/node/getPkg.ts
// 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 = _chunk6GT6VOS7js.toAbsolutePath.call(void 0, ensureSuffix("/package.json", url));
if (!_chunk53V2MMRCjs.isFile.call(void 0, resolvedPath))
throw new Error(`${resolvedPath} is not a file`);
return JSON.parse(await _promises2.default.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 _chunk53V2MMRCjs.isFile.call(void 0, _chunk6GT6VOS7js.toAbsolutePath.call(void 0, "./bun.lockb")):
return "bun";
case _chunk53V2MMRCjs.isFile.call(void 0, _chunk6GT6VOS7js.toAbsolutePath.call(void 0, "./pnpm-lock.yaml")):
case _chunk53V2MMRCjs.isFile.call(void 0, _chunk6GT6VOS7js.toAbsolutePath.call(void 0, "./pnpm-workspace.yaml")):
return "pnpm";
case _chunk53V2MMRCjs.isFile.call(void 0, _chunk6GT6VOS7js.toAbsolutePath.call(void 0, "./yarn.lock")):
case _chunk53V2MMRCjs.isFile.call(void 0, _chunk6GT6VOS7js.toAbsolutePath.call(void 0, "./lerna.json")):
return "yarn";
default:
return "npm";
}
}
// src/node/transformArgv.ts
function transformArgv() {
var _a, _b;
return (_b = (_a = _process2.default) == 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
function writeFile(paths, callback, encoding = "utf-8") {
if (_chunk3KT5TQ4Gjs.isStr.call(void 0, paths))
paths = [paths];
paths.forEach(async (relativepath, i) => {
const content = await _promises2.default.readFile(relativepath, encoding);
const result = (callback == null ? void 0 : callback(content, i)) || content;
_promises2.default.writeFile(relativepath, result).catch((err) => {
throw err;
});
});
}
// src/node/hasPkg.ts
async function hasPkg(rootPath) {
const url = _path2.default.resolve(rootPath, "package.json");
const { result } = await _chunk7C7ZRD6Hjs.jsShell.call(void 0,
`test -f "${url}" && echo "0"|| echo "1"`,
"pipe"
);
return result === "0";
}
// src/node/isInstallPkg.ts
async function isInstallPkg(pkg) {
const { status } = await _chunk7C7ZRD6Hjs.jsShell.call(void 0, `if ! command -v ${pkg} &> /dev/null; then
exit 1
else
exit 0
fi`);
return status === 0;
}
// src/node/isExist.ts
var _fs = require('fs'); var _fs2 = _interopRequireDefault(_fs);
function isExist(url) {
try {
_fs2.default.accessSync(_chunk6GT6VOS7js.toAbsolutePath.call(void 0, url));
return true;
} catch (error) {
return false;
}
}
// src/node/isGo.ts
async function isGo(rootPath = _process2.default.cwd()) {
const url = _path2.default.resolve(rootPath, "go.mod");
const { result } = await _chunk7C7ZRD6Hjs.jsShell.call(void 0,
`(test -f "main.go" || test -f "${url}") && echo "0"|| echo "1"`,
"pipe"
);
return result === "0";
}
// src/node/isRust.ts
async function isRust(rootPath = _process2.default.cwd()) {
const url = _path2.default.resolve(rootPath, "Cargo.toml");
const { result } = await _chunk7C7ZRD6Hjs.jsShell.call(void 0,
`test -f "${url}" && echo "0"|| echo "1"`,
"pipe"
);
return result === "0";
}
// src/node/isWritable.ts
function isWritable(filename) {
try {
_fs2.default.accessSync(filename, _fs2.default.constants.W_OK);
return true;
} catch (e) {
return false;
}
}
// src/node/isPkg.ts
async function isPkg(rootPath = _process2.default.cwd()) {
const url = _path2.default.resolve(
rootPath.replace(/package.json$/, ""),
"package.json"
);
const { result } = await _chunk7C7ZRD6Hjs.jsShell.call(void 0,
`test -f "${url}" && echo "0"|| echo "1"`,
"pipe"
);
return result === "0";
}
exports.fileCopy = fileCopy; exports.getExportBundle = getExportBundle; exports.getPkg = getPkg; exports.getPkgTool = getPkgTool; exports.hasPkg = hasPkg; exports.isExist = isExist; exports.isGo = isGo; exports.isInstallPkg = isInstallPkg; exports.isPkg = isPkg; exports.isRust = isRust; exports.isWritable = isWritable; exports.jsShell = _chunk7C7ZRD6Hjs.jsShell; exports.transformArgv = transformArgv; exports.useNodeWorker = _chunk7C7ZRD6Hjs.useNodeWorker; exports.useProcressNodeWorker = _chunk7C7ZRD6Hjs.useProcressNodeWorker; exports.withTaskName = withTaskName; exports.writeFile = writeFile;
;