zcatalyst-cli
Version:
Command Line Tool for CATALYST
91 lines (90 loc) • 3.72 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const ansi_colors_1 = require("ansi-colors");
const path_1 = require("path");
const index_js_1 = __importDefault(require("../error/index.js"));
const constants_1 = require("../util_modules/constants");
const fs_1 = require("../util_modules/fs");
const js_1 = require("../util_modules/js");
const project_1 = require("../util_modules/project");
class Config {
constructor(configPath, src = {}) {
this.configPath = configPath;
this.data = src;
this.isLoaded = false;
}
get(key, fallback) {
return js_1.JS.get(this.data, key, fallback);
}
set(key, value) {
return js_1.JS.set(this.data, key, value);
}
unset(key) {
return js_1.JS.unset(this.data, key);
}
has(key) {
return js_1.JS.has(this.data, key);
}
save() {
return __awaiter(this, void 0, void 0, function* () {
const content = JSON.stringify(this.data, null, 2) + '\n';
yield fs_1.ASYNC.ensureFile(this.configPath);
return fs_1.ASYNC.writeFile(this.configPath, content, 'utf8');
});
}
syncSave() {
const content = JSON.stringify(this.data, null, 2) + '\n';
fs_1.SYNC.ensureFile(this.configPath);
return fs_1.SYNC.writeFile(this.configPath, content, 'utf8');
}
get loaded() {
return this.isLoaded;
}
set loaded(load) {
this.isLoaded = load;
}
static load(canIgnore) {
return __awaiter(this, void 0, void 0, function* () {
const pd = (0, project_1.getProjectRoot)();
try {
const configPath = (0, path_1.join)(pd, constants_1.FILENAME.config);
const data = yield fs_1.ASYNC.readJSONFile(configPath, { checkpath: true });
if (data !== undefined) {
const config = new Config(configPath, data);
config.loaded = true;
return config;
}
}
catch (e) {
const err = index_js_1.default.getErrorInstance(e, {
message: 'There was an error loading a file',
skipHelp: false
});
err.errorId = 'CONFIG-1';
err.arg = [(0, ansi_colors_1.bold)(constants_1.FILENAME.config), err.message];
throw err;
}
if (canIgnore) {
return new Config((0, path_1.join)(pd, constants_1.FILENAME.config));
}
throw new index_js_1.default('Not in a Catalyst app directory', {
exit: 1,
errorId: 'CONFIG-2',
arg: [(0, ansi_colors_1.bold)(constants_1.FILENAME.config), (0, ansi_colors_1.bold)('catalyst init'), pd]
});
});
}
}
exports.default = Config;