Skip to content

Commit

Permalink
0.0.2
Browse files Browse the repository at this point in the history
  • Loading branch information
dmitrii-fediuk committed Jan 13, 2021
1 parent cdc9aed commit 168d780
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 83 deletions.
125 changes: 43 additions & 82 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,55 +1,35 @@
const { basename, resolve } = require('path');
const os = require('os');
const chalk = require('chalk');
const changeCase = require('change-case');
const inquirer = require('inquirer');
const execa = require('execa');
const chalk = require('chalk');
const gitUserInfo = require('git-user-info');
const isInvalidPath = require('is-invalid-path');
const inquirer = require('inquirer');
const isValidNpmName = require('is-valid-npm-name');
const os = require('os');
const pkg = require('../package.json');
const {
sampleBackends
} = require('@magento/pwa-buildpack/lib/cli/create-project');

const {basename, resolve} = require('path');
const {sampleBackends} = require('@magento/pwa-buildpack/lib/cli/create-project');
module.exports = async () => {
console.log(chalk.greenBright(`${pkg.name} v${pkg.version}`));
console.log(
chalk.white(`Creating a ${chalk.whiteBright('PWA Studio')} project`)
);
console.log(chalk.white(`Creating a ${chalk.whiteBright('PWA Studio')} project`));
const userAgent = process.env.npm_config_user_agent || '';
const isYarn = userAgent.includes('yarn');

const questions = [
{
name: 'directory',
message:
'Project root directory (will be created if it does not exist)',
validate: dir =>
!dir
? 'Please enter a directory path'
: isInvalidPath(dir)
? 'Invalid directory path; contains illegal characters'
: true
message: 'Project root directory (will be created if it does not exist)'
,name: 'directory'
,validate: dir => !dir ? 'Please enter a directory path' : true
},
{
name: 'name',
message:
'Short name of the project to put in the package.json "name" field',
validate: isValidNpmName,
default: ({ directory }) => basename(directory)
default: ({directory}) => basename(directory)
,message: 'Short name of the project to put in the package.json "name" field'
,name: 'name'
,validate: isValidNpmName
},
{
name: 'author',
message:
'Name of the author to put in the package.json "author" field',
default: () => {
const userInfo = os.userInfo();
let author = userInfo.username;
const gitInfo = gitUserInfo({
path: resolve(userInfo.homedir, '.gitconfig')
});

const gitInfo = gitUserInfo({path: resolve(userInfo.homedir, '.gitconfig')});
if (gitInfo) {
author = gitInfo.name || author;
if (gitInfo.email) {
Expand All @@ -58,61 +38,53 @@ module.exports = async () => {
}
return author;
}
,message: 'Name of the author to put in the package.json "author" field'
,name: 'author'
},
{
name: 'backendUrl',
type: 'list',
message:
'Magento instance to use as a backend (will be added to `.env` file)',
choices: sampleBackends.environments
.map(({ name, description, url }) => ({
name: description,
value: url,
short: name
}))
.concat([
{
name:
'Other (I will provide my own backing Magento instance)',
value: false,
short: 'Other'
}
])
.concat([{
name: 'Other (I will provide my own backing Magento instance)',
value: false,
short: 'Other'
}])
,message: 'Magento instance to use as a backend (will be added to `.env` file)'
,name: 'backendUrl'
,type: 'list'
},
{
name: 'customBackendUrl',
message:
'URL of a Magento instance to use as a backend (will be added to `.env` file)',
default: 'https://magento2.localhost',
when: ({ backendUrl }) => !backendUrl
default: 'https://magento2.localhost'
,message: 'URL of a Magento instance to use as a backend (will be added to `.env` file)'
,name: 'customBackendUrl'
,when: ({backendUrl}) => !backendUrl
},
{
name: 'braintreeToken',
message:
'Braintree API token to use to communicate with your Braintree instance (will be added to `.env` file)',
default: 'sandbox_8yrzsvtm_s2bg8fs563crhqzk'
,message: 'Braintree API token to use to communicate with your Braintree instance (will be added to `.env` file)'
,name: 'braintreeToken'
},
{
name: 'npmClient',
type: 'list',
message: 'NPM package management client to use',
choices: ['npm', 'yarn'],
default: isYarn ? 'yarn' : 'npm'
choices: ['npm', 'yarn']
,default: isYarn ? 'yarn' : 'npm'
,message: 'NPM package management client to use'
,name: 'npmClient'
,type: 'list'
},
{
name: 'install',
type: 'confirm',
message: ({ npmClient }) =>
`Install package dependencies with ${npmClient} after creating project`,
default: true
,message: ({ npmClient }) => `Install package dependencies with ${npmClient} after creating project`
,name: 'install'
,type: 'confirm'
}
];
let answers;
try {
answers = await inquirer.prompt(questions);
} catch (e) {
console.error('App creation cancelled.');
}
try {answers = await inquirer.prompt(questions);}
catch (e) {console.error('App creation cancelled.');}
answers.backendUrl = answers.backendUrl || answers.customBackendUrl;
const args = questions.reduce(
(args, q) => {
Expand All @@ -131,19 +103,8 @@ module.exports = async () => {
},
['create-project', answers.directory, '--template', '"venia-concept"']
);

const argsString = args.join(' ');

console.log(
'\nRunning command: \n\n' +
chalk.whiteBright(`buildpack ${argsString}\n\n`)
);

const buildpackBinLoc = resolve(
require.resolve('@magento/pwa-buildpack'),
'../../bin/buildpack'
);
await execa.shell(`${buildpackBinLoc} ${argsString}`, {
stdio: 'inherit'
});
console.log('\nRunning command: \n\n' + chalk.whiteBright(`buildpack ${argsString}\n\n`));
const buildpackBinLoc = resolve(require.resolve('@magento/pwa-buildpack'), '../../bin/buildpack');
await execa.shell(`${buildpackBinLoc} ${argsString}`, {stdio: 'inherit'});
};
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@mage2pro/create-pwa",
"version": "0.0.1",
"version": "0.0.2",
"description": "Create a PWA Studio app with a single command.",
"author": "Dmitry Fedyuk",
"dependencies": {
Expand Down

0 comments on commit 168d780

Please sign in to comment.