UNPKG

@nodescript/cli

Version:
79 lines 3.15 kB
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; var __metadata = (this && this.__metadata) || function (k, v) { if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v); }; import chalk from 'chalk'; import * as fs from 'fs'; import glob from 'glob'; import { dep } from 'mesh-ioc'; import path from 'path'; import { fileURLToPath } from 'url'; import Yaml from 'yaml'; import { CliOptionsSchema } from '../options.js'; import { isFileExists } from '../util.js'; const __dirname = path.dirname(fileURLToPath(import.meta.url)); export class ConfigManager { constructor() { this.options = CliOptionsSchema.decode({}); } async init() { // await this.copyResources(); await this.readOptions(); } get envFile() { return path.resolve(this.rootDir, '.env'); } get optionsFile() { return path.resolve(this.rootDir, '.nodescriptrc.yaml'); } async copyResources() { const resourcesDir = path.join(__dirname, '../../../resources'); const resources = await glob('**/*', { cwd: resourcesDir, dot: true, }); for (const filename of resources) { const sourceFile = path.join(resourcesDir, filename); const targetFile = path.join(this.rootDir, filename); await fs.promises.mkdir(path.dirname(targetFile), { recursive: true }); const exists = await isFileExists(targetFile); if (exists) { continue; } await fs.promises.cp(sourceFile, targetFile, { errorOnExist: true }); console.info('Created', chalk.yellow(filename), ' — please review and adjust it'); } } async readOptions() { await this.readOptionsFile(); await this.readEnv(); } async readOptionsFile() { const file = this.optionsFile; try { const text = await fs.promises.readFile(file, 'utf-8'); const opts = Yaml.parse(text); this.options = CliOptionsSchema.decode(opts); } catch (err) { } } async readEnv() { for (const key of Object.keys(this.options)) { const envKey = 'NODESCRIPT_' + key.replace(/([A-Z])/g, '_$1').toUpperCase(); const envValue = process.env[envKey]; if (envValue) { this.options[key] = envValue; } } } } __decorate([ dep({ key: 'rootDir' }), __metadata("design:type", String) ], ConfigManager.prototype, "rootDir", void 0); //# sourceMappingURL=config.js.map