@typecad/typecad
Version:
🤖programmatically 💥create 🛰️hardware
67 lines (66 loc) • 3.27 kB
JavaScript
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
if (kind === "m") throw new TypeError("Private method is not writable");
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
};
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
};
var _Config_instances, _Config_contents, _Config_open;
import fs from 'node:fs';
import process from 'node:process';
import chalk from 'chalk';
export class Config {
constructor() {
_Config_instances.add(this);
_Config_contents.set(this, '');
}
get(key) {
__classPrivateFieldGet(this, _Config_instances, "m", _Config_open).call(this);
// exit if no config file
if (__classPrivateFieldGet(this, _Config_contents, "f") == '')
return '';
if (key in __classPrivateFieldGet(this, _Config_contents, "f")) {
return __classPrivateFieldGet(this, _Config_contents, "f")[key];
}
else {
return '';
}
}
set(key, value) {
__classPrivateFieldGet(this, _Config_instances, "m", _Config_open).call(this);
// if there was no file, make a new object
if (__classPrivateFieldGet(this, _Config_contents, "f") == '')
__classPrivateFieldSet(this, _Config_contents, {}, "f");
// update insert new key/value
if (key in __classPrivateFieldGet(this, _Config_contents, "f")) {
__classPrivateFieldGet(this, _Config_contents, "f")[key] = value;
}
else {
__classPrivateFieldGet(this, _Config_contents, "f")[key] = value;
}
try {
fs.writeFileSync(process.cwd() + '/typecad.json', JSON.stringify(__classPrivateFieldGet(this, _Config_contents, "f")));
}
catch (err) {
console.error(err);
return false;
}
return true;
}
}
_Config_contents = new WeakMap(), _Config_instances = new WeakSet(), _Config_open = function _Config_open() {
try {
if (fs.existsSync(process.cwd() + '/typecad.json') == false) {
return __classPrivateFieldSet(this, _Config_contents, '', "f");
}
let config_text = fs.readFileSync(process.cwd() + '/typecad.json', "utf8");
__classPrivateFieldSet(this, _Config_contents, JSON.parse(config_text), "f");
}
catch (error) {
console.log('- ', chalk.red.bold('ERROR: cannot read/process typecad.json. Try editing or deleting the file.'));
}
};