UNPKG

devlien

Version:

Devlien is a lightweight, zero-dependency Node.js framework with clean MVC structure, built-in ORM, and intuitive routing for rapid backend development.

155 lines (115 loc) 3.53 kB
import fs from "fs"; import { pathToFileURL } from 'url'; import path, { join } from "path"; import { mkdir } from "fs/promises"; var config; try{ config = await import("../../../../../devlien.config.js"); config = config.default; } catch(error){ config = {}; } export default class DIR { path; constructor(_path=''){ this.path = (_path); } static getFileUrl(_path){ return pathToFileURL(_path).href; } static path(path){ return new this(path); } static async copy(source, destination) { try { fs.cpSync(source, destination, { recursive: true, force: true, errorOnExist: false }, (err) => { if (err) throw err; }); } catch (err) { throw err; } } static async move(source, destination) { try { fs.renameSync(source, destination, { recursive: true, force: true, errorOnExist: false }, (err) => { if (err) throw err; }); } catch (err) { throw err; } } static scan(_path, prefix=''){ return fs.readdirSync(path.join(process.cwd(), config.root?config.root:prefix, _path)) } scan(_path=''){ return fs.readdirSync(path.join(this.path, _path)) } static file(_path){ const dir = new this(); dir.path = path.join(process.cwd(), config.root, _path); return dir; } static async import(_path){ return (await import(pathToFileURL(_path).href)); } async import(_path=''){ return (await import(pathToFileURL(path.join(this.path, _path)).href)); } static async make(filePath){ const dirPath = path.dirname(filePath); return await fs.mkdir(dirPath, { recursive: true }); } async make(dirPath=null){ const basPath = dirPath ? dirPath : this.path; const basename = path.basename(basPath); const mainPath = basPath.replace(basename ? basename : '', ''); return await mkdir(mainPath, { recursive: true }); } async makeFile(filename=null, content=''){ filename = filename ? filename : this.filename(); await this.make(); let filePath = this.path.replace(this.filename(), ''); filePath = (path.join(filePath, filename)); fs.writeFileSync(filePath, content); } filename(isExtension=true){ if(isExtension) return path.basename(this.path); else { return path.basename(this.path, path.extname(this.path)); } } isExist() { return fs.existsSync(this.path) } extension(){ return path.extname(this.path); } static async remove(dirPath){ try { await fs.rm(dirPath, { recursive: true, force: true }, ()=>{ }); } catch (err) { console.error("Error removing directory:", err); } } static utilities(base=''){ let dir = new this(); dir.path = path.join(process.cwd(), 'node_modules/devlien/utilities', base); return dir; } static framework(base=''){ let dir = new this(); dir.path = path.join(process.cwd(), 'node_modules/devlien/framework', base); return dir; } static libraries(base=''){ let dir = new this(); dir.path = path.join(process.cwd(), 'node_modules/devlien/libraries', base); return dir; } }