|
| 1 | +const runCommand = require('../utility/run-command'); |
| 2 | +const path = require('path'); |
| 3 | +const fs = require('fs'); |
| 4 | +const createVueApp = require('@vue/cli/lib/create'); |
| 5 | +const runPrompts = require('../utility/prompts'); |
| 6 | +const templateCreator = require('../utility/template-creator'); |
| 7 | +const packageJsonUtils = require('../utility/package-json-utils'); |
| 8 | +const modifyJson = require('../utility/modify-json-file'); |
| 9 | +const insertItemToArray = require('../utility/file-content').insertItemToArray; |
| 10 | +const moduleUtils = require('../utility/module'); |
| 11 | +const stringUtils = require('../utility/string'); |
| 12 | +const defaultStyles = [ |
| 13 | + 'devextreme/dist/css/dx.light.css', |
| 14 | + 'devextreme/dist/css/dx.common.css' |
| 15 | +]; |
| 16 | +const layouts = [ |
| 17 | + { value: 'side-nav-outer-toolbar', title: 'Side navigation (outer toolbar)' }, |
| 18 | + { value: 'side-nav-inner-toolbar', title: 'Side navigation (inner toolbar)' } |
| 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-vue', 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: 'vue-router', version: '^3.0.1' } |
| 35 | + ]; |
| 36 | + const devDepends = [ |
| 37 | + { name: 'devextreme-cli', version: 'next' }, |
| 38 | + { name: 'sass-loader', version: '^7.1.0' } |
| 39 | + ]; |
| 40 | + const scripts = [ |
| 41 | + { name: 'build-themes', value: 'devextreme build' }, |
| 42 | + { name: 'postinstall', value: 'npm run build-themes' } |
| 43 | + ]; |
| 44 | + |
| 45 | + packageJsonUtils.addDependencies(packagePath, depends); |
| 46 | + packageJsonUtils.addDependencies(packagePath, devDepends, 'dev'); |
| 47 | + packageJsonUtils.updateScripts(packagePath, scripts); |
| 48 | + updateJsonPropName(packagePath, appName); |
| 49 | +}; |
| 50 | + |
| 51 | +const updateJsonPropName = (path, name) => { |
| 52 | + modifyJson(path, content => { |
| 53 | + content.name = name; |
| 54 | + |
| 55 | + return content; |
| 56 | + }); |
| 57 | +}; |
| 58 | + |
| 59 | +const getLayout = (options) => { |
| 60 | + const currentLayout = layouts.filter((layout) => { |
| 61 | + return layout.value === options.layout; |
| 62 | + }); |
| 63 | + |
| 64 | + return currentLayout.length ? [currentLayout[0].value] : undefined; |
| 65 | +}; |
| 66 | + |
| 67 | +const create = (appName, options) => { |
| 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 | + createVueApp(appName, { default: true }).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 | + layout: promptsResult.layout |
| 84 | + }); |
| 85 | + modifyIndexHtml(appPath, humanizedName); |
| 86 | + addTemplate(appPath, appName, templateOptions); |
| 87 | + }); |
| 88 | + }); |
| 89 | +}; |
| 90 | + |
| 91 | +const modifyIndexHtml = (appPath, appName) => { |
| 92 | + const indexHtmlPath = path.join(appPath, 'public', 'index.html'); |
| 93 | + let htmlContent = fs.readFileSync(indexHtmlPath).toString(); |
| 94 | + |
| 95 | + htmlContent = htmlContent.replace(/<title>(\w+\s*)+<\/title>/, `<title>${appName}<\/title>`); |
| 96 | + htmlContent = htmlContent.replace('<body>', '<body class="dx-viewport">'); |
| 97 | + fs.writeFileSync(indexHtmlPath, htmlContent); |
| 98 | +}; |
| 99 | + |
| 100 | +const addTemplate = (appPath, appName, templateOptions) => { |
| 101 | + const templateSourcePath = path.join(__dirname, '..', 'templates', 'vue', 'application'); |
| 102 | + const packagePath = path.join(appPath, 'package.json'); |
| 103 | + const styles = [ |
| 104 | + 'devextreme/dist/css/dx.common.css', |
| 105 | + './themes/generated/theme.additional.css', |
| 106 | + './themes/generated/theme.base.css']; |
| 107 | + |
| 108 | + templateCreator.moveTemplateFilesToProject(templateSourcePath, appPath, templateOptions); |
| 109 | + if(!templateOptions.empty) { |
| 110 | + addSamplePages(appPath); |
| 111 | + } |
| 112 | + preparePackageJsonForTemplate(packagePath, appName); |
| 113 | + install({}, appPath, styles); |
| 114 | +}; |
| 115 | + |
| 116 | +const install = (options, appPath, styles) => { |
| 117 | + appPath = appPath ? appPath : process.cwd(); |
| 118 | + const mainModulePath = path.join(appPath, 'src', 'main.js'); |
| 119 | + addStylesToApp(mainModulePath, 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 addSamplePages = (appPath) => { |
| 132 | + const templateSourcePath = path.join(__dirname, '..', 'templates', 'vue', 'sample-pages'); |
| 133 | + const pagesPath = createPathToPage(appPath); |
| 134 | + templateCreator.moveTemplateFilesToProject(templateSourcePath, pagesPath, {}); |
| 135 | +}; |
| 136 | + |
| 137 | +const getComponentPageName = (viewName) => { |
| 138 | + return `${stringUtils.classify(viewName)}`; |
| 139 | +}; |
| 140 | + |
| 141 | +const getVueRoute = (viewName, componentName, pagePath) => { |
| 142 | + const components = `{\n layout: defaultLayout,\n content: ${componentName}\n }\n `; |
| 143 | + return `{\n path: "/${pagePath}",\n name: "${stringUtils.dasherize(viewName)}",\n meta: { requiresAuth: true },\n components: ${components}}`; |
| 144 | +}; |
| 145 | + |
| 146 | +const getNavigationData = (viewName, componentName, icon) => { |
| 147 | + const pagePath = stringUtils.dasherize(viewName); |
| 148 | + return { |
| 149 | + route: getVueRoute(viewName, componentName, pagePath), |
| 150 | + navigation: `{\n text: \'${stringUtils.humanize(viewName)}\',\n path: \'/${pagePath}\',\n icon: \'${icon}\'\n }` |
| 151 | + }; |
| 152 | +}; |
| 153 | + |
| 154 | +const createPathToPage = (appPath) => { |
| 155 | + const pagesPath = path.join(appPath, 'src', 'views'); |
| 156 | + |
| 157 | + if(!fs.existsSync(pagesPath)) { |
| 158 | + fs.mkdirSync(pagesPath); |
| 159 | + } |
| 160 | + |
| 161 | + return pagesPath; |
| 162 | +}; |
| 163 | + |
| 164 | +const addView = (pageName, options) => { |
| 165 | + const componentName = getComponentPageName(pageName); |
| 166 | + const pathToPage = createPathToPage(process.cwd()); |
| 167 | + const pageTemplatePath = path.join(__dirname, '..', 'templates', 'vue', 'page'); |
| 168 | + const routingModulePath = path.join(process.cwd(), 'src', 'router.js'); |
| 169 | + const navigationModulePath = path.join(process.cwd(), 'src', 'app-navigation.js'); |
| 170 | + const navigationData = getNavigationData(pageName, componentName, options && options.icon || 'home'); |
| 171 | + |
| 172 | + templateCreator.addPageToApp(pageName, pathToPage, pageTemplatePath); |
| 173 | + moduleUtils.insertImport(routingModulePath, `./views/${pageName}`, componentName, true); |
| 174 | + insertItemToArray(routingModulePath, navigationData.route); |
| 175 | + insertItemToArray(navigationModulePath, navigationData.navigation); |
| 176 | +}; |
| 177 | + |
| 178 | +module.exports = { |
| 179 | + install, |
| 180 | + create, |
| 181 | + addTemplate, |
| 182 | + addView |
| 183 | +}; |
0 commit comments