Skip to content

Commit

Permalink
fix: improve providing project name in init command (#2071)
Browse files Browse the repository at this point in the history
* fix: improve providing project name

* chore: update descriptions

* test: update tests
  • Loading branch information
szymonrybczak authored Sep 19, 2023
1 parent d695144 commit c50e077
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 12 deletions.
5 changes: 3 additions & 2 deletions __e2e__/__snapshots__/default.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ Options:
-h, --help display help for command
Commands:
init [options] <projectName> Initialize a new React Native project named
<projectName> in a directory of the same name.
init [options] [projectName] New app will be initialized in the directory of
the same name. Android and iOS projects will
use this name for publishing setup.
doctor [options] Diagnose and fix common Node.js, iOS, Android &
React Native issues.
help [command] display help for command"
Expand Down
18 changes: 12 additions & 6 deletions __e2e__/init.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ import {
writeFiles,
} from '../jest/helpers';
import slash from 'slash';
import prompts from 'prompts';

jest.mock('prompts', () => jest.fn());

const DIR = getTempDirectory('command-init');

Expand Down Expand Up @@ -56,13 +59,16 @@ test('init fails if the directory already exists', () => {
);
});

test('init --template fails without package name', () => {
const {stderr} = runCLI(
DIR,
['init', '--template', 'react-native-new-template'],
{expectedFailure: true},
test('init should prompt for the project name', () => {
createCustomTemplateFiles();
const {stdout} = runCLI(DIR, ['init', 'test', '--template', templatePath]);

(prompts as jest.MockedFunction<typeof prompts>).mockReturnValue(
Promise.resolve({
name: 'TestInit',
}),
);
expect(stderr).toContain('missing required argument');
expect(stdout).toContain('Run instructions');
});

test('init --template filepath', () => {
Expand Down
6 changes: 5 additions & 1 deletion packages/cli-clean/src/__tests__/clean.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@ describe('clean', () => {
});

it('prompts if `--include` is omitted', async () => {
prompts.mockReturnValue({cache: []});
(prompts as jest.MockedFunction<typeof prompts>).mockReturnValue(
Promise.resolve({
cache: [],
}),
);

await clean([], mockConfig, {include: '', projectRoot: process.cwd()});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,11 @@ jest.mock('prompts', () => jest.fn());
describe('promptForTaskSelection', () => {
it('should prompt with correct tasks', () => {
(execa.sync as jest.Mock).mockReturnValueOnce({stdout: gradleTaskOutput});
prompts.mockReturnValue({task: []});
(prompts as jest.MockedFunction<typeof prompts>).mockReturnValue(
Promise.resolve({
task: [],
}),
);

promptForTaskSelection('install', 'sourceDir');

Expand Down
4 changes: 2 additions & 2 deletions packages/cli/src/commands/init/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import init from './init';
export default {
func: init,
detached: true,
name: 'init <projectName>',
name: 'init [projectName]',
description:
'Initialize a new React Native project named <projectName> in a directory of the same name.',
'New app will be initialized in the directory of the same name. Android and iOS projects will use this name for publishing setup.',
options: [
{
name: '--version <string>',
Expand Down
10 changes: 10 additions & 0 deletions packages/cli/src/commands/init/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import TemplateAndVersionError from './errors/TemplateAndVersionError';
import {getBunVersionIfAvailable} from '../../tools/bun';
import {getNpmVersionIfAvailable} from '../../tools/npm';
import {getYarnVersionIfAvailable} from '../../tools/yarn';
import prompts from 'prompts';

const DEFAULT_VERSION = 'latest';

Expand Down Expand Up @@ -274,6 +275,15 @@ export default (async function initialize(
[projectName]: Array<string>,
options: Options,
) {
if (!projectName) {
const {projName} = await prompts({
type: 'text',
name: 'projName',
message: 'How would you like to name the app?',
});
projectName = projName;
}

validateProjectName(projectName);

if (!!options.template && !!options.version) {
Expand Down

0 comments on commit c50e077

Please sign in to comment.