Skip to content

Instantly share code, notes, and snippets.

@macsf
Last active June 7, 2017 05:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save macsf/f27537cd312c2796522884025106e33a to your computer and use it in GitHub Desktop.
Save macsf/f27537cd312c2796522884025106e33a to your computer and use it in GitHub Desktop.
run current file in terminal
'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()
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