Skip to content

Commit ade7b1c

Browse files
committed
feat(prepare): add gemFileDir config option
1 parent 4b76d64 commit ade7b1c

File tree

6 files changed

+40
-17
lines changed

6 files changed

+40
-17
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -61,4 +61,4 @@ end
6161
| `gemHost` | The gem server to push the gem to. | `'https//rubygems.org'` |
6262
| `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` |
6363
| `gemPublish` | Whether to publish your gem to the gem server. | `true` |
64-
64+
| `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` |

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
"dependencies": {
4545
"@semantic-release/error": "^2.2.0",
4646
"execa": "^4.0.2",
47+
"fs-extra": "^9.0.1",
4748
"glob": "^7.1.6",
4849
"tempy": "^0.5.0"
4950
},

src/__tests__/prepare.test.js

+18-3
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ afterEach(async () => {
3333
await cleanUp();
3434
});
3535

36+
const expectFileExists = file => expect(access(path.resolve(cwd, file))).resolves.toBeUndefined();
37+
3638
it('writes the new version to the version.rb file', async () => {
3739
await prepare({}, context, { versionFile, gemspec, gemName });
3840
const versionContents = await readFile(path.resolve(cwd, versionFile), 'utf8');
@@ -68,7 +70,7 @@ end
6870
);
6971

7072
expect(gemFile).toEqual('a-test-gem-1.0.0.alpha.1.gem');
71-
await expect(access(path.resolve(cwd, gemFile))).resolves.toBeUndefined();
73+
await expectFileExists(gemFile);
7274
});
7375
});
7476

@@ -98,13 +100,26 @@ describe('when updateGemfileLock is set to a string', () => {
98100
gemspec,
99101
gemName,
100102
});
101-
await expect(access(path.resolve(cwd, 'command_run'))).resolves.toBeUndefined();
103+
await expectFileExists('command_run');
102104
});
103105
});
104106

105107
it('builds the gem', async () => {
106108
const { gemFile } = await prepare({}, context, { versionFile, gemspec, gemName });
107109

108110
expect(gemFile).toEqual('a-test-gem-1.2.0.gem');
109-
await expect(access(path.resolve(cwd, gemFile))).resolves.toBeUndefined();
111+
await expectFileExists(gemFile);
112+
});
113+
114+
describe('when gemFileDir is set', () => {
115+
it('builds the gem in the provided dir', async () => {
116+
const { gemFile } = await prepare({ gemFileDir: 'some_dir' }, context, {
117+
versionFile,
118+
gemspec,
119+
gemName,
120+
});
121+
122+
expect(gemFile).toEqual('some_dir/a-test-gem-1.2.0.gem');
123+
await expectFileExists(gemFile);
124+
});
110125
});

src/prepare.js

+15-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
const { readFile, writeFile } = require('fs').promises;
22
const path = require('path');
33
const execa = require('execa');
4+
const { move } = require('fs-extra');
45
const { VERSION_REGEX } = require('./common');
56

67
const writeVersion = async ({ versionFile, nextVersion, logger, cwd }) => {
@@ -39,7 +40,7 @@ const buildGem = async ({ gemspec, gemName, version, cwd, env, logger, stdout, s
3940
};
4041

4142
module.exports = async function prepare(
42-
{ updateGemfileLock = false },
43+
{ updateGemfileLock = false, gemFileDir = false },
4344
{ nextRelease: { version }, cwd, env, logger, stdout, stderr },
4445
{ versionFile, gemspec, gemName },
4546
) {
@@ -49,7 +50,7 @@ module.exports = async function prepare(
4950
await bundleInstall({ updateGemfileLock, cwd, env, logger, stdout, stderr });
5051
}
5152

52-
const gemFile = await buildGem({
53+
let gemFile = await buildGem({
5354
gemspec,
5455
gemName,
5556
version: gemVersion,
@@ -60,5 +61,17 @@ module.exports = async function prepare(
6061
stderr,
6162
});
6263

64+
if (gemFileDir) {
65+
const gemFileSource = path.resolve(cwd, gemFile);
66+
const gemFileDestination = path.resolve(cwd, gemFileDir.trim(), gemFile);
67+
68+
// Only move the gem file if we need to
69+
if (gemFileSource !== gemFileDestination) {
70+
await move(gemFileSource, gemFileDestination);
71+
}
72+
73+
gemFile = path.join(gemFileDir.trim(), gemFile);
74+
}
75+
6376
return { gemFile };
6477
};

src/publish.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ const { unlink } = require('fs').promises;
22
const execa = require('execa');
33

44
module.exports = async function publish(
5-
{ gemHost, gemPublish = true },
5+
{ gemHost, gemPublish = true, gemFileDir = false },
66
{ cwd, env, logger, nextRelease: { version }, stdout, stderr },
77
{ gemFile, gemName, credentialsFile },
88
) {
@@ -22,5 +22,7 @@ module.exports = async function publish(
2222
logger.log(`Skip publishing to gem server because gemPublish is ${gemPublish !== false}`);
2323
}
2424

25-
await unlink(gemFile);
25+
if (gemFileDir === false) {
26+
await unlink(gemFile);
27+
}
2628
};

yarn.lock

+1-9
Original file line numberDiff line numberDiff line change
@@ -2830,7 +2830,7 @@ from2@^2.1.0, from2@^2.3.0:
28302830
inherits "^2.0.1"
28312831
readable-stream "^2.0.0"
28322832

2833-
fs-extra@^9.0.0:
2833+
fs-extra@^9.0.0, fs-extra@^9.0.1:
28342834
version "9.0.1"
28352835
resolved "https://registry.yarnpkg.com/fs-extra/-/fs-extra-9.0.1.tgz#910da0062437ba4c39fedd863f1675ccfefcb9fc"
28362836
integrity sha512-h2iAoN838FqAFJY2/qVpzFXy+EBxfVE220PalAqQLDVsFOHLJrZvut5puAbCdNv6WJk+B8ihI+k0c7JK5erwqQ==
@@ -5262,7 +5262,6 @@ npm@^6.10.3:
52625262
cmd-shim "^3.0.3"
52635263
columnify "~1.5.4"
52645264
config-chain "^1.1.12"
5265-
debuglog "*"
52665265
detect-indent "~5.0.0"
52675266
detect-newline "^2.1.0"
52685267
dezalgo "~1.0.3"
@@ -5277,7 +5276,6 @@ npm@^6.10.3:
52775276
has-unicode "~2.0.1"
52785277
hosted-git-info "^2.8.8"
52795278
iferr "^1.0.2"
5280-
imurmurhash "*"
52815279
infer-owner "^1.0.4"
52825280
inflight "~1.0.6"
52835281
inherits "^2.0.4"
@@ -5296,14 +5294,8 @@ npm@^6.10.3:
52965294
libnpx "^10.2.2"
52975295
lock-verify "^2.1.0"
52985296
lockfile "^1.0.4"
5299-
lodash._baseindexof "*"
53005297
lodash._baseuniq "~4.6.0"
5301-
lodash._bindcallback "*"
5302-
lodash._cacheindexof "*"
5303-
lodash._createcache "*"
5304-
lodash._getnative "*"
53055298
lodash.clonedeep "~4.5.0"
5306-
lodash.restparam "*"
53075299
lodash.union "~4.6.0"
53085300
lodash.uniq "~4.5.0"
53095301
lodash.without "~4.4.0"

0 commit comments

Comments
 (0)