|
| 1 | +/** |
| 2 | + Copyright (c) 2015, 2017, Oracle and/or its affiliates. |
| 3 | + The Universal Permissive License (UPL), Version 1.0 |
| 4 | +*/ |
| 5 | +'use strict'; |
| 6 | + |
| 7 | +const fs = require('fs-extra'); |
| 8 | +const path = require('path'); |
| 9 | +const CONSTANTS = require('../util/constants'); |
| 10 | +const paths = require('../util/paths'); |
| 11 | + |
| 12 | +module.exports = |
| 13 | +{ |
| 14 | + writeComponentTemplate: function _writeComponentTemplate(generator) { |
| 15 | + return new Promise((resolve) => { |
| 16 | + if (generator.options.component) { |
| 17 | + const templateSrc = path.join(generator.templatePath(), '../../../template/component'); |
| 18 | + const isApp = fs.existsSync(path.join(process.cwd(), CONSTANTS.APP_CONFIG_JSON)) |
| 19 | + || generator.appDir !== undefined; |
| 20 | + |
| 21 | + if (!isApp) return resolve(generator); |
| 22 | + |
| 23 | + const appDir = generator.appDir === undefined |
| 24 | + ? process.cwd() : generator.destinationPath(generator.appDir); |
| 25 | + |
| 26 | + const _configPaths = generator.appDir === undefined |
| 27 | + ? paths.getConfiguredPaths(appDir) : paths.getDefaultPaths(); |
| 28 | + |
| 29 | + const destDirectory = generator.destinationPath( |
| 30 | + path.join(appDir, _configPaths.source, |
| 31 | + _configPaths.sourceJavascript, CONSTANTS.JET_COMPOSITES, generator.options.component)); |
| 32 | + |
| 33 | + // avoid overwrite component |
| 34 | + if (fs.existsSync(destDirectory)) { |
| 35 | + console.log('Component already exists. '); |
| 36 | + return resolve(generator); |
| 37 | + } |
| 38 | + fs.ensureDirSync(destDirectory); |
| 39 | + fs.copySync(templateSrc, destDirectory); |
| 40 | + _replaceComponentTemplateToken(generator); |
| 41 | + } |
| 42 | + |
| 43 | + return resolve(generator); |
| 44 | + }); |
| 45 | + } |
| 46 | +}; |
| 47 | + |
| 48 | +function _replaceComponentTemplateToken(generator) { |
| 49 | + const componentName = generator.componentName || generator.options.component; |
| 50 | + |
| 51 | + const appDir = generator.appDir === undefined |
| 52 | + ? process.cwd() : generator.destinationPath(generator.appDir); |
| 53 | + |
| 54 | + const _configPaths = generator.appDir === undefined |
| 55 | + ? paths.getConfiguredPaths(appDir) : paths.getDefaultPaths(); |
| 56 | + |
| 57 | + const base = path.join(appDir, _configPaths.source, |
| 58 | + _configPaths.sourceJavascript, CONSTANTS.JET_COMPOSITES, componentName); |
| 59 | + |
| 60 | + CONSTANTS.COMPONENT_FILES.forEach((file) => { |
| 61 | + const fileContent = fs.readFileSync(path.join(base, file), 'utf-8'); |
| 62 | + fs.outputFileSync(path.join(base, file), fileContent.replace(new RegExp('@component@', 'g'), componentName)); |
| 63 | + }); |
| 64 | +} |
0 commit comments