Skip to content

Added support for connectors. #7

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ const argv = require('yargs')
'Display the create-react-storefront version and exit',
`create-react-storefront v${getPackageJsonVersion()}`
)
.command('$0 <app-name>', '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',
Expand Down
2 changes: 2 additions & 0 deletions lib/connectors.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// List all supported connector packages here.
module.exports = [{ title: 'Magento 2', value: 'react-storefront-magento2-connector' }]
23 changes: 21 additions & 2 deletions lib/create-react-storefront-internal.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const {
copyResources,
processReactStorefrontConfigJsons,
processPackageJson,
processNextConfig,
} = require('./template-processing')
const { retrieveTemplate } = require('./retrieve-template')
const ora = require('ora')
Expand All @@ -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(
Expand Down Expand Up @@ -51,21 +52,39 @@ 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')
console.error(e)
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)
}

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')
Expand Down
48 changes: 16 additions & 32 deletions lib/prompt-for-config.js
Original file line number Diff line number Diff line change
@@ -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}`
Expand All @@ -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',
Expand All @@ -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) {
Expand Down
31 changes: 25 additions & 6 deletions lib/template-processing.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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',
Expand All @@ -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)
Expand All @@ -100,4 +118,5 @@ module.exports = {
processReactStorefrontConfigJsons,
processPackageJson,
copyResources,
processNextConfig,
}
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand All @@ -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}/**\""
Expand Down