@veecode-platform/safira-cli
Version:
Generate a microservice project from your spec.
148 lines (147 loc) • 5.02 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.FileSystemUtils = void 0;
const tslib_1 = require("tslib");
const fs = tslib_1.__importStar(require("fs"));
const path = tslib_1.__importStar(require("path"));
const os = tslib_1.__importStar(require("os"));
const child_process_1 = require("child_process");
const string_utils_1 = require("./string-utils");
class FileSystemUtils {
static getBreakLine() {
return os.EOL;
}
static getCurrentPath() {
return process.cwd();
}
static buildPath(...paths) {
return path.join(...paths);
}
static getFileSeparator() {
return path.sep;
}
static createFolder(path, recursive = true) {
if (!this.exists(path)) {
fs.mkdirSync(path, { recursive: recursive });
}
}
static deleteFolder(directoryPath) {
if (this.exists(directoryPath)) {
for (const file of this.readDir(directoryPath)) {
const curPath = this.buildPath(directoryPath, file);
if (fs.lstatSync(curPath).isDirectory()) {
this.deleteFolder(curPath);
}
else {
fs.unlinkSync(curPath);
}
}
fs.rmdirSync(directoryPath);
}
}
static deleteFile(path) {
if (this.exists(path)) {
fs.unlinkSync(path);
}
}
static exists(path) {
return fs.existsSync(path);
}
static copyFolderRecursiveSync(source, target) {
const targetFolder = this.buildPath(target, path.basename(source));
this.createFolder(targetFolder);
if (fs.lstatSync(source).isDirectory()) {
for (const file of this.readDir(source)) {
const curSource = FileSystemUtils.buildPath(source, file);
if (fs.lstatSync(curSource).isDirectory()) {
FileSystemUtils.copyFolderRecursiveSync(curSource, targetFolder);
}
else {
FileSystemUtils.copyFileSync(curSource, targetFolder);
}
}
}
}
static copyFileSync(source, target) {
let targetFile = target;
if (this.exists(target) && fs.lstatSync(target).isDirectory()) {
targetFile = this.buildPath(target, path.basename(source));
}
this.writeFile(targetFile, this.loadFile(source));
}
static fullPath(paths) {
if (!paths)
return "";
return path.resolve(paths);
}
static loadFile(path) {
return fs.readFileSync(path, "utf8");
}
static writeFile(path, content) {
const destinyFolder = this.getDirectoryPath(path);
if (!this.exists(destinyFolder))
this.createFolder(destinyFolder);
fs.writeFileSync(path, content);
}
static appendFile(path, content) {
if (!this.exists(path)) {
this.writeFile(path, content);
return;
}
fs.appendFileSync(path, content);
}
static chmod(path, mode) {
if (!this.exists(path))
return;
fs.chmodSync(path, mode);
}
static getOsUserHome() {
return os.homedir();
}
static getDirectoryPath(path) {
return path.slice(0, Math.max(0, path.lastIndexOf(this.getFileSeparator())));
}
static isFile(path) {
return fs.lstatSync(this.fullPath(path))?.isFile();
}
static isDirectory(path) {
return fs.lstatSync(this.fullPath(path))?.isDirectory();
}
static commandExists(command) {
try {
return !(0, child_process_1.spawnSync)(command).error;
}
catch {
return false;
}
}
static readDir(dirPath) {
return fs.readdirSync(this.fullPath(dirPath));
}
static getHighFolderFromPath(string) {
return string.split(FileSystemUtils.getFileSeparator()).pop() ?? "";
}
static relativePath(fullPath) {
return path.relative(process.cwd(), fullPath);
}
static listFilesRecursive(directory, filteringExtensions = []) {
const rootPath = FileSystemUtils.fullPath(directory);
const files = FileSystemUtils.readDir(rootPath);
const result = [];
for (const file of files) {
const fullPath = FileSystemUtils.buildPath(rootPath, file);
if (FileSystemUtils.isDirectory(fullPath)) {
const subFiles = this.listFilesRecursive(fullPath, filteringExtensions);
if (subFiles.length > 0)
result.push(...subFiles);
}
else if (filteringExtensions.length === 0 || filteringExtensions.includes(string_utils_1.StringUtils.getFileExtension(file)))
result.push(fullPath);
}
return result;
}
static getTempDirectory() {
return this.buildPath(os.tmpdir(), "safira-cli");
}
}
exports.FileSystemUtils = FileSystemUtils;