UNPKG

generator-wf-control

Version:
235 lines (194 loc) 10.1 kB
'use strict'; Object.defineProperty(exports, '__esModule', { value: true }); var _slicedToArray = (function () { function sliceIterator(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i['return']) _i['return'](); } finally { if (_d) throw _e; } } return _arr; } return function (arr, i) { if (Array.isArray(arr)) { return arr; } else if (Symbol.iterator in Object(arr)) { return sliceIterator(arr, i); } else { throw new TypeError('Invalid attempt to destructure non-iterable instance'); } }; })(); var _createClass = (function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ('value' in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; })(); var _get = function get(_x, _x2, _x3) { var _again = true; _function: while (_again) { var object = _x, property = _x2, receiver = _x3; desc = parent = getter = undefined; _again = false; if (object === null) object = Function.prototype; var desc = Object.getOwnPropertyDescriptor(object, property); if (desc === undefined) { var parent = Object.getPrototypeOf(object); if (parent === null) { return undefined; } else { _x = parent; _x2 = property; _x3 = receiver; _again = true; continue _function; } } else if ('value' in desc) { return desc.value; } else { var getter = desc.get; if (getter === undefined) { return undefined; } return getter.call(receiver); } } }; function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError('Cannot call a class as a function'); } } function _inherits(subClass, superClass) { if (typeof superClass !== 'function' && superClass !== null) { throw new TypeError('Super expression must either be null or a function, not ' + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } require('babel-core/polyfill'); var _yeomanGenerator = require('yeoman-generator'); var _path = require('path'); var _path2 = _interopRequireDefault(_path); var _lodash = require('lodash'); var _lodash2 = _interopRequireDefault(_lodash); var WfControlGenerator = (function (_Base) { _inherits(WfControlGenerator, _Base); function WfControlGenerator() { _classCallCheck(this, WfControlGenerator); for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) { args[_key] = arguments[_key]; } _get(Object.getPrototypeOf(WfControlGenerator.prototype), 'constructor', this).apply(this, args); this.argument('controlName', { desc: 'WF control name', type: String, required: false }); this.option('type', { desc: 'The type of WF control. Could be equal to `generic` or `informer`', type: String }); } _createClass(WfControlGenerator, [{ key: 'initializing', value: function initializing() { // These checks should be performed in initializing() method instead of the constructor // because this.env.error() call in the constructor can’t be caught with Mocha if (this.controlName && !WfControlGenerator._validateControlName(this.controlName)) { this.env.error(WfControlGenerator._validationErrorMessages.controlName); } if (this.options.type && !WfControlGenerator._validateControlType(this.options.type)) { this.env.error(WfControlGenerator._validationErrorMessages.controlType); } } }, { key: 'prompting', value: function prompting() { var done; return regeneratorRuntime.async(function prompting$(context$2$0) { while (1) switch (context$2$0.prev = context$2$0.next) { case 0: done = this.async(); if (this.controlName) { context$2$0.next = 4; break; } context$2$0.next = 4; return regeneratorRuntime.awrap(this._promptControlName()); case 4: if (this.options.type) { context$2$0.next = 7; break; } context$2$0.next = 7; return regeneratorRuntime.awrap(this._promptControlType()); case 7: done(); case 8: case 'end': return context$2$0.stop(); } }, null, this); } }, { key: 'configuring', value: function configuring() { this.jsControlName = this.controlName.replace('-', '_'); } }, { key: 'writing', value: function writing() { var _this = this; var controlType = this.options.type; var templateFiles = Object.keys(WfControlGenerator._templateConfig[controlType]).map(function (kindName) { return controlType + '/' + kindName; }); templateFiles.forEach(function (kind) { _this._writeTemplate(kind, { controlName: _this.controlName, jsControlName: _this.jsControlName }); }); } }, { key: '_promptControlName', value: function _promptControlName() { var _this2 = this; return new Promise(function (resolve) { var currentDirName = _path2['default'].basename(_this2.destinationRoot()); _this2.prompt({ message: 'How would you like to name the control?', type: 'input', name: 'controlName', 'default': currentDirName, // `validate` callback should return `true` when everything is OK and error message otherwise validate: function validate(value) { return WfControlGenerator._validateControlName(value) ? true : WfControlGenerator._validationErrorMessages.controlName; } }, function (answer) { _this2.controlName = answer.controlName; resolve(); }); }); } }, { key: '_promptControlType', value: function _promptControlType() { var _this3 = this; return new Promise(function (resolve) { _this3.prompt({ message: 'What type does the control have?', type: 'list', choices: _lodash2['default'].keys(WfControlGenerator._templateConfig), name: 'type' }, function (answer) { _this3.options.type = answer.type; resolve(); }); }); } }, { key: '_writeTemplate', value: function _writeTemplate(kind, data) { var _kind$split = kind.split('/'); var _kind$split2 = _slicedToArray(_kind$split, 2); var kindGroup = _kind$split2[0]; var kindName = _kind$split2[1]; var templateConfig = WfControlGenerator._templateConfig[kindGroup][kindName]; var templateFile = templateConfig.file; var targetExtension = templateConfig.targetExtension; this.fs.copyTpl(this.templatePath(templateFile), this.destinationPath(this.controlName + targetExtension), data); } }], [{ key: '_validateControlName', value: function _validateControlName(name) { return (/^[a-z0-9_-]+$/i.test(name) ); } }, { key: '_validateControlType', value: function _validateControlType(type) { return Object.keys(WfControlGenerator._templateConfig).includes(type); } }]); return WfControlGenerator; })(_yeomanGenerator.Base); exports['default'] = WfControlGenerator; WfControlGenerator._templateConfig = { 'generic': { 'wf': { file: 'generic/index.wfc', targetExtension: '.wfc' }, 'less': { file: 'generic/index.less', targetExtension: '.less' }, 'jsm': { file: 'generic/index.jsm', targetExtension: '.jsm' } }, 'informer': { 'wf': { file: 'informer/index.wfec', targetExtension: '.wfec' }, 'less': { file: 'informer/index.less', targetExtension: '.less' }, 'jsm': { file: 'informer/index.jsm', targetExtension: '.jsm' } } }; WfControlGenerator._validationErrorMessages = { controlName: 'The control name is invalid. It must only contain latin letters, digits, underscore and dash.', controlType: 'The control type is invalid. Possible values are `generic` and `informer`.' }; module.exports = exports['default'];