UNPKG

4.8 kBJavaScriptView Raw
1"use strict";function _interopDefault(e){return e&&"object"==typeof e&&"default"in e?e.default:e}var program=_interopDefault(require("commander")),chalk=_interopDefault(require("chalk")),path=_interopDefault(require("path")),execa=_interopDefault(require("execa")),Listr=_interopDefault(require("listr")),mkdirp=_interopDefault(require("mkdirp")),tar=_interopDefault(require("tar")),fs=_interopDefault(require("fs-extra")),flatMap=_interopDefault(require("lodash/flatMap")),kebabCase=_interopDefault(require("lodash/kebabCase")),inquirer=_interopDefault(require("inquirer"));const parseReplacementSpecTree=(e,a)=>Array.isArray(a)?{file:e,replacements:a}:flatMap(Object.entries(a),([a,t])=>parseReplacementSpecTree(path.join(e,a),t)),processFile=(e,a)=>async({file:t,replacements:r})=>{const o=await e(t,{encoding:"utf8",flag:"r"}),n=r.reduce((e,{regex:a,value:t})=>e.replace(a,t),o);return a(t,n,{encoding:"utf8",flag:"w"})},replaceInFiles=async(e,a,t=fs.readFile,r=fs.writeFile)=>Promise.all(parseReplacementSpecTree(e,a).map(processFile(t,r))),replaceSchemaWith=e=>a=>a.replace(/^(\w+:\/\/|)/,e),getDomainWithSchema=replaceSchemaWith("https://"),sanitizeAppName=kebabCase,verboseLog=e=>{console.log(`[VERBOSE]: ${e}`)},getTargetDirPath=(e,a)=>path.resolve(a||process.cwd(),e),copyAppFiles=async({targetDir:e})=>(mkdirp(e),tar.x({file:path.resolve(__dirname,"bootstrap.tgz"),strip:1,cwd:e})),performTemplateReplacements=({targetDir:e,sanitizedAppName:a,domain:t})=>{const r={"package.json":[{regex:/@gooddata\/gdc-app-name/,value:a}],src:{"constants.js":[{regex:/appName: "(.*?)"/,value:`appName: "${a}"`},{regex:/backend: "https:\/\/developer\.na\.gooddata\.com"/g,value:`backend: "${getDomainWithSchema(t)}"`}]}};return replaceInFiles(e,r)},runYarnInstall=({targetDir:e,install:a})=>a?execa("yarn",{cwd:e,stdio:[process.stdin,process.stdout,process.stderr]}).then(()=>!0).catch(()=>(console.log(chalk.red("Installation failed. Please make sure that you have yarn installed and try again.")),!1)):(console.log("Skipping installation because the --no-install flag was specified"),!0),outputFinalInstructions=({sanitizedAppName:e,install:a,targetDir:t})=>{console.log(`Success! Your GoodData-powered application "${e}" was created.`),console.log("You can start it using the following commands:"),console.log(chalk.cyan(` cd ${path.relative(process.cwd(),t)}`)),a||console.log(chalk.cyan(" yarn install")),console.log(chalk.cyan(" yarn start"))},main=async e=>{const a={...e,targetDir:getTargetDirPath(e.sanitizedAppName,e.targetDir)};a.verbose&&verboseLog(`Target directory: ${a.targetDir}`);const t=new Listr([{title:"Copy app files",task:()=>copyAppFiles(a)},{title:"Set up app",task:()=>performTemplateReplacements(a)}]);await t.run(),await runYarnInstall(a)&&outputFinalInstructions(a)},inquireName=()=>inquirer.prompt({message:"What is your application name?",name:"name",type:"input"}).then(e=>e.name),inquireDomain=()=>inquirer.prompt([{message:"What is your domain?",name:"domain",type:"list",choices:[{value:"secure.gooddata.com"},{value:"developer.na.gooddata.com"},{value:"salesengineering.na.gooddata.com"},{name:"I have a custom domain",value:"WHITE_LABELLED"}]},{message:"Insert your domain",name:"domain",type:"input",when:({domain:e})=>"WHITE_LABELLED"===e}]).then(e=>e.domain),getSanitizedAppName=async e=>{const a=e||await inquireName();if(!a)throw new Error("You must provide a name for your app");return sanitizeAppName(a)},getBootstrapData=async(e,{domainUrl:a})=>{return{sanitizedAppName:await getSanitizedAppName(e),domain:a||await inquireDomain()}},parseConfig=({domain:e,appName:a},t)=>{if(!e)throw new Error("You must provide a domain for your app");const r=sanitizeAppName(a||t);if(!r)throw new Error("You must provide a name for your app");return{sanitizedAppName:r,domain:e}},getBootstrapData$1=async(e,{config:a})=>{let t={};try{t=await fs.readJSON(a,{encoding:"utf8",flag:"r"})}catch(e){throw new Error("The config file specified using the -c option was not found. Please check that the path you provided is correct.")}return parseConfig(t,e)};process.on("unhandledRejection",e=>console.error(e.message)),program.name("@gooddata/create-react-app").arguments("[app-name]").option("-d, --domainUrl <domain>","URL of your GoodData domain").option("-c, --config <config>","path to configuration file").option("--target-dir <path>",'path to the directory to create the app in (default: ".")').option("--no-install","skip yarn installing the app dependencies").option("--verbose","output additional logs, useful mainly for debugging and bug reports").action(async e=>{const a=program.config?getBootstrapData$1:getBootstrapData,t=await a(e,program);return main({...t,install:program.install,targetDir:program.targetDir,verbose:program.verbose})}).parse(process.argv);