1
- const _ = require ( 'underscore' ) ,
2
- async = require ( 'neo-async' ) ,
3
- AWS = require ( 'aws-sdk' ) ,
4
- git = require ( './util/git' ) ,
5
- semver = require ( 'semver' ) ;
1
+ const AWS = require ( 'aws-sdk' ) ;
2
+ const git = require ( './util/git' ) ;
3
+ const { createRegisterAsyncTaskFn } = require ( './util/async-grunt-task' ) ;
4
+ const semver = require ( 'semver' ) ;
6
5
7
6
module . exports = function ( grunt ) {
8
- grunt . registerTask ( 'publish:latest' , function ( ) {
9
- const done = this . async ( ) ;
10
-
11
- git . debug ( function ( remotes , branches ) {
12
- grunt . log . writeln ( 'remotes: ' + remotes ) ;
13
- grunt . log . writeln ( 'branches: ' + branches ) ;
14
-
15
- git . commitInfo ( function ( err , info ) {
16
- grunt . log . writeln ( 'tag: ' + info . tagName ) ;
17
-
18
- const files = [ ] ;
19
-
20
- // Publish the master as "latest" and with the commit-id
21
- if ( info . isMaster ) {
22
- files . push ( '-latest' ) ;
23
- files . push ( '-' + info . head ) ;
24
- }
25
-
26
- // Publish tags by their tag-name
27
- if ( info . tagName && semver . valid ( info . tagName ) ) {
28
- files . push ( '-' + info . tagName ) ;
29
- }
30
-
31
- if ( files . length > 0 ) {
32
- initSDK ( ) ;
33
- grunt . log . writeln ( 'publishing files: ' + JSON . stringify ( files ) ) ;
34
- publish ( fileMap ( files ) , done ) ;
35
- } else {
36
- // Silently ignore for branches
37
- done ( ) ;
38
- }
39
- } ) ;
40
- } ) ;
41
- } ) ;
42
- grunt . registerTask ( 'publish:version' , function ( ) {
43
- const done = this . async ( ) ;
44
- initSDK ( ) ;
7
+ const registerAsyncTask = createRegisterAsyncTaskFn ( grunt ) ;
45
8
46
- git . commitInfo ( function ( err , info ) {
47
- if ( ! info . tagName ) {
48
- throw new Error ( 'The current commit must be tagged' ) ;
49
- }
50
- publish ( fileMap ( [ '-' + info . tagName ] ) , done ) ;
51
- } ) ;
9
+ registerAsyncTask ( 'publish:latest' , async ( ) => {
10
+ grunt . log . writeln ( 'remotes: ' + ( await git . remotes ( ) ) ) ;
11
+ grunt . log . writeln ( 'branches: ' + ( await git . branches ( ) ) ) ;
12
+
13
+ const commitInfo = await git . commitInfo ( ) ;
14
+ grunt . log . writeln ( 'tag: ' + commitInfo . tagName ) ;
15
+
16
+ const suffixes = [ ] ;
17
+
18
+ // Publish the master as "latest" and with the commit-id
19
+ if ( commitInfo . isMaster ) {
20
+ suffixes . push ( '-latest' ) ;
21
+ suffixes . push ( '-' + commitInfo . headSha ) ;
22
+ }
23
+
24
+ // Publish tags by their tag-name
25
+ if ( commitInfo . tagName && semver . valid ( commitInfo . tagName ) ) {
26
+ suffixes . push ( '-' + commitInfo . tagName ) ;
27
+ }
28
+
29
+ if ( suffixes . length > 0 ) {
30
+ initSDK ( ) ;
31
+ grunt . log . writeln (
32
+ 'publishing file-suffixes: ' + JSON . stringify ( suffixes )
33
+ ) ;
34
+ await publish ( suffixes ) ;
35
+ }
52
36
} ) ;
53
37
54
38
function initSDK ( ) {
@@ -62,45 +46,57 @@ module.exports = function(grunt) {
62
46
63
47
AWS . config . update ( { accessKeyId : key , secretAccessKey : secret } ) ;
64
48
}
65
- function publish ( files , callback ) {
66
- const s3 = new AWS . S3 ( ) ,
67
- bucket = process . env . S3_BUCKET_NAME ;
68
-
69
- async . each (
70
- _ . keys ( files ) ,
71
- function ( file , callback ) {
72
- const params = {
73
- Bucket : bucket ,
74
- Key : file ,
75
- Body : grunt . file . read ( files [ file ] )
76
- } ;
77
- s3 . putObject ( params , function ( err ) {
78
- if ( err ) {
79
- throw err ;
80
- } else {
81
- grunt . log . writeln ( 'Published ' + file + ' to build server.' ) ;
82
- callback ( ) ;
83
- }
84
- } ) ;
85
- } ,
86
- callback
87
- ) ;
49
+
50
+ async function publish ( suffixes ) {
51
+ const publishPromises = suffixes . map ( suffix => publishSuffix ( suffix ) ) ;
52
+ return Promise . all ( publishPromises ) ;
88
53
}
89
- function fileMap ( suffixes ) {
90
- const map = { } ;
91
- _ . each (
92
- [
93
- 'handlebars.js' ,
94
- 'handlebars.min.js' ,
95
- 'handlebars.runtime.js' ,
96
- 'handlebars.runtime.min.js'
97
- ] ,
98
- function ( file ) {
99
- _ . each ( suffixes , function ( suffix ) {
100
- map [ file . replace ( / \. j s $ / , suffix + '.js' ) ] = 'dist/' + file ;
101
- } ) ;
102
- }
103
- ) ;
104
- return map ;
54
+
55
+ async function publishSuffix ( suffix ) {
56
+ const filenames = [
57
+ 'handlebars.js' ,
58
+ 'handlebars.min.js' ,
59
+ 'handlebars.runtime.js' ,
60
+ 'handlebars.runtime.min.js'
61
+ ] ;
62
+ const publishPromises = filenames . map ( filename => {
63
+ const nameInBucket = getNameInBucket ( filename , suffix ) ;
64
+ const localFile = getLocalFile ( filename ) ;
65
+ uploadToBucket ( localFile , nameInBucket ) ;
66
+ grunt . log . writeln (
67
+ `Published ${ localFile } to build server (${ nameInBucket } )`
68
+ ) ;
69
+ } ) ;
70
+ return Promise . all ( publishPromises ) ;
71
+ }
72
+
73
+ async function uploadToBucket ( localFile , nameInBucket ) {
74
+ const bucket = process . env . S3_BUCKET_NAME ;
75
+ const uploadParams = {
76
+ Bucket : bucket ,
77
+ Key : nameInBucket ,
78
+ Body : grunt . file . read ( localFile )
79
+ } ;
80
+ return s3PutObject ( uploadParams ) ;
105
81
}
106
82
} ;
83
+
84
+ function s3PutObject ( uploadParams ) {
85
+ const s3 = new AWS . S3 ( ) ;
86
+ return new Promise ( ( resolve , reject ) => {
87
+ s3 . putObject ( uploadParams , err => {
88
+ if ( err != null ) {
89
+ return reject ( err ) ;
90
+ }
91
+ resolve ( ) ;
92
+ } ) ;
93
+ } ) ;
94
+ }
95
+
96
+ function getNameInBucket ( filename , suffix ) {
97
+ return filename . replace ( / \. j s $ / , suffix + '.js' ) ;
98
+ }
99
+
100
+ function getLocalFile ( filename ) {
101
+ return 'dist/' + filename ;
102
+ }
0 commit comments