Skip to content

Commit

Permalink
Merge pull request #24 from medly/refactor/inquirer
Browse files Browse the repository at this point in the history
refactor: show right set of questions if user does not pass any argum…
  • Loading branch information
gmukul01 authored Jun 23, 2021
2 parents 8b93906 + bc6c80d commit 0952c4c
Show file tree
Hide file tree
Showing 13 changed files with 149 additions and 97 deletions.
2 changes: 1 addition & 1 deletion packages/app/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ An application for generating simple react app.

## Getting started

**Usage**: `yarn create @medly/app <project-name> [options]` OR `yarn create @medly/app <project-name> -i`
**Usage**: `yarn create @medly/app <project-name> [options]` OR `yarn create @medly/app`

### Options

Expand Down
13 changes: 7 additions & 6 deletions packages/app/bin/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,28 +11,29 @@ const commander = require('commander');
const { addProjectDetails, installDependencies, printMedly, updateHuskyCommands, printGenericError } = require('@medly/starter-shared');

async function init() {
let projectName;
let cmdProjectName;

try {
printMedly();

// Command information
const program = new commander.Command(packageJson.name)
.version(packageJson.version)
.arguments('<project-name>')
.arguments('[project-name]')
.addOption(
new Option('-p, --package-manager <package-manager>', 'package manager').choices(['npm', 'yarn', 'pnpm']).default('yarn')
)
.option('-i, --interactive', 'show interactive questionnaire')
.description('An application for generating either ts module or simple ts app')
.usage(`${chalk.green('<project-name>')} [options]`)
.action(name => {
projectName = name;
cmdProjectName = name;
})
.parse(process.argv);

const options = program.opts(),
{ packageManager } = options.interactive ? await questions() : options;
const commanderOptions = program.opts(),
options = await questions({ ...commanderOptions, projectName: cmdProjectName }),
{ packageManager, projectName } = options;

// Create project directory
const projectRoot = path.resolve(projectName);
Expand All @@ -43,7 +44,7 @@ async function init() {
fs.copySync(template, projectRoot);

// Add project details
addProjectDetails(projectName, options);
addProjectDetails(options);

// Move to project directory
process.chdir(projectRoot);
Expand Down
27 changes: 23 additions & 4 deletions packages/app/bin/questions.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,26 @@
const inquirer = require('inquirer');
const chalk = require('chalk');

const questions = [{ type: 'list', name: 'packageManager', message: 'Package Manager', choices: ['npm', 'yarn', 'pnpm'], default: 'yarn' }];
const questions = cmdOptions => [
{
type: 'input',
name: 'projectName',
message: 'Project Name',
default: 'medly-app',
when: () => !cmdOptions.projectName
},
{
type: 'list',
name: 'packageManager',
message: 'Package Manager',
choices: [
{ name: chalk.hex('#CC3534')('npm'), value: 'npm' },
{ name: chalk.hex('#2C8EBB')('yarn'), value: 'yarn' },
{ name: chalk.hex('#F9AD00')('pnpm'), value: 'pnpm' }
],
default: 'yarn',
when: () => !cmdOptions.projectName || cmdOptions.interactive
}
];

module.exports = async function () {
return inquirer.prompt(questions);
};
module.exports = async cmdOptions => ({ ...cmdOptions, ...(await inquirer.prompt(questions(cmdOptions))) });
66 changes: 33 additions & 33 deletions packages/app/template/template.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,41 +35,41 @@
},
"prettier": "@medly/prettier-config",
"dependencies": [
"@medly-components/core",
"@medly-components/forms",
"@medly-components/layout",
"@medly-components/loaders",
"@medly-components/theme",
"axios",
"react",
"react-dom",
"react-redux",
"react-router-dom",
"redux",
"redux-saga",
"styled-components"
"@medly-components/core@latest",
"@medly-components/forms@latest",
"@medly-components/layout@latest",
"@medly-components/loaders@latest",
"@medly-components/theme@latest",
"axios@latest",
"react@latest",
"react-dom@latest",
"react-redux@latest",
"react-router-dom@latest",
"redux@latest",
"redux-saga@latest",
"styled-components@latest"
],
"devDependencies": [
"[email protected]",
"@medly/babel-config-react",
"@medly/eslint-config-react",
"@medly/prettier-config",
"@medly/stylelint-config",
"@medly/typescript-config-react",
"@medly/webpack-config",
"@medly/jest-config-react",
"@testing-library/react",
"@types/react",
"@types/react-dom",
"@types/react-redux",
"@types/react-router-dom",
"@types/redux-mock-store",
"@types/styled-components",
"axios-mock-adapter",
"lint-staged",
"npm-run-all",
"redux-devtools-extension",
"redux-mock-store",
"rimraf"
"@medly/babel-config-react@latest",
"@medly/eslint-config-react@latest",
"@medly/prettier-config@latest",
"@medly/stylelint-config@latest",
"@medly/typescript-config-react@latest",
"@medly/webpack-config@latest",
"@medly/jest-config-react@latest",
"@testing-library/react@latest",
"@types/react@latest",
"@types/react-dom@latest",
"@types/react-redux@latest",
"@types/react-router-dom@latest",
"@types/redux-mock-store@latest",
"@types/styled-components@latest",
"axios-mock-adapter@latest",
"lint-staged@latest",
"npm-run-all@latest",
"redux-devtools-extension@latest",
"redux-mock-store@latest",
"rimraf@latest"
]
}
2 changes: 1 addition & 1 deletion packages/module/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ An application for generating either ts module or simple ts app. You can also ch

## Getting started

**Usage**: `yarn create @medly/module <project-name> [options]` OR `yarn create @medly/module <project-name> -i`
**Usage**: `yarn create @medly/module <project-name> [options]` OR `yarn create @medly/module`

### Options

Expand Down
2 changes: 1 addition & 1 deletion packages/module/bin/copyTemplateFiles.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const fs = require('fs-extra');
const path = require('path');

module.exports = (projectName, { registry, language }) => {
module.exports = ({ projectName, registry, language }) => {
const projectRoot = path.resolve(projectName),
template = path.join(__dirname, '../template');

Expand Down
16 changes: 8 additions & 8 deletions packages/module/bin/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ const copyTemplateFiles = require('./copyTemplateFiles');
const { addProjectDetails, installDependencies, printMedly, printGenericError } = require('@medly/starter-shared');

async function init() {
let projectName;
let cmdProjectName;

try {
printMedly();

// Command information
const program = new commander.Command(packageJson.name)
.version(packageJson.version)
.arguments('<project-name>')
.arguments('[project-name]')
.option('-o, --owner <owner>', 'owner of the package')
.addOption(new Option('-r, --registry <registry>', 'registry to publish the module').choices(['npm', 'github']))
.addOption(
Expand All @@ -32,27 +32,27 @@ async function init() {
.usage(`${chalk.green('<project-name>')} [options]`)
.action((name, options) => {
if (options.registry && !options.owner) {
console.error('Error: Owner of the repo required when you add registry option');
console.error(chalk.red('Error: ') + '-o, --owner <owner> required when you have added registry option');
process.exit(1);
}
projectName = name;
cmdProjectName = name;
})
.parse(process.argv);

const commanderOptions = program.opts(),
options = commanderOptions.interactive ? await questions() : commanderOptions,
{ registry, packageManager } = options;
options = await questions({ ...commanderOptions, projectName: cmdProjectName }),
{ registry, packageManager, projectName } = options;

// Create project directory
const projectRoot = path.resolve(projectName);
fs.ensureDirSync(projectName);
console.log('Creating the project at ' + chalk.green(projectRoot));

// Copying template files
copyTemplateFiles(projectName, options);
copyTemplateFiles(options);

// Add project details
addProjectDetails(projectName, options);
addProjectDetails(options);

// Move to project directory
process.chdir(projectRoot);
Expand Down
52 changes: 42 additions & 10 deletions packages/module/bin/questions.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,51 @@
const inquirer = require('inquirer');
const chalk = require('chalk');

const questions = [
const questions = cmdOptions => [
{
type: 'input',
name: 'projectName',
message: 'Project Name',
default: 'medly-module',
when: () => !cmdOptions.projectName
},
{
type: 'list',
name: 'registry',
message: 'Registry to publish the module',
choices: ['none', 'github', 'npm'],
default: cmdOptions.registry || 'none',
when: () => !cmdOptions.projectName || cmdOptions.interactive
},
{
type: 'input',
name: 'owner',
message: 'Owner of the package',
when(answers) {
return answers.registry !== 'none';
}
when: answers => !cmdOptions.owner && answers.registry && answers.registry !== 'none'
},
{
type: 'list',
name: 'language',
message: 'Language',
choices: [
{ name: chalk.hex('#007acc')('typescript'), value: 'typescript' },
{ name: chalk.yellow('javascript'), value: 'javascript' }
],
default: cmdOptions.language,
when: () => !cmdOptions.projectName || cmdOptions.interactive
},
{ type: 'list', name: 'language', message: 'Language', choices: ['typescript', 'javascript'], default: 'typescript' },
{ type: 'list', name: 'registry', message: 'Registry to publish the module', choices: ['none', 'github', 'npm'], default: 'none' },
{ type: 'list', name: 'packageManager', message: 'Package Manager', choices: ['npm', 'yarn', 'pnpm'], default: 'yarn' }
{
type: 'list',
name: 'packageManager',
message: 'Package Manager',
choices: [
{ name: chalk.hex('#CC3534')('npm'), value: 'npm' },
{ name: chalk.hex('#2C8EBB')('yarn'), value: 'yarn' },
{ name: chalk.hex('#F9AD00')('pnpm'), value: 'pnpm' }
],
default: cmdOptions.packageManager,
when: () => !cmdOptions.projectName || cmdOptions.interactive
}
];

module.exports = async function () {
return inquirer.prompt(questions);
};
module.exports = async cmdOptions => ({ ...cmdOptions, ...(await inquirer.prompt(questions(cmdOptions))) });
22 changes: 11 additions & 11 deletions packages/module/template/publishable/javascript/template.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,17 @@
"dependencies": [],
"devDependencies": [
"[email protected]",
"@commitlint/cli",
"@commitlint/config-conventional",
"@medly/babel-config",
"@medly/eslint-config",
"@medly/prettier-config",
"@medly/rollup-config",
"@medly/semantic-release-config",
"commitizen",
"cross-env",
"cz-conventional-changelog",
"jest"
"@commitlint/cli@latest",
"@commitlint/config-conventional@latest",
"@medly/babel-config@latest",
"@medly/eslint-config@latest",
"@medly/prettier-config@latest",
"@medly/rollup-config@latest",
"@medly/semantic-release-config@latest",
"commitizen@latest",
"cross-env@latest",
"cz-conventional-changelog@latest",
"jest@latest"
],
"config": {
"commitizen": {
Expand Down
26 changes: 13 additions & 13 deletions packages/module/template/publishable/typescript/template.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,19 @@
"dependencies": [],
"devDependencies": [
"[email protected]",
"@commitlint/cli",
"@commitlint/config-conventional",
"@medly/babel-config",
"@medly/eslint-config",
"@medly/prettier-config",
"@medly/rollup-config",
"@medly/semantic-release-config",
"@medly/typescript-config",
"commitizen",
"cross-env",
"cz-conventional-changelog",
"jest",
"ts-node"
"@commitlint/cli@latest",
"@commitlint/config-conventional@latest",
"@medly/babel-config@latest",
"@medly/eslint-config@latest",
"@medly/prettier-config@latest",
"@medly/rollup-config@latest",
"@medly/semantic-release-config@latest",
"@medly/typescript-config@latest",
"commitizen@latest",
"cross-env@latest",
"cz-conventional-changelog@latest",
"jest@latest",
"ts-node@latest"
],
"config": {
"commitizen": {
Expand Down
2 changes: 1 addition & 1 deletion packages/module/template/simple/javascript/template.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"test": "jest --passWithNoTests --coverage -w=50%"
},
"dependencies": [],
"devDependencies": ["@medly/babel-config", "@medly/eslint-config", "@medly/prettier-config", "jest"],
"devDependencies": ["@medly/babel-config@latest", "@medly/eslint-config@latest", "@medly/prettier-config@latest", "jest@latest"],
"babel": {
"extends": "@medly/babel-config"
},
Expand Down
14 changes: 7 additions & 7 deletions packages/module/template/simple/typescript/template.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@
},
"dependencies": [],
"devDependencies": [
"@medly/babel-config",
"@medly/eslint-config",
"@medly/prettier-config",
"@medly/typescript-config",
"@types/node",
"jest",
"ts-node"
"@medly/babel-config@latest",
"@medly/eslint-config@latest",
"@medly/prettier-config@latest",
"@medly/typescript-config@latest",
"@types/node@latest",
"jest@latest",
"ts-node@latest"
],
"babel": {
"extends": "@medly/babel-config"
Expand Down
2 changes: 1 addition & 1 deletion packages/shared/src/addProjectDetails.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const fs = require('fs-extra');
const path = require('path');

module.exports = (projectName, { registry, owner }) => {
module.exports = ({ projectName, registry, owner }) => {
const projectRoot = path.resolve(projectName),
package = fs.readJSONSync(path.join(projectRoot, 'template.json'));

Expand Down

0 comments on commit 0952c4c

Please sign in to comment.