Skip to content

Instantly share code, notes, and snippets.

@jonschlinkert
Last active June 25, 2019 20:47
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jonschlinkert/76402f4947b46bd8682290dc96a48f7f to your computer and use it in GitHub Desktop.
Save jonschlinkert/76402f4947b46bd8682290dc96a48f7f to your computer and use it in GitHub Desktop.
Simple way to open a file or directory in a text editor or file manager, whichever is resolved first.
const cmds = { linux: 'xdg-open', win32: 'start', darwin: 'open' };
const open = cmds[process.platform];
const tryOpen = (dirname, editors = ['sublime', 'code', open]) => {
try {
cp.execSync([editors[0], dirname].join(' '));
console.log('Opening in ' + editors[0]);
} catch (err) {
if (editors.length === 0) {
throw new Error('Cannot find an editor to open');
}
return tryEditors(dirname, editors.slice(1));
}
};
/**
* Example usage
*/
const argv = require('minimist')(process.argv.slice(2));
// open the first resolved editor or file manager
if (argv.open === true) {
tryOpen(path.dirname(__dirname));
process.exit();
}
// open the specified application
if (typeof argv.open === 'string') {
tryOpen(path.dirname(__dirname), [argv.open]);
process.exit();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment