diff --git a/index.js b/index.js index e853486..2fa54e7 100755 --- a/index.js +++ b/index.js @@ -49,7 +49,7 @@ const argv = require('yargs') 'Display the create-react-storefront version and exit', `create-react-storefront v${getPackageJsonVersion()}` ) - .command('$0 ', 'Creates a new React Storefront app.', yargs => { + .command('$0 [app-name]', 'Creates a new React Storefront app.', yargs => { const returnedYargs = yargs .positional('app-name', { describe: 'A name for the new app', diff --git a/lib/connectors.js b/lib/connectors.js new file mode 100644 index 0000000..cf543e5 --- /dev/null +++ b/lib/connectors.js @@ -0,0 +1,2 @@ +// List all supported connector packages here. +module.exports = [{ title: 'Magento 2', value: 'react-storefront-magento2-connector' }] diff --git a/lib/create-react-storefront-internal.js b/lib/create-react-storefront-internal.js index fafe70a..ae936c5 100644 --- a/lib/create-react-storefront-internal.js +++ b/lib/create-react-storefront-internal.js @@ -4,6 +4,7 @@ const { copyResources, processReactStorefrontConfigJsons, processPackageJson, + processNextConfig, } = require('./template-processing') const { retrieveTemplate } = require('./retrieve-template') const ora = require('ora') @@ -21,7 +22,7 @@ const chalk = require('chalk') const createReactStorefrontInternal = async (options, userConfig) => { console.log('') - const targetPath = calculateReactStorefrontPath(options.appName, userConfig) + const targetPath = calculateReactStorefrontPath(userConfig.appName, userConfig) if (userConfig.createDirectory && !isTargetPathValid(targetPath)) { console.log( @@ -51,7 +52,7 @@ const createReactStorefrontInternal = async (options, userConfig) => { try { spinner = ora('Writing package.json...').start() - processPackageJson(options.appName, targetPath, userConfig) + processPackageJson(userConfig.appName, targetPath, userConfig) spinner.succeed('Writing package.json... done.') } catch (e) { spinner.fail('Failed') @@ -59,6 +60,16 @@ const createReactStorefrontInternal = async (options, userConfig) => { process.exit(1) } + try { + spinner = ora('Writing next.config.js...').start() + processNextConfig(targetPath, userConfig) + spinner.succeed('Writing next.config.js... done.') + } catch (e) { + spinner.fail('Failed') + console.error(e) + process.exit(1) + } + if (options.configureUpstream) { processReactStorefrontConfigJsons(targetPath, userConfig) } @@ -66,6 +77,14 @@ const createReactStorefrontInternal = async (options, userConfig) => { try { spinner = ora('Installing dependencies...').start() await exec('npm install --registry=https://npm-proxy.fury.io/moovweb/', { cwd: targetPath }) + + if (userConfig.connector) { + await exec( + `npm install --registry=https://npm-proxy.fury.io/moovweb/ --save ${userConfig.connector}`, + { cwd: targetPath } + ) + } + spinner.succeed('Installing dependencies... done.') } catch (e) { spinner.fail('npm install failed') diff --git a/lib/prompt-for-config.js b/lib/prompt-for-config.js index 48acb59..2a975e3 100644 --- a/lib/prompt-for-config.js +++ b/lib/prompt-for-config.js @@ -1,6 +1,7 @@ const _ = require('lodash') const prompts = require('prompts') const configDefaults = require('./config-defaults') +const connectors = require('./connectors') const _initialDevHostname = (_prev, values) => { return `dev.${values.prodHostname}` @@ -26,39 +27,15 @@ const _requireInput = value => { const questions = [ { - name: 'version', + name: 'appName', + message: 'Enter a name for your app:', type: 'text', - message: 'version', - initial: configDefaults.version, }, { - name: 'description', - type: 'text', - message: 'description', - }, - { - name: 'repoUrl', - type: 'text', - message: 'repository url', - }, - { - name: 'author', - type: 'text', - message: 'author', - }, - { - name: 'license', - type: 'text', - message: 'license', - initial: configDefaults.license, - }, - { - name: 'private', - type: 'toggle', - message: 'private', - initial: configDefaults.private, - active: configDefaults.private.toString(), - inactive: 'false', + name: 'connector', + message: 'Which ecommerce platform will you be using?', + type: 'select', + choices: [...connectors, { title: 'Other', value: null }], }, { name: 'xdn', @@ -81,12 +58,19 @@ const questions = [ /** * Prompt user for React Storefront configuration options. */ -const promptForConfig = async () => { +const promptForConfig = async options => { console.log( `\nLet's create a new React Storefront app! First, I need you to provide some information for package.json...\n` ) - const answers = await prompts(questions) + if (options.appName) { + questions.shift() + } + + const answers = { + appName: options.appName, + ...(await prompts(questions)), + } // If the user has not provided all input, abort. if (Object.keys(answers).length !== questions.length) { diff --git a/lib/template-processing.js b/lib/template-processing.js index 29cb458..b3faaf0 100644 --- a/lib/template-processing.js +++ b/lib/template-processing.js @@ -62,12 +62,13 @@ const processPackageJson = (name, targetPath, { xdn, ...config } = {}) => { const packageJson = _readConfigJson(packageJsonPath) packageJson.name = name - packageJson.version = config.version - packageJson.description = config.description - packageJson.homepage = config.repoUrl - packageJson.author = config.author - packageJson.license = config.license - packageJson.private = config.private + packageJson.version = '0.0.0' + packageJson.license = 'UNLICENSED' + packageJson.private = true + + delete packageJson.description + delete packageJson.homepage + delete packageJson.author if (!xdn) { // remove yalc commands from free apps @@ -76,6 +77,7 @@ const processPackageJson = (name, targetPath, { xdn, ...config } = {}) => { ) delete packageJson.scripts['rsf:link'] } + // remove react-storefront team from the deploy script, so it defaults to the user's own team: packageJson.scripts.deploy = packageJson.scripts.deploy.replace( 'xdn deploy react-storefront --environment=production', @@ -85,6 +87,22 @@ const processPackageJson = (name, targetPath, { xdn, ...config } = {}) => { _writeConfigJson(packageJsonPath, packageJson) } +/** + * Sets the connector in next.config.js + * @param {Object} userConfig The answers from questions + */ +const processNextConfig = (targetPath, { connector }) => { + if (connector) { + const file = path.join(targetPath, 'next.config.js') + + const content = fs + .readFileSync(file, 'utf8') + .replace('react-storefront/mock-connector', connector) + + fs.writeFileSync(file, content, 'utf8') + } +} + const copyResources = (targetPath, { xdn }) => { if (xdn) { fs.copySync(path.join(targetPath, 'crs-resources'), targetPath) @@ -100,4 +118,5 @@ module.exports = { processReactStorefrontConfigJsons, processPackageJson, copyResources, + processNextConfig, } diff --git a/package.json b/package.json index 2af7d1e..5425904 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "create-react-storefront", - "version": "8.0.0", + "version": "8.1.0", "description": "A CLI for creating React Storefront apps.", "main": "index.js", "dependencies": { @@ -27,7 +27,7 @@ "create-react-storefront": "./index.js" }, "scripts": { - "start": "node .", + "start": "cd run && rm -rf * && node ../index.js", "release": "yarn publish", "precommit": "lint-staged", "prettier": "prettier --write \"**/*.js\" \"!{node_modules,.next,.yalc}/**\""