OpenAsar/src/firstRun/win32.js

77 lines
2.3 KiB
JavaScript
Raw Normal View History

2021-12-13 08:11:10 +00:00
const fs = require('fs');
2022-01-29 11:12:39 +00:00
const { join, resolve, basename } = require('path');
2021-12-13 08:11:10 +00:00
const paths = require('../paths');
const windowsUtils = require('../utils/windowsUtils');
2021-12-13 08:11:10 +00:00
const Constants = require('../Constants');
2022-01-29 11:12:39 +00:00
const appPath = resolve(process.execPath, '..');
const rootPath = resolve(appPath, '..');
2021-12-13 08:11:10 +00:00
const iconFile = 'app.ico';
const copyIconToRoot = () => {
2022-01-29 11:12:39 +00:00
const currentPath = join(appPath, iconFile);
const newPath = join(rootPath, iconFile);
2021-12-13 08:11:10 +00:00
try {
fs.copyFileSync(currentPath, newPath);
return newPath;
} catch (e) {
log('FirstRun', 'Failed to copy icon to root', e);
return currentPath;
}
};
const updateShortcuts = (updater) => {
const filename = Constants.APP_NAME_FOR_HUMANS + '.lnk';
const paths = [
2022-01-29 11:12:39 +00:00
join(updater.getKnownFolder('desktop'), filename),
join(updater.getKnownFolder('programs'), Constants.APP_COMPANY, filename)
2021-12-13 08:11:10 +00:00
];
const icon = copyIconToRoot();
for (const path of paths) {
if (!fs.existsSync(path)) continue; // Don't update already deleted paths
updater.createShortcut({
2022-01-29 11:12:39 +00:00
target_path: join(rootPath, 'Update.exe'),
shortcut_path: path,
2022-01-29 11:12:39 +00:00
arguments: '--processStart ' + basename(process.execPath),
2021-12-13 08:11:10 +00:00
icon_path: icon,
icon_index: 0,
description: Constants.APP_DESCRIPTION,
app_user_model_id: Constants.APP_ID,
working_directory: appPath
});
}
};
2022-01-29 11:12:39 +00:00
const installProtocol = (protocol, callback) => {
const base = 'HKCU\\Software\\Classes\\' + protocol;
windowsUtils.addToRegistry([[base, '/ve', '/d', `URL:${protocol} Protocol`], [base, '/v', 'URL Protocol'], [base + '\\DefaultIcon', '/ve', '/d', '"' + process.execPath + '",-1'], [base + '\\shell\\open\\command', '/ve', '/d', `"${process.execPath}" --url -- "%1"`]], callback);
};
2021-12-13 08:11:10 +00:00
exports.performFirstRunTasks = (updater) => {
log('FirstRun', 'Perform');
2022-01-29 11:12:39 +00:00
const flagPath = join(paths.getUserDataVersioned(), '.first-run');
if (fs.existsSync(flagPath)) return; // Already done, skip
2021-12-13 08:11:10 +00:00
let shortcutSuccess = false;
try {
updateShortcuts(updater);
shortcutSuccess = true;
} catch (e) {
log('FirstRun', 'Error updating shortcuts', e);
}
installProtocol(Constants.APP_PROTOCOL, () => {
if (!shortcutSuccess) return;
try {
2022-02-16 09:10:18 +00:00
fs.writeFileSync(flagPath, 'true');
} catch (e) {
log('FirstRun', 'Error writing .first-run', e);
2021-12-13 08:11:10 +00:00
}
});
2022-01-29 11:12:39 +00:00
};