UNPKG

@bpanel/bpanel-cli

Version:

CLI tool for building bpanel plugins

119 lines (91 loc) 5.5 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); exports.setupModule = exports.modulesWithTarget = exports.setupTheme = exports.setupDeps = exports.insertText = exports.listToArray = exports.makePath = undefined; exports.checkExistence = checkExistence; exports.npmExists = npmExists; var _path = require('path'); var _fsExtra = require('fs-extra'); var _fsExtra2 = _interopRequireDefault(_fsExtra); var _assert = require('assert'); var _assert2 = _interopRequireDefault(_assert); var _underscore = require('underscore'); var _prettier = require('prettier'); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } const { URL } = require('url'); const brq = require('brq'); const makePath = exports.makePath = str => str.toLowerCase().replace(/ /g, '-'); const listToArray = exports.listToArray = list => list.replace(/ /g, '').split(','); const camelize = str => str // 1st deal w/ scoped packages (e.g. remove `@bpanel/`) .replace(/^\W+[\w]+[\W]/, '').replace(/_/g, '-').replace(/(?:^\w|[A-Z]|\b\w)/g, function (letter, index) { return index == 0 ? letter.toLowerCase() : letter.toUpperCase(); }).replace(/[^\w]/gi, ''); // Will return mutated string with str being inserted in destination // at specified target const insertText = exports.insertText = (str, destination, target) => { const index = destination.indexOf(target); (0, _assert2.default)(index > -1, 'Target in destination for string insertion did not exit'); const beginning = destination.slice(0, index); const end = destination.slice(index); return (0, _prettier.format)(beginning + str + '\n' + end, { parser: 'babylon' }); }; const setupDeps = exports.setupDeps = async (deps_, pluginRoot) => { const deps = Array.isArray(deps_) ? deps_ : listToArray(deps_); let pluginsIndex = ''; let pluginsList = ' '; deps.forEach(async dep => { const camelized = camelize(dep); pluginsList += `${camelized}, `; pluginsIndex += `import * as ${camelized} from '${dep}'; \n`; }); pluginsIndex += `\nexport default {${pluginsList}};`; pluginsIndex = (0, _prettier.format)(pluginsIndex, { singleQuote: true, parser: 'babylon' }); if (!_fsExtra2.default.existsSync((0, _path.resolve)(pluginRoot, 'lib'))) _fsExtra2.default.mkdirSync((0, _path.resolve)(pluginRoot, 'lib')); if (!_fsExtra2.default.existsSync((0, _path.resolve)(pluginRoot, 'dist'))) _fsExtra2.default.mkdirSync((0, _path.resolve)(pluginRoot, 'dist')); await _fsExtra2.default.appendFile((0, _path.resolve)(pluginRoot, 'lib', 'plugins.js'), pluginsIndex); return { import: (0, _prettier.format)(`import modules from './plugins';`), arr: (0, _prettier.format)(`const plugins = Object.values(modules);`, { parser: 'babylon' }), export: (0, _prettier.format)(`export const pluginConfig = {plugins};`, { parser: 'babylon' }) }; }; const setupTheme = exports.setupTheme = async (indexText_, pluginRoot) => { let indexText = indexText_; let configText = (0, _prettier.format)(`// Configuration for your plugin theme.` + `\n` + `// The "skeleton" of your styles` + `\n\n` + `import themeVariables from './themeVariables';` + `\n\n` + `const { } = themeVariables; // import the variables for your config here` + `\n\n` + `// setup your configs here, e.g. \`app\` or \`sidebar\` objects` + `\n` + `const themeCreator = defaultThemeVariables => {\n return {}; \n};` + `\n\n` + `export default themeCreator;` + `\n`); let varText = (0, _prettier.format)(`// Set the variables that you would like to update for your theme \n\n` + `// Setup variables object to be imported in your themeConfigs \n` + `const themeVariables = {}; \n\n` + `export default themeVariables;\n`, { parser: 'babylon' }); const themeImports = (0, _prettier.format)(`import themeVariables from './themeVariables';` + `\n` + `import themeConfig from './themeConfig';` + `\n`, { parser: 'babylon' }); const themeExport = (0, _prettier.format)(`export const decorateTheme = themeCreator => () =>` + `\n` + ` themeCreator(themeVariables, themeConfig);` + `\n`, { parser: 'babylon' }); await Promise.all([_fsExtra2.default.appendFile((0, _path.resolve)(pluginRoot, 'lib', 'themeConfig.js'), configText), _fsExtra2.default.appendFile((0, _path.resolve)(pluginRoot, 'lib', 'themeVariables.js'), varText)]); return { imports: themeImports, exports: themeExport }; }; const modulesWithTarget = exports.modulesWithTarget = ['decorateComponent', 'mapComponentState', 'mapComponentDispatch']; const setupModule = exports.setupModule = targetComponent => module => { let moduleTemplate = _fsExtra2.default.readFileSync((0, _path.resolve)(__dirname, '../indexTemplates/', `${module}.txt`), 'utf8'); if (targetComponent) { moduleTemplate = (0, _underscore.template)(moduleTemplate); return moduleTemplate({ targetComponent }); } return moduleTemplate; }; function checkExistence(name, list = [], local = false) { const exists = list.some(item => item === name); if (exists) return { message: `${name} already exists in ${local ? 'local ' : ''}plugins config` }; return false; } async function npmExists(_packageName) { const packageName = _packageName.toString(); const base = 'https://www.npmjs.org/package/'; const { href: path } = new URL(packageName, base); const response = await brq({ url: path, method: 'HEAD' }); if (200 <= response.statusCode && response.statusCode < 400) return true; return false; }