Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[RFC 638] Interactive way to create new Ember apps and addons
- Loading branch information
1 parent
519b67f
commit c2d8688
Showing
12 changed files
with
538 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,160 @@ | ||
'use strict'; | ||
|
||
const inquirer = require('inquirer'); | ||
const { isLangCode } = require('is-language-code'); | ||
const osLocale = require('os-locale'); | ||
|
||
const Task = require('../models/task'); | ||
const isValidProjectName = require('../utilities/valid-project-name'); | ||
|
||
const DEFAULT_LOCALE = 'en-US'; | ||
|
||
class InteractiveNewTask extends Task { | ||
async run(newCommandOptions, _testAnswers) { | ||
let prompt = inquirer.createPromptModule(); | ||
let questions = await this.getQuestions(newCommandOptions); | ||
let answers = await prompt(questions, _testAnswers); | ||
|
||
answers.lang = answers.langSelection || answers.langDifferent; | ||
|
||
delete answers.langSelection; | ||
delete answers.langDifferent; | ||
|
||
return answers; | ||
} | ||
|
||
async getQuestions(newCommandOptions = {}) { | ||
return [ | ||
{ | ||
name: 'blueprint', | ||
type: 'list', | ||
message: 'Is this an app or an addon?', | ||
choices: [ | ||
{ | ||
name: 'App', | ||
value: 'app', | ||
}, | ||
{ | ||
name: 'Addon', | ||
value: 'addon', | ||
}, | ||
], | ||
}, | ||
{ | ||
name: 'name', | ||
type: 'input', | ||
message: ({ blueprint }) => `Please provide the name of your ${blueprint}:`, | ||
when: !newCommandOptions.name, | ||
validate: (name) => { | ||
if (name) { | ||
if (isValidProjectName(name)) { | ||
return true; | ||
} | ||
|
||
return `We currently do not support \`${name}\` as a name.`; | ||
} | ||
|
||
return 'Please provide a name.'; | ||
}, | ||
}, | ||
{ | ||
name: 'langSelection', | ||
type: 'list', | ||
message: ({ blueprint }) => `Please provide the spoken/content language of your ${blueprint}:`, | ||
when: !newCommandOptions.lang, | ||
choices: await this.getLangChoices(), | ||
}, | ||
{ | ||
name: 'langDifferent', | ||
type: 'input', | ||
message: 'Please provide the different language:', | ||
when: ({ langSelection } = {}) => !newCommandOptions.lang && !langSelection, | ||
validate: (lang) => { | ||
if (isLangCode(lang).res) { | ||
return true; | ||
} | ||
|
||
return 'Please provide a valid locale code.'; | ||
}, | ||
}, | ||
{ | ||
name: 'packageManager', | ||
type: 'list', | ||
message: 'Pick the package manager to use when installing dependencies:', | ||
when: !newCommandOptions.yarn, | ||
choices: [ | ||
{ | ||
name: 'NPM', | ||
value: 'npm', | ||
}, | ||
{ | ||
name: 'Yarn', | ||
value: 'yarn', | ||
}, | ||
{ | ||
name: 'Ignore/Skip', | ||
value: null, | ||
}, | ||
], | ||
}, | ||
{ | ||
name: 'ciProvider', | ||
type: 'list', | ||
message: 'Which CI provider do you want to use?', | ||
// `newCommandOptions.ciProvider` has `github` as a default value, | ||
// so we need to check the presence of the `--ci-provider` flag to know | ||
// if the user provided a CI provider manually. | ||
when: !isCliOptionProvided('--ci-provider'), | ||
choices: [ | ||
{ | ||
name: 'GitHub Actions', | ||
value: 'github', | ||
}, | ||
{ | ||
name: 'Travis CI', | ||
value: 'travis', | ||
}, | ||
{ | ||
name: 'Ignore/Skip', | ||
value: null, | ||
}, | ||
], | ||
}, | ||
]; | ||
} | ||
|
||
async getLangChoices() { | ||
let userLocale = await this.getUserLocale(); | ||
let langChoices = [ | ||
{ | ||
name: DEFAULT_LOCALE, | ||
value: DEFAULT_LOCALE, | ||
}, | ||
]; | ||
|
||
if (userLocale !== DEFAULT_LOCALE) { | ||
langChoices.push({ | ||
name: userLocale, | ||
value: userLocale, | ||
}); | ||
} | ||
|
||
langChoices.push({ | ||
name: 'I want to manually provide a different language', | ||
value: null, | ||
}); | ||
|
||
return langChoices; | ||
} | ||
|
||
getUserLocale() { | ||
return osLocale(); | ||
} | ||
} | ||
|
||
function isCliOptionProvided(name) { | ||
return process.argv.some((arg) => arg.includes(name)); | ||
} | ||
|
||
module.exports = InteractiveNewTask; | ||
module.exports.DEFAULT_LOCALE = DEFAULT_LOCALE; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.