@darkobits/ts
Version:
Vite-based toolchain for Node projects.
47 lines (46 loc) • 1.62 kB
JavaScript
import path from "node:path";
import chalk from "chalk";
import fs from "fs-extra";
import log from "./log.js";
import { getPackageContext } from "./utils.js";
const prefix = chalk.dim.cyan("executable");
function executablePlugin() {
return {
name: "ts:executable-plugin",
enforce: "post",
async closeBundle() {
const binList = [];
const { root, packageJson: { bin } } = await getPackageContext();
if (!bin) {
log.debug(prefix, chalk.yellow.dim("Project does not declare any executable scripts."));
return;
}
if (typeof bin === "string") {
binList.push(path.resolve(root, bin));
} else if (typeof bin === "object") {
Object.values(bin).forEach((binPath) => binList.push(path.resolve(root, binPath)));
} else {
this.error(new TypeError(`Expected type of "bin" in package.json to be "string" or "object", got "${typeof bin}".`));
}
log.debug(prefix, `Root: ${root}`);
log.debug(prefix, "Executables:", binList);
try {
for (const binPath of binList) {
if (await fs.exists(binPath)) {
await fs.chmod(binPath, "0755");
log.info(prefix, chalk.green(path.relative(root, binPath)));
} else {
const relativeBinPath = path.relative(root, binPath);
log.warn(prefix, `Executable ${chalk.green(relativeBinPath)} declared in package.json does not exist.`);
}
}
} catch (error) {
this.error(error);
}
}
};
}
export {
executablePlugin as default
};
//# sourceMappingURL=executable-plugin.js.map