gulp-mock, gulp 下的 mock 插件 - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
DaniloSam
V2EX    Node.js

gulp-mock, gulp 下的 mock 插件

  •  1
     
  •   DaniloSam 2015-01-28 13:30:10 +08:00 4059 次点击
    这是一个创建于 3908 天前的主题,其中的信息可能已经有所发展或是发生改变。

    小弟前几天刚上传的小插件,欢迎大家批判性的试用

    npm

    github

    gulp-mock

    Gulp mock 插件 (模拟数据,测试用).

    安装

    npm install --save-dev gulp-mock 

    用法

    所有 mock 模版 必须 放在 source 路径中.

    所有生成的数据会被放入 api 路径中.

    必须指定 apiPath, sourcePath, dirName 这三个变量.

    // in gulpfile.js var gulp = require('gulp'), connect = require('gulp-connect'), mock = require('gulp-mock'); const sourcePath = 'source'; const apiPath = 'api'; mock.config({ sourcePath: sourcePath, // Mock template should put here. e.g: source/path/to/foo.json apiPath: apiPath, // Mocked files would be this path. e.g: api/path/to/foo.json dirName: __dirname }); gulp.task('api', function () { gulp.src(sourcePath + 'path/to/*.json') .pipe(mock()) // Just add this line to what ever files you wanna mocked. .pipe(gulp.dest(apiPath + 'path/to/')) .pipe(connect.reload()) }); gulp.task('default', ['api', 'server', 'watch']); 

    需要livereload和JSONP?

    Gulp-mock 使用 middleware 来处理livereload和JSONP请求.

    gulp.task('watch', function () { gulp.watch('source/**/*.json', ['api']); }); gulp.task('server', function () { connect.server({ port: 3000, livereload: true, middleware: function (connect, opt) { // Just add this line in middleware. Then you'll get jsonp support and live load. return [mock.middleware(connect, opt)]; } }); }); 

    Mock 模版

    { "data|2-10": { "code": "String:7", "percent": "String:7", "tels|2-5": "Number:13|x.xxxxxxxxxxxxxxx", "array|5-10": "String:7-12", "time": "Date|YYYY-MM-DD", "String:4|2-5": { "name": "String:7|xxx.xxxx", "time": "String:7", "String:2-5": "Number:10" }, "avatar": "Image|200x200", "thumbnails|2-5": "Image|200x300-400x500" } } 

    mock 后的数据如下:

    时间的 filter 使用 momment.js, see docs

    { data: [{ // "data|2-10" Should return a array, the array's length should within 2 to 10 code: "byother", // "String:7" Should return a string, the string's length should equal to 7. percent: "upinstr", tels: [ 7.558816198259, // "Number:13|x.xxxxxxxxxxxxxxx" Should return a number and transformed like 'x.xxxxxxxxxxxxxxx'. 4.997925737872, 4.765942785888 ], array: [ "oeverymajo", // "String:7-12" Should return a string, the string's length should within 7 to 12. "utplaysnbson", "lationabo", "actorso", "terandp", "nisedassha", "rtainhispla", "ainhigh" ], time: "2015-01-23", onal: [ // Random key name, in case sometime we need that. { name: "the.grea", // "String:7|xxx.xxxx" Just work. time: "reemine", dt: 3782816532 }, { name: "sus.anna", time: "tilabou", tth: 5975610705 }, { name: "ces.andc", time: "ndbroug", lon: 2345818821 }, { name: "rig.htsh", time: "eoftheb", clu: 6192378209 }, { name: "few.reco", time: "olarshi", hts: 4497031601 } ], avatar: "data:image/png;base64,iVBORw0KGg...", // "Image|200x200" Canvas Image, with 200px width and 200px height. thumbnails: [ "data:image/png;base64,iVBORw0KGg...", // "Image|200x300-400x500" Canvas Image, with 200px to 300px width and 400px to 500px height. "data:image/png;base64,iVBORw0KGg...", "data:image/png;base64,iVBORw0KGg...", "data:image/png;base64,iVBORwAADM...", "data:image/png;base64,iVBORw0KGg..." ] }] // and more } 

    同样可以使用如下方式直接返回数组:

    [{ "data": "..." }, "2-5"] 

    其会返回一个数组,长度介于2和5之间.
    你也可以省略 "2-5",会直接返回一个默认长度介于1到20之间的数组.

    目前尚无回复
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     2229 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 26ms UTC 16:05 PVG 00:05 LAX 09:05 JFK 12:05
    Do have faith in what you're doing.
    ubao snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86