@notes-sync/service
Version:
Background service for AI-powered note synchronization
178 lines (169 loc) • 5.93 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.WindowsServiceManager = void 0;
const path = __importStar(require("path"));
const child_process_1 = require("child_process");
const fs = __importStar(require("fs"));
/**
* Windows service manager implementation
*/
class WindowsServiceManager {
/**
* Checks if node-windows is installed
*/
static checkDependencies() {
try {
require.resolve(this.NODE_WINDOWS_MODULE);
return true;
}
catch (e) {
return false;
}
}
/**
* Installs node-windows if not already installed
*/
static async installDependencies() {
if (!this.checkDependencies()) {
console.log('Installing node-windows package...');
(0, child_process_1.execSync)('npm install node-windows --save', { stdio: 'inherit' });
}
}
/**
* Creates and installs a Windows service
*/
static async install() {
try {
await this.installDependencies();
// Create a Windows service install script
const scriptPath = path.join(__dirname, '..', '..', 'install-windows-service.js');
// Generate the script content
const scriptContent = this.generateServiceScript();
// Write the script to file
fs.writeFileSync(scriptPath, scriptContent);
// Execute the script
console.log('Installing Notes Sync as a Windows service...');
(0, child_process_1.execSync)(`node "${scriptPath}"`, { stdio: 'inherit' });
// Clean up the temporary script
fs.unlinkSync(scriptPath);
console.log('Windows service installed successfully!');
}
catch (error) {
console.error('Failed to install Windows service:', error);
throw error;
}
}
/**
* Uninstalls the Windows service
*/
static async uninstall() {
try {
await this.installDependencies();
// Create an uninstall script
const scriptPath = path.join(__dirname, '..', '..', 'uninstall-windows-service.js');
// Generate the uninstall script content
const scriptContent = this.generateUninstallScript();
// Write the script to file
fs.writeFileSync(scriptPath, scriptContent);
// Execute the script
console.log('Uninstalling Notes Sync Windows service...');
(0, child_process_1.execSync)(`node "${scriptPath}"`, { stdio: 'inherit' });
// Clean up the temporary script
fs.unlinkSync(scriptPath);
console.log('Windows service uninstalled successfully!');
}
catch (error) {
console.error('Failed to uninstall Windows service:', error);
throw error;
}
}
/**
* Generates a script for installing the Windows service
*/
static generateServiceScript() {
return `const Service = require('node-windows').Service;
const path = require('path');
// Create a new service object
const svc = new Service({
name: '${this.SERVICE_NAME}',
description: 'Notes Sync Service - Auto-syncs and manages your notes',
script: path.join(__dirname, 'dist', 'main.js'),
nodeOptions: [],
workingDirectory: __dirname,
allowServiceLogon: true
});
// Listen for service install events
svc.on('install', () => {
console.log('Service installed successfully');
svc.start();
});
svc.on('start', () => {
console.log('Service started');
});
svc.on('error', (err) => {
console.error('Service error:', err);
});
// Install the service
svc.install();
`;
}
/**
* Generates a script for uninstalling the Windows service
*/
static generateUninstallScript() {
return `const Service = require('node-windows').Service;
const path = require('path');
// Create a new service object
const svc = new Service({
name: '${this.SERVICE_NAME}',
script: path.join(__dirname, 'dist', 'main.js')
});
// Listen for uninstall events
svc.on('uninstall', () => {
console.log('Service uninstalled successfully');
});
svc.on('error', (err) => {
console.error('Service uninstall error:', err);
});
// Uninstall the service
svc.uninstall();
`;
}
}
exports.WindowsServiceManager = WindowsServiceManager;
WindowsServiceManager.SERVICE_NAME = 'NotesSyncService';
WindowsServiceManager.NODE_WINDOWS_MODULE = 'node-windows';
//# sourceMappingURL=windows-service-manager.js.map