diff --git a/README.md b/README.md index cbfb6e9..6fc0c09 100644 --- a/README.md +++ b/README.md @@ -100,7 +100,8 @@ sherpa build [options] #### Options: - `-i`, `--input ` path to SherpaJS server, defaults to current directory - `-o`, `--output ` path to server output, defaults to input directory - - `-b`, `--bundler ` platform bundler ("**Vercel**", "*local**", *default: "local"*) + - `-b`, `--bundler ` 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 diff --git a/src/cli/index.ts b/src/cli/index.ts index 930308f..5f6d12e 100644 --- a/src/cli/index.ts +++ b/src/cli/index.ts @@ -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(); @@ -28,12 +29,19 @@ CLI.command("build") .option("-i, --input ", "path to SherpaJS server, defaults to current directory") .option("-o, --output ", "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 ", "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, @@ -46,7 +54,8 @@ CLI.command("build") } }, environment: { - files: getEnvironmentFiles(input) + files: getEnvironmentFiles(input), + variables: variables.values } } }); diff --git a/src/cli/utilities.ts b/src/cli/utilities.ts index e512184..38c53dc 100644 --- a/src/cli/utilities.ts +++ b/src/cli/utilities.ts @@ -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[] { @@ -33,6 +34,29 @@ export function getAbsolutePath(filepath:string|undefined, fallback:string):stri } +export function getKeyValuePairs(values:string[]|undefined):{ logs:Message[], values:Record } { + if (!values) { + return { logs: [], values: {} }; + } + + let logs:Message[] = []; + let result:Record = {}; + 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");