Skip to content

Commit

Permalink
Merge pull request #95 from sellersindustry/59-cli-environment-variables
Browse files Browse the repository at this point in the history
CLI Environment Variables
  • Loading branch information
SellersEvan authored Apr 22, 2024
2 parents a5a712d + 98af003 commit 8d69386
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 5 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,8 @@ sherpa build [options]
#### Options:
- `-i`, `--input <path>` path to SherpaJS server, defaults to current directory
- `-o`, `--output <path>` path to server output, defaults to input directory
- `-b`, `--bundler <type>` platform bundler ("**Vercel**", "*local**", *default: "local"*)
- `-b`, `--bundler <type>` platform bundler ("**Vercel**", "*local**", *default: "local"*)
- `-v`, `--variable [key values...]` Specify optional environment variables as key=value pairs Ex. `foo=bar test="1234 HI"`
- `--dev` enable development mode, does not minify output
- `-h`, `--help` display help for command

Expand Down
17 changes: 13 additions & 4 deletions src/cli/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@

import { Command, Option } from "commander";
import { Compiler, BundlerType } from "../compiler/index.js";
import { getEnvironmentFiles, getAbsolutePath, getVersion } from "./utilities.js";
import { getEnvironmentFiles, getAbsolutePath, getKeyValuePairs, getVersion } from "./utilities.js";
import { Logger } from "../compiler/utilities/logger/index.js";
let CLI = new Command();


Expand All @@ -28,12 +29,19 @@ CLI.command("build")
.option("-i, --input <path>", "path to SherpaJS server, defaults to current directory")
.option("-o, --output <path>", "path to server output, defaults to input directory")
.option("--dev", "enable development mode, do not minify output")
.option("-v, --variable [keyvalue...]", "Specify optional environment variables as key=value pairs")
.addOption(new Option("-b, --bundler <type>", "platform bundler")
.choices(Object.values(BundlerType))
.default(BundlerType.local))
.action((options) => {
let input = getAbsolutePath(options.input, process.cwd());
let output = getAbsolutePath(options.output, input);
let input = getAbsolutePath(options.input, process.cwd());
let output = getAbsolutePath(options.output, input);
let variables = getKeyValuePairs(options.variable);

if (Logger.hasError(variables.logs)) {
Logger.format(variables.logs);
Logger.exit();
}

Compiler.build({
input: input,
Expand All @@ -46,7 +54,8 @@ CLI.command("build")
}
},
environment: {
files: getEnvironmentFiles(input)
files: getEnvironmentFiles(input),
variables: variables.values
}
}
});
Expand Down
24 changes: 24 additions & 0 deletions src/cli/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import fs from "fs";
import path from "path";
import { Path } from "../compiler/utilities/path/index.js";
import { Level, Message } from "../compiler/utilities/logger/model.js";


export function getEnvironmentFiles(input:string):string[] {
Expand All @@ -33,6 +34,29 @@ export function getAbsolutePath(filepath:string|undefined, fallback:string):stri
}


export function getKeyValuePairs(values:string[]|undefined):{ logs:Message[], values:Record<string, string> } {
if (!values) {
return { logs: [], values: {} };
}

let logs:Message[] = [];
let result:Record<string, string> = {};
for (let entry of values) {
let [key, value] = entry.split("=");
if (!key || !value) {
logs.push({
level: Level.ERROR,
text: `Invalid key/value pair: "${entry}"`,
content: `The key and value must be separated by an equal sign. Ex. "key=value".`
});
continue;
}
result[key] = value;
}
return { logs: logs, values: result };
}


export function getVersion():string|undefined {
try {
let filepath = Path.join(Path.getRootDirectory(), "package.json");
Expand Down

0 comments on commit 8d69386

Please sign in to comment.