Last active
June 7, 2017 05:52
-
-
Save macsf/f27537cd312c2796522884025106e33a to your computer and use it in GitHub Desktop.
run current file in terminal
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use babel' | |
import * as helper from './helper' | |
const { exec } = require('child_process') | |
const ws = atom.workspace | |
class RunFileInTerminal { | |
run = () => { | |
const ed = ws.getActiveTextEditor() | |
const av = atom.views.getView(ed) | |
const scopeName = helper.getScope() | |
const file = ed.buffer.file.getPath() | |
const runScript = `${process.env.HOME}/.toolbox/bin/run_in_terminal.sh` | |
const runCmd = [ | |
{ scope: 'js', cmd: 'babel-node' }, | |
{ scope: 'ruby', cmd: 'ruby -W0' }, | |
{ scope: 'php', cmd: 'php -f' }, | |
{ scope: 'python', cmd: 'python' }, | |
{ scope: 'shell', cmd: 'bash' } | |
] | |
const cmd = [] | |
let runCommand = [] | |
let saveBeforeRun = atom.config.get('helper.saveBeforeRun') | |
if (typeof saveBeforeRun === 'undefined') { | |
saveBeforeRun = true | |
atom.config.set('helper.saveBeforeRun', saveBeforeRun) | |
} | |
if (scopeName.includes('js') && !scopeName.includes('jsx')) { | |
cmd.push(runCmd.find(rc => rc.scope === 'js').cmd) | |
} else { | |
runCmd.filter(rc => rc.scope !== 'js').forEach(rc => { | |
if (scopeName.includes(rc.scope)) { | |
cmd.push(rc.cmd) | |
} | |
}) | |
} | |
if (cmd.length > 0) { | |
cmd.push(file) | |
runCommand = [runScript, '"', cmd.join(' '), '"'] | |
if (saveBeforeRun) { | |
atom.commands.dispatch(av, 'core:save') | |
} | |
exec(runCommand.join(' '), (err, stdout, stderr) => { | |
if (err) { | |
console.error(`Exec error: ${err}`) | |
} | |
}) | |
} else { | |
atom.notifications.addWarning("Don't know how to run this file, yet.") | |
} | |
} | |
} | |
export default new RunFileInTerminal() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const helper = require('./helper') | |
const runFileInTerminal = require('./run_file_in_terminal') | |
atom.commands.add('atom-text-editor', { | |
'helper:run-file-in-terminal': () => { | |
runFileInTerminal.run() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment