Skip to content

Commit

Permalink
Merge branch 'release/5.0.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
jacob-meacham committed Oct 31, 2020
2 parents 62cab54 + 45e679c commit 98a71f3
Show file tree
Hide file tree
Showing 9 changed files with 9,869 additions and 5,050 deletions.
23 changes: 0 additions & 23 deletions .babelrc

This file was deleted.

2 changes: 1 addition & 1 deletion .travis.yml
@@ -1,5 +1,5 @@
language: node_js
node_js:
- '7'
- '12'
script: npm run build
after_success: npm run ci:coverage
2 changes: 2 additions & 0 deletions README.md
Expand Up @@ -66,6 +66,8 @@ For more information on annotated and lightweight tags go to the [git documentat
The tags (`${git:tags}`) is used to get info about which git tags (separated by ::) are pointing to current commit and if none it will show commit ID as fallback.

# Version History
* 5.0.0
- Rely on a more modern version of Node, which allows removal of runtime dependencies
* 4.1.0
- Fix sporadic failure with git write-tree (Thanks to @navrkald and @james-hu)
* 4.0.0
Expand Down
256 changes: 133 additions & 123 deletions lib/index.js
@@ -1,50 +1,35 @@
'use strict';
"use strict";

Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;

var _promise = require('babel-runtime/core-js/promise');
var _child_process = _interopRequireDefault(require("child_process"));

var _promise2 = _interopRequireDefault(_promise);
var _path = _interopRequireDefault(require("path"));

var _asyncToGenerator2 = require('babel-runtime/helpers/asyncToGenerator');
var _os = _interopRequireDefault(require("os"));

var _asyncToGenerator3 = _interopRequireDefault(_asyncToGenerator2);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

let _exec = (() => {
var _ref = (0, _asyncToGenerator3.default)(function* (cmd, options = { timeout: 1000 }) {
return new _promise2.default(function (resolve, reject) {
_child_process2.default.exec(cmd, options, function (err, stdout) {
if (err) {
reject(err);
} else {
resolve(stdout.trim());
}
});
// TODO: Consider using nodegit instead
const GIT_PREFIX = 'git';

async function _exec(cmd, options = {
timeout: 1000
}) {
return new Promise((resolve, reject) => {
_child_process.default.exec(cmd, options, (err, stdout) => {
if (err) {
reject(err);
} else {
resolve(stdout.trim());
}
});
});
}

return function _exec(_x) {
return _ref.apply(this, arguments);
};
})();

var _child_process = require('child_process');

var _child_process2 = _interopRequireDefault(_child_process);

var _path = require('path');

var _path2 = _interopRequireDefault(_path);

var _os = require('os');

var _os2 = _interopRequireDefault(_os);

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

const GIT_PREFIX = 'git'; // TODO: Consider using nodegit instead
class ServerlessGitVariables {
constructor(serverless, options) {
this.serverless = serverless;
Expand All @@ -59,118 +44,142 @@ class ServerlessGitVariables {

return delegate(variableString);
};

this.hooks = {
'after:package:initialize': this.exportGitVariables.bind(this),
'before:offline:start': this.exportGitVariables.bind(this),
'before:offline:start:init': this.exportGitVariables.bind(this)
};
}

_getValue(variable) {
var _this = this;

return (0, _asyncToGenerator3.default)(function* () {
if (_this.resolvedValues[variable]) {
return _promise2.default.resolve(_this.resolvedValues[variable]);
}
async _getValue(variable) {
if (this.resolvedValues[variable]) {
return Promise.resolve(this.resolvedValues[variable]);
}

return _this._getValueFromGit(variable);
})();
return this._getValueFromGit(variable);
}

_getValueFromGit(variable) {
var _this2 = this;
async _getValueFromGit(variable) {
let value = null;

return (0, _asyncToGenerator3.default)(function* () {
let value = null;
switch (variable) {
case 'describe':
value = yield _exec('git describe --always');
break;
case 'describeLight':
value = yield _exec('git describe --always --tags');
break;
case 'sha1':
value = yield _exec('git rev-parse --short HEAD');
break;
case 'commit':
value = yield _exec('git rev-parse HEAD');
break;
case 'branch':
value = yield _exec('git rev-parse --abbrev-ref HEAD');
break;
case 'message':
value = yield _exec('git log -1 --pretty=%B');
break;
case 'user':
value = yield _exec('git config user.name');
break;
case 'email':
value = yield _exec('git config user.email');
break;
case 'isDirty':
const changes = yield _exec(`git diff --stat`);
switch (variable) {
case 'describe':
value = await _exec('git describe --always');
break;

case 'describeLight':
value = await _exec('git describe --always --tags');
break;

case 'sha1':
value = await _exec('git rev-parse --short HEAD');
break;

case 'commit':
value = await _exec('git rev-parse HEAD');
break;

case 'branch':
value = await _exec('git rev-parse --abbrev-ref HEAD');
break;

case 'message':
value = await _exec('git log -1 --pretty=%B');
break;

case 'user':
value = await _exec('git config user.name');
break;

case 'email':
value = await _exec('git config user.email');
break;

case 'isDirty':
{
const changes = await _exec('git diff --stat');
value = `${changes.length > 0}`;
break;
case 'repository':
const pathName = yield _exec('git rev-parse --show-toplevel');
value = _path2.default.basename(pathName);
break;
case 'tags':
value = yield _exec('git tag --points-at HEAD');
value = value.split(_os2.default.EOL).join('::');
if (value === '') {
value = yield _exec('git rev-parse --short HEAD');
}
break;
default:
throw new Error(`Git variable ${variable} is unknown. Candidates are 'describe', 'describeLight', 'sha1', 'commit', 'branch', 'message', 'user', 'email', 'isDirty', 'repository', 'tags'`);
}

// TODO: Figure out why if I don't log, the deasync promise
// never resolves. Catching it in the debugger or logging
// causes it to work fine.
process.stdout.write('');
}

// Cache before returning
_this2.resolvedValues[variable] = value;
return value;
})();
}
case 'repository':
{
const pathName = await _exec('git rev-parse --show-toplevel');
value = _path.default.basename(pathName);
break;
}

exportGitVariables() {
var _this3 = this;
case 'tags':
value = await _exec('git tag --points-at HEAD');
value = value.split(_os.default.EOL).join('::');

return (0, _asyncToGenerator3.default)(function* () {
const exportGitVariables = _this3.serverless.service.custom && _this3.serverless.service.custom.exportGitVariables;
if (value === '') {
value = await _exec('git rev-parse --short HEAD');
}

let envWhitelist = _this3.serverless.service.custom && _this3.serverless.service.custom.gitVariablesEnvWhitelist;
let tagsWhitelist = _this3.serverless.service.custom && _this3.serverless.service.custom.gitVariablesTagsWhitelist;
break;

if (exportGitVariables === false) {
return;
}
default:
throw new Error(`Git variable ${variable} is unknown. Candidates are 'describe', 'describeLight', 'sha1', 'commit', 'branch', 'message', 'user', 'email', 'isDirty', 'repository', 'tags'`);
} // TODO: Figure out why if I don't log, the deasync promise
// never resolves. Catching it in the debugger or logging
// causes it to work fine.

const exportList = [{ value: 'sha1', variableName: 'GIT_COMMIT_SHORT' }, { value: 'commit', variableName: 'GIT_COMMIT_LONG' }, { value: 'branch', variableName: 'GIT_BRANCH' }, { value: 'isDirty', variableName: 'GIT_IS_DIRTY' }, { value: 'repository', variableName: 'GIT_REPOSITORY' }, { value: 'tags', variableName: 'GIT_TAGS' }];

for (const functionName of _this3.serverless.service.getAllFunctions()) {
const func = _this3.serverless.service.getFunction(functionName);
process.stdout.write(''); // Cache before returning

for (const _ref2 of exportList) {
const { value, variableName } = _ref2;
this.resolvedValues[variable] = value;
return value;
}

const setOnEnv = !envWhitelist || envWhitelist.includes(variableName);
const setOnTags = !tagsWhitelist || tagsWhitelist.includes(variableName);
async exportGitVariables() {
const exportGitVariables = this.serverless.service.custom && this.serverless.service.custom.exportGitVariables;
const envWhitelist = this.serverless.service.custom && this.serverless.service.custom.gitVariablesEnvWhitelist;
const tagsWhitelist = this.serverless.service.custom && this.serverless.service.custom.gitVariablesTagsWhitelist;

if (!setOnEnv && !setOnTags) {
continue;
}
if (exportGitVariables === false) {
return;
}

const gitValue = yield _this3._getValue(value);
_this3.exportGitVariable(func, variableName, gitValue, setOnEnv, setOnTags);
const exportList = [{
value: 'sha1',
variableName: 'GIT_COMMIT_SHORT'
}, {
value: 'commit',
variableName: 'GIT_COMMIT_LONG'
}, {
value: 'branch',
variableName: 'GIT_BRANCH'
}, {
value: 'isDirty',
variableName: 'GIT_IS_DIRTY'
}, {
value: 'repository',
variableName: 'GIT_REPOSITORY'
}, {
value: 'tags',
variableName: 'GIT_TAGS'
}];

for (const functionName of this.serverless.service.getAllFunctions()) {
const func = this.serverless.service.getFunction(functionName);

for (const {
value,
variableName
} of exportList) {
const setOnEnv = !envWhitelist || envWhitelist.includes(variableName);
const setOnTags = !tagsWhitelist || tagsWhitelist.includes(variableName);

if (!setOnEnv && !setOnTags) {
continue;
}

const gitValue = await this._getValue(value);
this.exportGitVariable(func, variableName, gitValue, setOnEnv, setOnTags);
}
})();
}
}

exportGitVariable(func, variableName, gitValue, setOnEnv = true, setOnTags = true) {
Expand All @@ -194,6 +203,7 @@ class ServerlessGitVariables {
}
}
}

}
exports.default = ServerlessGitVariables;
module.exports = exports['default'];

exports.default = ServerlessGitVariables;

0 comments on commit 98a71f3

Please sign in to comment.