This repository has been archived by the owner on Jan 7, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathpost-packaging.js
136 lines (107 loc) · 3.78 KB
/
post-packaging.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
#!/usr/bin/env node
const exec = require('child_process').exec;
const path = require('path');
const fs = require('fs-extra');
const archiver = require('archiver');
const commandLineArgs = require('command-line-args')
const crypto = require('crypto');
const nodeFs = require('fs');
const env = process.env.NODE_ENV || 'production';
const isBuildingDev = /^dev/.test( env );
const optionDefinitions = [
{ name: 'example', alias: 'e', type: String,defaultOption: true, defaultValue: 'web_hosting_manager' },
]
const options = commandLineArgs(optionDefinitions)
const pkg = require(`./package.json`);
let targetDir = path.resolve( __dirname, 'release');
const platform = process.platform;
const OSX = 'darwin';
const LINUX = 'linux';
const WINDOWS = 'win32';
const LOG_TOML = path.resolve( __dirname, 'log.toml');
const pkgName = pkg.name;
let PLATFORM_NAME;
let releaseFolder;
let dirForLog;
if (platform === OSX) {
releaseFolder = path.resolve( targetDir, 'mac');
dirForLog = path.resolve(releaseFolder, `${pkgName}.app/Contents/Frameworks/${pkgName} Helper.app/Contents/Resources`);
PLATFORM_NAME = 'osx';
fs.ensureDirSync(dirForLog);
}
if (platform === LINUX ) {
releaseFolder = path.resolve(targetDir, 'linux-unpacked');
dirForLog = releaseFolder;
PLATFORM_NAME = LINUX;
fs.ensureDirSync(dirForLog);
}
if (platform === WINDOWS ) {
releaseFolder = path.resolve(targetDir, 'win-unpacked');
dirForLog = releaseFolder;
PLATFORM_NAME = 'win';
fs.ensureDirSync(dirForLog);
}
let devModifier = '';
if( isBuildingDev )
{
devModifier = '-dev'
}
const RELEASE_FOLDER_NAME = `${pkgName}-v${pkg.version}-${PLATFORM_NAME}-x64${devModifier}`;
// Add log where it's needed
fs.copySync(LOG_TOML, `${dirForLog}/log.toml`, { overwrite: true } );
//add version file
fs.outputFileSync(path.resolve(releaseFolder, 'version'), pkg.version);
// remove licenses
const removalArray = ['LICENSE.electron.txt','LICENSES.chromium.html', 'LICENSE'];
removalArray.forEach( ( file) =>
{
fs.removeSync( `${releaseFolder}/${file}` );
})
console.log("Renaming package to:", path.resolve( targetDir,`${RELEASE_FOLDER_NAME}` ));
//rename release folder
fs.moveSync( releaseFolder, path.resolve( targetDir,`${RELEASE_FOLDER_NAME}` ), { overwrite: true } );
// create a file to stream archive data to.
var output = fs.createWriteStream( path.resolve( targetDir, `${RELEASE_FOLDER_NAME}.zip` ) );
var archive = archiver('zip', {
zlib: { level: 9 } // Sets the compression level.
});
// listen for all archive data to be written
output.on('close', function() {
console.log(archive.pointer() + ' total bytes');
console.log('archiver has been finalized and the output file descriptor has closed.');
// remove osx junk
if (platform === OSX) {
console.log('run to remove zipped nonsense: zip -d release/web-hosting-manager-v0.4.1-osx-x64.zip *.DS_Store');
exec(`zip -d ${targetDir}/${RELEASE_FOLDER_NAME}.zip *.DS_Store`, (error, stdout, stderr) => {
if (error) {
console.error(`exec error: ${error}`);
return;
}
console.log(`stdout: ${stdout}`);
console.log(`stderr: ${stderr}`);
});
}
});
console.log("Zipping...");
archive.on('warning', function(err) {
if (err.code === 'ENOENT') {
console.warn( err )
} else {
console.error( err )
throw err;
}
});
// pipe archive data to the file
archive.pipe(output);
archive.directory(`${targetDir}/${RELEASE_FOLDER_NAME}`, RELEASE_FOLDER_NAME);
archive.finalize().then(() => {
const algorithm = 'sha256';
const shasum = crypto.createHash(algorithm);
const filePath = `${targetDir}/${RELEASE_FOLDER_NAME}.zip`;
const input = nodeFs.createReadStream(filePath);
input.on('data', (d) => { shasum.update(d); });
input.on('end', () => {
const d = shasum.digest('hex');
fs.writeFileSync(`${targetDir}/${RELEASE_FOLDER_NAME}.txt`, d);
});
});