diff --git a/lib/live_reload/index.js b/lib/live_reload/index.js index 45b4337..1476064 100644 --- a/lib/live_reload/index.js +++ b/lib/live_reload/index.js @@ -1,9 +1,14 @@ + 'use strict'; var _path = require('path'); var _path2 = _interopRequireDefault(_path); +var _koaBrowserSync = require('koa-browser-sync'); + +var _koaBrowserSync2 = _interopRequireDefault(_koaBrowserSync); + function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } var __root = fozy.__root; @@ -15,7 +20,7 @@ files2Watch = files2Watch.concat(config.watch.map(function (item) { return _path2.default.join(__root, item); })); -module.exports = require('koa-browser-sync')({ +module.exports = (0, _koaBrowserSync2.default)({ init: true, files: files2Watch, notify: false, diff --git a/lib/mock/modules/local_mock/async_data.js b/lib/mock/modules/local_mock/async_data.js index 5d4aa2e..6d170b8 100644 --- a/lib/mock/modules/local_mock/async_data.js +++ b/lib/mock/modules/local_mock/async_data.js @@ -32,55 +32,44 @@ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { de function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } var __root = fozy.__root; -var config = require(_path2.default.join(__root, 'fozy.config')); +var config = fozy.__config; var AsyncData = function () { function AsyncData(ctx) { _classCallCheck(this, AsyncData); - this.files = {}; this.ctx = ctx; + this.filePath = this.getFilePath(ctx) || ''; this.data = {}; } _createClass(AsyncData, [{ key: 'getData', value: function getData() { - var files = this.files = this.getFiles(this.ctx); - - var data = this.data = new _local_data2.default({ - path: files.json + this.data = new _local_data2.default({ + path: this.filePath }).getData(); - return this.processData(data); + return this.processData(this.data, this.ctx); } }, { key: 'processData', - value: function processData(data) { - var ctx = this.ctx; + value: function processData(data, ctx) { var proc = new _jsonProcessor2.default({ - module: this.files.js + '.js', + module: this.filePath + '.js', preStringify: false }); return proc.process(data || {}, ctx.request.body, _querystring2.default.parse(ctx.url.split('?')[1]), ctx); } }, { - key: 'getFiles', - value: function getFiles(ctx) { - var files = {}; + key: 'getFilePath', + value: function getFilePath(ctx) { var url = _extend2.default.removePostfix(ctx.url.split('?')[0]); var method = ctx.method.toLocaleLowerCase(); var root = _path2.default.join(__root, config.mock.api.root, method && config.mock.api[method]); - var fileName = config.mock.fileName; - if (!fileName) { - files.json = _path2.default.join(root, url); - files.js = _path2.default.join(root, url); - } else { - files.json = _path2.default.join(root, url, fileName); - files.js = _path2.default.join(root, url, fileName); - } - return files; + var fileName = config.mock.fileName || ''; + return _path2.default.join(root, url, fileName); } }]); diff --git a/lib/mock/modules/template_engine/global_sync_data.js b/lib/mock/modules/template_engine/global_sync_data.js index b2d0a14..388f8fb 100644 --- a/lib/mock/modules/template_engine/global_sync_data.js +++ b/lib/mock/modules/template_engine/global_sync_data.js @@ -20,7 +20,7 @@ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { de function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } var __root = fozy.__root; -var config = require(_path2.default.join(__root, 'fozy.config')); +var config = fozy.__config; var globalJsonPath = _path2.default.join(__root, config.template.mock, '__global/data'); var GlobalSyncData = function () { diff --git a/lib/mock/modules/template_engine/index.js b/lib/mock/modules/template_engine/index.js index 97408c4..6fbb4b4 100644 --- a/lib/mock/modules/template_engine/index.js +++ b/lib/mock/modules/template_engine/index.js @@ -22,7 +22,7 @@ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { de function _asyncToGenerator(fn) { return function () { var gen = fn.apply(this, arguments); return new Promise(function (resolve, reject) { function step(key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { return Promise.resolve(value).then(function (value) { step("next", value); }, function (err) { step("throw", err); }); } } return step("next"); }); }; } var __root = fozy.__root; -var config = require(_path2.default.join(__root, 'fozy.config')); +var config = fozy.__config; var templateRoot = _path2.default.join(__root, config.template.root || ''); var engine = void 0; diff --git a/lib/mock/modules/template_engine/sync_data.js b/lib/mock/modules/template_engine/sync_data.js index 1aa1bc6..2022804 100644 --- a/lib/mock/modules/template_engine/sync_data.js +++ b/lib/mock/modules/template_engine/sync_data.js @@ -36,7 +36,7 @@ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { de function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } var __root = fozy.__root; -var config = require(_path3.default.join(__root, 'fozy.config')); +var config = fozy.__config; var MockData = function () { function MockData(option) { diff --git a/src/live_reload/index.js b/src/live_reload/index.js index 8b462aa..0d99664 100644 --- a/src/live_reload/index.js +++ b/src/live_reload/index.js @@ -1,4 +1,9 @@ + +'use strict'; + import path from 'path'; +import koaBrowserSync from 'koa-browser-sync'; + const __root = fozy.__root; const config = fozy.__config; @@ -8,7 +13,8 @@ files2Watch = files2Watch.concat(config.watch.map(function(item){ return path.join(__root, item); })); -module.exports = require('koa-browser-sync')({ + +module.exports = koaBrowserSync({ init: true, files: files2Watch, notify: false, @@ -16,5 +22,3 @@ module.exports = require('koa-browser-sync')({ online: false, open: config.autoOpen, }); - - diff --git a/src/mock/modules/local_mock/async_data.js b/src/mock/modules/local_mock/async_data.js index 92368e2..c53256b 100644 --- a/src/mock/modules/local_mock/async_data.js +++ b/src/mock/modules/local_mock/async_data.js @@ -4,7 +4,7 @@ import path from 'path'; import _ from '../../../util/extend'; const __root = fozy.__root; -const config = require(path.join(__root, 'fozy.config')); +const config = fozy.__config; import qs from 'querystring'; import JSONProcessor from '../../../util/json.processor.js'; @@ -12,25 +12,22 @@ import LocalData from '../local_data'; export default class AsyncData { constructor(ctx) { - this.files = {}; this.ctx = ctx; + this.filePath = this.getFilePath(ctx) || ''; this.data = {}; } getData() { - let files = this.files = this.getFiles(this.ctx); - - let data = this.data = new LocalData({ - path: files.json, + this.data = new LocalData({ + path: this.filePath, }).getData(); - return this.processData(data); + return this.processData(this.data, this.ctx); } - processData(data) { - let ctx = this.ctx; + processData(data, ctx) { let proc = new JSONProcessor({ - module: this.files.js + '.js', + module: this.filePath + '.js', preStringify: false, }); @@ -42,20 +39,12 @@ export default class AsyncData { ); } - getFiles(ctx){ - let files = {}; + getFilePath(ctx){ let url = _.removePostfix(ctx.url.split('?')[0]); let method = ctx.method.toLocaleLowerCase(); let root = path.join(__root, config.mock.api.root, method && config.mock.api[method]); - let fileName = config.mock.fileName; - if(!fileName) { - files.json = path.join(root, url); - files.js = path.join(root, url); - } else { - files.json = path.join(root, url, fileName); - files.js = path.join(root, url, fileName); - } - return files; + let fileName = config.mock.fileName || ''; + return path.join(root, url, fileName); } } diff --git a/src/mock/modules/template_engine/global_sync_data.js b/src/mock/modules/template_engine/global_sync_data.js index af056eb..8a3c1db 100644 --- a/src/mock/modules/template_engine/global_sync_data.js +++ b/src/mock/modules/template_engine/global_sync_data.js @@ -5,7 +5,7 @@ import path from 'path'; import LocalData from '../local_data'; const __root = fozy.__root; -const config = require(path.join(__root, 'fozy.config')); +const config = fozy.__config; const globalJsonPath = path.join(__root, config.template.mock, '__global/data'); export default class GlobalSyncData { diff --git a/src/mock/modules/template_engine/index.js b/src/mock/modules/template_engine/index.js index b66d6c0..238eed5 100644 --- a/src/mock/modules/template_engine/index.js +++ b/src/mock/modules/template_engine/index.js @@ -4,7 +4,7 @@ import path from 'path'; import _ from '../../../util/extend'; const __root = fozy.__root; -const config = require(path.join(__root, 'fozy.config')); +const config = fozy.__config; const templateRoot = path.join(__root, config.template.root || ''); import SyncData from './sync_data.js'; diff --git a/src/mock/modules/template_engine/sync_data.js b/src/mock/modules/template_engine/sync_data.js index 9707428..33269fd 100644 --- a/src/mock/modules/template_engine/sync_data.js +++ b/src/mock/modules/template_engine/sync_data.js @@ -4,7 +4,7 @@ import _path from 'path'; import _ from '../../../util/extend'; const __root = fozy.__root; -const config = require(_path.join(__root, 'fozy.config')); +const config = fozy.__config; import qs from 'querystring'; import LocalData from '../local_data';