Skip to content

Commit c50e077

Browse files
fix: improve providing project name in init command (#2071)
* fix: improve providing project name * chore: update descriptions * test: update tests
1 parent d695144 commit c50e077

File tree

6 files changed

+37
-12
lines changed

6 files changed

+37
-12
lines changed

__e2e__/__snapshots__/default.test.ts.snap

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@ Options:
88
-h, --help display help for command
99
1010
Commands:
11-
init [options] <projectName> Initialize a new React Native project named
12-
<projectName> in a directory of the same name.
11+
init [options] [projectName] New app will be initialized in the directory of
12+
the same name. Android and iOS projects will
13+
use this name for publishing setup.
1314
doctor [options] Diagnose and fix common Node.js, iOS, Android &
1415
React Native issues.
1516
help [command] display help for command"

__e2e__/init.test.ts

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ import {
77
writeFiles,
88
} from '../jest/helpers';
99
import slash from 'slash';
10+
import prompts from 'prompts';
11+
12+
jest.mock('prompts', () => jest.fn());
1013

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

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

59-
test('init --template fails without package name', () => {
60-
const {stderr} = runCLI(
61-
DIR,
62-
['init', '--template', 'react-native-new-template'],
63-
{expectedFailure: true},
62+
test('init should prompt for the project name', () => {
63+
createCustomTemplateFiles();
64+
const {stdout} = runCLI(DIR, ['init', 'test', '--template', templatePath]);
65+
66+
(prompts as jest.MockedFunction<typeof prompts>).mockReturnValue(
67+
Promise.resolve({
68+
name: 'TestInit',
69+
}),
6470
);
65-
expect(stderr).toContain('missing required argument');
71+
expect(stdout).toContain('Run instructions');
6672
});
6773

6874
test('init --template filepath', () => {

packages/cli-clean/src/__tests__/clean.test.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,11 @@ describe('clean', () => {
1818
});
1919

2020
it('prompts if `--include` is omitted', async () => {
21-
prompts.mockReturnValue({cache: []});
21+
(prompts as jest.MockedFunction<typeof prompts>).mockReturnValue(
22+
Promise.resolve({
23+
cache: [],
24+
}),
25+
);
2226

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

packages/cli-platform-android/src/commands/runAndroid/__tests__/listAndroidTasks.test.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,11 @@ jest.mock('prompts', () => jest.fn());
105105
describe('promptForTaskSelection', () => {
106106
it('should prompt with correct tasks', () => {
107107
(execa.sync as jest.Mock).mockReturnValueOnce({stdout: gradleTaskOutput});
108-
prompts.mockReturnValue({task: []});
108+
(prompts as jest.MockedFunction<typeof prompts>).mockReturnValue(
109+
Promise.resolve({
110+
task: [],
111+
}),
112+
);
109113

110114
promptForTaskSelection('install', 'sourceDir');
111115

packages/cli/src/commands/init/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ import init from './init';
33
export default {
44
func: init,
55
detached: true,
6-
name: 'init <projectName>',
6+
name: 'init [projectName]',
77
description:
8-
'Initialize a new React Native project named <projectName> in a directory of the same name.',
8+
'New app will be initialized in the directory of the same name. Android and iOS projects will use this name for publishing setup.',
99
options: [
1010
{
1111
name: '--version <string>',

packages/cli/src/commands/init/init.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import TemplateAndVersionError from './errors/TemplateAndVersionError';
2424
import {getBunVersionIfAvailable} from '../../tools/bun';
2525
import {getNpmVersionIfAvailable} from '../../tools/npm';
2626
import {getYarnVersionIfAvailable} from '../../tools/yarn';
27+
import prompts from 'prompts';
2728

2829
const DEFAULT_VERSION = 'latest';
2930

@@ -274,6 +275,15 @@ export default (async function initialize(
274275
[projectName]: Array<string>,
275276
options: Options,
276277
) {
278+
if (!projectName) {
279+
const {projName} = await prompts({
280+
type: 'text',
281+
name: 'projName',
282+
message: 'How would you like to name the app?',
283+
});
284+
projectName = projName;
285+
}
286+
277287
validateProjectName(projectName);
278288

279289
if (!!options.template && !!options.version) {

0 commit comments

Comments
 (0)