Skip to content
This repository has been archived by the owner on Dec 20, 2017. It is now read-only.

Commit

Permalink
Merge pull request #77 from sdjcw/master
Browse files Browse the repository at this point in the history
bugfix: 项目类型检测的处理不正确; 增加 Python 项目的本地运行和部署
  • Loading branch information
killme2008 committed May 29, 2015
2 parents 6578789 + f36c336 commit 4f31fe5
Show file tree
Hide file tree
Showing 3 changed files with 149 additions and 103 deletions.
202 changes: 109 additions & 93 deletions bin/avoscloud
Expand Up @@ -73,22 +73,69 @@ run.queryLatestVersion();
run.sendStats(CMD);
//Execute command.

// 不需要在项目目录执行的命令
if(CMD) {
switch (CMD) {
case "search":
case 'new':
run.createNewProject(callback);
break;

case "deploy":
run.logProjectHome();
if (program.git) {
run.deployGitCloudCode(program.revision || 'master', program.giturl, callback);
} else {
if (path.resolve(CLOUD_PATH) != path.resolve('.'))
return exitWith("'avoscloud deploy' 命令必须在 LeanEngine 项目目录中使用");
runtime.detect(CLOUD_PATH, function(err, runtimeInfo) {
if (err) {
return callback(err);
}
run.deployLocalCloudCode(runtimeInfo, CLOUD_PATH, program.log, callback);
});
}
break;

case "undeploy":
run.logProjectHome();
run.undeployCloudCode(callback);
break;

case "publish":
run.logProjectHome();
run.publishCloudCode(callback);
break;

case "status":
run.logProjectHome();
run.queryStatus(callback);
break;

case 'logs':
run.logProjectHome();
run.viewCloudLog(program.lines, program.tailf, null, callback);
break;

case "upload":
run.initAVOSCloudSDK();
run.logProjectHome();
if (!program.args[1]) {
console.log("请使用:avoscloud search <关键字>");
console.log("请使用:avoscloud upload <文件或目录>");
process.exit(1);
}
program.args.shift();
exec('open https://leancloud.cn/search.html?q=' + encodeURIComponent(program.args.join(' ')));
return callback();
case 'new':
return run.createNewProject(callback);
case "clear":
run.deleteMasterKeys();
return callback();
run.importFiles(program.args, function(err) {
if (err)
console.log(err);
}, callback);
break;

case "app":
//app <list>
var isList = program.args[1] == 'list';
run.appStatus(isList);
callback();
break;

case "add":
// add <name> <app id>
var name = program.args[1];
Expand All @@ -98,96 +145,65 @@ if(CMD) {
if (!appId)
return exitWith("请使用:avoscloud add <name> <app id>");
run.addApp(name, appId);
return callback();
callback();
break;

case "rm":
//rm <name>
var name = program.args[1];
if (!name)
return exitWith("请使用:avoscloud rm <name>");
run.removeApp(name);
callback();
break;

case "checkout":
//checkout <name>
var name = program.args[1];
if (!name)
return exitWith("请使用:avoscloud checkout <name>");
run.checkoutApp(name);
callback();
break;

case "cql":
run.doCloudQuery(callback);
break;

case "clear":
run.deleteMasterKeys();
callback();
break;

case "search":
if (!program.args[1]) {
console.log("请使用:avoscloud search <关键字>");
process.exit(1);
}
program.args.shift();
exec('open https://leancloud.cn/search.html?q=' + encodeURIComponent(program.args.join(' ')));
callback();
break;

case "lint":
run.doLint();
return callback();
}
}
run.doLint(callback);
break;

// 需要在项目目录执行的命令
runtime.detect(CLOUD_PATH, function(err, runtimeInfo) {
if (err) {
return callback(err);
default:
program.help();
break;
}

runtimeInfo.setDebug(program.debug);
if(CMD) {
switch (CMD) {
case "deploy":
run.logProjectHome();
if (program.git) {
run.deployGitCloudCode(program.revision || 'master', program.giturl, callback);
} else {
if (path.resolve(CLOUD_PATH) != path.resolve('.'))
return exitWith("'avoscloud deploy' 命令必须在 LeanEngine 项目目录中使用");
run.deployLocalCloudCode(runtimeInfo, CLOUD_PATH, program.log, callback);
}
break;
case "undeploy":
run.logProjectHome();
run.undeployCloudCode(callback);
break;
case "publish":
run.logProjectHome();
run.publishCloudCode(callback);
break;
case "status":
run.logProjectHome();
run.queryStatus(callback);
break;
case 'logs':
run.logProjectHome();
run.viewCloudLog(program.lines, program.tailf, null, callback);
break;
case "upload":
run.initAVOSCloudSDK();
run.logProjectHome();
if (!program.args[1]) {
console.log("请使用:avoscloud upload <文件或目录>");
process.exit(1);
}
program.args.shift();
run.importFiles(program.args, function(err) {
if (err)
console.log(err);
}, callback);
break;
case "app":
//app <list>
var isList = program.args[1] == 'list';
run.appStatus(isList);
callback();
break;
case "rm":
//rm <name>
var name = program.args[1];
if (!name)
return exitWith("请使用:avoscloud rm <name>");
run.removeApp(name);
callback();
break;
case "checkout":
//checkout <name>
var name = program.args[1];
if (!name)
return exitWith("请使用:avoscloud checkout <name>");
run.checkoutApp(name);
callback();
break;
case "cql":
run.doCloudQuery(callback);
break;
default:
program.help();
break;
} else {
runtime.detect(CLOUD_PATH, function(err, runtimeInfo) {
if (err) {
return callback(err);
}
} else {
runtimeInfo.setDebug(program.debug);
var monconfig = runtimeInfo.getMonconfig(args);
console.log(color.green('提示:您可以敲入 rs 命令并回车来重启本进程'));
nodemon(monconfig);
nodemon.on('restart', function (files) {
console.log('[INFO] 因为文件变更而项目重启:', files);
});
}
});
});
}
5 changes: 3 additions & 2 deletions bin/run.js
Expand Up @@ -919,15 +919,16 @@ exports.doCloudQuery = doCloudQuery = function(cb) {
});
}

