vs-deploy
Version:
Commands for deploying files of your workspace to a destination.
519 lines • 15.9 kB
JavaScript
;
/// <reference types="node" />
Object.defineProperty(exports, "__esModule", { value: true });
const deploy_globals = require("./globals");
const deploy_helpers = require("./helpers");
const deploy_workspace = require("./workspace");
const FS = require("fs");
const OS = require("os");
const Path = require("path");
/**
* A basic value.
*/
class ValueBase {
/**
* Initializes a new instance of that class.
*
* @param {deploy_contracts.ValueWithName} [item] The underlying item.
*/
constructor(item) {
if (!item) {
item = {
name: undefined,
type: undefined,
};
}
this._ITEM = item;
}
/**
* Gets the underlying item.
*/
get item() {
return this._ITEM;
}
/**
* Gets the list of "other" values.
*/
get otherValues() {
let result = [];
let ovp = this.otherValueProvider;
if (ovp) {
try {
result = result.concat(deploy_helpers.asArray(ovp()));
}
catch (e) {
//TODO: log
}
}
return result.filter(x => x);
}
/** @inheritdoc */
get name() {
return this.item.name;
}
}
exports.ValueBase = ValueBase;
/**
* A value generated by (JavaScript) code.
*/
class CodeValue extends ValueBase {
/** @inheritdoc */
constructor(value) {
super(value);
}
/** @inheritdoc */
get code() {
return deploy_helpers.toStringSafe(this.item.code);
}
/** @inheritdoc */
get item() {
return super.item;
}
/** @inheritdoc */
get value() {
let $cwd = process.cwd();
let $homeDir = OS.homedir();
let $globalState = globalScriptValueState;
let $me = this;
let $others = {};
let $require = function (id) {
return require(deploy_helpers.toStringSafe(id));
};
let $workspaceRoot = deploy_workspace.getRootPath();
// define properties for $others
this.otherValues.forEach(ov => {
try {
let propertyName = deploy_helpers.toStringSafe(ov.name);
if ('' === propertyName) {
return;
}
Object.defineProperty($others, propertyName, {
enumerable: true,
configurable: true,
get: () => {
return ov.value;
}
});
}
catch (e) {
//TODO: log
}
});
return eval(this.code);
}
}
exports.CodeValue = CodeValue;
/**
* A value that accesses an environment variable.
*/
class EnvValue extends ValueBase {
/** @inheritdoc */
constructor(value) {
super(value);
}
/** @inheritdoc */
get alias() {
return this.item.alias;
}
/** @inheritdoc */
get name() {
return deploy_helpers.isEmptyString(this.alias) ? this.realName
: this.alias;
}
/** @inheritdoc */
get item() {
return super.item;
}
/** @inheritdoc */
get value() {
let value;
let myName = deploy_helpers.toStringSafe(this.realName).trim();
for (let p in process.env) {
if (deploy_helpers.toStringSafe(p).trim() === myName) {
value = process.env[p]; // found
break;
}
}
return value;
}
/**
* Gets the "real" name.
*/
get realName() {
return super.name;
}
}
exports.EnvValue = EnvValue;
/**
* A value from a file.
*/
class FileValue extends ValueBase {
/** @inheritdoc */
constructor(value) {
super(value);
}
/** @inheritdoc */
get item() {
return super.item;
}
/** @inheritdoc */
get value() {
let file = deploy_helpers.toStringSafe(this.item.file);
file = replaceWithValues(this.otherValues, file);
if (!Path.isAbsolute(file)) {
file = Path.join(deploy_workspace.getRootPath(), file);
}
file = Path.resolve(file);
let content = FS.readFileSync(file);
if (content) {
if (deploy_helpers.toBooleanSafe(this.item.asBinary)) {
return content;
}
else {
// as string
let enc = deploy_helpers.normalizeString(this.item.encoding);
if ('' === enc) {
enc = 'utf8';
}
let str = content.toString(enc);
if (deploy_helpers.toBooleanSafe(this.item.usePlaceholders)) {
str = replaceWithValues(this.otherValues, str);
}
return str;
}
}
return content;
}
}
exports.FileValue = FileValue;
/**
* A value provided by a script.
*/
class ScriptValue extends ValueBase {
/** @inheritdoc */
constructor(value, cfg) {
super(value);
this._config = cfg;
}
/**
* Gets the underlying configuration.
*/
get config() {
return this._config || {};
}
/** @inheritdoc */
get item() {
return super.item;
}
/** @inheritdoc */
get value() {
let me = this;
let result;
let script = deploy_helpers.toStringSafe(me.item.script);
script = replaceWithValues(me.otherValues, script);
if (!deploy_helpers.isEmptyString(script)) {
if (!Path.isAbsolute(script)) {
script = Path.join(deploy_workspace.getRootPath(), script);
}
script = Path.resolve(script);
if (FS.existsSync(script)) {
let scriptModule = deploy_helpers.loadModule(script);
if (scriptModule) {
if (scriptModule.getValue) {
let args = {
emitGlobal: function () {
return deploy_globals.EVENTS.emit
.apply(deploy_globals.EVENTS, arguments);
},
globals: deploy_helpers.cloneObject(me.config.globals),
globalState: undefined,
name: undefined,
options: deploy_helpers.cloneObject(me.item.options),
others: undefined,
replaceWithValues: function (val) {
return replaceWithValues(me.otherValues, val);
},
require: (id) => {
return require(deploy_helpers.toStringSafe(id));
},
state: undefined,
};
let others = {};
me.otherValues.forEach(ov => {
try {
let propertyName = deploy_helpers.toStringSafe(ov.name);
if ('' === propertyName) {
return;
}
Object.defineProperty(others, propertyName, {
enumerable: true,
configurable: true,
get: () => {
return ov.value;
}
});
}
catch (e) {
//TODO: log
}
});
// args.globalState
Object.defineProperty(args, 'globalState', {
enumerable: true,
get: () => {
return globalScriptValueState;
},
});
// args.name
Object.defineProperty(args, 'name', {
enumerable: true,
get: () => {
return me.name;
},
});
// args.others
Object.defineProperty(args, 'others', {
enumerable: true,
get: () => {
return others;
},
});
// args.state
Object.defineProperty(args, 'state', {
enumerable: true,
get: () => {
return scriptValueStates[script];
},
set: (newValue) => {
scriptValueStates[script] = newValue;
}
});
result = scriptModule.getValue(args);
}
}
}
}
return result;
}
}
exports.ScriptValue = ScriptValue;
/**
* A static value.
*/
class StaticValue extends ValueBase {
/** @inheritdoc */
constructor(value) {
super(value);
}
/** @inheritdoc */
get item() {
return super.item;
}
/** @inheritdoc */
get value() {
return this.item.value;
}
}
exports.StaticValue = StaticValue;
let additionalValues;
let globalScriptValueState;
let scriptValueStates;
/**
* Returns a list of "build-in" values.
*
* @return {ValueBase[]} The list of values.
*/
function getBuildInValues() {
let objs = [];
// ${cwd}
{
objs.push(new CodeValue({
name: 'cwd',
type: "code",
code: "process.cwd()",
}));
}
// ${EOL}
{
objs.push(new CodeValue({
name: 'EOL',
type: "code",
code: "require('os').EOL",
}));
}
// ${hostName}
{
objs.push(new CodeValue({
name: 'hostName',
type: "code",
code: "require('os').hostname()",
}));
}
// ${homeDir}
{
objs.push(new CodeValue({
name: 'homeDir',
type: "code",
code: "require('os').homedir()",
}));
}
// ${tempDir}
{
objs.push(new CodeValue({
name: 'tempDir',
type: "code",
code: "require('os').tmpdir()",
}));
}
// ${userName}
{
objs.push(new CodeValue({
name: 'userName',
type: "code",
code: "require('os').userInfo().username",
}));
}
// ${workspaceRoot}
{
objs.push(new CodeValue({
name: 'workspaceRoot',
type: "code",
code: "require('./workspace').getRootPath()",
}));
}
return objs;
}
exports.getBuildInValues = getBuildInValues;
/**
* Gets the current list of values.
*
* @return {ValueBase[]} The values.
*/
function getValues() {
let me = this;
let myName = me.name;
let values = deploy_helpers.asArray(me.config.values)
.filter(x => x);
// isFor
values = values.filter(v => {
let validHosts = deploy_helpers.asArray(v.isFor)
.map(x => deploy_helpers.normalizeString(x))
.filter(x => '' !== x);
if (validHosts.length < 1) {
return true;
}
return validHosts.indexOf(myName) > -1;
});
// platforms
values = deploy_helpers.filterPlatformItems(values);
let objs = toValueObjects(values, me.config);
objs = objs.concat(additionalValues || []);
objs = objs.concat(getBuildInValues());
return objs;
}
exports.getValues = getValues;
/**
* Reloads the list of additional values.
*/
function reloadAdditionalValues() {
let me = this;
let cfg = me.config;
let av = [];
if (cfg.env) {
if (deploy_helpers.toBooleanSafe(cfg.env.importVarsAsPlaceholders)) {
// automatically import environment variables as
// placeholders / values
for (let p in process.env) {
av.push(new EnvValue({
name: p,
type: 'env',
}));
}
}
}
additionalValues = av;
}
exports.reloadAdditionalValues = reloadAdditionalValues;
/**
* Handles a value as string and replaces placeholders.
*
* @param {deploy_contracts.ObjectWithNameAndValue|deploy_contracts.ObjectWithNameAndValue[]} values The "placeholders".
* @param {any} val The value to parse.
*
* @return {string} The parsed value.
*/
function replaceWithValues(values, val) {
let allValues = deploy_helpers.asArray(values).filter(x => x);
if (!deploy_helpers.isNullOrUndefined(val)) {
let str = deploy_helpers.toStringSafe(val);
allValues.forEach(v => {
let vn = deploy_helpers.normalizeString(v.name);
// ${VAR_NAME}
str = str.replace(/(\$)(\{)([^\}]*)(\})/gm, (match, varIdentifier, openBracket, varName, closedBracked) => {
let newValue = match;
if (deploy_helpers.normalizeString(varName) === vn) {
try {
newValue = deploy_helpers.toStringSafe(v.value);
}
catch (e) {
//TODO: log
}
}
return newValue;
});
});
return str;
}
return val;
}
exports.replaceWithValues = replaceWithValues;
/**
* Resets all code / script based state values and objects.
*/
function resetScriptStates() {
globalScriptValueState = {};
scriptValueStates = {};
}
exports.resetScriptStates = resetScriptStates;
/**
* Converts a list of value items to objects.
*
* @param {(deploy_contracts.ValueWithName|deploy_contracts.ValueWithName[])} items The item(s) to convert.
*
* @returns {ValueBase[]} The items as objects.
*/
function toValueObjects(items, cfg) {
let result = [];
deploy_helpers.asArray(items).filter(i => i).forEach((i, idx) => {
let newValue;
switch (deploy_helpers.normalizeString(i.type)) {
case '':
case 'static':
newValue = new StaticValue(i);
break;
case 'code':
newValue = new CodeValue(i);
break;
case 'env':
case 'environment':
newValue = new EnvValue(i);
break;
case 'file':
newValue = new FileValue(i);
break;
case 'script':
newValue = new ScriptValue(i, cfg);
break;
}
if (newValue) {
newValue.id = idx;
newValue.otherValueProvider = () => {
return result.filter(x => x.id !== newValue.id);
};
result.push(newValue);
}
});
return result;
}
exports.toValueObjects = toValueObjects;
//# sourceMappingURL=values.js.map