Skip to content

Commit

Permalink
feat: add use cmd
Browse files Browse the repository at this point in the history
  • Loading branch information
avwo committed Jun 15, 2018
1 parent ca89aa3 commit a5a8157
Showing 1 changed file with 22 additions and 7 deletions.
29 changes: 22 additions & 7 deletions bin/use.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ var colors = require('colors/safe');
var fse = require('fs-extra2');
var getPluginPaths = require('../lib/plugins/module-paths').getPaths;

/*eslint no-console: "off"*/
var pluginPaths = getPluginPaths();
var MAX_RULES_LEN = 1024 * 16;
var CHECK_RUNNING_CMD = process.platform === 'win32' ?
'tasklist /fi "PID eq %s" | findstr /i "node.exe"'
Expand All @@ -30,17 +32,33 @@ function showStartWhistleTips(storage) {
+ '` to start whistle first.'));
}

function handleRules(options, filepath, callback) {
function handleRules(filepath, callback, port) {
var getRules = require(filepath);
if (typeof getRules !== 'function') {
return callback(getRules);
}
getRules(options, callback);
getRules(callback, {
port: port,
checkPlugin: checkPlugin
});
}

function getString(str) {
return typeof str !== 'string' ? '' : str.trim();
}

function checkPlugin(name) {
if (!name || typeof name !== 'string') {
return false;
}
for (var i, len = pluginPaths.length; i < len; i++) {
try {
if (fs.statSync(path.join(pluginPaths[i], name)).isDirectory()) {
return true;
}
} catch(e) {}
}
}

module.exports = function(filepath, storage) {
var dataDir = path.resolve(getHomedir(), '.startingAppData');
Expand All @@ -60,10 +78,7 @@ module.exports = function(filepath, storage) {
}
filepath = path.resolve(filepath || '.whistle.js');
var port = options.port > 0 ? options.port : 8899;
handleRules({
pluginPaths: getPluginPaths(),
port: port
}, filepath, function(result) {
handleRules(filepath, function(result) {
if (!result) {
console.log(colors.red('name and rules cannot be empty.'));
return;
Expand All @@ -79,6 +94,6 @@ module.exports = function(filepath, storage) {
return;
}
console.log(colors.green('[127.0.0.1:' + port + '] Setting successful.'));
});
}, port);
});
};

0 comments on commit a5a8157

Please sign in to comment.