From d2e0e172f94d837b392ea5b5322fe568ab12f6ab Mon Sep 17 00:00:00 2001 From: Thomas Ruoff Date: Mon, 18 Jan 2016 10:07:57 +0100 Subject: [PATCH] split cli and helper, add bin entry to package.json --- .npmignore | 2 ++ cli.js | 32 ++++++++++++++++++++++++++++++++ package.json | 1 + index.js => swm.js | 38 +++++++------------------------------- 4 files changed, 42 insertions(+), 31 deletions(-) create mode 100644 .npmignore create mode 100755 cli.js rename index.js => swm.js (71%) mode change 100755 => 100644 diff --git a/.npmignore b/.npmignore new file mode 100644 index 0000000..301733d --- /dev/null +++ b/.npmignore @@ -0,0 +1,2 @@ +.editorconfig +.jshintrc diff --git a/cli.js b/cli.js new file mode 100755 index 0000000..3fc0594 --- /dev/null +++ b/cli.js @@ -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); + }); diff --git a/package.json b/package.json index 355bc8d..c89bb82 100644 --- a/package.json +++ b/package.json @@ -6,6 +6,7 @@ "scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, + "bin" : { "swm" : "./cli.js" }, "repository": { "type": "git", "url": "git+https://github.com/tomru/switchmon.git" diff --git a/index.js b/swm.js old mode 100755 new mode 100644 similarity index 71% rename from index.js rename to swm.js index 7d35f3b..6910aa2 --- a/index.js +++ b/swm.js @@ -1,7 +1,5 @@ -#!/usr/bin/env node 'use strict'; -const argv = require('minimist')(process.argv.slice(2)); const xrandrParse = require('xrandr-parse'); const exec = require('child_process').exec; @@ -71,12 +69,12 @@ function switchDevices(xrandrOptions) { }); } -function executePostCmd() { - if (!argv.postCmd) { +function executePostCmd(postCmd) { + if (!postCmd) { return; } return new Promise((resolve, reject) => { - exec(argv.postCmd, (err, stdout, stderr) => { + exec(postCmd, (err, stdout, stderr) => { if (err || stderr) { reject(err); return; @@ -86,29 +84,7 @@ function executePostCmd() { }); } -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); -} - -getDevices() - .then(generateXrandrOptions.bind(null, argv._)) - .then(switchDevices) - .then(executePostCmd) - .catch(err => { - console.error(err); - }); +module.exports.getDevices = getDevices; +module.exports.generateXrandrOptions = generateXrandrOptions; +module.exports.switchDevices = switchDevices; +module.exports.executePostCmd = executePostCmd;