Skip to content

Commit

Permalink
Handle config after install
Browse files Browse the repository at this point in the history
  • Loading branch information
jmoalves committed Nov 16, 2020
1 parent 49aefb1 commit 1b0ba00
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 0 deletions.
1 change: 1 addition & 0 deletions levain.levain.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
14 changes: 14 additions & 0 deletions src/action/saveConfig.ts
Original file line number Diff line number Diff line change
@@ -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();
}
}
29 changes: 29 additions & 0 deletions src/lib/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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");
}
Expand Down Expand Up @@ -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 => {
Expand All @@ -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;
}

Expand Down Expand Up @@ -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));
});
}
Expand Down
4 changes: 4 additions & 0 deletions src/lib/loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down Expand Up @@ -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);

Expand Down

0 comments on commit 1b0ba00

Please sign in to comment.