electron-winstore-auto-launch
Version:
Configure your Electron to start automatically when the user logs in Edit
141 lines (140 loc) • 4.6 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.WindowsStoreAutoLaunch = exports.StartupTaskState = void 0;
let appModel = null;
var StartupTaskState;
(function (StartupTaskState) {
StartupTaskState[StartupTaskState["disabled"] = 0] = "disabled";
StartupTaskState[StartupTaskState["disabledByUser"] = 1] = "disabledByUser";
StartupTaskState[StartupTaskState["enabled"] = 2] = "enabled";
})(StartupTaskState = exports.StartupTaskState || (exports.StartupTaskState = {}));
class WindowsStoreAutoLaunch {
/**
* Returns the windows.applicationModel namespace for this model. If the app is not
* running inside an appx container, this will fail.
*
* @static
* @param {(reason: string) => void} [reject]
* @returns
* @memberof WindowsStoreAutoLaunch
*/
static getAppModel(reject) {
try {
if (process.platform !== 'win32') {
if (reject) {
return reject(`Platform is not win32`);
}
else {
throw new Error('Platform is not win32');
}
}
return appModel || require('@nodert-win10-au/windows.applicationmodel');
}
catch (error) {
if (reject) {
return reject(error);
}
else {
throw error;
}
}
}
/**
* Returns all startupTasks for this package.
*
* @static
* @returns {Promise<Array<StartupTask>>}
* @memberof WindowsStoreAutoLaunch
*/
static getStartupTasks() {
return new Promise((resolve, reject) => {
const appModel = WindowsStoreAutoLaunch.getAppModel(reject);
appModel.StartupTask.getForCurrentPackageAsync((error, tasks) => {
if (error) {
return reject(error);
}
resolve(tasks);
});
});
}
/**
* Returns the first found startupTask.
*
* @static
* @returns {(Promise<StartupTask | null>)}
* @memberof WindowsStoreAutoLaunch
*/
static async getStartupTask() {
const startupTasks = await WindowsStoreAutoLaunch.getStartupTasks();
if (startupTasks && startupTasks.length > 0) {
return startupTasks[0];
}
else {
return null;
}
}
/**
* Returns a StartupTaskState if a StartupTask is found - or null if
* we could not find one.
*
* @static
* @returns {(Promise<StartupTaskState | null>)}
* @memberof WindowsStoreAutoLaunch
*/
static async getStatus() {
// We're assuming that this app has just one startup task, so let's go for it
const startupTask = await WindowsStoreAutoLaunch.getStartupTask();
if (startupTask) {
return startupTask.state;
}
else {
return null;
}
}
/**
* Takes the first startup task and attempts to disable it.
*
* @static
* @returns {(Promise<boolean | null>)}
* @memberof WindowsStoreAutoLaunch
*/
static async disable() {
// We're assuming that this app has just one startup task, so let's go for it
const startupTask = await WindowsStoreAutoLaunch.getStartupTask();
if (startupTask) {
startupTask.disable();
return true;
}
else {
return null;
}
}
/**
* Takes the first startup task and attempts to enable it.
*
* @static
* @returns {(Promise<StartupTaskState | null>)}
* @memberof WindowsStoreAutoLaunch
*/
static async enable() {
// We're assuming that this app has just one startup task, so let's go for it
function enableTask(startupTask) {
return new Promise((resolve, reject) => {
startupTask.requestEnableAsync((error, newStatus) => {
if (error) {
return reject(error);
}
resolve(newStatus);
});
});
}
const startupTask = await WindowsStoreAutoLaunch.getStartupTask();
if (startupTask) {
return enableTask(startupTask);
}
else {
return null;
}
}
}
exports.WindowsStoreAutoLaunch = WindowsStoreAutoLaunch;