-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathindex.js
305 lines (269 loc) · 8.72 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
const fs = require( 'fs' );
const path = require( 'path' );
const launchui = require( 'launchui' );
const extract = require( 'extract-zip' );
const archiver = require( 'archiver' );
const rimraf = require( 'rimraf' );
const mkdirp = require( 'mkdirp' );
const rcedit = require( 'rcedit' );
const glob = require( 'glob' );
const plist = require( 'plist' );
function packager( opts, callback ) {
const { name, version, entry } = opts;
if ( typeof name !== 'string' || name === '' )
throw new TypeError( 'Invalid or missing option: name' );
if ( typeof version !== 'string' || version === '' )
throw new TypeError( 'Invalid or missing option: version' );
if ( typeof entry !== 'string' || entry === '' )
throw new TypeError( 'Invalid or missing option: entry' );
const {
out = '.',
launchuiOpts = {},
platform = process.platform,
arch = process.arch,
overwrite = false,
pack = false,
company,
copyright,
identifier,
category,
icon,
license,
dir,
files = '**'
} = opts;
const targetPath = path.resolve( out );
if ( pack !== 'zip' && pack !== false )
throw new TypeError( 'Invalid value of option: pack' );
console.log( 'Packaging ' + name + ' version ' + version + ' for ' + platform + '-' + arch );
const dirName = name + '-v' + version + '-' + platform + '-' + arch;
const zipName = dirName + '.zip';
const dirPath = path.join( targetPath, dirName );
const zipPath = path.join( targetPath, zipName );
let appPath;
if ( !overwrite ) {
if ( pack && fs.existsSync( zipPath ) ) {
console.log( 'Output package already exists: ' + zipName );
return callback( null, zipPath );
}
if ( fs.existsSync( dirPath ) ) {
console.log( 'Output directory already exists: ' + dirName );
return finalize();
}
}
Object.assign( launchuiOpts, { platform, arch } );
launchui.download( launchuiOpts, ( error, lauchuiPath ) => {
if ( error != null )
return callback( error, null );
if ( fs.existsSync( dirPath ) ) {
rimraf( dirPath, error => {
if ( error != null )
return callback( error, null );
makePackageDir( lauchuiPath );
} );
} else {
makePackageDir( lauchuiPath );
}
} );
function makePackageDir( lauchuiPath ) {
if ( !fs.existsSync( dirPath ) ) {
mkdirp( dirPath, error => {
if ( error != null )
return callback( error, null );
extractPackage( lauchuiPath );
} );
} else {
extractPackage( lauchuiPath );
}
}
function extractPackage( lauchuiPath ) {
console.log( 'Extracting ' + path.basename( lauchuiPath ) );
extract( lauchuiPath, { dir: dirPath }, error => {
if ( error != null )
return callback( error, null );
if ( platform == 'darwin' )
renameApplication();
else
renameExecutable();
} );
}
function renameApplication() {
const oldPath = path.join( dirPath, 'launchui.app' );
const newPath = path.join( dirPath, name + '.app' );
fs.rename( oldPath, newPath, error => {
if ( error != null )
return callback( error, null );
updatePlist();
} );
}
function updatePlist() {
const plistPath = path.join( dirPath, name + '.app/Contents/Info.plist' );
fs.readFile( plistPath, 'utf8', ( error, data ) => {
if ( error != null )
return callback( error, null );
const metadata = plist.parse( data );
metadata.CFBundleName = name;
metadata.CFBundleExecutable = name;
metadata.CFBundleIdentifier = identifier != null ? identifier : 'org.mimec.launchui.' + name;
metadata.CFBundleVersion = version;
metadata.CFBundleShortVersionString = version;
if ( copyright != null )
metadata.NSHumanReadableCopyright = copyright;
if ( category != null )
metadata.LSApplicationCategoryType = category;
const newData = plist.build( metadata );
fs.writeFile( plistPath, newData, 'utf8', error => {
if ( error != null )
return callback( error, null );
renameExecutable();
} );
} );
}
function renameExecutable() {
let oldPath, newPath;
if ( platform == 'win32' ) {
oldPath = path.join( dirPath, 'launchui.exe' );
newPath = path.join( dirPath, name + '.exe' );
} else if ( platform == 'darwin' ) {
oldPath = path.join( dirPath, name + '.app/Contents/MacOS/launchui' );
newPath = path.join( dirPath, name + '.app/Contents/MacOS/' + name );
} else {
oldPath = path.join( dirPath, 'launchui' );
newPath = path.join( dirPath, name );
}
fs.rename( oldPath, newPath, error => {
if ( error != null )
return callback( error, null );
if ( platform == 'win32' )
callRcedit();
else if ( platform == 'darwin' )
copyIcns();
else
copyEntryScript();
} );
}
function callRcedit() {
const exePath = path.join( dirPath, name + '.exe' );
const versionString = {
'FileDescription': name,
'OriginalFilename': name + '.exe',
'ProductName': name
};
if ( company != null )
versionString[ 'CompanyName' ] = company;
if ( copyright != null )
versionString[ 'LegalCopyright' ] = copyright;
rcedit( exePath, { 'version-string': versionString, 'file-version': version, 'product-version': version, icon }, error => {
if ( error != null )
return callback( error, null );
copyEntryScript();
} );
}
function copyIcns() {
if ( icon != null ) {
const destPath = path.join( dirPath, name + '.app/Contents/Resources/launchui.icns' );
fs.copyFile( icon, destPath, error => {
if ( error != null )
return callback( error, null );
copyEntryScript();
} );
} else {
copyEntryScript();
}
}
function copyEntryScript() {
if ( platform == 'darwin' )
appPath = path.join( dirPath, name + '.app/Contents/Resources/app' );
else
appPath = path.join( dirPath, 'app' );
const destPath = path.join( appPath, 'main.js' );
fs.copyFile( entry, destPath, error => {
if ( error != null )
return callback( error, null );
copyLicense();
} );
}
function copyLicense() {
if ( license != null ) {
const destPath = path.join( dirPath, 'LICENSE' );
fs.copyFile( license, destPath, error => {
if ( error != null )
return callback( error, null );
copyDirectory();
} );
} else {
copyDirectory();
}
}
function copyDirectory() {
if ( dir != null ) {
const srcDir = path.resolve( dir );
const filesArray = Array.isArray( files ) ? files : [ files ];
findFiles( 0 );
function findFiles( fileIndex ) {
if ( fileIndex < filesArray.length ) {
glob( filesArray[ fileIndex ], { cwd: srcDir, nodir: true }, ( error, matches ) => {
if ( error != null )
return callback( error, null );
processFile( matches, 0, fileIndex );
} );
} else {
finalize();
}
}
function processFile( matches, matchIndex, fileIndex ) {
if ( matchIndex < matches.length ) {
const srcPath = path.join( srcDir, matches[ matchIndex ] );
const destPath = path.join( appPath, matches[ matchIndex ] );
const destDir = path.dirname( destPath );
if ( fs.existsSync( destDir ) ) {
copyFile( srcPath, destPath, matches, matchIndex, fileIndex );
} else {
mkdirp( destDir, error => {
if ( error != null )
return callback( error, null );
copyFile( srcPath, destPath, matches, matchIndex, fileIndex );
} );
}
} else {
findFiles( fileIndex + 1 );
}
}
function copyFile( srcPath, destPath, matches, matchIndex, fileIndex ) {
fs.copyFile( srcPath, destPath, error => {
if ( error != null )
return callback( error, null );
processFile( matches, matchIndex + 1, fileIndex );
} );
}
} else {
finalize();
}
}
function finalize() {
if ( pack )
packZip();
else
callback( null, dirPath );
}
function packZip() {
console.log( 'Packing ' + zipName );
let output, archive;
try {
output = fs.createWriteStream( path.join( zipPath ) );
archive = archiver( 'zip', { zlib: { level: 9 } } );
archive.pipe( output );
} catch ( error ) {
return callback( error, null );
}
output.on( 'close', () => {
callback( null, zipPath );
} );
archive.on( 'error', error => {
callback( error, null );
} );
archive.directory( dirPath, false );
archive.finalize();
}
}
module.exports = packager;