1+ const path = require ( 'path' ) ;
2+ const fs = require ( 'fs' ) ;
3+ const jszip = require ( 'jszip' ) ;
4+
5+ const iconFile = path . join ( __dirname , '../icon.png' ) ;
6+ const pluginJSON = path . join ( __dirname , '../plugin.json' ) ;
7+ const distFolder = path . join ( __dirname , '../dist' ) ;
8+ let readmeDotMd = path . join ( __dirname , '../readme.md' ) ;
9+
10+ if ( ! fs . existsSync ( readmeDotMd ) ) {
11+ readmeDotMd = path . join ( __dirname , '../README.md' ) ;
12+ }
13+
14+ // create zip file of dist folder
15+
16+ const zip = new jszip ( ) ;
17+
18+ zip . file ( 'icon.png' , fs . readFileSync ( iconFile ) ) ;
19+ zip . file ( 'plugin.json' , fs . readFileSync ( pluginJSON ) ) ;
20+ zip . file ( 'readme.md' , fs . readFileSync ( readmeDotMd ) ) ;
21+
22+ loadFile ( '' , distFolder ) ;
23+
24+ zip
25+ . generateNodeStream ( { type : 'nodebuffer' , streamFiles : true } )
26+ . pipe ( fs . createWriteStream ( path . join ( __dirname , '../dist.zip' ) ) )
27+ . on ( 'finish' , ( ) => {
28+ console . log ( 'dist.zip written.' ) ;
29+ } ) ;
30+
31+ function loadFile ( root , folder ) {
32+ const distFiles = fs . readdirSync ( folder ) ;
33+ distFiles . forEach ( ( file ) => {
34+
35+ const stat = fs . statSync ( path . join ( folder , file ) ) ;
36+
37+ if ( stat . isDirectory ( ) ) {
38+ zip . folder ( file ) ;
39+ loadFile ( path . join ( root , file ) , path . join ( folder , file ) ) ;
40+ return ;
41+ }
42+
43+ if ( ! / L I C E N S E .t x t / . test ( file ) ) {
44+ zip . file ( path . join ( root , file ) , fs . readFileSync ( path . join ( folder , file ) ) ) ;
45+ }
46+ } ) ;
47+ }
0 commit comments