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.
39 lines (33 loc) • 966 B
JavaScript
import fs from "fs";
import path from "path";
/**
* Package class for handling read-modify-write operations
* on the local package.json file.
*/
export default new class Package {
#_PATH = path.join(process.cwd(), 'package.json');
_JSON;
/**
* Constructor: Reads and
* parses package.json into _JSON.
*/
constructor(){
const content = fs.readFileSync(this.#_PATH, 'utf-8');
this._JSON = JSON.parse(content);
}
/**
* Accepts a callback function to modify the package.json content.
* @param {Function} fn - A function that receives the JSON object for modification.
* @returns {Package} - Returns 'this' for method chaining.
*/
add(fn){
fn(this._JSON);
return this;
}
/**
* Saves the modified JSON back to package.json with indentation.
*/
async save(){
fs.writeFileSync(this.#_PATH, JSON.stringify(this._JSON, null, 2));
}
}