diff --git a/TODO.md b/TODO.md index b9a996e..2316e18 100644 --- a/TODO.md +++ b/TODO.md @@ -34,6 +34,10 @@ - Setup generator-star-spacing when babel-eslint issue 316 will be fixed - https://github.com/babel/babel-eslint/issues/316 +## local cache + +- Take poosh environment into account + ## internal - Const nazification? diff --git a/packages/poosh-cli/src/helpers/argv.js b/packages/poosh-cli/src/helpers/argv.js index 2fea9dd..d8ad7aa 100644 --- a/packages/poosh-cli/src/helpers/argv.js +++ b/packages/poosh-cli/src/helpers/argv.js @@ -71,3 +71,7 @@ export function parseArgs(args: Array): Object { return { subcommand }; } + +export function parseEnv(val: string): String { + return val; +} diff --git a/packages/poosh-cli/src/main.js b/packages/poosh-cli/src/main.js index 8215623..c1f7424 100644 --- a/packages/poosh-cli/src/main.js +++ b/packages/poosh-cli/src/main.js @@ -20,6 +20,10 @@ function initCommander() { "force mode, can be remote, cache or both (default)", argv.parseForce); + commander.option("-e, --env [string]", + "poosh environment key", + argv.parseEnv); + commander.option("-v, --verbose", "get more detailed output on every files", (v, total) => total + 1, @@ -57,7 +61,7 @@ export default async function main() { .addOptions(commander.readonly ? { readonly: commander.readonly } : null) .addOptions(commander.force ? { force: commander.force } : null); - let options = optionManager.getNormalized(); + let options = optionManager.getNormalized(commander.env); let poosh = new Poosh(options); let logger; diff --git a/packages/poosh-core/src/OptionManager.js b/packages/poosh-core/src/OptionManager.js index e445b8e..eebc45f 100644 --- a/packages/poosh-core/src/OptionManager.js +++ b/packages/poosh-core/src/OptionManager.js @@ -10,6 +10,11 @@ import PluginCollection from "./helpers/options/PluginCollection"; const CONF_FILENAME = ".poosh.json5"; +function getPooshEnvironment() { + // eslint-disable-next-line no-process-env + return process.env.POOSH_ENV || process.env.NODE_ENV; +} + /** * Normalize readonly and force options objects. * @@ -124,29 +129,42 @@ export default class OptionManager { /** * Get normalized options, ready to use for Poosh constructor. + * @param {string} env Environment * @returns Normalized and validated options. * @throws Error */ - getNormalized(): Object { + getNormalized(env): Object { let plugins = this._plugins.get(); - // Clone merged raw options + // 1. Clone merged raw options let options = Object.assign({}, this._options); - // Make sure MERGED raw options are valid + // 2. Environment + if (options.env) { + env = env || getPooshEnvironment(); + if (env && options.env[env]) { + // 2a. Merge environment specific options + Object.assign(options, options.env[env]); + } + + // 2b. Cleanup + Reflect.deleteProperty(options, "env"); + } + + // 3. Make sure MERGED raw options are valid let error = OptionValidator.validateSync(options, plugins); if (error) { // TODO: Throw a custom error (eg OptionValidationError) throw error; } - // Default values + // 4. Default values if (!options.concurrency) { options.concurrency = 3; } - // Normalization + // 5. Normalization options.plugins = plugins; options.readonly = normalizeCacheRemote(options.readonly); options.force = normalizeCacheRemote(options.force);