From 73d0b90d669a15ae93fa76edae2fa00ae90f276b Mon Sep 17 00:00:00 2001 From: qm3ster Date: Tue, 12 Mar 2019 18:27:08 +0200 Subject: [PATCH 1/3] refactor: consistent `process.env` usage --- lib/git-helpers.js | 10 ++++++---- lib/update-lockfile.js | 10 ++++++---- test/update.js | 16 +++++++++------- 3 files changed, 21 insertions(+), 15 deletions(-) diff --git a/lib/git-helpers.js b/lib/git-helpers.js index a2fb34b4..d4820ee6 100644 --- a/lib/git-helpers.js +++ b/lib/git-helpers.js @@ -4,16 +4,18 @@ const url = require('url') const exec = require('child_process').execSync const _ = require('lodash') -const DEBUG = process.env.GK_LOCK_DEBUG || false +const env = process.env + +const DEBUG = env.GK_LOCK_DEBUG || false function setupRemote (info) { let remote = `git@github.com:${info.repoSlug}` if (info.gitUrl) remote = info.gitUrl - if (process.env.GH_TOKEN) { + if (env.GH_TOKEN) { if (remote.slice(0, 5) !== 'https') remote = `https://github.com/${info.repoSlug}` const urlParsed = url.parse(remote) - urlParsed.auth = process.env.GH_TOKEN + urlParsed.auth = env.GH_TOKEN remote = url.format(urlParsed) } exec(`git remote add gk-origin ${remote} || git remote set-url gk-origin ${remote}`) @@ -44,7 +46,7 @@ module.exports = { }, hasLockfileCommit: function hasLockfileCommit (info) { setupRemote(info) - const defaultBranch = process.env.GK_LOCK_DEFAULT_BRANCH || 'master' + const defaultBranch = env.GK_LOCK_DEFAULT_BRANCH || 'master' // CI clones are often shallow clones. Let’s make sure we have enough to work with // https://stackoverflow.com/a/44036486/242298 // console.log(`git config --replace-all remote.origin.fetch +refs/heads/*:refs/remotes/origin/*`) diff --git a/lib/update-lockfile.js b/lib/update-lockfile.js index 052d94d7..7ff42c8c 100644 --- a/lib/update-lockfile.js +++ b/lib/update-lockfile.js @@ -4,6 +4,8 @@ const exec = require('child_process').execSync const _ = require('lodash') const semver = require('semver') +const env = process.env + const flags = { 'dependencies': ' -S', 'devDependencies': ' -D', @@ -22,7 +24,7 @@ module.exports.updateLockfile = function updateLockfile (dependency, options) { } else { if (options.yarn) { const flag = yarnFlags[dependency.type] - const envArgs = process.env.GK_LOCK_YARN_OPTS ? ` ${process.env.GK_LOCK_YARN_OPTS.trim()}` : '' + const envArgs = env.GK_LOCK_YARN_OPTS ? ` ${env.GK_LOCK_YARN_OPTS.trim()}` : '' const args = `${flag}${envArgs} '${dependency.name}@${dependency.range}'` exec(`yarn add${args}`) } @@ -53,9 +55,9 @@ module.exports.stageLockfile = function stageLockfile (options) { } module.exports.commitLockfiles = function commitLockfiles () { - const commitEmail = process.env.GK_LOCK_COMMIT_EMAIL ? process.env.GK_LOCK_COMMIT_EMAIL.trim() : 'support@greenkeeper.io' - const commitName = process.env.GK_LOCK_COMMIT_NAME ? process.env.GK_LOCK_COMMIT_NAME.trim() : 'greenkeeperio-bot' - const shouldAmend = !_.includes([undefined, `0`, 'false', 'null', 'undefined'], process.env.GK_LOCK_COMMIT_AMEND) + const commitEmail = env.GK_LOCK_COMMIT_EMAIL ? env.GK_LOCK_COMMIT_EMAIL.trim() : 'support@greenkeeper.io' + const commitName = env.GK_LOCK_COMMIT_NAME ? env.GK_LOCK_COMMIT_NAME.trim() : 'greenkeeperio-bot' + const shouldAmend = !_.includes([undefined, `0`, 'false', 'null', 'undefined'], env.GK_LOCK_COMMIT_AMEND) exec(`git config user.email "${commitEmail}"`) exec(`git config user.name "${commitName}"`) diff --git a/test/update.js b/test/update.js index 79cb721d..19e45936 100644 --- a/test/update.js +++ b/test/update.js @@ -8,9 +8,11 @@ const path = require('path') const update = require('../update') -process.env.JENKINS_URL = 'true' -process.env.BUILD_NUMBER = '1' -process.env.GIT_BRANCH = 'fooo/greenkeeper/my-dependency-1.0.0' +const env = process.env + +env.JENKINS_URL = 'true' +env.BUILD_NUMBER = '1' +env.GIT_BRANCH = 'fooo/greenkeeper/my-dependency-1.0.0' afterAll(() => { childProcess.execSync.restore() @@ -27,15 +29,15 @@ function testFixture (fixtureDirectory, noLog) { process.chdir(path.join(__dirname, fixtureDirectory)) let oldGhToken = false - if (process.env.GH_TOKEN) { - oldGhToken = process.env.GH_TOKEN - delete process.env.GH_TOKEN + if (env.GH_TOKEN) { + oldGhToken = env.GH_TOKEN + delete env.GH_TOKEN } update() if (oldGhToken) { - process.env.GH_TOKEN = oldGhToken + env.GH_TOKEN = oldGhToken } expect(exec.getCalls().map(call => call.args[0])).toMatchSnapshot() From d5f86550716fc6cc00cfdc3cf77eb3638ac49b20 Mon Sep 17 00:00:00 2001 From: qm3ster Date: Tue, 12 Mar 2019 18:31:57 +0200 Subject: [PATCH 2/3] refactor: no-var --- lib/git-helpers.js | 6 +++--- lib/update-lockfile.js | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/git-helpers.js b/lib/git-helpers.js index d4820ee6..a2a82eaf 100644 --- a/lib/git-helpers.js +++ b/lib/git-helpers.js @@ -38,8 +38,8 @@ module.exports = { return exec('git config remote.origin.url').toString().trim() }, getRepoSlug: function getRepoSlug (githubUrl) { - var ghRegex = /\S+[:|/](\w+(?:[-]\w+)*)\/(\w+(?:[-]\w+)*)/g - var parsed = ghRegex.exec(githubUrl) + const ghRegex = /\S+[:|/](\w+(?:[-]\w+)*)\/(\w+(?:[-]\w+)*)/g + const parsed = ghRegex.exec(githubUrl) return ( `${parsed[1]}/${parsed[2]}` ) @@ -92,7 +92,7 @@ module.exports = { return true }, getBranch: function getBranch () { - var branch = exec('git show -s --pretty=%d HEAD') + const branch = exec('git show -s --pretty=%d HEAD') .toString() .match(/\(?([^)]+)\)?/)[1] .split(', ') diff --git a/lib/update-lockfile.js b/lib/update-lockfile.js index 7ff42c8c..b075617e 100644 --- a/lib/update-lockfile.js +++ b/lib/update-lockfile.js @@ -33,7 +33,7 @@ module.exports.updateLockfile = function updateLockfile (dependency, options) { const flag = flags[dependency.type] const prefix = dependency.prefix ? ` --save-prefix="${dependency.prefix}"` : '' const args = `${flag}${prefix} ${dependency.name}@${dependency.version}` - var npmBin = 'npm' + let npmBin = 'npm' try { exec('npm5 -v') npmBin = 'npm5' From ec6f5591411cc6a55de28d4cedd5a11155a7ae7c Mon Sep 17 00:00:00 2001 From: qm3ster Date: Tue, 12 Mar 2019 18:33:32 +0200 Subject: [PATCH 3/3] refactor: eliminate lodash All replacements compatible with node v6, hence no `Object.entries()` --- ci-services/circleci.js | 3 +-- ci-services/index.js | 11 ++++----- ci-services/semaphoreci.js | 4 +--- lib/config.js | 9 ++++--- lib/git-helpers.js | 7 +++--- lib/update-lockfile.js | 3 +-- package-lock.json | 48 +++++++++++++++++++++++++++----------- package.json | 1 - 8 files changed, 50 insertions(+), 36 deletions(-) diff --git a/ci-services/circleci.js b/ci-services/circleci.js index 97503d00..64cb6a54 100644 --- a/ci-services/circleci.js +++ b/ci-services/circleci.js @@ -1,6 +1,5 @@ 'use strict' -const _ = require('lodash') const gitHelpers = require('../lib/git-helpers') const env = process.env @@ -18,7 +17,7 @@ module.exports = { repoSlug: `${env.CIRCLE_PROJECT_USERNAME}/${env.CIRCLE_PROJECT_REPONAME}`, branchName: env.CIRCLE_BRANCH, // update, CIRCLE_PREVIOUS_BUILD_NUM is only null on the first job of the first workflow on the branch - correctBuild: _.isEmpty(env.CI_PULL_REQUEST) && !env.CIRCLE_PREVIOUS_BUILD_NUM, + correctBuild: !env.CI_PULL_REQUEST && !env.CIRCLE_PREVIOUS_BUILD_NUM, // upload when last commit is lockfile update uploadBuild: env.CIRCLE_NODE_INDEX === `${env.BUILD_LEADER_ID || 0}` && isLockfileUpdate() } diff --git a/ci-services/index.js b/ci-services/index.js index 47cf2f15..884ba5c2 100644 --- a/ci-services/index.js +++ b/ci-services/index.js @@ -1,13 +1,12 @@ 'use strict' -const _ = require('lodash') - const tests = require('./tests') module.exports = () => { - const service = _.findKey(tests, test => test()) - - if (!service) throw new Error('Could not detect a CI Service.') + for (const service in tests) { + const test = tests[service] + if (test()) return require(`./${service}`) + } - return require(`./${service}`) + throw new Error('Could not detect a CI Service.') } diff --git a/ci-services/semaphoreci.js b/ci-services/semaphoreci.js index 8442f08c..35bab68d 100644 --- a/ci-services/semaphoreci.js +++ b/ci-services/semaphoreci.js @@ -1,12 +1,10 @@ 'use strict' -const _ = require('lodash') - const env = process.env module.exports = { repoSlug: env.SEMAPHORE_REPO_SLUG, branchName: env.BRANCH_NAME, - correctBuild: _.isEmpty(env.PULL_REQUEST_NUMBER), + correctBuild: !env.PULL_REQUEST_NUMBER, uploadBuild: env.SEMAPHORE_CURRENT_JOB === '1' } diff --git a/lib/config.js b/lib/config.js index 8e3522f7..1fff31d9 100644 --- a/lib/config.js +++ b/lib/config.js @@ -1,10 +1,9 @@ 'use strict' -const _ = require('lodash') const relative = require('require-relative') -const pkg = relative('./package.json') +const { greenkeeper = {} } = relative('./package.json') -module.exports = _.defaults(pkg.greenkeeper, { - branchPrefix: 'greenkeeper/' -}) +if (typeof greenkeeper.branchPrefix === 'undefined') greenkeeper.branchPrefix = 'greenkeeper/' + +module.exports = greenkeeper diff --git a/lib/git-helpers.js b/lib/git-helpers.js index a2a82eaf..469eb651 100644 --- a/lib/git-helpers.js +++ b/lib/git-helpers.js @@ -3,7 +3,6 @@ const url = require('url') const exec = require('child_process').execSync -const _ = require('lodash') const env = process.env const DEBUG = env.GK_LOCK_DEBUG || false @@ -25,11 +24,11 @@ module.exports = { getNumberOfCommitsOnBranch: function getNumberOfCommitsOnBranch (branch) { const refArgument = `$(git for-each-ref '--format=%(refname)' refs/ | grep /${branch} | head -1)` const notArgument = `$(git for-each-ref '--format=%(refname)' refs/ | grep -v /${branch})` - return _.toNumber( + return parseInt( exec( `git log ${refArgument} --oneline --not ${notArgument} | wc -l` - ).toString() - ) + ).toString(), + 10) }, getLastCommitMessage: function getLastCommitMessage () { return exec('git log --format=%B -1').toString() diff --git a/lib/update-lockfile.js b/lib/update-lockfile.js index b075617e..f8c64a34 100644 --- a/lib/update-lockfile.js +++ b/lib/update-lockfile.js @@ -1,7 +1,6 @@ 'use strict' const exec = require('child_process').execSync -const _ = require('lodash') const semver = require('semver') const env = process.env @@ -57,7 +56,7 @@ module.exports.stageLockfile = function stageLockfile (options) { module.exports.commitLockfiles = function commitLockfiles () { const commitEmail = env.GK_LOCK_COMMIT_EMAIL ? env.GK_LOCK_COMMIT_EMAIL.trim() : 'support@greenkeeper.io' const commitName = env.GK_LOCK_COMMIT_NAME ? env.GK_LOCK_COMMIT_NAME.trim() : 'greenkeeperio-bot' - const shouldAmend = !_.includes([undefined, `0`, 'false', 'null', 'undefined'], env.GK_LOCK_COMMIT_AMEND) + const shouldAmend = ![undefined, `0`, 'false', 'null', 'undefined'].includes(env.GK_LOCK_COMMIT_AMEND) exec(`git config user.email "${commitEmail}"`) exec(`git config user.name "${commitName}"`) diff --git a/package-lock.json b/package-lock.json index 9e83a610..7f3d9718 100644 --- a/package-lock.json +++ b/package-lock.json @@ -392,6 +392,7 @@ "resolved": "https://registry.npmjs.org/align-text/-/align-text-0.1.4.tgz", "integrity": "sha1-DNkKVhCT810KmSVsIrcGlDP60Rc=", "dev": true, + "optional": true, "requires": { "kind-of": "^3.0.2", "longest": "^1.0.1", @@ -3014,7 +3015,8 @@ "ansi-regex": { "version": "2.1.1", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "aproba": { "version": "1.2.0", @@ -3035,12 +3037,14 @@ "balanced-match": { "version": "1.0.0", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "brace-expansion": { "version": "1.1.11", "bundled": true, "dev": true, + "optional": true, "requires": { "balanced-match": "^1.0.0", "concat-map": "0.0.1" @@ -3055,17 +3059,20 @@ "code-point-at": { "version": "1.1.0", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "concat-map": { "version": "0.0.1", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "console-control-strings": { "version": "1.1.0", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "core-util-is": { "version": "1.0.2", @@ -3182,7 +3189,8 @@ "inherits": { "version": "2.0.3", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "ini": { "version": "1.3.5", @@ -3194,6 +3202,7 @@ "version": "1.0.0", "bundled": true, "dev": true, + "optional": true, "requires": { "number-is-nan": "^1.0.0" } @@ -3208,6 +3217,7 @@ "version": "3.0.4", "bundled": true, "dev": true, + "optional": true, "requires": { "brace-expansion": "^1.1.7" } @@ -3215,12 +3225,14 @@ "minimist": { "version": "0.0.8", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "minipass": { "version": "2.2.4", "bundled": true, "dev": true, + "optional": true, "requires": { "safe-buffer": "^5.1.1", "yallist": "^3.0.0" @@ -3239,6 +3251,7 @@ "version": "0.5.1", "bundled": true, "dev": true, + "optional": true, "requires": { "minimist": "0.0.8" } @@ -3319,7 +3332,8 @@ "number-is-nan": { "version": "1.0.1", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "object-assign": { "version": "4.1.1", @@ -3331,6 +3345,7 @@ "version": "1.4.0", "bundled": true, "dev": true, + "optional": true, "requires": { "wrappy": "1" } @@ -3416,7 +3431,8 @@ "safe-buffer": { "version": "5.1.1", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "safer-buffer": { "version": "2.1.2", @@ -3452,6 +3468,7 @@ "version": "1.0.2", "bundled": true, "dev": true, + "optional": true, "requires": { "code-point-at": "^1.0.0", "is-fullwidth-code-point": "^1.0.0", @@ -3471,6 +3488,7 @@ "version": "3.0.1", "bundled": true, "dev": true, + "optional": true, "requires": { "ansi-regex": "^2.0.0" } @@ -3514,12 +3532,14 @@ "wrappy": { "version": "1.0.2", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "yallist": { "version": "3.0.2", "bundled": true, - "dev": true + "dev": true, + "optional": true } } }, @@ -5748,7 +5768,8 @@ "lodash": { "version": "4.17.10", "resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.10.tgz", - "integrity": "sha512-UejweD1pDoXu+AD825lWwp4ZGtSwgnpZxb3JDViD7StjQz+Nb/6l093lx4OQ0foGWNRoc19mWy7BzL+UAK2iVg==" + "integrity": "sha512-UejweD1pDoXu+AD825lWwp4ZGtSwgnpZxb3JDViD7StjQz+Nb/6l093lx4OQ0foGWNRoc19mWy7BzL+UAK2iVg==", + "dev": true }, "lodash.assign": { "version": "4.2.0", @@ -5784,7 +5805,8 @@ "version": "1.0.1", "resolved": "https://registry.npmjs.org/longest/-/longest-1.0.1.tgz", "integrity": "sha1-MKCy2jj3N3DoKUoNIuZiXtd9AJc=", - "dev": true + "dev": true, + "optional": true }, "loose-envify": { "version": "1.3.1", diff --git a/package.json b/package.json index 883001e1..5bb32464 100644 --- a/package.json +++ b/package.json @@ -15,7 +15,6 @@ "dependencies": { "fast-glob": "^2.2.0", "greenkeeper-monorepo-definitions": "^1.0.0", - "lodash": "^4.17.10", "require-relative": "^0.8.7", "semver": "^5.3.0" },