-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
132 lines (120 loc) · 5.05 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
import { exec } from 'child_process'
import { arch, type } from 'os'
import { readFileSync, writeFileSync, existsSync, mkdirSync, unlinkSync } from 'fs'
import path from 'path';
import { fileURLToPath } from 'url';
// funny debug variable
let debug = false;
let isWin = false;
// Get os type & arch to find out which binary to use.
const __dirname = path.dirname(fileURLToPath(import.meta.url)); // Get dir containing our ./bin/ folder
let bin = __dirname + "/bin/zsign_"
switch (type()) {
case "Linux":
bin += "linux_"
break;
case "Darwin":
bin += "macos_"
break;
case "Windows_NT" || "Windows":
bin += "win_"
isWin = true
break;
default:
throw new Error("OS Type not supported: We don't have a binary for zsign for your OS.")
}
switch (arch()) {
case "arm64":
bin += "arm64"
break;
case "x64":
bin += "x64"
break;
default:
throw new Error("Arch not supported: We don't have a binary for zsign for your arch.")
}
if(isWin) bin += ".exe" // Add exe extension when on windows.
// If we don't have a binary for the current os/arch combination, throw an error.
if(!existsSync(bin)) throw new Error("Binary not found: We don't have a binary for zsign for your OS/arch combination.")
// Handles running the zsign binary.
const runCommand = (args, callback) => {
// if debug, print out the bin string.
if(debug) console.log("[zsignjs] Binary:", bin)
// construct and run the command, and then pass the output to the provided callback function.
const cmd = `${bin} ${args.join(' ')}`;
exec(cmd, (error, stdout, stderr) => {
if (error) {
callback(error, null);
return;
}
callback(null, stdout || stderr);
});
};
// zsign object
export const zsign = {
/**
* Prints help, Not really useful except for CLI usage cases.
* @param {Function} callback
*/
showHelp: (callback) => {
runCommand(['--help'], callback);
},
/**
* Prints out version information
* @param {Function} callback
*/
getVersion: (callback) => {
runCommand(['-v'], callback);
},
/**
* Signs a package with the given options.
*
* @param {string} inputPackage - The package to be signed.
* @param {Object} [options={}] - The signing options. REQUIRED
* @param {string} [options.pkey] - The private key for signing. REQUIRED
* @param {string} [options.prov] - The provisioning profile. REQUIRED
* @param {string} [options.cert] - The certificate for signing. REQUIRED
* @param {boolean} [options.debug] - Enable debug mode.
* @param {boolean} [options.force] - Force sign without cache when signing folder.
* @param {string} [options.output] - The output path for the signed package.
* @param {string} [options.password] - The password for the private key. REQUIRED
* @param {string} [options.bundle_id] - The bundle identifier.
* @param {string} [options.bundle_name] - The bundle name.
* @param {string} [options.bundle_version] - The bundle version.
* @param {string} [options.entitlements] - The entitlements file.
* @param {number} [options.zip_level] - The compression level for the zip.
* @param {string} [options.dylib] - Path to the dynamic library to inject if any.
* @param {boolean} [options.weak] - Mark the dylib as weak.
* @param {boolean} [options.install] - Install the package after signing.
* @param {boolean} [options.quiet] - Suppress output.
* @param {Function} callback - The callback function to execute after signing.
*/
sign: (inputPackage, options = {}, callback) => {
const args = []
if (options.pkey) args.push('--pkey', options.pkey);
if (options.prov) args.push('--prov', options.prov);
if (options.cert) args.push('--cert', options.cert);
if (options.debug) args.push('--debug');
if (options.force) args.push('--force');
if (options.output) args.push('--output', options.output);
if (options.password) args.push('--password', options.password);
if (options.bundle_id) args.push('--bundle_id', options.bundle_id);
if (options.bundle_name) args.push('--bundle_name', options.bundle_name);
if (options.bundle_version) args.push('--bundle_version', options.bundle_version);
if (options.entitlements) args.push('--entitlements', options.entitlements);
if (options.zip_level !== undefined) args.push('--zip_level', options.zip_level);
if (options.dylib) args.push('--dylib', options.dylib);
if (options.weak) args.push('--weak');
if (options.install) args.push('--install');
if (options.quiet) args.push('--quiet');
if(existsSync(options.output)) {
if(options.force) {
unlinkSync(options.output)
} else {
throw new Error("Output file already exists, and the force option wasn't used.")
}
}
args.push(inputPackage)
runCommand(args, callback);
}
}