UNPKG

@zambelz/zhc

Version:
68 lines 2.7 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.isFileExecutable = exports.isDirectory = exports.removeFileExtension = exports.fileLists = exports.appendFile = exports.createFile = exports.createDirectory = void 0; const node_fs_1 = __importDefault(require("node:fs")); const node_child_process_1 = require("node:child_process"); const createDirectory = ({ path, force = false, recursive = false }) => { if (!force && node_fs_1.default.existsSync(path)) { throw new Error(`"${path}" is exists`); } if (force && node_fs_1.default.existsSync(path)) { node_fs_1.default.rmdirSync(path, { recursive: true }); } node_fs_1.default.mkdirSync(path, { recursive }); if (!node_fs_1.default.existsSync(path)) { throw new Error(`Failed to create "${path}"`); } }; exports.createDirectory = createDirectory; const createFile = ({ path, force = false, content = "" }) => { if (!force && node_fs_1.default.existsSync(path)) { throw new Error(`"${path}" is exists`); } if (force && node_fs_1.default.existsSync(path)) { node_fs_1.default.rmSync(path, { recursive: true }); } node_fs_1.default.writeFileSync(path, content); if (!node_fs_1.default.existsSync(path)) { throw new Error(`Failed to create "${path}"`); } }; exports.createFile = createFile; const appendFile = ({ path, force = false, contents = [] }) => { if (force && node_fs_1.default.existsSync(path)) { node_fs_1.default.rmSync(path, { recursive: true }); } if (!node_fs_1.default.existsSync(path)) { node_fs_1.default.writeFileSync(path, ""); } for (const content of contents) { node_fs_1.default.appendFileSync(path, content); } if (!node_fs_1.default.existsSync(path)) { throw new Error(`Failed to create "${path}"`); } }; exports.appendFile = appendFile; const fileLists = (filePath) => { return node_fs_1.default.readdirSync(filePath); }; exports.fileLists = fileLists; const removeFileExtension = (name) => { return name.split(".")[0]; }; exports.removeFileExtension = removeFileExtension; const isDirectory = (filePath) => { return node_fs_1.default.lstatSync(filePath).isDirectory(); }; exports.isDirectory = isDirectory; const isFileExecutable = (filePath) => { return (0, node_child_process_1.execSync)(`ls -l ${filePath} | awk '{ print $1 }'`) .toString() .charAt(3) === "x"; }; exports.isFileExecutable = isFileExecutable; //# sourceMappingURL=fileOperation.js.map