mirror of
https://github.com/tomru/switchmon.git
synced 2026-03-02 22:17:23 +01:00
split cli and helper, add bin entry to package.json
This commit is contained in:
2
.npmignore
Normal file
2
.npmignore
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
.editorconfig
|
||||||
|
.jshintrc
|
||||||
32
cli.js
Executable file
32
cli.js
Executable 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);
|
||||||
|
});
|
||||||
@@ -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
38
index.js → swm.js
Executable file → Normal 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);
|
|
||||||
});
|
|
||||||
Reference in New Issue
Block a user