UNPKG

@commercelayer/cli

Version:
203 lines (202 loc) 8.05 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.paramDefault = exports.paramExists = exports.paramEditable = exports.configParam = exports.ConfigParamsEditable = exports.ConfigParams = exports.filterApplications = exports.currentOrganization = exports.currentApplication = exports.deleteTokenFile = exports.readTokenFile = exports.writeTokenFile = exports.tokenFileExists = exports.tokenFilePath = exports.deleteConfigFile = exports.readConfigFile = exports.writeConfigFile = exports.configFileExists = exports.configFilePath = exports.appsDirExists = exports.appsDir = exports.appsDirRead = exports.appsDirCreate = void 0; const tslib_1 = require("tslib"); const configstore_1 = tslib_1.__importDefault(require("configstore")); const node_path_1 = require("node:path"); const node_fs_1 = require("node:fs"); const cli_core_1 = require("@commercelayer/cli-core"); const packageJson = require('../package.json'); const clicfg = new configstore_1.default(packageJson.name, null, { globalConfigPath: true }); exports.default = clicfg; const fixed = { applicationsDir: 'applications', configSuffix: 'config.json', tokenSuffix: 'token.json', encoding: 'utf-8' }; const appsDir = (config) => { return (0, node_path_1.join)(config.configDir, fixed.applicationsDir); }; exports.appsDir = appsDir; const appsDirExists = (config) => { return (0, node_fs_1.existsSync)(appsDir(config)); }; exports.appsDirExists = appsDirExists; const appsDirCreate = (config) => { (0, node_fs_1.mkdirSync)(appsDir(config), { recursive: true }); }; exports.appsDirCreate = appsDirCreate; const configFilePath = (config, { key }) => { return (0, node_path_1.join)(appsDir(config), `${key}.${fixed.configSuffix}`); }; exports.configFilePath = configFilePath; const tokenFilePath = (config, { key }) => { return (0, node_path_1.join)(appsDir(config), `${key}.${fixed.tokenSuffix}`); }; exports.tokenFilePath = tokenFilePath; const configFileExists = (config, app) => { const filePath = configFilePath(config, app); return (0, node_fs_1.existsSync)(filePath); }; exports.configFileExists = configFileExists; const tokenFileExists = (config, app) => { const filePath = tokenFilePath(config, app); return (0, node_fs_1.existsSync)(filePath); }; exports.tokenFileExists = tokenFileExists; const writeConfigFile = (config, app) => { const filePath = configFilePath(config, app); try { (0, node_fs_1.writeFileSync)(filePath, JSON.stringify(app, null, 4)); } catch (err) { throw new Error(`Error saving config file: ${err.message}`); } }; exports.writeConfigFile = writeConfigFile; const writeTokenFile = (config, app, token) => { const filePath = tokenFilePath(config, app); try { (0, node_fs_1.writeFileSync)(filePath, JSON.stringify(token, null, 4)); } catch (err) { throw new Error(`Error saving access token: ${err.message}`); } }; exports.writeTokenFile = writeTokenFile; const readConfigFile = (config, app) => { const filePath = configFilePath(config, app); const cliConfig = (0, node_fs_1.readFileSync)(filePath, { encoding: fixed.encoding }); return JSON.parse(cliConfig); }; exports.readConfigFile = readConfigFile; const readTokenFile = (config, app) => { const filePath = tokenFilePath(config, app); const token = (0, node_fs_1.readFileSync)(filePath, { encoding: fixed.encoding }); return JSON.parse(token); }; exports.readTokenFile = readTokenFile; const deleteConfigFile = (config, app) => { const filePath = configFilePath(config, app); (0, node_fs_1.unlinkSync)(filePath); return true; }; exports.deleteConfigFile = deleteConfigFile; const deleteTokenFile = (config, app) => { const filePath = tokenFilePath(config, app); (0, node_fs_1.unlinkSync)(filePath); return true; }; exports.deleteTokenFile = deleteTokenFile; const appsDirRead = (config, filter) => { if (!appsDirExists(config)) return []; const files = (0, node_fs_1.readdirSync)(appsDir(config)).map(f => { const fc = f.split('.'); if (fc.length !== 3) return undefined; if (fc[fc.length - 2] !== 'config') return undefined; if (fc[fc.length - 1] !== 'json') return undefined; const key = fc[0]; if (filter.key && (key !== filter.key)) return undefined; return { key }; }).filter(f => f); // not undefined applications return files.map(f => { return readConfigFile(config, f); }); }; exports.appsDirRead = appsDirRead; const currentApplication = (app) => { if (app) clicfg.set(ConfigParams.currentApplication, { key: app.key, id: app.id, name: app.name, slug: app.slug, domain: app.domain, kind: app.kind, mode: app.mode, organization: app.organization, scope: app.scope, email: app.email, alias: app.alias, api: app.api }); return clicfg.get(ConfigParams.currentApplication); }; exports.currentApplication = currentApplication; const currentOrganization = () => { const current = clicfg.get(ConfigParams.currentApplication); return current?.slug; }; exports.currentOrganization = currentOrganization; const filterApplications = (config, flags) => { const mode = flags.mode || (flags.live ? cli_core_1.clApi.execMode(flags.live) : undefined); const applications = appsDirRead(config, { key: flags.key || flags.appkey }).filter(app => { if (flags.organization && (flags.organization !== app.slug)) return false; if (flags.domain && (flags.domain !== app.domain)) return false; if (flags.kind && (flags.kind !== app.kind)) return false; if (mode && (mode !== app.mode)) return false; if (flags.id && (flags.id !== app.id)) return false; if (flags.alias && (flags.alias !== app.alias)) return false; if (flags.api && (flags.api !== app.api)) return false; return true; }); // Fix scope applications.forEach(a => { a.api = a.api || 'core'; }); return applications; }; exports.filterApplications = filterApplications; var ConfigParams; (function (ConfigParams) { ConfigParams["currentApplication"] = "currentApplication"; ConfigParams["commandRetention"] = "commandRetention"; ConfigParams["applicationTypeCheck"] = "applicationTypeCheck"; ConfigParams["scopeCheck"] = "scopeCheck"; ConfigParams["defaultDomain"] = "defaultDomain"; ConfigParams["test"] = "test"; })(ConfigParams || (exports.ConfigParams = ConfigParams = {})); var ConfigParamsEditable; (function (ConfigParamsEditable) { ConfigParamsEditable[ConfigParamsEditable["test"] = 0] = "test"; ConfigParamsEditable[ConfigParamsEditable["commandRetention"] = 1] = "commandRetention"; })(ConfigParamsEditable || (exports.ConfigParamsEditable = ConfigParamsEditable = {})); const defaultConfig = { test: 'defaultTestValue', commandRetention: 30, // days of retention defaultDomain: cli_core_1.clConfig.api.default_domain, applicationTypeCheck: cli_core_1.clConfig.cli.applications, scopeCheck: cli_core_1.clConfig.application.login_scopes }; const paramEditable = (param) => { return (Object.keys(ConfigParamsEditable).includes(param)); }; exports.paramEditable = paramEditable; const paramExists = (param) => { return (Object.keys(ConfigParams).includes(param)); }; exports.paramExists = paramExists; const paramDefault = (param) => { return defaultConfig[param]; }; exports.paramDefault = paramDefault; const configParam = (param, value) => { if (value) { if (!paramEditable(param)) throw new Error(`Parameter ${param} is not editable`); clicfg.set(param, value); } return clicfg.get(param) || paramDefault(param); }; exports.configParam = configParam;