Skip to content

Instantly share code, notes, and snippets.

@zhoukekestar
Last active February 23, 2017 06:31
Show Gist options
  • Save zhoukekestar/ccacd3d0beaa86aab8c1f8157c4beef3 to your computer and use it in GitHub Desktop.
Save zhoukekestar/ccacd3d0beaa86aab8c1f8157c4beef3 to your computer and use it in GitHub Desktop.
auto-run for atom on windows.
const {executeCommand, getCurrentEditorProjectHome, isFileExist} = require('./atom-utils.js');
const fs = require('fs')
const portMap = {}
const autorunIfNeeded = function autorunIfNeeded(editor = atom.workspace.getActiveTextEditor()) {
const project = getCurrentEditorProjectHome(editor);
if (isFileExist(`${project}/Dockerfile`)) {
const content = fs.readFileSync(`${project}/Dockerfile`);
let port = String(content).match(/ENV NODE_PORT (\d+)/);
port = port && port[1];
const reg = new RegExp(`:${port}.* LISTENING`)
executeCommand(`netstat -aon`, (err, res) => {
if (err) {
console.log(res)
return;
}
if (reg.test(res) || portMap[port]) {
return;
} else {
console.log(`run ${project}/run.cmd`)
portMap[port] = executeCommand(`${project}/run.cmd`, {
cwd: project,
concatOutput: false,
}, (err, res) => {
delete portMap[port];
})
return;
}
});
}
}
module.exports = function(atom, windows) {
console.log('autorun')
atom.workspace.observeTextEditors(editor => {
editor.onDidSave(e => {
autorunIfNeeded();
});
});
atom.workspace.onDidAddTextEditor(editor => {
autorunIfNeeded(editor.textEditor)
});
}
const {BufferedProcess} = require('atom');
const fs = require('fs')
// execute shell command
const executeCommand = function executeCommand(command, options, callback) {
command = command.split(' ');
const args = command.slice(1);
command = command[0];
if (typeof options === 'function') {
callback = options;
options = {};
}
options.concatOutput = options.concatOutput === undefined ? true : options.concatOutput;
let result = '';
return new BufferedProcess({
command,
options,
args,
stdout(output) {
if (callback && options.concatOutput) {
result += output;
} else {
console.log(output)
}
},
stderr(output) {
if (callback && options.concatOutput) {
result += output;
} else {
console.log(output)
}
},
exit(code) {
if (code === 0) {
callback && callback(null, result)
} else {
callback && callback(code, result)
}
}
})
}
// Get current file's project home
const getCurrentEditorProjectHome = function getCurrentEditorProjectHome(editor = atom.workspace.getActiveTextEditor()) {
return atom.project.relativizePath(editor.getPath())[0]
}
const isFileExist = function isFileExist(path) {
try {
fs.statSync(path)
} catch (e) {
if (e.code === 'ENOENT'){
return false;
} else {
console.log(e)
return false;
}
}
return true;
}
module.exports.executeCommand = executeCommand;
module.exports.getCurrentEditorProjectHome = getCurrentEditorProjectHome;
module.exports.isFileExist = isFileExist;
autorun = require 'your-path/atom-autorun.js'
autorun(atom, window);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment