UNPKG

r8s-cli

Version:

A command line tool for Reaction Commerce to be used with kubernetes

150 lines (118 loc) 4.96 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); var _reject2 = require('lodash/reject'); var _reject3 = _interopRequireDefault(_reject2); exports.default = function () { // get imports from each plugin directory var core = getImportPaths(corePlugins); var included = getImportPaths(includedPlugins); var custom = getImportPaths(customPlugins); // concat all imports var clientImports = [].concat(core.client, included.client, custom.client); var serverImports = [].concat(core.server, included.server, custom.server, core.registry, included.registry, custom.registry); var appRoot = _path2.default.resolve('.').split('.meteor')[0]; // create import files on client and server and write import statements generateImportsFile(appRoot + '/client/plugins.js', clientImports); generateImportsFile(appRoot + '/server/plugins.js', serverImports); }; var _fs = require('fs'); var _fs2 = _interopRequireDefault(_fs); var _path = require('path'); var _path2 = _interopRequireDefault(_path); var _child_process = require('child_process'); var _logger = require('./logger'); var _logger2 = _interopRequireDefault(_logger); var _fs3 = require('./fs'); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } // add a message to the top of the plugins import file var importFileMessage = '\n/**\n * ***** DO NOT EDIT THIS FILE MANUALLY *****\n * This file is generated automatically by the Reaction\n * plugin loader and will be reset at each startup.\n */\n\n'; /** * Create a plugin imports file on client or server * @param {String} file - absolute path to file to write * @param {Array} imports - array of import path strings * @return {Boolean} returns true if no error */ function generateImportsFile(file, imports) { // create/reset imports file try { _fs2.default.writeFileSync(file, ''); _fs2.default.writeFileSync(file, importFileMessage); } catch (e) { _logger2.default.error('Failed to reset plugins file at ' + file); process.exit(1); } // populate plugins file with imports imports.forEach(function (importPath) { try { _fs2.default.appendFileSync(file, 'import \'' + importPath + '\';\n'); } catch (e) { _logger2.default.error('Failed to write to plugins file at ' + importPath); process.exit(1); } }); } /** * Import Reaction plugins * @param {String} baseDirPath - path to a plugins sub-directory (core/included/custom) * @return {Object} - returns object with client, server, and registry path arrays */ function getImportPaths(baseDirPath) { // get app root path var appRoot = _path2.default.resolve('.').split('.meteor')[0]; // create the import path var getImportPath = function getImportPath(pluginFile) { var importPath = '/' + _path2.default.relative(appRoot, pluginFile); return importPath.replace(/\\/g, '/'); }; // get all plugin directories at provided base path // (ignore directories starting with a dot '.') var pluginDirs = (0, _reject3.default)((0, _fs3.getDirectories)(baseDirPath), function (d) { return d.charAt(0) === '.'; }); var clientImportPaths = []; var serverImportPaths = []; var registryImportPaths = []; // read registry.json and require server/index.js if they exist pluginDirs.forEach(function (plugin) { var clientImport = baseDirPath + plugin + '/client/index.js'; var serverImport = baseDirPath + plugin + '/server/index.js'; var registryImport = baseDirPath + plugin + '/register.js'; var packageDotJson = baseDirPath + plugin + '/package.json'; // import the client files if they exist if ((0, _fs3.exists)(clientImport)) { clientImportPaths.push(getImportPath(clientImport.replace('/index.js', ''))); } // import the server files if they exist if ((0, _fs3.exists)(serverImport)) { serverImportPaths.push(getImportPath(serverImport.replace('/index.js', ''))); } // import plugin registry files if ((0, _fs3.exists)(registryImport)) { registryImportPaths.push(getImportPath(registryImport)); } // run npm install if package.json exists if ((0, _fs3.exists)(packageDotJson)) { _logger2.default.info('Installing dependencies for ' + plugin + '...\n'); try { (0, _child_process.execSync)('cd ' + baseDirPath + plugin + ' && meteor npm i', { stdio: 'inherit' }); } catch (err) { _logger2.default.error('Failed to install npm dependencies for plugin: ' + plugin); process.exit(1); } } }); return { client: clientImportPaths, server: serverImportPaths, registry: registryImportPaths }; } /** * Define base plugin paths */ var pluginsPath = _path2.default.resolve('.').split('.meteor')[0] + '/imports/plugins/'; var corePlugins = pluginsPath + 'core/'; var includedPlugins = pluginsPath + 'included/'; var customPlugins = pluginsPath + 'custom/';