forked from nwutils/nw-builder
-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.js
194 lines (170 loc) · 6.49 KB
/
utils.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
var fs = require('fs-extra');
var path = require('path');
var Promise = require('bluebird');
var plist = require('plist');
var Glob = require('simple-glob');
var temp = require('temp');
var archiver = require('archiver');
// Automatically track and cleanup files at exit
temp.track();
module.exports = {
getPackageInfo: function(path) {
return new Promise(function(resolve, reject) {
fs.readFile(path, function (err, data) {
if (err) return reject(err);
try {
var appPkg = JSON.parse(data);
} catch(e) {
reject("Invalid package.json: " + e + "\nMake sure the file is encoded as utf-8");
return;
}
if (!appPkg.name || !appPkg.version) {
reject("Please make sure that your project's package.json includes a version and a name value");
} else {
resolve(appPkg);
}
});
});
},
getFileList: function(fileglob) {
var self = this,
jsonfile, destFiles = [],
srcFiles = [],
package_path,
matches = Glob(fileglob);
return new Promise(function(resolve, reject) {
if(!matches.length) return reject('No files matching');
matches.forEach(function(file) {
var internalFileName = path.normalize(file);
if (internalFileName.match('package.json')) {
jsonfile = self.closerPathDepth(internalFileName, jsonfile);
package_path = path.normalize(jsonfile.split('package.json')[0] || './');
}
if(!fs.lstatSync(internalFileName).isDirectory()) {
srcFiles.push(internalFileName);
}
});
if (!jsonfile) {
return reject('Could not find a package.json in your src folder');
}
srcFiles.forEach(function(file) {
destFiles.push({
src: file,
dest: file.replace(package_path, '')
});
});
resolve({
files: destFiles,
json: jsonfile
});
});
},
closerPathDepth: function(path1, path2) {
if (!path2) { return path1; }
var d1 = this.pathDepth(path1),
d2 = this.pathDepth(path2);
return d1 < d2 ? path1 : path2;
},
pathDepth: function(absolutePath) {
return absolutePath.split(path.sep).length;
},
copyFile: function (src, dest) {
return new Promise(function(resolve, reject) {
var stats = fs.lstatSync(src);
fs.copy(src, dest, function (err) {
if(err) return reject(err);
fs.chmodSync(dest, stats.mode);
resolve();
});
});
},
mergeFiles: function (app, zipfile, chmod) {
// we need to pipe the app into the zipfile and chmod it
return new Promise(function(resolve, reject) {
var zipStream = fs.createReadStream(zipfile),
writeStream = fs.createWriteStream(app, {flags:'a'});
zipStream.on('error', reject);
writeStream.on('error', reject);
writeStream.on('finish', function () {
if(chmod) {
fs.chmodSync(app, chmod);
}
resolve();
});
zipStream.pipe(writeStream);
});
},
generateZipFile: function (files, _event) {
var destStream = temp.createWriteStream(),
archive = archiver('zip');
return new Promise(function(resolve, reject) {
// Resolve on close
destStream.on('close', function () {
resolve(destStream.path);
});
// Reject on Error
archive.on('error', reject);
// Add the files
files.forEach(function (file) {
archive.file(file.src, { name:file.dest });
});
// Some logs
archive.on('entry', function (file) {
_event.emit('log', 'Zipping ' + file.name);
});
// Pipe the stream
archive.pipe(destStream);
archive.finalize();
});
},
editPlist: function (plistInput, plistOutput, options) {
options = options || {};
if(!options.appName || !options.appVersion) {
return Promise.reject('You have to set the appName and appVersion in the Plist options');
}
return new Promise(function(resolve, reject) {
// Handle the Info.plist file
var info = plist.parse(fs.readFileSync(plistInput, 'utf8') );
var keys = Object.keys(options);
if(!options.mac_document_types) {
info.CFBundleDocumentTypes = [];
}
info.UTExportedTypeDeclarations = [];
keys.forEach(function(key) {
var value = options[key];
switch(key) {
case 'appName':
info.CFBundleName = value;
info.CFBundleDisplayName = value;
break;
case 'appVersion':
info.CFBundleVersion = value;
info.CFBundleShortVersionString = 'Version ' + value;
break;
case 'copyright':
info.NSHumanReadableCopyright = value;
break;
case 'mac_bundle_id':
info.CFBundleIdentifier = value;
break;
case 'mac_document_types':
info.CFBundleDocumentTypes = value.map(function(type) {
return {
CFBundleTypeName: type.name,
CFBundleTypeExtensions: type.extensions,
CFBundleTypeRole: type.role,
LSIsAppleDefaultForType: type.isDefault
};
});
break;
default:
info[key] = value;
}
});
fs.writeFile(plistOutput, plist.build(info), function(err) {
if(err) { reject(err); }
else { resolve(); }
});
});
}
};