exports.doLint = function() {
exports.doLint = function(cb) {
console.log("linting ...");
var cmd = path.join(__dirname, '..', 'node_modules', 'jshint', 'bin', 'jshint') + ' cloud';
var cmd = path.join(__dirname, '..', 'node_modules', 'jshint', 'bin', 'jshint') + ' . --exclude node_modules';
exec(cmd, function(err, stdout, stderr) {
console.log(stdout);
if (err) {
process.exit(err.code);
} else {
console.log('lint ok');
cb();
}
});
}
Expand Down
45 changes: 37 additions & 8 deletions lib/runtime.js
Expand Up @@ -4,18 +4,13 @@ var path = require('path'),
run = require('../bin/run');

exports.detect = function(appPath, cb) {
var innerCb = function(err, runtimeInfo) {
cb(err, runtimeInfo);
};
if (fs.existsSync(path.join(appPath, 'cloud', 'main.js'))){ // cloud code v2.0
return getCloudCodeRuntimeInfo(appPath, innerCb);
return getCloudCodeRuntimeInfo(appPath, cb);
} else if (fs.existsSync(path.join(appPath, 'server.js'))) { // Node.js
return getNodeRuntimeInfo(appPath, innerCb);
return getNodeRuntimeInfo(appPath, cb);
} else if (fs.existsSync(path.join(appPath, 'requirements.txt')) &&
fs.existsSync(path.join(appPath, 'wsgi.py'))) { // Python
return {
// TODO Python
}
return getPythonRuntimeInfo(appPath, cb);
} else {
cb(new Error('不在 LeanEngine 项目根目录,或目录结构不对。'));
}
Expand Down Expand Up @@ -85,3 +80,37 @@ var getNodeRuntimeInfo = function(appPath, cb) {
});
});
}

var getPythonRuntimeInfo = function(appPath, cb) {
var app = run.getAppSync();
run.initMasterKey(function(masterKey) {
cb(null, {
exec: 'python',
setDebug: function(debug) {
if (debug) {
this.exec = 'python -m pdb';
}
},
getMonconfig: function(args) {
return {
exec: this.exec,
ignore: [
'.git'
],
"env": {
LC_APP_ID: app.appId,
LC_APP_KEY: masterKey,
LC_APP_MASTER_KEY: masterKey,
LC_APP_PORT: run.getPort()
},
ext: 'py',
script: 'wsgi.py',
args: args
};
},
bulk: function() {
return [{ src: ['**']}]
}
});
});
}

0 comments on commit 4f31fe5

Please sign in to comment.