Skip to content
This repository has been archived by the owner on Aug 31, 2023. It is now read-only.

Commit

Permalink
Merge pull request #164 from facebookexperimental/interactive-fixes
Browse files Browse the repository at this point in the history
Interactive CLI and init command fixes
  • Loading branch information
sebmck authored Mar 10, 2020
2 parents 2723f2d + 1a7f69c commit e36c03e
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 6 deletions.
23 changes: 22 additions & 1 deletion packages/@romejs/cli-reporter/Reporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -501,7 +501,7 @@ export default class Reporter {
): Promise<keyof Options> {
const set = await this.select(message, {...arg, radio: true});

// Should always have at least one elemet
// Should always have at least one element
return Array.from(set)[0];
}

Expand All @@ -527,6 +527,16 @@ export default class Reporter {
let prompt = `<brightBlack>❯</brightBlack> <emphasis>${message}</emphasis>`;
this.logAll(prompt);

if (radio) {
this.info(
'Use arrow keys and then <emphasis>enter</emphasis> to select an option',
);
} else {
this.info(
'Use arrow keys and <emphasis>space</emphasis> to select or deselect options and then <emphasis>enter</emphasis> to confirm',
);
}

const selectedOptions: Set<keyof Options> = new Set(defaults);
let activeOption = 0;

Expand Down Expand Up @@ -605,6 +615,10 @@ export default class Reporter {
const finish = () => {
cleanup();

// Remove initial help message
this.writeAll(escapes.cursorUp());
this.writeAll(escapes.eraseLine);

// Remove initial log message
this.writeAll(escapes.cursorUp());
this.writeAll(escapes.eraseLine);
Expand Down Expand Up @@ -651,11 +665,18 @@ export default class Reporter {

case 'c':
if (key.ctrl) {
this.spacer();
this.warn('Cancelled by user');
process.exit(1);
}
return;

case 'escape':
this.spacer();
this.warn('Cancelled by user');
process.exit(1);
return;

case 'return':
finish();
return;
Expand Down
23 changes: 18 additions & 5 deletions packages/@romejs/core/client/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import executeMain from '../common/utils/executeMain';
import {createSingleDiagnosticError} from '@romejs/diagnostics';
import {createAbsoluteFilePath} from '@romejs/path';
import {Dict} from '@romejs/typescript-helpers';
import {writeFile} from '@romejs/fs';
import {writeFile, exists} from '@romejs/fs';
import {VERSION} from '../common/constants';

export const localCommands: Map<string, LocalCommand<any>> = new Map();
Expand All @@ -37,10 +37,18 @@ localCommands.set('init', {

const config: Dict<unknown> = {};

const configPath = req.client.flags.cwd.append('rome.json');
if (await exists(configPath)) {
reporter.error(
`<filelink target="${configPath.join()}" emphasis>rome.json</filelink> file already exists`,
);
reporter.info(
'Use <command>rome config</command> to update an existing config',
);
return false;
}

reporter.heading('Welcome to Rome!');
reporter.info(
'Press <emphasis>space</emphasis> to select an option and <emphasis>enter</emphasis> to confirm',
);

if (flags.defaults === false) {
const useDefaults = await reporter.radioConfirm(
Expand All @@ -67,6 +75,9 @@ localCommands.set('init', {
format: {
label: 'Format',
},
tests: {
label: 'Testing',
},
},
defaults: ['lint'],
});
Expand All @@ -76,8 +87,10 @@ localCommands.set('init', {
if (enabledComponents.has('format')) {
config.format = {enabled: true};
}
if (enabledComponents.has('tests')) {
config.tests = {enabled: true};
}

const configPath = req.client.flags.cwd.append('rome.json');
await writeFile(configPath, `${JSON.stringify(config, null, ' ')}\n`);

reporter.success(
Expand Down

0 comments on commit e36c03e

Please sign in to comment.