@mail-core/cli
Version:
Инструментарий для написания cli-скриптов
63 lines • 2.16 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.shebangFix = void 0;
const fs_1 = require("fs");
const path_1 = require("path");
const command_1 = require("../../../command/command");
const pkg_1 = require("../../../pkg/pkg");
exports.shebangFix = command_1.createCommand({
name: 'shebang-fix',
describe: 'auto add shebang',
options: {
path: {
type: 'string',
default: './cli/',
description: 'path to apply',
},
reg: {
type: 'boolean',
description: 'register on publishOnly hook'
},
},
handler({ path, reg }, { console, describe }) {
const installType = pkg_1.getPackageInstallType(__dirname);
if (!installType || !pkg_1.isRootPackage(__dirname) && installType !== 'self') {
console.verbose('skipped');
return;
}
const shebang = `#!/usr/bin/env node`;
const fix = console.spinner(describe, {});
try {
if (!fs_1.existsSync(path)) {
fix.warn();
return;
}
fs_1.readdirSync(path).forEach((entry) => {
const file = path_1.join(path, entry);
if (/\.js$/.test(file)) {
const content = fs_1.readFileSync(file) + '';
if (!content.includes(shebang)) {
fs_1.writeFileSync(file, `${shebang}\n\n${content}`);
}
}
});
}
catch (err) {
fix.fail();
console.warn('shebang fix failed:', err);
throw err;
}
fix.succeed();
if (reg && !pkg_1.IS_NPX_ENV) {
const fix = console.spinner('register prepublishOnly npm hook');
if (pkg_1.readPackageJson() !== pkg_1.readPackageJson(__dirname)) {
pkg_1.runCommandByScriptHook('prepublishOnly', 'npx mail-core-cli shebang-fix', false);
fix.succeed();
}
else {
fix.warn();
}
}
},
});
//# sourceMappingURL=index.js.map