This repository has been archived by the owner on Mar 4, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 26
Implement 'builder envs' action. #44
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
"use strict"; | ||
|
||
var fs = require("fs"); | ||
var path = require("path"); | ||
var _ = require("lodash"); | ||
var chalk = require("chalk"); | ||
|
@@ -43,7 +44,7 @@ var Task = module.exports = function (opts) { | |
} | ||
}; | ||
|
||
Task.prototype.ACTIONS = ["help", "run", "concurrent", "install"]; | ||
Task.prototype.ACTIONS = ["help", "run", "concurrent", "envs"]; | ||
|
||
Task.prototype.toString = function () { | ||
var cmd = this._command; | ||
|
@@ -142,7 +143,7 @@ Task.prototype.run = function (callback) { | |
|
||
var env = this._env.env; // Raw environment object. | ||
var task = this.getCommand(this._command); | ||
var flags = args.concurrent(this.argv); | ||
var flags = args.run(this.argv); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. BUGFIX! |
||
var opts = _.extend({}, flags); | ||
|
||
log.info(this._action, this._command + chalk.gray(" - " + task)); | ||
|
@@ -170,6 +171,66 @@ Task.prototype.concurrent = function (callback) { | |
runner.concurrent(tasks, { env: env }, opts, callback); | ||
}; | ||
|
||
Task.prototype._parseJson = function (objStr) { | ||
try { | ||
return JSON.parse(objStr); | ||
} catch (err) { | ||
log.error(this._action + ":json-obj", "Failed to load JSON object: " + objStr); | ||
throw err; | ||
} | ||
}; | ||
|
||
Task.prototype._parseJsonFile = function (filePath) { | ||
try { | ||
return JSON.parse(fs.readFileSync(filePath)); | ||
} catch (err) { | ||
log.error(this._action + ":json-file", "Failed to load JSON file: " + filePath); | ||
throw err; | ||
} | ||
}; | ||
|
||
/** | ||
* Run multiple environments. | ||
* | ||
* @param {Function} callback Callback function `(err)` | ||
* @returns {void} | ||
*/ | ||
Task.prototype.envs = function (callback) { | ||
/*eslint max-statements: [2, 20]*/ | ||
// Core setup. | ||
var env = this._env.env; | ||
var task = this.getCommand(this._command); | ||
var flags = args.envs(this.argv); | ||
var opts = _.extend({}, flags); | ||
|
||
// Get task environment array. | ||
var envsStr = this._commands[1]; | ||
if (envsStr) { | ||
// Try string on command line first: | ||
// $ builder envs <task> '[{ "FOO": "VAL1" }, { "FOO": "VAL2" }]' | ||
opts._envs = this._parseJson(envsStr); | ||
} else if (opts.envsPath) { | ||
// Try JSON file path next: | ||
// $ builder envs <task> --envs-path=my-environment-vars.json | ||
opts._envs = this._parseJsonFile(opts.envsPath); | ||
} | ||
|
||
// Validation | ||
var err; | ||
if (_.isEmpty(opts._envs)) { | ||
err = new Error("Empty/null JSON environments array."); | ||
} else if (!_.isArray(opts._envs)) { | ||
err = new Error("Non-array JSON environments object: " + JSON.stringify(opts._envs)); | ||
} | ||
|
||
if (err) { | ||
log.error("envs:json-error", err); | ||
return callback(err); | ||
} | ||
|
||
runner.envs(task, { env: env }, opts, callback); | ||
}; | ||
|
||
/** | ||
* Execute task or tasks. | ||
* | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This includes the clean up of removing
install
(the underlying Task was remove previously).