alwaysai
Version:
The alwaysAI command-line interface (CLI)
159 lines • 6.39 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.getSystemId = exports.setSystemId = exports.RemoteAaiCfg = exports.LocalAaiCfg = exports.aaiConfigSchema = exports.SYSTEM_IDS = void 0;
const config_nodejs_1 = require("@alwaysai/config-nodejs");
const ajv_1 = require("ajv");
const path_1 = require("path");
const constants_1 = require("../constants");
const environment_1 = require("../environment");
const paths_1 = require("../paths");
const util_1 = require("../util");
exports.SYSTEM_IDS = Object.keys(constants_1.SystemId);
const path = (0, path_1.join)(paths_1.LOCAL_AAI_CFG_DIR, paths_1.ALWAYSAI_CONFIG_FILE_NAME);
exports.aaiConfigSchema = {
type: 'object',
properties: {
systemId: {
type: 'string',
enum: exports.SYSTEM_IDS
}
},
required: ['systemId']
};
const ajv = new ajv_1.default();
const validateAaiConfig = ajv.compile(exports.aaiConfigSchema);
function AaiConfigFile(baseDir) {
const filePath = (0, path_1.join)(baseDir !== null && baseDir !== void 0 ? baseDir : path);
const configFile = (0, config_nodejs_1.ConfigFileSchema)({
path: filePath,
validateFunction: validateAaiConfig,
initialValue: { systemId: 'production' }
});
return configFile;
}
class AaiCfg {
constructor(baseDir) {
this.baseDir = baseDir;
}
writeAaiCfgFile() {
// intentionally empty
}
readAaiCfgFile() {
// intentionally empty
}
getFileName() {
return paths_1.ALWAYSAI_CONFIG_FILE_NAME;
}
}
class LocalAaiCfg extends AaiCfg {
constructor(baseDir = paths_1.LOCAL_AAI_CFG_DIR) {
super(baseDir);
this.fullPath = (0, path_1.join)(this.baseDir, this.getFileName());
this.aaiConfigFile = AaiConfigFile(this.fullPath);
}
async getValidationErrors() {
return validateAaiConfig.errors;
}
async writeAaiCfgFile() {
let contents = { systemId: getSystemId() };
if (this.aaiConfigFile.exists()) {
try {
// NOTE: readAaiCfgFile will do a validation
const origParsed = await this.readAaiCfgFile();
contents = Object.assign(Object.assign({}, origParsed), contents);
}
catch (err) {
util_1.logger.error(`${this.getFileName()} is invalid:\n${JSON.stringify(this.getValidationErrors(), null, 2)}`);
this.aaiConfigFile.remove();
}
}
this.aaiConfigFile.write(contents);
}
async readAaiCfgFile() {
const parsedContents = this.aaiConfigFile.read();
return parsedContents;
}
getFileName() {
return super.getFileName();
}
}
exports.LocalAaiCfg = LocalAaiCfg;
class RemoteAaiCfg extends AaiCfg {
constructor(targetHostName, baseDir = paths_1.REMOTE_AAI_CFG_DIR_LINUX) {
super(baseDir);
this.fullPath = path_1.posix.join(this.baseDir, this.getFileName());
this.spawner = (0, util_1.SshSpawner)({
targetHostname: targetHostName,
targetPath: this.baseDir
});
this.aaiConfigFile = AaiConfigFile(this.fullPath);
}
validate(parsedContents) {
return this.aaiConfigFile.validate(parsedContents);
}
async writeAaiCfgFile() {
let contents = { systemId: getSystemId() };
if (await this.spawner.exists(this.getFileName())) {
util_1.logger.debug(`${this.getFileName()} already exists, updating file.`);
try {
// NOTE: readAaiCfgFile will do a validation
const origParsed = await this.readAaiCfgFile();
contents = Object.assign(Object.assign({}, origParsed), contents);
}
catch (e) {
util_1.logger.error(`${this.getFileName()} is invalid:\n${JSON.stringify(this.getValidationErrors(), null, 2)}`);
await this.spawner.rimraf(this.getFileName());
}
}
await this.spawner.mkdirp();
await this.spawner.writeFile(this.getFileName(), JSON.stringify(contents, null, 2));
}
async getValidationErrors() {
return validateAaiConfig.errors;
}
async readAaiCfgFile() {
const origContents = await this.spawner.readFile(this.getFileName());
const origParsed = JSON.parse(origContents);
if (!this.validate(origParsed)) {
throw new Error(`Validation of ${this.getFileName()} failed:\n${JSON.stringify(this.getValidationErrors(), null, 2)}!`);
}
return origParsed;
}
}
exports.RemoteAaiCfg = RemoteAaiCfg;
/*===================================================================
System ID Usage
===================================================================*/
function setSystemId(systemId) {
const localAaiConfigFile = new LocalAaiCfg();
// TODO: replace with pure wrapper functionality
return localAaiConfigFile.aaiConfigFile.update((json) => {
json.systemId = systemId;
});
}
exports.setSystemId = setSystemId;
function getSystemId() {
var _a;
const localAaiConfigFile = new LocalAaiCfg();
// TODO: can catch validation errors here and display them
const maybeConfig = localAaiConfigFile.aaiConfigFile.readIfExists();
if (environment_1.ALWAYSAI_SYSTEM_ID) {
// When the env var is set return the overridden value without updating the config file.
// This prevents one-off commands from having side effects.
if (Object.keys(constants_1.SystemId).includes(environment_1.ALWAYSAI_SYSTEM_ID)) {
if (!(maybeConfig === null || maybeConfig === void 0 ? void 0 : maybeConfig.systemId)) {
setSystemId(environment_1.ALWAYSAI_SYSTEM_ID);
}
return environment_1.ALWAYSAI_SYSTEM_ID;
}
throw new Error(`Invalid ALWAYSAI_SYSTEM_ID: ${environment_1.ALWAYSAI_SYSTEM_ID}`);
}
// TODO: replace with pure wrapper functionality
if (!(maybeConfig === null || maybeConfig === void 0 ? void 0 : maybeConfig.systemId)) {
setSystemId('production');
}
const systemId = (_a = maybeConfig === null || maybeConfig === void 0 ? void 0 : maybeConfig.systemId) !== null && _a !== void 0 ? _a : 'production';
return systemId;
}
exports.getSystemId = getSystemId;
//# sourceMappingURL=system-id.js.map