1111const AWS = require ( 'aws-sdk' )
1212const fs = require ( 'fs' )
1313const exec = require ( 'child_process' ) . exec
14+ const colors = require ( "colors/safe" )
15+ const zlib = require ( 'zlib' )
16+
1417let manifest = require ( './manifest.json' )
1518
1619const expectedArgs = [ "bucket" , "accessKeyId" , "secretAccessKey" ]
@@ -25,9 +28,11 @@ process.argv.forEach(arg => {
2528 }
2629} )
2730
28- console . log ( "\n ----------------------------" )
29- console . log ( " Plugin S3 plugin uploader " )
30- console . log ( " ----------------------------\n" )
31+ console . log ( '\n' )
32+ console . log ( `\t ${ colors . bgWhite ( colors . black ( " --------------------------- " ) ) } ` )
33+ console . log ( `\t ${ colors . bgWhite ( colors . black ( " Plugin S3 plugin uploader " ) ) } ` )
34+ console . log ( `\t ${ colors . bgWhite ( colors . black ( " --------------------------- " ) ) } ` )
35+ console . log ( '\n' )
3136
3237if ( ! args . accessKeyId ) {
3338 console . log ( "💥 Failed to upload plugin, missing arg accessKeyId" )
@@ -42,10 +47,9 @@ if (!args.accessKeyId) {
4247
4348const s3Bucket = new AWS . S3 ( { accessKeyId : args . accessKeyId , secretAccessKey : args . secretAccessKey } )
4449
45- const pluginName = manifest . name . replace ( / \w \S * / g, function ( txt ) { return txt . charAt ( 0 ) . toUpperCase ( ) + txt . substr ( 1 ) . toLowerCase ( ) } ) . replace ( / \s / g, '' )
4650
47- const versionPaths = manifest . version . split ( "." )
4851const bundlePath = manifest . bundle . replace ( / \. / g, '-' ) . toLowerCase ( )
52+ const versionPaths = manifest . version . split ( "." )
4953const majorVersionPath = versionPaths [ 0 ]
5054const baseKey = `${ bundlePath } /v${ majorVersionPath } `
5155
@@ -65,58 +69,135 @@ fs.readFile('./icon.png', (err, pluginIconData) => {
6569 } ) . catch ( err => console . log ( "💥 " + err ) )
6670 }
6771
68- if ( shouldUploadPluginIcon ) {
69- uploadPluginIcon ( pluginIconData ) . then ( location => {
70- console . log ( "> uploaded plugin icon > " + location + "\n" )
71-
72- if ( manifest . graphic_url != location ) {
73- manifest . graphic_url = location
74-
75- fs . writeFile ( './manifest.json' , JSON . stringify ( manifest , null , 4 ) , 'utf8' , err => {
76- if ( ! err ) {
77- uploadManifest ( )
78- } else {
79- console . log ( "> failed to set manifest graphic url... (will continue to upload manifest) \n" )
80-
81- uploadManifest ( )
82- }
83- } )
84- } else {
85- uploadManifest ( )
86- }
87- } ) . catch ( err => console . log ( "💥 " + err ) )
88- } else {
89- uploadManifest ( )
72+ /**
73+ * Check if icon.png is exist.
74+ * Upload icon.png
75+ */
76+ uploadIcon ( shouldUploadPluginIcon , pluginIconData ) . then ( iconLocation => {
77+ if ( iconLocation ) {
78+ console . log ( "> uploaded plugin icon > " + iconLocation + "\n" )
9079
91- if ( manifest . graphic_url ) {
92- delete manifest . graphic_url
93- fs . writeFile ( './manifest.json' , JSON . stringify ( manifest , null , 4 ) , 'utf8' , ( ) => remove ( "icon.png" ) )
80+ manifest . graphic_url = iconLocation
9481 }
95- }
9682
83+ /**
84+ * Check if thumbnail.png is exist.
85+ * Upload thumbnail.png
86+ */
87+ uploadThumbnail ( ) . then ( thumbnailLocation => {
88+ if ( thumbnailLocation ) {
89+ console . log ( "> uploaded plugin thumbnail > " + thumbnailLocation + "\n" )
90+
91+ manifest . thumbnail_url = thumbnailLocation
92+ }
93+
94+ /**
95+ * Check if markdown.md is exist.
96+ * Upload markdown.md
97+ */
98+ uploadMarkdown ( ) . then ( markDownLocation => {
99+ if ( markDownLocation ) {
100+ console . log ( "> uploaded plugin markdown > " + markDownLocation + "\n" )
101+
102+ manifest . markdown_url = markDownLocation
103+ }
104+
105+ /**
106+ * Set timestamp as a build version to avoid browser cache.
107+ * buildVersion won't be stored with plugin data.
108+ */
109+ manifest . buildVersion = Date . now ( )
110+
111+ /**
112+ * Update the manifest file with new urls
113+ * Upload manifest.json
114+ */
115+ updateManifestFile ( ) . then ( ( ) => uploadManifest ( ) )
116+ } )
117+ } )
118+ } )
97119 } ) . catch ( err => console . log ( "💥 " + err ) )
98120} )
99121
122+ function updateManifestFile ( ) {
123+ return new Promise ( resolve => {
124+ fs . writeFile ( './manifest.json' , JSON . stringify ( manifest , null , 4 ) , 'utf8' , err => {
125+ if ( ! err ) {
126+ resolve ( )
127+ } else {
128+ console . log ( "> failed to set manifest properties (will continue to upload manifest) \n" )
129+
130+ resolve ( )
131+ }
132+ } )
133+ } )
134+ }
135+
136+ function uploadIcon ( shouldUploadPluginIcon , pluginIconData ) {
137+ return new Promise ( resolve => {
138+ if ( shouldUploadPluginIcon ) {
139+ uploadPluginIcon ( pluginIconData ) . then ( iconLocation => resolve ( iconLocation ) ) . catch ( err => {
140+ console . log ( "💥 " + err )
141+
142+ resolve ( null )
143+ } )
144+ } else {
145+ resolve ( null )
146+ }
147+ } )
148+ }
149+
150+ function uploadThumbnail ( ) {
151+ return new Promise ( resolve => {
152+ fs . readFile ( './thumbnail.png' , ( err , pluginThumbnail ) => {
153+ if ( ! err && pluginThumbnail ) {
154+ upload ( {
155+ resolve : resolve ,
156+ reject : err => {
157+ console . log ( "💥 " + err )
158+ resolve ( null )
159+ } ,
160+ key : "thumbnail.png" ,
161+ data : pluginThumbnail ,
162+ contentType : "image/png"
163+ } )
164+ } else {
165+ resolve ( null )
166+ }
167+ } )
168+ } )
169+ }
170+
100171function uploadIndexJS ( ) {
101172 return new Promise ( ( resolve , reject ) => {
102- upload ( {
103- resolve : resolve ,
104- reject : reject ,
105- key : "index.js" ,
106- data : fs . readFileSync ( "dist/index.js" ) ,
107- contentType : "text/javascript"
173+ zlib . gzip ( fs . readFileSync ( "dist/index.js" ) , ( error , result ) => {
174+ if ( error ) throw error
175+
176+ upload ( {
177+ resolve : resolve ,
178+ reject : reject ,
179+ key : "index.js" ,
180+ data : result ,
181+ contentType : "text/javascript" ,
182+ contentEncoding : "gzip"
183+ } )
108184 } )
109185 } )
110186}
111187
112188function uploadStyleCSS ( ) {
113189 return new Promise ( ( resolve , reject ) => {
114- upload ( {
115- resolve : resolve ,
116- reject : reject ,
117- key : "style.css" ,
118- data : fs . readFileSync ( "dist/style.css" ) ,
119- contentType : "text/css"
190+ zlib . gzip ( fs . readFileSync ( "dist/style.css" ) , ( error , result ) => {
191+ if ( error ) throw error
192+
193+ upload ( {
194+ resolve : resolve ,
195+ reject : reject ,
196+ key : "style.css" ,
197+ data : result ,
198+ contentType : "text/css" ,
199+ contentEncoding : "gzip"
200+ } )
120201 } )
121202 } )
122203}
@@ -133,13 +214,34 @@ function uploadPluginIcon(data) {
133214 } )
134215}
135216
217+ function uploadMarkdown ( ) {
218+ return new Promise ( resolve => {
219+ fs . readFile ( './markdown.md' , ( err , pluginMarkDown ) => {
220+ if ( ! err && pluginMarkDown ) {
221+ upload ( {
222+ resolve : resolve ,
223+ reject : err => {
224+ console . log ( "💥 " + err )
225+ resolve ( null )
226+ } ,
227+ key : "markdown.md" ,
228+ data : pluginMarkDown ,
229+ contentType : "text/markdown"
230+ } )
231+ } else {
232+ resolve ( null )
233+ }
234+ } )
235+ } )
236+ }
237+
136238function uploadManifestJSON ( ) {
137239 return new Promise ( ( resolve , reject ) => {
138240 upload ( {
139241 resolve : resolve ,
140242 reject : reject ,
141243 key : "manifest.json" ,
142- data : fs . readFileSync ( "dist/ manifest.json" ) ,
244+ data : JSON . stringify ( manifest , null , 4 ) ,
143245 contentType : "application/json"
144246 } )
145247 } )
@@ -151,7 +253,7 @@ function upload(params) {
151253 Key : baseKey + "/" + params . key ,
152254 Body : params . data ,
153255 ContentType : params . contentType ,
154- ContentEncoding : "UTF-8" ,
256+ ContentEncoding : params . contentEncoding || "UTF-8" ,
155257 ACL : "public-read"
156258 } , ( err , data ) => err ? params . reject ( err ) : params . resolve ( data . Location ) )
157259}
0 commit comments