Skip to content

Commit

Permalink
feat: upload assets to GitHub release (#334)
Browse files Browse the repository at this point in the history
* feat: upload assets to GitHub release

* feat: accept function and glob pattern

* fix: async runStep

* fix: use releases instead of release

* fix: quote path with spaces

* test: add test for createGitHubRelease

* test: fix test name

* test: add eslint disable comment
  • Loading branch information
uetchy authored and Eunjae Lee committed Oct 16, 2019
1 parent 8654748 commit fcf2807
Show file tree
Hide file tree
Showing 9 changed files with 284 additions and 25 deletions.
3 changes: 3 additions & 0 deletions packages/shipjs-lib/src/lib/config/defaultConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -183,4 +183,7 @@ export default {
],
}),
},
releases: {
assetsToUpload: [],
},
};
11 changes: 10 additions & 1 deletion packages/shipjs/babel.config.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
module.exports = {
presets: ['@babel/preset-env'],
presets: [
[
'@babel/preset-env',
{
targets: {
node: 'current',
},
},
],
],
};
2 changes: 2 additions & 0 deletions packages/shipjs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@
"change-case": "3.1.0",
"conventional-changelog-cli": "2.0.25",
"esm": "3.2.25",
"globby": "^10.0.1",
"inquirer": "7.0.0",
"shell-quote": "^1.7.2",
"shipjs-lib": "0.6.0",
"temp-write": "4.0.0"
},
Expand Down
2 changes: 1 addition & 1 deletion packages/shipjs/src/flow/release.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ async function release({ help = false, dir = '.', dryRun = false }) {
await runAfterPublish({ config, dir, dryRun });
const { tagName } = createGitTag({ version, config, dir, dryRun });
gitPush({ tagName, config, dir, dryRun });
createGitHubRelease({ version, config, dir, dryRun });
await createGitHubRelease({ version, config, dir, dryRun });
await notifyReleaseSuccess({
config,
appName,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import fs from 'fs';
import path from 'path';

import defaultConfig from 'shipjs-lib/src/lib/config/defaultConfig';

let stdout = '';
const mockedLog = (...args) => {
stdout += args.join(' ');
stdout += '\n';
};

beforeEach(() => {
// eslint-disable-next-line no-console
console.log = mockedLog;
});

afterEach(() => {
stdout = '';
});

it('create github release', async () => {
const createGitHubRelease = require('../createGitHubRelease');
const dryRun = true;
const dir = path.resolve(__dirname, 'fixtures');
const version = '0.5.5';
const config = {
...defaultConfig,
releases: {
assetsToUpload: 'asset with spaces.md',
},
};
await createGitHubRelease.default({ version, dryRun, config, dir });
const changelogPath = stdout.match(/-F ([^\s]+)/)[1];
const changelog = fs.readFileSync(changelogPath, 'utf-8');

expect(changelog).toEqual(`${version}
## [${version}](https://example.com) (2019-10-01)
### Bug Fixes
- bug fixes
`);

expect(stdout).toContain(
`-a '${path.join(dir, config.releases.assetsToUpload)}'`
);
});
22 changes: 22 additions & 0 deletions packages/shipjs/src/step/release/__tests__/fixtures/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# [0.6.0](https://example.com/v0.5.5...v0.6.0) (2019-10-04)

### Bug Fixes

* don't do this ([#294](https://example.com)) ([eb1ab44](https://example.com))
* fix bug ([#301](https://example.com)) ([f89ba1d](https://example.com))

### Features

* add hook ([#293](https://example.com)) ([0de5d68](https://example.com))

## [0.5.5](https://example.com) (2019-10-01)


### Bug Fixes

- bug fixes


# 0.1.0 (2019-08-02)

### Initial Release
Empty file.
80 changes: 58 additions & 22 deletions packages/shipjs/src/step/release/createGitHubRelease.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import fs from 'fs';
import path from 'path';
import globby from 'globby';
import tempWrite from 'temp-write';
import { quote } from 'shell-quote';

import runStep from '../runStep';

Expand All @@ -25,25 +27,59 @@ function getChangelog(version, rootDir) {
return null;
}

export default ({ version, config, dir, dryRun }) =>
runStep({ title: 'Creating a release on GitHub repository' }, ({ run }) => {
const { getTagName } = config;
const tagName = getTagName({ version });

// extract matching changelog
const changelog = config.updateChangelog
? getChangelog(version, dir)
: null;
const exportedPath = tempWrite.sync(changelog || tagName);

// create GitHub release
const releaseCommand = [
'hub',
'release',
'create',
`-F ${exportedPath}`,
tagName,
].join(' ');

run({ command: releaseCommand, dir, dryRun });
});
export default async ({ version, config, dir, dryRun }) =>
await runStep(
{ title: 'Creating a release on GitHub repository' },
async ({ run }) => {
const { getTagName, releases } = config;
const tagName = getTagName({ version });
const args = [];

// extract matching changelog
const changelog = config.updateChangelog
? getChangelog(version, dir)
: null;
const exportedPath = tempWrite.sync(changelog || tagName);
args.push('-F', quote([exportedPath]));

// handle assets
if (releases && releases.assetsToUpload) {
const option = releases.assetsToUpload;
const assetPaths = [];

if (typeof option === 'function') {
// function
// assetsToUpload: ({dir, version, tagName}) => [...]
const files = await Promise.resolve(
option({ dir, version, tagName })
);
assetPaths.push(...files);
} else if (Array.isArray(option) && option.length > 0) {
// list
// assetsToUpload: ['package.json', 'dist/*.zip']
for (const asset of option) {
const files = await globby(asset, { cwd: dir });
if (files) {
assetPaths.push(...files);
}
}
} else if (typeof option === 'string') {
// string
// assetsToUpload: 'archive.zip'
const files = await globby(option, { cwd: dir });
if (files) {
assetPaths.push(...files);
}
}

for (const asset of assetPaths) {
args.push('-a', quote([path.resolve(dir, asset)]));
}
}

// create GitHub release
const hubCommand = ['hub', 'release', 'create'];
const command = [...hubCommand, ...args, tagName].join(' ');
run({ command, dir, dryRun });
}
);
Loading

0 comments on commit fcf2807

Please sign in to comment.