|
| 1 | +const runCommand = require('../utility/run-command'); |
| 2 | +const path = require('path'); |
| 3 | +const fs = require('fs'); |
| 4 | +const runPrompts = require('../utility/prompts'); |
| 5 | +const templateCreator = require('../utility/template-creator'); |
| 6 | +const packageJsonUtils = require('../utility/package-json-utils'); |
| 7 | +const modifyJson = require('../utility/modify-json-file'); |
| 8 | +const insertItemToArray = require('../utility/file-content').insertItemToArray; |
| 9 | +const moduleUtils = require('../utility/module'); |
| 10 | +const stringUtils = require('../utility/string'); |
| 11 | +const pathToPagesIndex = path.join(process.cwd(), 'src', 'pages', 'index.js'); |
| 12 | +const defaultStyles = [ |
| 13 | + 'devextreme/dist/css/dx.light.css', |
| 14 | + 'devextreme/dist/css/dx.common.css' |
| 15 | +]; |
| 16 | +const layouts = [ |
| 17 | + { fullName: 'side-nav-outer-toolbar', title: 'Side navigation (outer toolbar)', value: 'SideNavOuterToolbar' }, |
| 18 | + { fullName: 'side-nav-inner-toolbar', title: 'Side navigation (inner toolbar)', value: 'SideNavInnerToolbar' } |
| 19 | +]; |
| 20 | + |
| 21 | +const addDevextremeToPackageJson = (appPath, dxversion) => { |
| 22 | + const packagePath = path.join(appPath, 'package.json'); |
| 23 | + const depends = [ |
| 24 | + { name: 'devextreme', version: dxversion }, |
| 25 | + { name: 'devextreme-react', version: dxversion } |
| 26 | + ]; |
| 27 | + |
| 28 | + packageJsonUtils.addDependencies(packagePath, depends); |
| 29 | +}; |
| 30 | + |
| 31 | +const preparePackageJsonForTemplate = (packagePath, appName) => { |
| 32 | + const depends = [ |
| 33 | + { name: 'node-sass', version: '^4.11.0' }, |
| 34 | + { name: 'react-router-dom', version: '^5.0.0' } |
| 35 | + ]; |
| 36 | + const devDepends = [ |
| 37 | + { name: 'devextreme-cli', version: 'latest' } |
| 38 | + ]; |
| 39 | + const scripts = [ |
| 40 | + { name: 'build-themes', value: 'devextreme build' }, |
| 41 | + { name: 'postinstall', value: 'npm run build-themes' } |
| 42 | + ]; |
| 43 | + |
| 44 | + packageJsonUtils.addDependencies(packagePath, depends); |
| 45 | + packageJsonUtils.addDependencies(packagePath, devDepends, 'dev'); |
| 46 | + packageJsonUtils.updateScripts(packagePath, scripts); |
| 47 | + updateJsonPropName(packagePath, appName); |
| 48 | +}; |
| 49 | + |
| 50 | +const updateJsonPropName = (path, name) => { |
| 51 | + modifyJson(path, content => { |
| 52 | + content.name = name; |
| 53 | + |
| 54 | + return content; |
| 55 | + }); |
| 56 | +}; |
| 57 | + |
| 58 | +const getLayout = (options) => { |
| 59 | + const currentLayout = layouts.filter((layout) => { |
| 60 | + return layout.fullName === options.layout; |
| 61 | + }); |
| 62 | + |
| 63 | + return currentLayout.length ? [currentLayout[0].value] : undefined; |
| 64 | +}; |
| 65 | + |
| 66 | +const create = (appName, options) => { |
| 67 | + const commandArguments = ['create-react-app', appName]; |
| 68 | + const prompts = [ |
| 69 | + { |
| 70 | + type: 'select', |
| 71 | + name: 'layout', |
| 72 | + message: 'What layout do you want to add?', |
| 73 | + choices: layouts |
| 74 | + } |
| 75 | + ]; |
| 76 | + |
| 77 | + runPrompts(options, prompts, getLayout).then((promptsResult) => { |
| 78 | + runCommand('npx', commandArguments).then(() => { |
| 79 | + const appPath = path.join(process.cwd(), appName); |
| 80 | + const humanizedName = stringUtils.humanize(appName); |
| 81 | + const templateOptions = Object.assign({}, options, { |
| 82 | + project: humanizedName, |
| 83 | + skipFolder: options.empty ? 'pages' : '', |
| 84 | + layout: promptsResult.layout |
| 85 | + }); |
| 86 | + modifyIndexHtml(appPath, humanizedName); |
| 87 | + addTemplate(appPath, appName, templateOptions); |
| 88 | + }); |
| 89 | + }); |
| 90 | +}; |
| 91 | + |
| 92 | +const modifyIndexHtml = (appPath, appName) => { |
| 93 | + const indexHtmlPath = path.join(appPath, 'public', 'index.html'); |
| 94 | + let htmlContent = fs.readFileSync(indexHtmlPath).toString(); |
| 95 | + |
| 96 | + htmlContent = htmlContent.replace(/<title>(\w+\s*)+<\/title>/, `<title>${appName}<\/title>`); |
| 97 | + htmlContent = htmlContent.replace('<body>', '<body class="dx-viewport">'); |
| 98 | + fs.writeFileSync(indexHtmlPath, htmlContent); |
| 99 | +}; |
| 100 | + |
| 101 | +const addTemplate = (appPath, appName, templateOptions) => { |
| 102 | + const templateSourcePath = path.join(__dirname, '..', 'templates', 'react', 'application'); |
| 103 | + const packagePath = path.join(appPath, 'package.json'); |
| 104 | + const manifestPath = path.join(appPath, 'public', 'manifest.json'); |
| 105 | + const styles = [ |
| 106 | + 'devextreme/dist/css/dx.common.css', |
| 107 | + './themes/generated/theme.additional.css', |
| 108 | + './themes/generated/theme.base.css']; |
| 109 | + |
| 110 | + templateCreator.moveTemplateFilesToProject(templateSourcePath, appPath, templateOptions); |
| 111 | + preparePackageJsonForTemplate(packagePath, appName); |
| 112 | + updateJsonPropName(manifestPath, appName); |
| 113 | + install({}, appPath, styles); |
| 114 | +}; |
| 115 | + |
| 116 | +const install = (options, appPath, styles) => { |
| 117 | + appPath = appPath ? appPath : process.cwd(); |
| 118 | + const pathToMainComponent = path.join(appPath, 'src', 'App.js'); |
| 119 | + addStylesToApp(pathToMainComponent, styles || defaultStyles); |
| 120 | + addDevextremeToPackageJson(appPath, options.dxversion || 'latest'); |
| 121 | + |
| 122 | + runCommand('npm', ['install'], { cwd: appPath }); |
| 123 | +}; |
| 124 | + |
| 125 | +const addStylesToApp = (filePath, styles) => { |
| 126 | + styles.forEach(style => { |
| 127 | + moduleUtils.insertImport(filePath, style); |
| 128 | + }); |
| 129 | +}; |
| 130 | + |
| 131 | +const getComponentPageName = (viewName) => { |
| 132 | + return `${stringUtils.classify(viewName)}Page`; |
| 133 | +}; |
| 134 | + |
| 135 | +const getNavigationData = (viewName, componentName, icon) => { |
| 136 | + const pagePath = stringUtils.dasherize(viewName); |
| 137 | + return { |
| 138 | + route: `\n{\n path: \'/${pagePath}\',\n component: ${componentName}\n }`, |
| 139 | + navigation: `{\n text: \'${stringUtils.humanize(viewName)}\',\n path: \'/${pagePath}\',\n icon: \'${icon}\'\n }` |
| 140 | + }; |
| 141 | +}; |
| 142 | + |
| 143 | +const createPathToPage = (pageName) => { |
| 144 | + const pagesPath = path.join(process.cwd(), 'src', 'pages'); |
| 145 | + const newPagePath = path.join(pagesPath, pageName); |
| 146 | + |
| 147 | + if(!fs.existsSync(pagesPath)) { |
| 148 | + fs.mkdirSync(pagesPath); |
| 149 | + createPagesIndex(); |
| 150 | + } |
| 151 | + |
| 152 | + if(fs.existsSync(newPagePath)) { |
| 153 | + console.error('The page already exists'); |
| 154 | + process.exit(); |
| 155 | + } |
| 156 | + |
| 157 | + fs.mkdirSync(newPagePath); |
| 158 | + |
| 159 | + return newPagePath; |
| 160 | +}; |
| 161 | + |
| 162 | +const createPagesIndex = () => { |
| 163 | + fs.writeFileSync(pathToPagesIndex, ''); |
| 164 | +}; |
| 165 | + |
| 166 | +const addView = (pageName, options) => { |
| 167 | + const componentName = getComponentPageName(pageName); |
| 168 | + const pathToPage = createPathToPage(pageName); |
| 169 | + const pageTemplatePath = path.join(__dirname, '..', 'templates', 'react', 'page'); |
| 170 | + const routingModulePath = path.join(process.cwd(), 'src', 'app-routes.js'); |
| 171 | + const navigationModulePath = path.join(process.cwd(), 'src', 'app-navigation.js'); |
| 172 | + const navigationData = getNavigationData(pageName, componentName, options && options.icon || 'home'); |
| 173 | + |
| 174 | + templateCreator.addPageToApp(pageName, pathToPage, pageTemplatePath); |
| 175 | + moduleUtils.insertExport(pathToPagesIndex, componentName, `./${pageName}/${pageName}`); |
| 176 | + moduleUtils.insertImport(routingModulePath, './pages', componentName); |
| 177 | + insertItemToArray(routingModulePath, navigationData.route); |
| 178 | + insertItemToArray(navigationModulePath, navigationData.navigation); |
| 179 | +}; |
| 180 | + |
| 181 | +module.exports = { |
| 182 | + install, |
| 183 | + create, |
| 184 | + addTemplate, |
| 185 | + addView |
| 186 | +}; |
0 commit comments