@@ -6,6 +6,7 @@ import 'shelljs/global';
6
6
import path from 'path' ;
7
7
import semver from 'semver' ;
8
8
import yargs from 'yargs' ;
9
+ import request from 'request' ;
9
10
10
11
// do not die on errors
11
12
config . fatal = false ;
@@ -18,10 +19,10 @@ const changelog = path.join(repoRoot, 'CHANGELOG.md');
18
19
19
20
const npmjson = JSON . parse ( cat ( packagePath ) ) ;
20
21
const isPrivate = npmjson . private ;
22
+ const devDepsNode = npmjson . devDependencies ;
21
23
22
24
//------------------------------------------------------------------------------
23
25
// check if one of 'rf-changelog' or 'mt-changelog' is used by project
24
- const devDepsNode = npmjson . devDependencies ;
25
26
const isCommitsChangelogUsed = devDepsNode &&
26
27
devDepsNode [ 'rf-changelog' ] || devDepsNode [ 'mt-changelog' ] ;
27
28
if ( isCommitsChangelogUsed && ! which ( 'changelog' ) ) {
@@ -33,25 +34,18 @@ if (isCommitsChangelogUsed && !which('changelog')) {
33
34
const configOptions = npmjson [ 'release-script' ] || { } ;
34
35
const bowerRoot = path . join ( repoRoot , ( configOptions . bowerRoot || 'amd/' ) ) ;
35
36
const tmpBowerRepo = path . join ( repoRoot , ( configOptions . tmpBowerRepo || 'tmp-bower-repo' ) ) ;
36
-
37
- // let bowerRepo;
38
- // if (npmjson.bowerRepo) {
39
- // bowerRepo = npmjson.bowerRepo;
40
- // } else {
41
- // let match = npmjson.repository.url.match(/^git@github\.com:(.*)\.git$/);
42
- // match = match || npmjson.repository.url.match(/^git\+https:\/\/github\.com\/(.*)\.git$/);
43
- // let gitUrlBase = match && match[1];
44
- // gitUrlBase = gitUrlBase || npmjson.repository.url;
45
- // bowerRepo = `[email protected] :${gitUrlBase}-bower.git`;
46
- // }
47
37
const bowerRepo = configOptions . bowerRepo ; // if it is not set, then there is no bower repo
48
38
39
+ const githubToken = process . env . GITHUB_TOKEN ;
40
+
41
+
49
42
//------------------------------------------------------------------------------
50
43
// command line options
51
44
const yargsConf = yargs
52
45
. usage ( 'Usage: $0 <version> [--preid <identifier>]' )
53
46
. example ( '$0 minor --preid beta' , 'Release with minor version bump with pre-release tag' )
54
47
. example ( '$0 major' , 'Release with major version bump' )
48
+ . example ( '$0 major --notes "This is new cool version"' , 'Add a custom message to release' )
55
49
. example ( '$0 major --dry-run' , 'Release dry run with patch version bump' )
56
50
. example ( '$0 --preid beta' , 'Release same version with pre-release bump' )
57
51
. command ( 'patch' , 'Release patch' )
@@ -73,6 +67,11 @@ const yargsConf = yargs
73
67
demand : false ,
74
68
default : false ,
75
69
describe : 'Increased debug output'
70
+ } )
71
+ . option ( 'notes' , {
72
+ demand : false ,
73
+ default : false ,
74
+ describe : 'A custom message for release. Overrides [rf|mt]changelog message'
76
75
} ) ;
77
76
78
77
const argv = yargsConf . argv ;
@@ -92,6 +91,8 @@ if (versionBumpOptions.type === undefined && versionBumpOptions.preid === undefi
92
91
exit ( 1 ) ;
93
92
}
94
93
94
+ let notesForRelease = argv . notes ;
95
+
95
96
96
97
//------------------------------------------------------------------------------
97
98
// functions
@@ -114,6 +115,20 @@ function safeRun(command) {
114
115
}
115
116
}
116
117
118
+ /**
119
+ * Npm's `package.json` 'repository.url' could be set to one of three forms:
120
+ * [email protected] :<author>/<repo-name>.git
121
+ * git+https://github.com/<author>/<repo-name>.git
122
+ * or just <author>/<repo-name>
123
+ * @returns [<author>, <repo-name>] array
124
+ */
125
+ function getOwnerAndRepo ( url ) {
126
+ let match = url . match ( / ^ g i t @ g i t h u b \. c o m : ( .* ) \. g i t $ / ) ;
127
+ match = match || url . match ( / ^ g i t \+ h t t p s : \/ \/ g i t h u b \. c o m \/ ( .* ) \. g i t $ / ) ;
128
+ let gitUrlBase = match && match [ 1 ] ;
129
+ return ( gitUrlBase || url ) . split ( '/' ) ;
130
+ }
131
+
117
132
function release ( { type, preid } ) {
118
133
if ( type === undefined && ! preid ) printErrorAndExit ( 'Must specify version type or preid' ) ;
119
134
@@ -174,10 +189,11 @@ function release({ type, preid }) {
174
189
}
175
190
176
191
const vVersion = `v${ newVersion } ` ;
192
+ const versionAndNotes = notesForRelease = notesForRelease ? `${ vVersion } ${ notesForRelease } ` : vVersion ;
177
193
178
194
// generate changelog
179
195
if ( isCommitsChangelogUsed ) {
180
- run ( `changelog --title ${ vVersion } --out ${ changelog } ` ) ;
196
+ run ( `changelog --title=" ${ versionAndNotes } " --out ${ changelog } ` ) ;
181
197
safeRun ( `git add ${ changelog } ` ) ;
182
198
console . log ( 'Generated Changelog' . cyan ) ;
183
199
}
@@ -187,14 +203,55 @@ function release({ type, preid }) {
187
203
// tag and release
188
204
console . log ( 'Tagging: ' . cyan + vVersion . green ) ;
189
205
if ( isCommitsChangelogUsed ) {
190
- safeRun ( `changelog --title ${ vVersion } -s | git tag -a -F - ${ vVersion } ` ) ;
206
+ notesForRelease = run ( `changelog --title="${ versionAndNotes } " -s` ) ;
207
+ safeRun ( `changelog --title="${ versionAndNotes } " -s | git tag -a -F - ${ vVersion } ` ) ;
191
208
} else {
192
- safeRun ( `git tag -a --message=${ vVersion } ${ vVersion } ` ) ;
209
+ safeRun ( `git tag -a --message=" ${ versionAndNotes } " ${ vVersion } ` ) ;
193
210
}
194
211
safeRun ( 'git push' ) ;
195
212
safeRun ( 'git push --tags' ) ;
196
213
console . log ( 'Tagged: ' . cyan + vVersion . green ) ;
197
214
215
+ // publish to GitHub
216
+ if ( githubToken ) {
217
+ console . log ( `GitHub token found ${ githubToken } ` . green ) ;
218
+ console . log ( 'Publishing to GitHub: ' . cyan + vVersion . green ) ;
219
+
220
+ if ( argv . dryRun ) {
221
+ console . log ( `[publishing to GitHub]` . grey , 'DRY RUN' . magenta ) ;
222
+ } else {
223
+ const [ githubOwner , githubRepo ] = getOwnerAndRepo ( npmjson . repository . url ) ;
224
+
225
+ request ( {
226
+ uri : `https://api.github.com/repos/${ githubOwner } /${ githubRepo } /releases` ,
227
+ method : 'POST' ,
228
+ json : true ,
229
+ body : {
230
+ tag_name : vVersion , // eslint-disable-line camelcase
231
+ name : `${ githubRepo } ${ vVersion } ` ,
232
+ body : notesForRelease ,
233
+ draft : false ,
234
+ prerelease : ! ! preid
235
+ } ,
236
+ headers : {
237
+ 'Authorization' : `token ${ githubToken } ` ,
238
+ 'User-Agent' : 'release-script (https://github.com/alexkval/release-script)'
239
+ }
240
+ } , function ( err , res , body ) {
241
+ if ( err ) {
242
+ console . log ( 'API request to GitHub, error has occured:' . red ) ;
243
+ console . log ( err ) ;
244
+ console . log ( 'Skip GitHub releasing' . yellow ) ;
245
+ } else if ( res . statusMessage === 'Unauthorized' ) {
246
+ console . log ( `GitHub token ${ githubToken } is wrong` . red ) ;
247
+ console . log ( 'Skip GitHub releasing' . yellow ) ;
248
+ } else {
249
+ console . log ( `Published at ${ body . html_url } ` . green ) ;
250
+ }
251
+ } ) ;
252
+ }
253
+ }
254
+
198
255
// npm
199
256
if ( isPrivate ) {
200
257
console . log ( 'Package is private, skipping npm release' . yellow ) ;
0 commit comments