fh-serverless
Version:
A Node.js CLI for optimizing and generating configuration files for Serverless projects
68 lines (67 loc) • 2.21 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.cleanFiles = exports.saveYml = exports.getYml = exports.getJson = exports.listRecursiveSync = void 0;
const path_1 = __importDefault(require("path"));
const fs_1 = __importDefault(require("fs"));
const js_yaml_1 = __importDefault(require("js-yaml"));
function listRecursiveSync(directoryPath) {
const files = fs_1.default.readdirSync(directoryPath);
return files.reduce(function (acc, file) {
const filePath = path_1.default.join(directoryPath, file);
const stats = fs_1.default.lstatSync(filePath);
stats.isDirectory() ? acc.folders.push(listRecursiveSync(filePath)) : acc.files.push(filePath);
return acc;
}, {
folderName: path_1.default.basename(directoryPath),
files: [],
folders: [],
});
}
exports.listRecursiveSync = listRecursiveSync;
function getJson(filePath) {
try {
const data = fs_1.default.readFileSync(filePath, 'utf-8');
return JSON.parse(data);
}
catch (error) {
const fileName = path_1.default.basename(filePath);
console.warn(`File ${fileName} not found`);
return undefined;
}
}
exports.getJson = getJson;
function getYml(filePath) {
try {
const data = fs_1.default.readFileSync(filePath, 'utf-8');
return js_yaml_1.default.load(data);
}
catch (error) {
const fileName = path_1.default.basename(filePath);
console.warn(`File ${fileName} not found`);
return undefined;
}
}
exports.getYml = getYml;
function saveYml(filePath, data) {
try {
fs_1.default.writeFileSync(filePath, js_yaml_1.default.dump(data));
}
catch (error) {
const fileName = path_1.default.basename(filePath);
console.warn(`File ${fileName} not found`);
return undefined;
}
}
exports.saveYml = saveYml;
function cleanFiles(filePath) {
try {
fs_1.default.unlinkSync(filePath);
}
catch (e) {
return false;
}
}
exports.cleanFiles = cleanFiles;