UNPKG

bayascript

Version:

Write computer programming in Hausa language

78 lines (75 loc) 2.27 kB
const os = require("os"); const fs = require("fs"); const fse = require("fs-extra"); const path = require("node:path"); const _ = require("underscore"); class FileSystemClass { sourceCopy(baseDir, sourceDir, destinationDir, param) { try { if (!fse.existsSync(sourceDir)) { console.error(`Bayascript ya kasa ganin wani aikin sa a ${sourceDir}`); return; } if (!fse.existsSync(path.dirname(destinationDir))) { fse.mkdirp(path.dirname(destinationDir)); } const oldBuildDir = `${baseDir}/build`; if (fs.existsSync(oldBuildDir)) { fs.rmSync(oldBuildDir, { recursive: true }); } var options = { overwrite: true, recursive: true, copySymlinksAsFiles: true, dereference: true, }; if ( (!param || param !== "link") && fse.existsSync(path.dirname(`${destinationDir}/node_modules`)) ) { //remove node_modules if not forced options.filter = (path) => { if (fs.lstatSync(path).isFile()) return true; return !(path.indexOf("node_modules") > -1); }; } fse.copySync(sourceDir, destinationDir, options); } catch (ex) { console.warn( `Bayascript ya kasa kwafar source daga ${sourceDir} zuwa ${destinationDir})` ); } } remove(dir) { return fs.rmSync(dir, { recursive: true, force: true }); } readFileAsync(path) { return fs.readFileSync(path, "utf-8"); } writeFileSync(filePath, data) { const dir = filePath.substr(0, filePath.lastIndexOf("/")); if (!fs.existsSync(dir)) { fs.mkdirSync(dir); } return fs.writeFileSync(filePath, data); } cleanup(dir) { const filter = ".bs"; if (!fs.existsSync(dir)) { console.warn(`Bayascript ya kasa ganin wajan aiki`); return; } var files = fs.readdirSync(dir); for (var i = 0; i < files.length; i++) { var filename = path.join(dir, files[i]); var stat = fs.lstatSync(filename); if (stat.isDirectory()) { this.cleanup(filename); //recurse } else if (filename.endsWith(filter)) { fs.unlinkSync(filename); } } } } const FileSystem = new FileSystemClass(); module.exports = FileSystem;