UNPKG

ng-apimock-with-presets

Version:

An ng-apimock fork with preset functionality

94 lines 3.6 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var async = require("async"); var chokidar = require("chokidar"); var fs = require("fs-extra"); var processor_1 = require("./processor"); (module).exports = function () { 'use strict'; var processor = new processor_1.default(); var defaults = { destination: '.tmp/mocks/' }; var utils = require('../lib/utils.js'); return { run: run, watch: watch }; function run(configuration) { if (configuration === undefined) { console.error('No configuration has been specified.'); throw new Error('No configuration has been specified.'); } if (configuration.src === undefined) { console.error('No mock source directory have been specified.'); throw new Error('No mock source directory have been specified.'); } var config = { source: configuration.src, done: configuration.done !== undefined ? configuration.done : function () { }, destination: configuration.outputDir !== undefined ? configuration.outputDir : defaults.destination }; var mocks; var presets; async.series({ processMocks: function (callback) { console.info('Process all the mocks'); mocks = processor.processMocks(config.source); callback(null, 'processed mocks'); }, registerMocks: function (callback) { console.info('Register mocks'); utils.registerMocks(mocks); callback(null, 'registered mocks'); }, processPresets: function (callback) { console.info('Process all the presets'); presets = processor.processPresets(config.source); callback(null, 'processed presets'); }, registerPresets: function (callback) { console.info('Register presets'); utils.registerPresets(presets); callback(null, 'registered presets'); }, generateMockingInterface: function (callback) { console.info('Generate the mocking web interface'); processor.generateMockingInterface(config.destination); callback(null, 'generated mocking interface'); }, generateProtractorMock: function (callback) { console.info('Generate protractor.mock.js'); processor.generateProtractorMock(config.destination); callback(null, 200); } }, function (err) { if (err !== undefined && err !== null) { console.error(err); } config.done(); }); } function watch(directory) { console.info('Watching', directory, 'for changes'); var watcher = chokidar.watch(directory + '/**/*.json', { ignored: /[\/\\]\./, persistent: true }); watcher .on('add', function (path) { return _processMock(path); }) .on('change', function (path) { return _processMock(path); }) .on('unlink', function (path) { return _processMock(path); }); } function _processMock(file) { try { var mock = fs.readJsonSync(file); utils.updateMock(mock); } catch (ex) { console.info(file, 'contains invalid json'); } } }; //# sourceMappingURL=index.js.map