-
Notifications
You must be signed in to change notification settings - Fork 302
/
osx.js
189 lines (167 loc) · 6.36 KB
/
osx.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
import console from 'node:console';
import fs from 'node:fs';
import path from 'node:path';
import process from 'node:process';
import plist from 'plist';
/**
* Function to update Helper App Plist Files
* @param {string} plistPath - Path to Helper App Plist File
* @param {string} helperName - Helper App Name
* @param {string} helperId - Helper App ID
* @param {string} appCFBundleIdentifier - options.app.CFBundleIdentifier
*/
async function updateHelperPlist (plistPath, helperName, helperId, appCFBundleIdentifier) {
const plistFullPath = path.resolve(plistPath, 'Contents/Info.plist');
const plistJson = plist.parse(await fs.promises.readFile(plistFullPath, 'utf-8'));
plistJson.CFBundleDisplayName = helperName;
plistJson.CFBundleName = helperName;
plistJson.CFBundleExecutable = helperName;
plistJson.CFBundleIdentifier = `${appCFBundleIdentifier}.${helperId}`;
await fs.promises.writeFile(plistFullPath, plist.build(plistJson));
}
/**
*
* @param {object} options - Options.
* @param {object} options.app - Application configuration.
* @param {string} options.outDir - Output directory.
* @param {string} options.releaseInfo - Release information.
* @returns {Promise<void>} - Promise.
*/
export default async function setOsxConfig({ app, outDir, releaseInfo }) {
if (process.platform === 'win32') {
console.warn(
'MacOS apps built on Windows platform do not preserve all file permissions. See #716',
);
}
try {
/**
* @type {string | null}
*/
const chromiumVersion = releaseInfo?.components?.chromium;
if (!chromiumVersion) {
throw new Error('Chromium version is missing.');
}
/**
* Path to MacOS application.
* @type {string}
*/
const nwjsApp = path.resolve(outDir, 'nwjs.app');
/**
* Path to renamed MacOS application.
* @type {string}
*/
const outApp = path.resolve(outDir, `${app.name}.app`);
/* Rename `nwjs.app` to `${app.name}.app` */
await fs.promises.rename(nwjsApp, outApp);
/* Rename `Contents/MacOS/nwjs` to `Contents/MacOS/${app.name}` */
await fs.promises.rename(
path.resolve(outApp, 'Contents', 'MacOS', 'nwjs'),
path.resolve(outApp, 'Contents', 'MacOS', app.name),
);
/* Rename all Helper apps */
const helperBaseDir = path.resolve(
outApp,
'Contents/Frameworks/nwjs Framework.framework/Versions',
chromiumVersion,
'Helpers/'
);
const helperApps = [
{ name: 'nwjs Helper (Alerts).app', id: 'helper.alert' },
{ name: 'nwjs Helper (GPU).app', id: 'helper.gpu' },
{ name: 'nwjs Helper (Plugin).app', id: 'helper.plugin' },
{ name: 'nwjs Helper (Renderer).app', id: 'helper.renderer' },
{ name: 'nwjs Helper.app', id: 'helper' },
];
for (const helperApp of helperApps) {
const newHelperAppName = helperApp.name.replace(/^nwjs/, app.name);
const oldPath = path.resolve(helperBaseDir, helperApp.name);
const newPath = path.resolve(helperBaseDir, newHelperAppName);
// Rename Helper base directory
await fs.promises.rename(oldPath, newPath);
// Rename Helper sub-directory
const helperBaseName = helperApp.name.replace(/.app$/, '');
const subPathBase = path.resolve(newPath, 'Contents/MacOS/');
const oldSubPath = path.resolve(subPathBase, helperBaseName);
const newSubPath = path.resolve(subPathBase, helperBaseName.replace(/^nwjs/, app.name));
await fs.promises.rename(oldSubPath, newSubPath);
// Update Helper Plist file
await updateHelperPlist(newPath, newHelperAppName.replace(/.app$/, ''), helperApp.id, app.CFBundleIdentifier);
}
/* Replace default icon with user defined icon if specified. */
if (app.icon !== undefined) {
await fs.promises.copyFile(
path.resolve(app.icon),
path.resolve(outApp, 'Contents', 'Resources', 'app.icns'),
);
}
/**
* Path to `nwjs.app/Contents/Info.plist`
* @type {string}
*/
const contentsInfoPlistPath = path.resolve(
outApp,
'Contents',
'Info.plist'
);
/**
* Path to `nwjs.app/Contents/Resources/en.lproj/InfoPlist.settings`
* @type {string}
*/
const contentsResourcesEnLprojInfoPlistStringsPath = path.resolve(
outApp,
'Contents',
'Resources',
'en.lproj',
'InfoPlist.strings',
);
/**
* JSON from `nwjs.app/Contents/Info.plist`
* @type {object}
*/
const contentsInfoPlistJson = plist.parse(
await fs.promises.readFile(
contentsInfoPlistPath,
'utf-8'
)
);
/* Update Plist with user defined values. */
contentsInfoPlistJson.LSApplicationCategoryType = app.LSApplicationCategoryType;
contentsInfoPlistJson.CFBundleIdentifier = app.CFBundleIdentifier;
contentsInfoPlistJson.CFBundleName = app.CFBundleName;
contentsInfoPlistJson.CFBundleDisplayName = app.CFBundleDisplayName;
contentsInfoPlistJson.CFBundleSpokenName = app.CFBundleSpokenName;
contentsInfoPlistJson.CFBundleVersion = app.CFBundleVersion;
contentsInfoPlistJson.CFBundleShortVersionString = app.CFBundleShortVersionString;
contentsInfoPlistJson.CFBundleExecutable = app.name;
/* Remove properties that were not updated by the user. */
Object.keys(contentsInfoPlistJson).forEach((option) => {
if (contentsInfoPlistJson[option] === undefined) {
delete contentsInfoPlistJson[option];
}
});
/**
* Data from `nwjs.app/Contents/Resources/en.lproj/InfoPlist.settings`
* @type {string[]}
*/
const contentsResourcesEnLprojInfoPlistStringsArray = (await fs.promises.readFile(
contentsResourcesEnLprojInfoPlistStringsPath,
'utf-8',
)).split('\n');
contentsResourcesEnLprojInfoPlistStringsArray.forEach((line, idx, arr) => {
if (line.includes('NSHumanReadableCopyright')) {
arr[idx] =
`NSHumanReadableCopyright = "${app.NSHumanReadableCopyright}";`;
}
});
/* Write the updated values to their config files. */
await fs.promises.writeFile(
contentsInfoPlistPath,
plist.build(contentsInfoPlistJson));
await fs.promises.writeFile(
contentsResourcesEnLprojInfoPlistStringsPath,
contentsResourcesEnLprojInfoPlistStringsArray.toString().replace(/,/g, '\n'),
);
} catch (error) {
console.error(error);
}
};