1+ const fs = require ( 'fs-extra' )
2+ const semver = require ( 'semver' ) ;
3+ const PQueue = require ( 'p-queue' ) ;
4+ const path = require ( 'path' ) ;
5+ const gcs = require ( '@google-cloud/storage' ) ( {
6+ projectId : 'code-angularjs-org-338b8' ,
7+ keyFilename : 'code-angularjs-org.json'
8+ } ) ;
9+ const gcsBucket = 'code-angularjs-org-338b8.appspot.com' ;
10+
11+ const bucket = gcs . bucket ( gcsBucket ) ;
12+
13+ function getDirectories ( path ) {
14+ return fs . readdirSync ( path ) . filter ( function ( file ) {
15+ return fs . statSync ( path + '/' + file ) . isDirectory ( ) ;
16+ } ) ;
17+ }
18+
19+ module . exports = function ( grunt ) {
20+
21+ function upload ( files , basePath , zipped ) {
22+ const queue = new PQueue ( { concurrency : 15 } ) ;
23+
24+ files . forEach ( function ( filePath , index ) {
25+ var fromPath = path . join ( basePath , filePath ) ;
26+
27+ const options = {
28+ destination : filePath
29+ }
30+
31+ if ( zipped ) {
32+ options . metadata = {
33+ contentEncoding : 'gzip'
34+ }
35+ }
36+
37+ queue . add ( ( ) => {
38+ return bucket . upload ( fromPath , options ) . then ( ( ) => {
39+ console . log ( 'uploaded ' + filePath )
40+ } ) . catch ( ( error ) => {
41+ queue . clear ( ) ;
42+ grunt . util . error ( 'Upload failed' , error ) ;
43+ } ) ;
44+ } ) ;
45+
46+ } ) ;
47+
48+ console . log ( 'files queued: ' , queue . size ) ;
49+
50+ return new Promise ( function ( resolve , reject ) {
51+ queue . onEmpty ( ) . then ( ( ) => {
52+
53+ if ( queue . pending === 0 ) {
54+ console . log ( 'success' ) ;
55+ resolve ( 'success' ) ;
56+ }
57+
58+ } ) . catch ( ( error ) => {
59+ grunt . util . error ( 'Upload failed' , error ) ;
60+ resolve ( error ) ;
61+ } ) ;
62+ } ) ;
63+
64+ }
65+
66+ // this loads all the node_modules that start with `grunt-` as plugins
67+ require ( 'load-grunt-tasks' ) ( grunt ) ;
68+
69+ const version = grunt . option ( 'target' ) ;
70+
71+ grunt . initConfig ( {
72+
73+ compress : {
74+ firebaseCodeDeploy : {
75+ options : {
76+ mode : 'gzip'
77+ } ,
78+ src : [ '**' ] ,
79+ cwd : version ,
80+ expand : true ,
81+ dest : 'upload/' + version + '/'
82+ }
83+ }
84+ } ) ;
85+
86+ grunt . registerTask ( 'gcs' , function ( ) {
87+ const doneFn = this . async ( ) ;
88+
89+ const deployVersion = grunt . option ( 'target' ) ;
90+ if ( ! deployVersion ) {
91+ console . log ( 'No target version supplied. Use --target="<version>"' ) ;
92+ doneFn ( false ) ;
93+ return ;
94+ }
95+
96+ const glob = deployVersion + '/' + ( grunt . option ( 'filter' ) || '**/*' ) ;
97+
98+ const files = grunt . file . expand ( {
99+ filter : 'isFile' ,
100+ cwd : 'upload/'
101+ } , glob ) ;
102+
103+ if ( ! files . length ) {
104+ console . log ( 'no files to deploy in upload/' + deployVersion ) ;
105+ doneFn ( false ) ;
106+ return ;
107+ }
108+
109+ console . log ( 'uploading files from ' + glob ) ;
110+
111+ upload ( files , 'upload' , true ) . then ( function ( ) {
112+ doneFn ( ) ;
113+ } ) ;
114+
115+ } ) ;
116+
117+ grunt . registerTask ( 'default' , [ 'compress' , 'gcs' ] ) ;
118+ }
0 commit comments