From 4b76d64679c3d3c0767e395853343793eff08293 Mon Sep 17 00:00:00 2001 From: Rylan Collins Date: Thu, 23 Jul 2020 16:03:05 -0700 Subject: [PATCH 1/2] feat(publish): add gemPublish config option --- README.md | 4 +++- src/publish.js | 25 +++++++++++++++---------- 2 files changed, 18 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index f3f18ca..208e615 100644 --- a/README.md +++ b/README.md @@ -59,4 +59,6 @@ end | Options | Description | Default | |--------------|---------------------------------------------------------------------------------------------------------------------|----------------------------------------------------------------------------------------------------------------------------------| | `gemHost` | The gem server to push the gem to. | `'https//rubygems.org'` | -| `updateGemfileLock` | Whether to update the version of the gem to publish in the `Gemfile.lock`. This is useful if you are using the [`@semantic-release/git`](https://github.com/semantic-release/git) plugin to keep the version up to date in your git repo. When set to `true` the plugin will run `bundle install` to update the version. If another command is desired, it can be set by passing a string (e.g. `bundle appraisal install`). | `false` +| `updateGemfileLock` | Whether to update the version of the gem to publish in the `Gemfile.lock`. This is useful if you are using the [`@semantic-release/git`](https://github.com/semantic-release/git) plugin to keep the version up to date in your git repo. When set to `true` the plugin will run `bundle install` to update the version. If another command is desired, it can be set by passing a string (e.g. `bundle appraisal install`). | `false` | +| `gemPublish` | Whether to publish your gem to the gem server. | `true` | + diff --git a/src/publish.js b/src/publish.js index e574bd9..285bf33 100644 --- a/src/publish.js +++ b/src/publish.js @@ -2,20 +2,25 @@ const { unlink } = require('fs').promises; const execa = require('execa'); module.exports = async function publish( - { gemHost }, + { gemHost, gemPublish = true }, { cwd, env, logger, nextRelease: { version }, stdout, stderr }, { gemFile, gemName, credentialsFile }, ) { - logger.log(`Publishing version ${version} to gem server`); - const args = ['push', gemFile, '--config-file', credentialsFile]; - if (gemHost) { - args.push('--host', gemHost); + if (gemPublish !== false) { + logger.log(`Publishing version ${version} to gem server`); + const args = ['push', gemFile, '--config-file', credentialsFile]; + if (gemHost) { + args.push('--host', gemHost); + } + const pushResult = execa('gem', args, { cwd, env }); + pushResult.stdout.pipe(stdout, { end: false }); + pushResult.stderr.pipe(stderr, { end: false }); + await pushResult; + + logger.log(`Published version ${version} of ${gemName} to gem server`); + } else { + logger.log(`Skip publishing to gem server because gemPublish is ${gemPublish !== false}`); } - const pushResult = execa('gem', args, { cwd, env }); - pushResult.stdout.pipe(stdout, { end: false }); - pushResult.stderr.pipe(stderr, { end: false }); - await pushResult; - logger.log(`Published version ${version} of ${gemName} to gem server`); await unlink(gemFile); }; From ade7b1c6977b66bf19b737d6b8b5a5ed2f00b1e2 Mon Sep 17 00:00:00 2001 From: Rylan Collins Date: Thu, 23 Jul 2020 16:03:55 -0700 Subject: [PATCH 2/2] feat(prepare): add gemFileDir config option --- README.md | 2 +- package.json | 1 + src/__tests__/prepare.test.js | 21 ++++++++++++++++++--- src/prepare.js | 17 +++++++++++++++-- src/publish.js | 6 ++++-- yarn.lock | 10 +--------- 6 files changed, 40 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index 208e615..ca31501 100644 --- a/README.md +++ b/README.md @@ -61,4 +61,4 @@ end | `gemHost` | The gem server to push the gem to. | `'https//rubygems.org'` | | `updateGemfileLock` | Whether to update the version of the gem to publish in the `Gemfile.lock`. This is useful if you are using the [`@semantic-release/git`](https://github.com/semantic-release/git) plugin to keep the version up to date in your git repo. When set to `true` the plugin will run `bundle install` to update the version. If another command is desired, it can be set by passing a string (e.g. `bundle appraisal install`). | `false` | | `gemPublish` | Whether to publish your gem to the gem server. | `true` | - +| `gemFileDir` | Directory path in which to write the the built `.gem` file. If `false`, the `.gem` file will not be kept on the file system. | `false` | diff --git a/package.json b/package.json index 1ef2d72..ccf4cb9 100644 --- a/package.json +++ b/package.json @@ -44,6 +44,7 @@ "dependencies": { "@semantic-release/error": "^2.2.0", "execa": "^4.0.2", + "fs-extra": "^9.0.1", "glob": "^7.1.6", "tempy": "^0.5.0" }, diff --git a/src/__tests__/prepare.test.js b/src/__tests__/prepare.test.js index 06ee50f..ea0ac91 100644 --- a/src/__tests__/prepare.test.js +++ b/src/__tests__/prepare.test.js @@ -33,6 +33,8 @@ afterEach(async () => { await cleanUp(); }); +const expectFileExists = file => expect(access(path.resolve(cwd, file))).resolves.toBeUndefined(); + it('writes the new version to the version.rb file', async () => { await prepare({}, context, { versionFile, gemspec, gemName }); const versionContents = await readFile(path.resolve(cwd, versionFile), 'utf8'); @@ -68,7 +70,7 @@ end ); expect(gemFile).toEqual('a-test-gem-1.0.0.alpha.1.gem'); - await expect(access(path.resolve(cwd, gemFile))).resolves.toBeUndefined(); + await expectFileExists(gemFile); }); }); @@ -98,7 +100,7 @@ describe('when updateGemfileLock is set to a string', () => { gemspec, gemName, }); - await expect(access(path.resolve(cwd, 'command_run'))).resolves.toBeUndefined(); + await expectFileExists('command_run'); }); }); @@ -106,5 +108,18 @@ it('builds the gem', async () => { const { gemFile } = await prepare({}, context, { versionFile, gemspec, gemName }); expect(gemFile).toEqual('a-test-gem-1.2.0.gem'); - await expect(access(path.resolve(cwd, gemFile))).resolves.toBeUndefined(); + await expectFileExists(gemFile); +}); + +describe('when gemFileDir is set', () => { + it('builds the gem in the provided dir', async () => { + const { gemFile } = await prepare({ gemFileDir: 'some_dir' }, context, { + versionFile, + gemspec, + gemName, + }); + + expect(gemFile).toEqual('some_dir/a-test-gem-1.2.0.gem'); + await expectFileExists(gemFile); + }); }); diff --git a/src/prepare.js b/src/prepare.js index d2b64ef..5b52c73 100644 --- a/src/prepare.js +++ b/src/prepare.js @@ -1,6 +1,7 @@ const { readFile, writeFile } = require('fs').promises; const path = require('path'); const execa = require('execa'); +const { move } = require('fs-extra'); const { VERSION_REGEX } = require('./common'); const writeVersion = async ({ versionFile, nextVersion, logger, cwd }) => { @@ -39,7 +40,7 @@ const buildGem = async ({ gemspec, gemName, version, cwd, env, logger, stdout, s }; module.exports = async function prepare( - { updateGemfileLock = false }, + { updateGemfileLock = false, gemFileDir = false }, { nextRelease: { version }, cwd, env, logger, stdout, stderr }, { versionFile, gemspec, gemName }, ) { @@ -49,7 +50,7 @@ module.exports = async function prepare( await bundleInstall({ updateGemfileLock, cwd, env, logger, stdout, stderr }); } - const gemFile = await buildGem({ + let gemFile = await buildGem({ gemspec, gemName, version: gemVersion, @@ -60,5 +61,17 @@ module.exports = async function prepare( stderr, }); + if (gemFileDir) { + const gemFileSource = path.resolve(cwd, gemFile); + const gemFileDestination = path.resolve(cwd, gemFileDir.trim(), gemFile); + + // Only move the gem file if we need to + if (gemFileSource !== gemFileDestination) { + await move(gemFileSource, gemFileDestination); + } + + gemFile = path.join(gemFileDir.trim(), gemFile); + } + return { gemFile }; }; diff --git a/src/publish.js b/src/publish.js index 285bf33..2caebf8 100644 --- a/src/publish.js +++ b/src/publish.js @@ -2,7 +2,7 @@ const { unlink } = require('fs').promises; const execa = require('execa'); module.exports = async function publish( - { gemHost, gemPublish = true }, + { gemHost, gemPublish = true, gemFileDir = false }, { cwd, env, logger, nextRelease: { version }, stdout, stderr }, { gemFile, gemName, credentialsFile }, ) { @@ -22,5 +22,7 @@ module.exports = async function publish( logger.log(`Skip publishing to gem server because gemPublish is ${gemPublish !== false}`); } - await unlink(gemFile); + if (gemFileDir === false) { + await unlink(gemFile); + } }; diff --git a/yarn.lock b/yarn.lock index 93e1696..4a2a275 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2830,7 +2830,7 @@ from2@^2.1.0, from2@^2.3.0: inherits "^2.0.1" readable-stream "^2.0.0" -fs-extra@^9.0.0: +fs-extra@^9.0.0, fs-extra@^9.0.1: version "9.0.1" resolved "https://registry.yarnpkg.com/fs-extra/-/fs-extra-9.0.1.tgz#910da0062437ba4c39fedd863f1675ccfefcb9fc" integrity sha512-h2iAoN838FqAFJY2/qVpzFXy+EBxfVE220PalAqQLDVsFOHLJrZvut5puAbCdNv6WJk+B8ihI+k0c7JK5erwqQ== @@ -5262,7 +5262,6 @@ npm@^6.10.3: cmd-shim "^3.0.3" columnify "~1.5.4" config-chain "^1.1.12" - debuglog "*" detect-indent "~5.0.0" detect-newline "^2.1.0" dezalgo "~1.0.3" @@ -5277,7 +5276,6 @@ npm@^6.10.3: has-unicode "~2.0.1" hosted-git-info "^2.8.8" iferr "^1.0.2" - imurmurhash "*" infer-owner "^1.0.4" inflight "~1.0.6" inherits "^2.0.4" @@ -5296,14 +5294,8 @@ npm@^6.10.3: libnpx "^10.2.2" lock-verify "^2.1.0" lockfile "^1.0.4" - lodash._baseindexof "*" lodash._baseuniq "~4.6.0" - lodash._bindcallback "*" - lodash._cacheindexof "*" - lodash._createcache "*" - lodash._getnative "*" lodash.clonedeep "~4.5.0" - lodash.restparam "*" lodash.union "~4.6.0" lodash.uniq "~4.5.0" lodash.without "~4.4.0"