Skip to content

Commit

Permalink
feat(cli): Improve UX for init sub-command
Browse files Browse the repository at this point in the history
- Check the existence of yarn.
- Check if package install is required.
- `connection` prompt message.
  • Loading branch information
wadackel committed Dec 16, 2020
1 parent 1ac3385 commit 04e9aed
Showing 1 changed file with 37 additions and 15 deletions.
52 changes: 37 additions & 15 deletions packages/cli/src/commands/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,19 @@ const validateNpmClient: Validator = (value) => {
return true;
};

const isPuppeteerInstalled = async (): Promise<boolean> => {
const isPuppeteerInstalled = async () => {
return (await findChrome({ channel: 'puppeteer' })) !== null;
};

const isYarnExists = async () => {
try {
await execa.command('yarn --version', { stdio: 'ignore' });
return true;
} catch (e) {
return false;
}
};

const promptUserIfNeeded = async (defaults: Partial<PromptResult>) => {
const result: PromptResult = {
origin: '',
Expand Down Expand Up @@ -133,13 +142,13 @@ const promptUserIfNeeded = async (defaults: Partial<PromptResult>) => {
choices: [
{
name: 'exiting',
message: 'Running server',
hint: '(e.g. https://example.com)',
message: 'Existing server',
hint: '(e.g. "https://example.com")',
},
{
name: 'command',
message: 'Local server',
hint: '(e.g. http://localhost:8000)',
message: 'Launch server via command',
hint: '(e.g. "npm start")',
},
],
});
Expand Down Expand Up @@ -215,7 +224,9 @@ const promptUserIfNeeded = async (defaults: Partial<PromptResult>) => {
if (defaults.installPuppeteer !== undefined) {
result.installPuppeteer = defaults.installPuppeteer;
} else {
if (await isPuppeteerInstalled()) {
const puppeteerInstalled = await isPuppeteerInstalled();

if (puppeteerInstalled) {
result.installPuppeteer = false;
} else {
result.installPuppeteer = await prompt({
Expand All @@ -230,15 +241,26 @@ const promptUserIfNeeded = async (defaults: Partial<PromptResult>) => {
if (defaults.npmClient !== undefined) {
result.npmClient = defaults.npmClient!.trim();
} else {
result.npmClient = await prompt({
type: 'select',
name: 'npmClient',
message:
'Which is the npm client used to install the dependent packages?',
choices: NPM_CLIENTS,
initial: 0,
validate: validateNpmClient,
});
const shoudPackageInstall =
result.useConfig || result.runner !== '' || result.installPuppeteer;

const yarnExists = await isYarnExists();

if (shoudPackageInstall) {
if (yarnExists) {
result.npmClient = await prompt({
type: 'select',
name: 'npmClient',
message:
'Which is the npm client used to install the dependent packages?',
choices: NPM_CLIENTS,
initial: 0,
validate: validateNpmClient,
});
} else {
result.npmClient = NPM_CLIENTS[0];
}
}
}

return result;
Expand Down

0 comments on commit 04e9aed

Please sign in to comment.