r8s-cli
Version:
A command line tool for Reaction Commerce to be used with kubernetes
145 lines (117 loc) • 4.84 kB
JavaScript
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = function () {
// get style imports from each plugin directory
var core = getImportPaths(corePlugins);
var included = getImportPaths(includedPlugins);
var custom = getImportPaths(customPlugins);
// concat all imports
var cssImports = [].concat(core.css, included.css, custom.css);
var lessImports = [].concat(core.less, included.less, custom.less);
var stylusImports = [].concat(core.stylus, included.stylus, custom.stylus);
var scssImports = [].concat(core.scss, included.scss, custom.scss);
var appRoot = _path2.default.resolve('.').split('.meteor')[0];
// create style import files on client and write import statements
generateImportsFile(appRoot + '/client/plugins.css', cssImports);
generateImportsFile(appRoot + '/client/plugins.less', lessImports);
generateImportsFile(appRoot + '/client/plugins.styl', stylusImports);
generateImportsFile(appRoot + '/client/plugins.scss', scssImports);
};
var _fs = require('fs');
var _fs2 = _interopRequireDefault(_fs);
var _path = require('path');
var _path2 = _interopRequireDefault(_path);
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 * style 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) {
// Don't create a file if there is nothing to import.
// This prevents the need to have to include all
// css preprocessors since CSS / LESS is predominately used
if (imports.length) {
// 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
var pluginDirs = (0, _fs3.getDirectories)(baseDirPath);
var cssImportPaths = [];
var lessImportPaths = [];
var stylusImportPaths = [];
var scssImportPaths = [];
// read registry.json and require server/index.js if they exist
pluginDirs.forEach(function (plugin) {
var cssImport = baseDirPath + plugin + '/client/index.css';
var lessImport = baseDirPath + plugin + '/client/index.less';
var stylusImport = baseDirPath + plugin + '/client/index.styl';
var scssImport = baseDirPath + plugin + '/client/index.scss';
// import the client CSS files if they exist
if ((0, _fs3.exists)(cssImport)) {
cssImportPaths.push(getImportPath(cssImport));
}
// import the client LESS files if they exist
if ((0, _fs3.exists)(lessImport)) {
lessImportPaths.push(getImportPath(lessImport));
}
// import the client STYLUS files if they exist
if ((0, _fs3.exists)(stylusImport)) {
stylusImportPaths.push(getImportPath(stylusImport));
}
// import the client SCSS files if they exist
if ((0, _fs3.exists)(scssImport)) {
scssImportPaths.push(getImportPath(scssImport));
}
});
return {
css: cssImportPaths,
less: lessImportPaths,
stylus: stylusImportPaths,
scss: scssImportPaths
};
}
/**
* 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/';