split cli and helper, add bin entry to package.json

This commit is contained in:
Thomas Ruoff
2016-01-18 10:07:57 +01:00
parent 610fd07bec
commit d2e0e172f9
4 changed files with 42 additions and 31 deletions

2
.npmignore Normal file
View File

@@ -0,0 +1,2 @@
.editorconfig
.jshintrc

32
cli.js Executable file
View File

@@ -0,0 +1,32 @@
#!/usr/bin/env node
'use strict';
const argv = require('minimist')(process.argv.slice(2));
const swm = require('./swm.js');
if (argv.help || argv.h) {
console.log(
`Simple helper for turning on/off connected/disconnected monitors with 'xrandr'.
Usage:
'swm [monitor-1...montior-n]' e.g. 'swm LVDS1 HDMI1'
If 'monitor-1' to 'monitor-n' is specified 'swm' will turn on these monitors
and place them from left to right in the order given. If a provided monitor is
not connected it will be skipped.
If no monitors are specified all connected monitors will be turned on and
placed from left to right in alphabetical order of their name.`
);
process.exit(2);
}
swm.getDevices()
.then(swm.generateXrandrOptions.bind(null, argv._))
.then(swm.switchDevices)
.then(swm.executePostCmd.bind(null, argv.postCmd))
.catch(err => {
console.error(err);
});

View File

@@ -6,6 +6,7 @@
"scripts": { "scripts": {
"test": "echo \"Error: no test specified\" && exit 1" "test": "echo \"Error: no test specified\" && exit 1"
}, },
"bin" : { "swm" : "./cli.js" },
"repository": { "repository": {
"type": "git", "type": "git",
"url": "git+https://github.com/tomru/switchmon.git" "url": "git+https://github.com/tomru/switchmon.git"

38
index.js → swm.js Executable file → Normal file
View File

@@ -1,7 +1,5 @@
#!/usr/bin/env node
'use strict'; 'use strict';
const argv = require('minimist')(process.argv.slice(2));
const xrandrParse = require('xrandr-parse'); const xrandrParse = require('xrandr-parse');
const exec = require('child_process').exec; const exec = require('child_process').exec;
@@ -71,12 +69,12 @@ function switchDevices(xrandrOptions) {
}); });
} }
function executePostCmd() { function executePostCmd(postCmd) {
if (!argv.postCmd) { if (!postCmd) {
return; return;
} }
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
exec(argv.postCmd, (err, stdout, stderr) => { exec(postCmd, (err, stdout, stderr) => {
if (err || stderr) { if (err || stderr) {
reject(err); reject(err);
return; return;
@@ -86,29 +84,7 @@ function executePostCmd() {
}); });
} }
if (argv.help || argv.h) { module.exports.getDevices = getDevices;
console.log( module.exports.generateXrandrOptions = generateXrandrOptions;
`Simple helper for turning on/off connected/disconnected monitors with 'xrandr'. module.exports.switchDevices = switchDevices;
module.exports.executePostCmd = executePostCmd;
Usage:
'swm [monitor-1...montior-n]' e.g. 'swm LVDS1 HDMI1'
If 'monitor-1' to 'monitor-n' is specified 'swm' will turn on these monitors
and place them from left to right in the order given. If a provided monitor is
not connected it will be skipped.
If no monitors are specified all connected monitors will be turned on and
placed from left to right in alphabetical order of their name.`
);
process.exit(2);
}
getDevices()
.then(generateXrandrOptions.bind(null, argv._))
.then(switchDevices)
.then(executePostCmd)
.catch(err => {
console.error(err);
});