Skip to content

Commit

Permalink
feat: add support for launchd (#25)
Browse files Browse the repository at this point in the history
  • Loading branch information
derevnjuk authored Aug 14, 2023
1 parent 15aec51 commit 21794a1
Show file tree
Hide file tree
Showing 5 changed files with 303 additions and 86 deletions.
80 changes: 52 additions & 28 deletions example/periodic-logger.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,44 +9,68 @@ const service = require('../');
const fs = require('fs');

function usage() {
console.log(
'usage: node periodic-logger --add <name> [username] [password] [dep dep ...]'
);
console.log(' node periodic-logger --remove <name>');
console.log(' node periodic-logger --run');
process.exit(-1);
console.log(
'usage: node periodic-logger --add <name> [username] [password] [dep dep ...]'
);
console.log(' node periodic-logger --remove <name>');
console.log(' node periodic-logger --run');
process.exit(-1);
}

process.title = process.argv[3];

if (process.argv[2] == '--add' && process.argv.length >= 4) {
const options = {
args: [process.argv[1], '--run', 'me']
};
const options = {
args: [process.argv[1], '--run', 'me']
};

if (process.argv.length > 4) {
options.username = process.argv[4];
}

if (process.argv.length > 4) {options.username = process.argv[4];}
if (process.argv.length > 5) {
options.password = process.argv[5];
}

if (process.argv.length > 5) {options.password = process.argv[5];}
if (process.argv.length > 6) {
options.dependencies = process.argv.splice(6);
}

if (process.argv.length > 6) {options.dependencies = process.argv.splice(6);}
service.add(process.argv[3], options, (error) => {
if (error) {
return console.error(error);
}

service.add(process.argv[3], options, (error) => {
if (error) {console.log(error.toString());}
});
service.enable(process.argv[3], (error) => {
if (error) {
console.error(error);
}
})
});
} else if (process.argv[2] == '--remove' && process.argv.length >= 4) {
service.remove(process.argv[3], (error) => {
if (error) {console.log(error.toString());}
});
service.disable(process.argv[3], (error) => {
if (error) {
return console.error(error);
}

service.remove(process.argv[3], (error) => {
if (error) {
console.error(error);
}
});
})
} else if (process.argv[2] == '--run') {
service.run(() => {
service.stop(0);
});
service.run(() => {
service.stop(0);
});

const logStream = fs.createWriteStream(process.argv[1] + '.log');
const logStream = fs.createWriteStream(process.argv[1] + '.log');

// Here is our long running code, simply print a date/time string to
// our log file
setInterval(() => {
logStream.write(new Date().toString() + '\n');
}, 1000);
// Here is our long running code, simply print a date/time string to
// our log file
setInterval(() => {
logStream.write(new Date().toString() + '\n');
}, 1000);
} else {
usage();
usage();
}
23 changes: 23 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,29 @@ export function add(name: string, callback?: (error?: Error) => void): void;
*/
export function remove(name: string, callback: (error?: Error) => void): void;

/**
* The enable() function enable a service.
* The service must be in a stopped state for it to be enabled.
* @param name Specifies the name of the service to enable.
* This will be the same name parameter specified when adding the service.
* @param callback Is called once the service has been enabled. The following
* arguments will be passed to the callback function:
* error - Instance of the Error class, or null if no error occurred
*/
export function enable(name: string, callback: (error?: Error) => void): void;

/**
* The disable() function disable a service.
* The service must be in a running state for it to be disabled.
* @param name Specifies the name of the service to disable.
* This will be the same name parameter specified when adding the service.
* @param callback Is called once the service has been disabled. The following
* arguments will be passed to the callback function:
* error - Instance of the Error class, or null if no error occurred
*/
export function disable(name: string, callback: (error?: Error) => void): void;


/**
* The run() function will attempt to run the program as a service.
* @param stopCallback Will be called when the service receives a stop request,
Expand Down
Loading

0 comments on commit 21794a1

Please sign in to comment.