|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const fs = require('fs-extra'); |
| 4 | +const os = require('os'); |
| 5 | +const path = require('path'); |
| 6 | +const fsCli = require('fs-cli-goodies'); |
| 7 | +const inquirer = require('inquirer'); |
| 8 | +const osUtils = require('./osUtils'); |
| 9 | + |
| 10 | +module.exports = { |
| 11 | + installFrontierDependencies, |
| 12 | + promptForConfig, |
| 13 | + packageJsonWritten, |
| 14 | + cleanupFrontierCode, |
| 15 | +}; |
| 16 | + |
| 17 | +async function promptForConfig() { |
| 18 | + console.log(fsCli.fsLogo('Frontier React Scripts')); |
| 19 | + const questions = [ |
| 20 | + { |
| 21 | + type: 'checkbox', |
| 22 | + name: 'additionalFeatures', |
| 23 | + message: 'What additional features does your app require', |
| 24 | + choices: [ |
| 25 | + { |
| 26 | + value: 'polymer', |
| 27 | + name: 'Using a shared Polymer Component?', |
| 28 | + }, |
| 29 | + { |
| 30 | + name: 'Redux', |
| 31 | + value: 'redux', |
| 32 | + }, |
| 33 | + ], |
| 34 | + }, |
| 35 | + ]; |
| 36 | + const answers = await inquirer.prompt(questions); |
| 37 | + return answers; |
| 38 | +} |
| 39 | + |
| 40 | +function packageJsonWritten() {} |
| 41 | + |
| 42 | +function installFrontierDependencies(appPath, answers, useYarn, ownPath) { |
| 43 | + const { additionalFeatures } = answers; |
| 44 | + |
| 45 | + if (additionalFeatures.includes('polymer')) { |
| 46 | + configurePolymer(appPath, useYarn); |
| 47 | + } |
| 48 | + if (additionalFeatures.includes('redux')) { |
| 49 | + configureRedux(appPath, useYarn, ownPath); |
| 50 | + } |
| 51 | + injectPolymerCode(appPath); |
| 52 | + |
| 53 | + const defaultModules = [ |
| 54 | + |
| 55 | + |
| 56 | + ]; |
| 57 | + installModulesSync(defaultModules, useYarn); |
| 58 | +} |
| 59 | + |
| 60 | +function configurePolymer(appPath, useYarn) { |
| 61 | + const appPackage = require(path.join(appPath, 'package.json')); |
| 62 | + appPackage.vendorCopy = [ |
| 63 | + { |
| 64 | + from: |
| 65 | + 'node_modules/@webcomponents/webcomponentsjs/custom-elements-es5-adapter.js', |
| 66 | + to: 'public/vendor/custom-elements-es5-adapter.js', |
| 67 | + }, |
| 68 | + { |
| 69 | + from: |
| 70 | + 'node_modules/@webcomponents/webcomponentsjs/webcomponents-bundle.js', |
| 71 | + to: 'public/vendor/webcomponents-bundle.js', |
| 72 | + }, |
| 73 | + ]; |
| 74 | + |
| 75 | + const { postinstall } = appPackage.scripts; |
| 76 | + appPackage.scripts.postinstall = postinstall |
| 77 | + ? `${postinstall} && ` |
| 78 | + : '' + 'vendor-copy'; |
| 79 | + |
| 80 | + fs.writeFileSync( |
| 81 | + path.join(appPath, 'package.json'), |
| 82 | + JSON.stringify(appPackage, null, 2) + os.EOL |
| 83 | + ); |
| 84 | + |
| 85 | + const polymerModules = [ |
| 86 | + |
| 87 | + '@webcomponents/[email protected]', |
| 88 | + ]; |
| 89 | + installModulesSync(polymerModules, useYarn, true); |
| 90 | +} |
| 91 | + |
| 92 | +function injectPolymerCode(appPath) { |
| 93 | + const indexPath = path.join(appPath, 'public/index.html'); |
| 94 | + const polymerCode = ` |
| 95 | + <script src="%PUBLIC_URL%/vendor/webcomponents-bundle.js"></script> |
| 96 | + <script src="%PUBLIC_URL%/vendor/custom-elements-es5-adapter.js"></script> |
| 97 | + `; |
| 98 | + |
| 99 | + let indexHtml = fs.readFileSync(indexPath, 'UTF8'); |
| 100 | + indexHtml = indexHtml.replace( |
| 101 | + '<!--FRONTIER WEBCOMPONENT LOADER CODE FRONTIER -->', |
| 102 | + polymerCode |
| 103 | + ); |
| 104 | + fs.writeFileSync(indexPath, indexHtml); |
| 105 | +} |
| 106 | + |
| 107 | +function configureRedux(appPath, useYarn, ownPath) { |
| 108 | + const reduxModules = [ |
| 109 | + |
| 110 | + |
| 111 | + |
| 112 | + |
| 113 | + ]; |
| 114 | + installModulesSync(reduxModules, useYarn); |
| 115 | + |
| 116 | + const templatePath = path.join(ownPath, 'template-redux'); |
| 117 | + fs.copySync(templatePath, appPath, { overwrite: true }); |
| 118 | +} |
| 119 | + |
| 120 | +function cleanupFrontierCode(appPath) {} |
| 121 | + |
| 122 | +function installModulesSync(modules, useYarn, saveDev = false) { |
| 123 | + const { command, args } = buildInstallCommandAndArgs(useYarn, saveDev); |
| 124 | + osUtils.runExternalCommandSync(command, args.concat(modules)); |
| 125 | +} |
| 126 | + |
| 127 | +function buildInstallCommandAndArgs(useYarn, saveDev = false) { |
| 128 | + let command; |
| 129 | + let args; |
| 130 | + if (useYarn) { |
| 131 | + command = 'yarnpkg'; |
| 132 | + args = ['add']; |
| 133 | + if (saveDev) { |
| 134 | + args.push('--dev'); |
| 135 | + } |
| 136 | + } else { |
| 137 | + command = 'npm'; |
| 138 | + args = ['install', '--save']; |
| 139 | + if (saveDev) { |
| 140 | + args[1] = '--save-dev'; |
| 141 | + } |
| 142 | + } |
| 143 | + return { command, args }; |
| 144 | +} |
0 commit comments