Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

grunt-mock2easy

Package Overview
Dependencies
Maintainers
1
Versions
80
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

grunt-mock2easy - npm Package Compare versions

Comparing version 0.2.23 to 0.2.24

6

package.json
{
"name": "grunt-mock2easy",
"description": "通过nodejs的服务实现ajax的跨域访问,利用mockjs动态生成json接口,模拟各种恶略环境测试系统是否健全,测试接口生成接口文档",
"version": "0.2.23",
"version": "0.2.24",
"homepage": "https://github.com/appLhui/grunt-mock2easy",

@@ -61,6 +61,6 @@ "author": {

"node.extend": "~1.1.0",
"underscore":"~1.7.0"
"underscore": "~1.7.0"
},
"readmeFilename": "README.md",
"_id": "grunt-mock2easy@0.2.22",
"_id": "grunt-mock2easy@0.2.23",
"_from": "grunt-mock2easy@",

@@ -67,0 +67,0 @@ "bugs": {

@@ -31,2 +31,3 @@ # grunt-mock2easy

port:3000,
lazyLoadTime:3000,
database:'database',

@@ -51,4 +52,12 @@ doc:'doc',

启动的服务的端口号
启动的服务的端口号
#### options.lazyLoadTime
Type: `Number`
Default value: 3000
启动接口延时加载,调用接口的延迟时间
#### options.database

@@ -55,0 +64,0 @@

@@ -32,2 +32,3 @@ /*

port:3000,
lazyLoadTime:3000,
database:'mock2easy',

@@ -39,5 +40,8 @@ doc:'doc',

});
server[action](options);
try{
server[action](options);
} catch (e){
grunt.log.writeln(e.red);
}
});
};

@@ -43,13 +43,15 @@

//检验请求方式 和 请求参数
require('../server/verifyReqParameter')(grunt)(url,method,params,_data,ignoreField);
setTimeout(function(){
try{
require('../server/verifyReqParameter')(grunt)(url,method,params,_data,ignoreField);
setTimeout(function(){
res.send(Mock.mock(require('../util/response2json')(hashObj,grunt,true)));
},_data.lazyLoad=="yes"?3000:0);
},_data.lazyLoad=="yes"?global.options.lazyLoadTime:0);
} catch (err){
res.send(500,err);
}
}
}
});
};
}

@@ -26,20 +26,29 @@ /**

return function(req, res, next) {
var child_process = require('child_process');
try{
var child_process = require('child_process');
var sh = ['curl',' ',
global.options.curl.domain,
req.originalUrl.split('?')[0],' ',
'--data "'+obj2StrParams(extend(true, {}, req.body,req.query,{secToken:global.options.curl.secToken}))+'" ',
"-H 'Cookie: login_aliyunid_ticket=",global.options.curl.login_aliyunid_ticket,"; '"
];
sh = sh.join('');
grunt.log.writeln();
grunt.log.writeln('+---------------------Curl命令--------------------------'.yellow);
grunt.log.writeln('| '.yellow + sh.green);
grunt.log.writeln('+-------------------------------------------------------'.yellow);
child_process.exec(sh,function(error, stdout, stderr){
res.send(stdout);
});
var sh = ['curl',' ',
global.options.curl.domain,
req.originalUrl.split('?')[0],' ',
'--data "'+obj2StrParams(extend(true, {}, req.body,req.query,{secToken:global.options.curl.secToken}))+'" ',
"-H 'Cookie: login_aliyunid_ticket=",global.options.curl.login_aliyunid_ticket,"; '"
];
sh = sh.join('');
grunt.log.writeln();
grunt.log.writeln('+---------------------Curl命令--------------------------'.yellow);
grunt.log.writeln('| '.yellow + sh.green);
grunt.log.writeln('+-------------------------------------------------------'.yellow);
child_process.exec(sh,function(err, stdout, stderr){
if(err){
res.json(500,err);
}else{
res.send(stdout);
}
});
} catch(err){
res.json(500,err);
}
}
};

@@ -9,11 +9,13 @@ module.exports = function(grunt) {

router.get('/', function(req, res) {
res.render('index', { title: 'Express' });
res.render('index');
});
//获取已经生成的接口路径
//获取已经生成的接口路径
router.post('/getList',function(req,res){
require('../server/loadInterface')(grunt,require('../server/getAllFiles')(grunt)).then(function(o){
res.json(o);
},function(err){
res.send(500,'['+ err +']接口读取有异常!');
});

@@ -26,5 +28,11 @@ });

grunt.log.error(err);
res.send(500,err);
}else{
if(data){
res.json(JSON.parse(data));
try{
res.json(JSON.parse(data));
} catch (err){
res.send(500,'接口读取有异常!');
}
}

@@ -40,2 +48,3 @@ }

grunt.log.error(err);
res.send(500,err);
}else{

@@ -46,3 +55,5 @@ if(data){

require('../util/writeFile')((global.options.database)+req.body.interfaceUrl,JSON.stringify(_data,undefined,5),grunt).then(function(){
res.json({success:1});
res.json({success:true});
},function(err){
res.send(500,err);
});

@@ -60,5 +71,8 @@ }

grunt.log.error(err);
res.send(500,err);
}else{
require('../server/cleanInterface')(grunt).then(function(){
res.json({success:1});
res.json({success:true});
},function(err){
res.json(500,err);
});

@@ -74,4 +88,8 @@ }

require('../server/cleanInterface')(grunt).then(function(){
res.json({success:1});
res.json({success:true});
},function(err){
res.json(500,err);
});
},function(err){
res.json(500,err);
});

@@ -84,4 +102,8 @@ });

require('../server/cleanInterface')(grunt).then(function(){
res.json({success:1});
res.json({success:true});
},function(err){
res.json(500,err);
});
},function(err){
res.json(500,err);
});

@@ -93,4 +115,8 @@ });

require('../server/cleanInterface')(grunt).then(function(){
res.json({success:1});
res.json({success:true});
},function(err){
res.json(500,err);
});
},function(err){
res.json(500,err);
});

@@ -97,0 +123,0 @@ })

@@ -119,2 +119,4 @@ 'use strict';

process.on('exit', this.stop);
},function(err){
grunt.log.error(err);
});

@@ -121,0 +123,0 @@ });

@@ -16,2 +16,3 @@ /**

grunt.log.error(err);
deferred.reject(err);
}else {

@@ -24,2 +25,4 @@ if (data) {

deferred.resolve();
},function(err){
deferred.reject(err);
});

@@ -26,0 +29,0 @@ }

@@ -32,38 +32,55 @@ /**

grunt.log.error(err);
callback(err);
}else{
if(data){
_json = JSON.parse(data);
try{
if(data) {
_json = JSON.parse(data);
_json.requiredParameters.forEach(function(o){
o.remark = o.rule != undefined ? o.rule : o.remark;
o.required = o.required != undefined ? o.required : true;
delete o.rule;
});
_json.responseParameters.forEach(function(o){
if(o.kind == 'boolean'|| o.kind == 'number'){
o.rule = o.rule + '';
o.kind = 'mock';
}
});
if(!!!_json.reqError){
_json.reqError = [];
}
if(!!!_json.docError){
_json.docError = [];
}
_path.push('['+_json.interfaceName+'](./'+global.options.doc+_json.interfaceUrl.replace('.json','.md')+')\n');
_json.requiredParameters.forEach(function (o) {
o.remark = o.rule != undefined ? o.rule : o.remark;
o.required = o.required != undefined ? o.required : true;
delete o.rule;
});
_json.responseParameters.forEach(function (o) {
if (o.kind == 'boolean' || o.kind == 'number') {
o.rule = o.rule + '';
o.kind = 'mock';
}
});
if (!!!_json.reqError) {
_json.reqError = [];
}
if (!!!_json.docError) {
_json.docError = [];
}
_path.push('[' + _json.interfaceName + '](./' + global.options.doc + _json.interfaceUrl.replace('.json', '.md') + ')\n');
setConfiguration(grunt,_json).then(function(){
callback();
});
}
setConfiguration(grunt, _json).then(function () {
callback();
},function(err){
callback(err);
});
} else{
callback();
}
}catch (err){
grunt.log.error(err);
callback(err);
}
}
});
},function(){
_path.sort().forEach(function(o,i){
_menu += (i+1) + '. ' + o;
});
require('../util/createFile')(path.resolve(global.options.doc)+'/menu.md',_menu,grunt).then(function(){
},function(err){
if(err){
deferred.reject(err);
}else {
_path.sort().forEach(function(o,i){
_menu += (i+1) + '. ' + o;
});
require('../util/createFile')(path.resolve(global.options.doc)+'/menu.md',_menu,grunt).then(function(){
deferred.resolve();
});
},function(err){
deferred.reject(err);
});
}
});

@@ -70,0 +87,0 @@

@@ -18,2 +18,4 @@ /**

deferred.resolve();
},function(err){
deferred.reject(err);
});

@@ -20,0 +22,0 @@

@@ -18,52 +18,57 @@ /**

var arry = file.split(global.options.database);
var arry = file.split(global.options.database);
var _json = {};
fs.readFile(file,'utf-8',function(err,data){
if(err){
grunt.log.error(err);
}else{
var _json = {};
fs.readFile(file,'utf-8',function(err,data){
if(err){
grunt.log.error(err);
}else{
process.nextTick(function(){
try{
if(data){
_json = JSON.parse(data);
result.data.push({
url:arry[arry.length-1],
note:_json.interfaceName,
lazyLoad:_json.lazyLoad == 'yes'
});
var _path_ = '';
_json.interfaceUrl.split('/').forEach(function(o,i){
if(i && _json.interfaceUrl.split('/').length - 1 > i){
_path_ += '/' + o ;
if(result.path.indexOf(_path_)==-1){
result.path.push(_path_);
}
}
});
process.nextTick(function(){
if(data){
_json = JSON.parse(data);
result.data.push({
url:arry[arry.length-1],
note:_json.interfaceName,
lazyLoad:_json.lazyLoad == 'yes'
});
var _path_ = '';
_json.interfaceUrl.split('/').forEach(function(o,i){
if(i && _json.interfaceUrl.split('/').length - 1 > i){
_path_ += '/' + o ;
if(result.path.indexOf(_path_)==-1){
result.path.push(_path_);
}
}
});
if(_json.methodError || _json.reqError.length || _json.docError.length){
var _logObj = {url:_json.interfaceUrl};
if(_json.methodError){
_logObj.methodError = _json.methodError;
}
if(_json.reqError.length){
_logObj.reqError = _json.reqError;
}
if(_json.docError.length){
_logObj.docError = _json.docError;
}
result.log.push(_logObj);
}
}
callback(null, result);
if(_json.methodError || _json.reqError.length || _json.docError.length){
var _logObj = {url:_json.interfaceUrl};
if(_json.methodError){
_logObj.methodError = _json.methodError;
}
if(_json.reqError.length){
_logObj.reqError = _json.reqError;
}
if(_json.docError.length){
_logObj.docError = _json.docError;
}
result.log.push(_logObj);
}
}
callback(null, result);
}catch (err){
grunt.log.error(err);
callback(file, result);
}
});
}
});
}
});
}, function(err, result){
deferred.resolve(result);
if(err){
deferred.reject(err);
}else{
deferred.resolve(result);
}
});

@@ -70,0 +75,0 @@

@@ -7,125 +7,146 @@ /**

module.exports = function(grunt,body){
var path = require('path');
var fs = require('fs');
var Mock = require('mockjs');
var deferred = require('Q').defer();
var _reqStr = '';
var path = require('path');
var fs = require('fs');
var Mock = require('mockjs');
var deferred = require('Q').defer();
var _reqStr = '';
var createFile = require('../util/createFile');
var createFile = require('../util/createFile');
createFile(path.resolve(global.options.database)+body.interfaceUrl,JSON.stringify(body,undefined,5),grunt).then(function(){
writeDoc();
});
createFile(path.resolve(global.options.database)+body.interfaceUrl,JSON.stringify(body,undefined,5),grunt).then(function(){
try{
writeDoc();
}catch (err){
deferred.reject(err);
}
},function(err){
deferred.reject(err);
});
//获取 注释的开始 和结束的坐标,以及换行的坐标
var getCoord = function(json,_i,_step,_rejson){
_i = _i || 0;
_step = _step || 1;
_rejson = _rejson || [];
if(_step == 1){
var _str = json.indexOf('//',_i);
if(_str !== -1){
_rejson.push(_str);
getCoord(json, _str+1,2,_rejson);
}
}else if(_step == 2){
var _str = json.indexOf('"',_i);
_rejson[_rejson.length-1] += '|' + _str;
getCoord(json, _str+1,3,_rejson);
}else{
var _str = json.indexOf('\n',_i);
_rejson[_rejson.length-1] += '|' + _str;
getCoord(json, _str+1,1,_rejson);
//获取 注释的开始 和结束的坐标,以及换行的坐标
var getCoord = function(json,_i,_step,_rejson){
_i = _i || 0;
_step = _step || 1;
_rejson = _rejson || [];
if(_step == 1){
var _str = json.indexOf('//',_i);
if(_str !== -1){
_rejson.push(_str);
getCoord(json, _str+1,2,_rejson);
}
return _rejson;
}else if(_step == 2){
var _str = json.indexOf('"',_i);
_rejson[_rejson.length-1] += '|' + _str;
getCoord(json, _str+1,3,_rejson);
}else{
var _str = json.indexOf('\n',_i);
_rejson[_rejson.length-1] += '|' + _str;
getCoord(json, _str+1,1,_rejson);
}
return _rejson;
}
var json2md = function(json,coords){
if(coords.length){
for (var i in coords) {
var _coord = coords[i].split('|');
var _str1 = json.substring(0,parseInt(_coord[0]));
var _str2 = json.substring(parseInt(_coord[0]),parseInt(_coord[1]));
var _str3 = json.substring(parseInt(_coord[1]),parseInt(_coord[2]));
var _str4 = json.substring(parseInt(_coord[2]));
json = _str1 + _str3 +_str2 + _str4;
}
var json2md = function(json,coords){
if(coords.length){
for (var i in coords) {
var _coord = coords[i].split('|');
var _str1 = json.substring(0,parseInt(_coord[0]));
var _str2 = json.substring(parseInt(_coord[0]),parseInt(_coord[1]));
var _str3 = json.substring(parseInt(_coord[1]),parseInt(_coord[2]));
var _str4 = json.substring(parseInt(_coord[2]));
json = _str1 + _str3 +_str2 + _str4;
}
return json.replace(/\/\//g,' //');
}
return json.replace(/\/\//g,' //');
}
var writeDoc = function(){
if(body.requiredParameters.length>0){
_reqStr = '#### 请求参数\n';
body.requiredParameters.forEach(function(o){
if(o.remark && o.remark.length){
_reqStr += '\t'+ o.name + ': `' + o.remark + '`\n';
}else{
_reqStr += '\t'+ o.name + '\n';
}
});
}
var writeDoc = function(){
if(body.requiredParameters.length>0){
_reqStr = '#### 请求参数\n';
body.requiredParameters.forEach(function(o){
if(o.remark && o.remark.length){
_reqStr += '\t'+ o.name + ': `' + o.remark + '`\n';
}else{
_reqStr += '\t'+ o.name + '\n';
}
});
}
var responseParameters = body.responseParameters;
var hashObj = {};
for (var i in responseParameters) {
var o = responseParameters[i];
//规范返回的接口文档
if(o.kind == 'array(object)'){
o.name = o.name.split('|')[0]
}else if(o.kind == 'string' && o.rule.toLowerCase() == '@word'){
o.rule = 'xxxxxxx';
}else if(o.kind == 'string' && o.rule.toLowerCase() == '@email'){
o.rule = 'xxxxxxx@mock2easy.com';
}else if(o.kind == 'string' && o.rule.toLowerCase() == '@url'){
o.rule = 'www.npmjs.org/package/grunt-mock2easy';
}else if(o.kind == 'string' && o.rule.toLowerCase() == '@date'){
o.rule = '2013-02-11';
}else if(o.kind == 'string' && o.rule.toLowerCase() == '@time'){
o.rule = '17:48:42';
}else if(o.kind == 'string' && o.rule.toLowerCase() == '@datetime'){
o.rule = '1994-11-25 00:34:56';
}else if(o.kind == 'string' && o.rule.toLowerCase() == '@ad_size'){
o.rule = '234x60';
}else if(o.kind == 'mock'){ // 用户如果使用 aaa|1:['a','b'] => aaa:'a'
eval('var _rule = '+ o.rule);
if(_rule instanceof Array && o.name.indexOf('|') != -1){
o.rule = _rule.length ? _rule[0] : '';
o.name = o.name.split('|')[0];
o.kind = 'string';
}
var responseParameters = body.responseParameters;
var hashObj = {};
for (var i in responseParameters) {
var o = responseParameters[i];
//规范返回的接口文档
if(o.kind == 'array(object)'){
o.name = o.name.split('|')[0]
}else if(o.kind == 'string' && o.rule.toLowerCase() == '@word'){
o.rule = 'xxxxxxx';
}else if(o.kind == 'string' && o.rule.toLowerCase() == '@email'){
o.rule = 'xxxxxxx@mock2easy.com';
}else if(o.kind == 'string' && o.rule.toLowerCase() == '@url'){
o.rule = 'www.npmjs.org/package/grunt-mock2easy';
}else if(o.kind == 'string' && o.rule.toLowerCase() == '@date'){
o.rule = '2013-02-11';
}else if(o.kind == 'string' && o.rule.toLowerCase() == '@time'){
o.rule = '17:48:42';
}else if(o.kind == 'string' && o.rule.toLowerCase() == '@datetime'){
o.rule = '1994-11-25 00:34:56';
}else if(o.kind == 'string' && o.rule.toLowerCase() == '@ad_size'){
o.rule = '234x60';
}else if(o.kind == 'string' && o.rule.toLowerCase() == '@ip'){
o.rule = '127.0.0.1';
}else if(o.kind == 'mock'){
eval('var _rule = '+ o.rule);
// 用户如果使用 aaa|1-100:10 => aaa:0
if(o.name.indexOf('|') != -1 && o.name.indexOf('-') != -1){
try{
parseInt(_rule);
o.rule = 0;
o.name = o.name.split('|')[0];
o.kind = 'string';
}catch (e){
}
}
hashObj[o.id] = o;
// 用户如果使用 aaa|1:['a','b'] => aaa:'a'
if(_rule instanceof Array && o.name.indexOf('|') != -1){
o.rule = _rule.length ? _rule[0] : '';
o.name = o.name.split('|')[0];
o.kind = 'string';
}
}
hashObj[o.id] = o;
}
var _str = responseParameters.length?JSON.stringify(Mock.mock(require('../util/response2json')(hashObj,grunt)), undefined,1):'';
var _md =['## ',
body.interfaceName,
,'\n',
'#### 接口类型\n',
'\t',body.interfaceType,'\n',
'#### 请求URL\n',
'\t',body.interfaceUrl,'\n',
_reqStr,
'#### 返回接口\n```js\n',
json2md(_str,getCoord(_str)),
'\n```'
];
var _str = responseParameters.length?JSON.stringify(Mock.mock(require('../util/response2json')(hashObj,grunt)), undefined,1):'';
var _md =['## ',
body.interfaceName,
,'\n',
'#### 接口类型\n',
'\t',body.interfaceType,'\n',
'#### 请求URL\n',
'\t',body.interfaceUrl,'\n',
_reqStr,
'#### 返回接口\n```js\n',
json2md(_str,getCoord(_str)),
'\n```'
];
var _arry = body.interfaceUrl.split('\/');
delete _arry[_arry.length-1];
deferred.resolve();
var _arry = body.interfaceUrl.split('\/');
delete _arry[_arry.length-1];
deferred.resolve();
var _urlArray = body.interfaceUrl.split('\/');
var _urlArray = body.interfaceUrl.split('\/');
createFile(path.resolve(global.options.doc)+body.interfaceUrl.replace(/.json/,'.md'),_md.join(''),grunt).then(function(){
deferred.resolve();
});
createFile(path.resolve(global.options.doc)+body.interfaceUrl.replace(/.json/,'.md'),_md.join(''),grunt).then(function(){
deferred.resolve();
},function(err){
deferred.reject(err);
});
}
}
return deferred.promise;
return deferred.promise;
}

@@ -15,2 +15,3 @@ /**

return function(url,method,params,jsonData,ignoreField){
var doWrite = false;
jsonData.reqError = [];

@@ -22,2 +23,3 @@ jsonData.docError = [];

jsonData.methodError ='请求类型异常请确认';
doWrite = true;
}

@@ -28,2 +30,3 @@

jsonData.reqError.push('请求参数['+o.name+']应该为必填项');
doWrite = true;
}

@@ -43,2 +46,3 @@ });

jsonData.docError.push('接口文档缺少['+ name +']做为请求参数');
doWrite = true;
}

@@ -66,15 +70,6 @@ }

}
fs.open(path.resolve(global.options.database)+ url,"w",0644,function(e,fd){
if(e){
grunt.log.write(e);
}{
fs.write(fd,JSON.stringify(jsonData,undefined,5),0,'utf8',function(e){
if(e) throw e;
fs.closeSync(fd);
})
}
});
if(doWrite){
fs.writeFileSync(path.resolve(global.options.database)+ url,JSON.stringify(jsonData,undefined,5));
}
}
};

@@ -25,3 +25,5 @@ /**

deferred.resolve();
});
},function(err){
deferred.reject(err);
});
}else{

@@ -31,5 +33,8 @@ mkdirp(_path, function (err) {

grunt.log.error(err);
deferred.reject(err);
}else{
require('./writeFile')(url,doc,grunt).then(function(){
deferred.resolve();
},function(err){
deferred.reject(err);
});

@@ -36,0 +41,0 @@ }

@@ -18,2 +18,3 @@ /**

grunt.log.error(err);
deferred.reject(err);
}else{

@@ -23,2 +24,3 @@ fs.write(fd,doc,0,'utf8',function(err){

grunt.log.error(err);
deferred.reject(err);
}else{

@@ -25,0 +27,0 @@ fs.closeSync(fd);

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc