user-startup
Advanced tools
Comparing version 0.2.1 to 0.2.2
@@ -5,2 +5,4 @@ 'use strict'; | ||
var platform = os.platform(); | ||
module.exports = require('./startup/' + platform + '.js'); | ||
var startup = require('./startup/' + platform + '.js'); | ||
module.exports = startup; |
'use strict'; | ||
Object.defineProperty(exports, '__esModule', { | ||
value: true | ||
}); | ||
exports.getFile = getFile; | ||
exports.add = add; | ||
exports.create = create; | ||
exports.remove = remove; | ||
var fs = require('fs'); | ||
@@ -17,3 +10,9 @@ var cp = require('child_process'); | ||
exports.dir = dir; | ||
module.exports = { | ||
dir: dir, | ||
getFile: getFile, | ||
add: add, | ||
create: create, | ||
remove: remove | ||
}; | ||
@@ -24,3 +23,6 @@ function getFile(name) { | ||
function add(name, cmd, args, out) { | ||
function add(name, cmd) { | ||
var args = arguments.length <= 2 || arguments[2] === undefined ? [] : arguments[2]; | ||
var out = arguments[3]; | ||
var array = [cmd].concat(args).map(function (a) { | ||
@@ -32,4 +34,12 @@ return ' <string>' + a + '</string>'; | ||
var data = ['<?xml version="1.0" encoding="UTF-8"?>', '<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">', '<plist version="1.0">', '<dict>', ' <key>Label</key>', ' <string>' + name + '</string>', ' <key>ProgramArguments</key>', ' <array>', array, ' </array>', ' <key>RunAtLoad</key>', ' <true/>', ' <key>StandardOutPath</key>', ' <string>' + out + '</string>', ' <key>StandardErrorPath</key>', ' <string>' + out + '</string>', '</dict>', '</plist>'].join('\n'); | ||
var lines = ['<?xml version="1.0" encoding="UTF-8"?>', '<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">', '<plist version="1.0">', '<dict>', ' <key>Label</key>', ' <string>' + name + '</string>', ' <key>ProgramArguments</key>', ' <array>', array, ' </array>', ' <key>RunAtLoad</key>', ' <true/>']; | ||
if (out) { | ||
lines.push(' <key>StandardOutPath</key>', ' <string>' + out + '</string>', ' <key>StandardErrorPath</key>', ' <string>' + out + '</string>'); | ||
} | ||
lines.push('</dict>', '</plist>'); | ||
var data = lines.join('\n'); | ||
mkdirp.sync(dir); | ||
@@ -40,3 +50,6 @@ fs.writeFileSync(file, data); | ||
function create(name, cmd, args, out) { | ||
function create(name, cmd) { | ||
var args = arguments.length <= 2 || arguments[2] === undefined ? [] : arguments[2]; | ||
var out = arguments[3]; | ||
var file = add(name, cmd, args, out); | ||
@@ -43,0 +56,0 @@ cp.execSync('launchctl load ' + file); |
'use strict'; | ||
Object.defineProperty(exports, '__esModule', { | ||
value: true | ||
}); | ||
exports.getFile = getFile; | ||
exports.add = add; | ||
exports.create = create; | ||
exports.remove = remove; | ||
var fs = require('fs'); | ||
@@ -17,9 +10,24 @@ var cp = require('child_process'); | ||
exports.dir = dir; | ||
function spawn(cmd, args, out) { | ||
var fd = fs.openSync(out, 'w'); | ||
cp.spawn(cmd, args, { | ||
stdio: ['ignore', fd, fd], | ||
module.exports = { | ||
dir: dir, | ||
getFile: getFile, | ||
add: add, | ||
create: create, | ||
remove: remove | ||
}; | ||
function spawn(cmd) { | ||
var args = arguments.length <= 1 || arguments[1] === undefined ? [] : arguments[1]; | ||
var out = arguments[2]; | ||
var opts = { | ||
detached: true | ||
}).on('error', console.log).unref(); | ||
}; | ||
if (out) { | ||
var fd = fs.openSync(out, 'w'); | ||
opts.stdio = ['ignore', fd, fd]; | ||
} | ||
cp.spawn(cmd, args, opts).on('error', console.log).unref(); | ||
} | ||
@@ -31,7 +39,20 @@ | ||
function add(name, cmd, args, out) { | ||
function add(name, cmd) { | ||
var args = arguments.length <= 2 || arguments[2] === undefined ? [] : arguments[2]; | ||
var out = arguments[3]; | ||
var file = getFile(name); | ||
var data = ['[Desktop Entry]', 'Type=Application', 'Vestion=1.0', 'Name=' + name, 'Comment=' + name + ' startup script', 'Exec=' + cmd + ' ' + args + ' > ' + out, 'StartupNotify=false', 'Terminal=false'].join('\n'); | ||
var command = cmd; | ||
if (args.length) { | ||
command += ' ' + args.join(' '); | ||
} | ||
if (out) { | ||
command += ' > ' + out; | ||
} | ||
var data = ['[Desktop Entry]', 'Type=Application', 'Vestion=1.0', 'Name=' + name, 'Comment=' + name + ' startup script', 'Exec=' + command, 'StartupNotify=false', 'Terminal=false'].join('\n'); | ||
mkdirp.sync(dir); | ||
@@ -42,3 +63,6 @@ fs.writeFileSync(file, data); | ||
function create(name, cmd, args, out) { | ||
function create(name, cmd) { | ||
var args = arguments.length <= 2 || arguments[2] === undefined ? [] : arguments[2]; | ||
var out = arguments[3]; | ||
add(name, cmd, args, out); | ||
@@ -45,0 +69,0 @@ spawn(cmd, args, out); |
'use strict'; | ||
Object.defineProperty(exports, '__esModule', { | ||
value: true | ||
}); | ||
exports.getFile = getFile; | ||
exports.add = add; | ||
exports.create = create; | ||
exports.remove = remove; | ||
var fs = require('fs'); | ||
@@ -17,3 +10,9 @@ var cp = require('child_process'); | ||
exports.dir = dir; | ||
module.exports = { | ||
dir: dir, | ||
getFile: getFile, | ||
add: add, | ||
create: create, | ||
remove: remove | ||
}; | ||
@@ -24,15 +23,23 @@ function getFile(name) { | ||
function add(name, cmd, args, out) { | ||
function add(name, cmd) { | ||
var args = arguments.length <= 2 || arguments[2] === undefined ? [] : arguments[2]; | ||
var out = arguments[3]; | ||
var file = getFile(name); | ||
var escapedCmd = '""' + cmd + '""'; | ||
var escapedArgs = args.map(function (a) { | ||
return '""' + a + '""'; | ||
}).join(' '); | ||
var escapedOut = '""' + out + '""'; | ||
var command = '""' + cmd + '""'; | ||
var command = '""' + escapedCmd + ' ' + escapedArgs + ' > ' + escapedOut + '""'; | ||
if (args.length) { | ||
var escapedArgs = args.map(function (a) { | ||
return '""' + a + '""'; | ||
}).join(' '); | ||
command += ' ' + escapedArgs; | ||
} | ||
var data = 'CreateObject("Wscript.Shell").Run "cmd /c ' + command + '", 0, true'; | ||
if (out) { | ||
command += ' > ""' + out + '""'; | ||
} | ||
var data = 'CreateObject("Wscript.Shell").Run "cmd /c ""' + command + '""", 0, true'; | ||
mkdirp.sync(dir); | ||
@@ -39,0 +46,0 @@ fs.writeFileSync(file, data); |
{ | ||
"name": "user-startup", | ||
"version": "0.2.1", | ||
"version": "0.2.2", | ||
"description": "Automatically run commands when the user logs in (cross-platform)", | ||
@@ -9,3 +9,4 @@ "main": "lib/index.js", | ||
"build": "babel src --out-dir lib", | ||
"test": "standard && babel-node test" | ||
"test": "standard && babel-node test", | ||
"precommit": "npm test" | ||
}, | ||
@@ -33,4 +34,6 @@ "repository": { | ||
"devDependencies": { | ||
"babel": "^5.8.3", | ||
"standard": "^5.3.1" | ||
"babel-cli": "^6.14.0", | ||
"babel-preset-es2015": "^6.14.0", | ||
"husky": "^0.11.6", | ||
"standard": "^8.0.0" | ||
}, | ||
@@ -37,0 +40,0 @@ "dependencies": { |
@@ -5,4 +5,4 @@ # user-startup [![Build Status](https://travis-ci.org/typicode/user-startup.svg)](https://travis-ci.org/typicode/user-startup) [![npm version](https://badge.fury.io/js/user-startup.svg)](https://www.npmjs.com/package/user-startup) | ||
* __No admin privileges required__ | ||
* Cross-platform (OS X, Linux and Windows) | ||
* No admin privileges required | ||
@@ -18,23 +18,32 @@ ## Install | ||
```javascript | ||
var startup = require('user-startup') | ||
const startup = require('user-startup') | ||
var nodePath = process.execPath | ||
var args = ['/some/path/app.js'] | ||
var out = '/some/path/out.log' | ||
const nodePath = process.execPath | ||
const args = ['/some/path/app.js'] | ||
const out = '/some/path/out.log' | ||
// Create startup script and spawn process | ||
// Creates startup script and spawns process | ||
startup.create('my-script', nodePath, args, out) | ||
// Remove startup script | ||
// Removes startup script | ||
startup.remove('my-script') | ||
``` | ||
Note: you should check that root isn't running this code to avoid startup scripts being created in root's home directory. Use [sudo-block](https://github.com/sindresorhus/sudo-block), [is-root](https://github.com/sindresorhus/is-root) or similar modules. | ||
Note: you should check that root isn't running this code to avoid startup scripts being created in root's home directory. Use [sudo-block](https://github.com/sindresorhus/sudo-block) or [is-root](https://github.com/sindresorhus/is-root). | ||
## API | ||
`add(id, cmd, args, out)` | ||
`add(id, cmd[, args[, out]])` | ||
* `id <String>` id | ||
* `cmd <String>` command | ||
* `args <Array>` optional list of string arguments | ||
* `out <String>` optional path to output file (directory needs to exist before) | ||
Creates startup script. | ||
`create(id, cmd[, args[, out]])` | ||
Creates a startup script __and__ spawns process. | ||
`remove(id)` | ||
@@ -44,6 +53,2 @@ | ||
`create(id, cmd, args, out)` | ||
Creates a startup script and spawns process. | ||
`dir` | ||
@@ -50,0 +55,0 @@ |
@@ -1,25 +0,40 @@ | ||
let os = require('os') | ||
let fs = require('fs') | ||
let untildify = require('untildify') | ||
let assert = require('assert') | ||
let startup = require('../src') | ||
const os = require('os') | ||
const fs = require('fs') | ||
const untildify = require('untildify') | ||
const assert = require('assert') | ||
const startup = require('../src') | ||
if (os.platform() !== 'linux') process.exit() | ||
let tmp = os.tmpdir() | ||
let id = 'test' | ||
let cmd = 'touch' | ||
let testFile = `${tmp}/foo` | ||
let log = `${tmp}/foo.log` | ||
let startupDir = untildify(`~/.config/autostart`) | ||
let startupFile = `${startupDir}/${id}.desktop` | ||
if (os.platform() === 'win32') process.exit() | ||
for (let f of [testFile, startupFile, log]) { | ||
fs.existsSync(f) && fs.unlinkSync(f) | ||
const tmp = os.tmpdir() | ||
const id = 'test' | ||
const cmd = 'touch' | ||
const testFile = `${tmp}/foo` | ||
const log = `${tmp}/foo.log` | ||
let startupDir | ||
let startupFile | ||
if (os.platform() === 'linux') { | ||
startupDir = untildify(`~/.config/autostart`) | ||
startupFile = `${startupDir}/${id}.desktop` | ||
} else { | ||
startupDir = untildify(`~/Library/LaunchAgents`) | ||
startupFile = `${startupDir}/${id}.plist` | ||
} | ||
assert(startup.dir, startupDir) | ||
function clean () { | ||
const files = [testFile, startupFile, log] | ||
files.forEach((f) => { | ||
fs.existsSync(f) && fs.unlinkSync(f) | ||
}) | ||
} | ||
clean() | ||
assert.equal(startup.dir, startupDir) | ||
startup.create(id, cmd, [testFile], log) | ||
setTimeout(function () { | ||
setTimeout(() => { | ||
assert(fs.existsSync(startupFile)) | ||
@@ -31,3 +46,14 @@ assert(fs.existsSync(testFile)) | ||
assert(!fs.existsSync(startupFile)) | ||
console.log('OK') | ||
console.log('OK 1/2') | ||
clean() | ||
// Test without log | ||
startup.create(id, cmd, [testFile]) | ||
setTimeout(() => { | ||
assert(fs.existsSync(startupFile)) | ||
assert(fs.existsSync(testFile)) | ||
assert(!fs.existsSync(log)) | ||
console.log('OK 2/2') | ||
clean() | ||
}, 100) | ||
}, 100) |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
9999
11
196
68
4