From 1b0ba00c1b8a465cbd1a883480dd2fa375f9c6fd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Mauricio=20de=20O=2E=20Alves?= Date: Sun, 15 Nov 2020 22:12:12 -0300 Subject: [PATCH] Handle config after install --- levain.levain.yaml | 1 + src/action/saveConfig.ts | 14 ++++++++++++++ src/lib/config.ts | 29 +++++++++++++++++++++++++++++ src/lib/loader.ts | 4 ++++ 4 files changed, 48 insertions(+) create mode 100644 src/action/saveConfig.ts diff --git a/levain.levain.yaml b/levain.levain.yaml index a9cbe6dc..3ad92e20 100644 --- a/levain.levain.yaml +++ b/levain.levain.yaml @@ -2,6 +2,7 @@ version: 0.0.1 cmd.install: - copy --verbose src bin levain.cmd levain.sh ${baseDir} + - saveConfig - levainShell setx PATH ${baseDir};${baseDir}\bin cmd.shell: diff --git a/src/action/saveConfig.ts b/src/action/saveConfig.ts new file mode 100644 index 00000000..cb2a97bc --- /dev/null +++ b/src/action/saveConfig.ts @@ -0,0 +1,14 @@ +import * as log from "https://deno.land/std/log/mod.ts"; + +import Action from "../lib/action.ts"; +import Config from "../lib/config.ts"; +import Package from '../lib/package/package.ts'; + +export default class SaveConfig implements Action { + constructor(private config:Config) { + } + + execute(pkg:Package, parameters:string[]): void { + this.config.save(); + } +} \ No newline at end of file diff --git a/src/lib/config.ts b/src/lib/config.ts index baaecb16..33013b50 100644 --- a/src/lib/config.ts +++ b/src/lib/config.ts @@ -16,6 +16,9 @@ export default class Config { private _env:any = {}; private _context:any = {}; // Do we really need two of them (_env and _context)? + private _extraRepos: string[] = []; + + constructor(args: any) { this.configEnv(args); this.configHome(); @@ -38,6 +41,10 @@ export default class Config { return this._env["levainHome"]; } + get levainConfigFile(): string { + return path.resolve(this.levainHome, ".levain", "config.json"); + } + get levainRegistry(): string { return path.resolve(this.levainHome, ".levain", "registry"); } @@ -97,6 +104,17 @@ export default class Config { return path.resolve(this.levainSrcDir, "extra-bin", Deno.build.os); } + public save(): void { + let cfg:any = {}; + cfg.levainHome = this.levainHome; + cfg.repos = this._extraRepos; + + let fileName = this.levainConfigFile; + + log.info(`SAVE ${fileName}`); + Deno.writeTextFileSync(fileName, JSON.stringify(cfg, null, 3)); + } + ///////////////////////////////////////////////////////////////////////////////// private configEnv(args: any): void { Object.keys(args).forEach(key => { @@ -107,7 +125,17 @@ export default class Config { } private configHome(): void { + log.info(""); + if (this._env["levainHome"]) { + log.info(`ARG levainHome=${this._env["levainHome"]}`) + return; + } + + let config = path.resolve(this.levainSrcDir, "..", ".levain", "config.json"); + if (Deno.statSync(config)) { + this._env["levainHome"] = path.resolve(this.levainSrcDir, ".."); + log.info(`CFG levainHome=${this._env["levainHome"]}`) return; } @@ -180,6 +208,7 @@ export default class Config { let repoPath = path.resolve(repo); log.info(`LevainRepo: addRepo ${repoPath}`); + this._extraRepos.push(repoPath); repos.push(new FileSystemRepository(this, repoPath)); }); } diff --git a/src/lib/loader.ts b/src/lib/loader.ts index 0134cfaf..dba6a2fb 100644 --- a/src/lib/loader.ts +++ b/src/lib/loader.ts @@ -15,6 +15,7 @@ import Copy from "../action/copy.ts"; import Extract from "../action/extract.ts"; import LevainShell from "../action/levainShell.ts"; import Mkdir from "../action/mkdir.ts"; +import SaveConfig from "../action/saveConfig.ts"; import SetEnv from "../action/setEnv.ts"; import Template from "../action/template.ts"; @@ -75,6 +76,9 @@ export default class Loader { case 'mkdir': return new Mkdir(this.config); + case 'saveConfig': + return new SaveConfig(this.config); + case 'setEnv': return new SetEnv(this.config);