Skip to content

Commit b5ee46c

Browse files
authored
feat: add platform-name option to init for out of tree platforms init (#2170)
* feat: add platform-name option to init for out of tree platforms init * chore: add tests, update docs
1 parent 42889b6 commit b5ee46c

File tree

4 files changed

+49
-6
lines changed

4 files changed

+49
-6
lines changed

__e2e__/init.test.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,3 +168,29 @@ test('init uses npm as the package manager with --npm', () => {
168168
expect(fs.existsSync(path.join(initDirPath, file))).toBe(true);
169169
});
170170
});
171+
172+
test('init --platform-name should work for out of tree platform', () => {
173+
createCustomTemplateFiles();
174+
const outOfTreePlatformName = 'react-native-macos';
175+
176+
const {stdout} = runCLI(DIR, [
177+
'init',
178+
PROJECT_NAME,
179+
'--platform-name',
180+
outOfTreePlatformName,
181+
'--skip-install',
182+
'--verbose',
183+
]);
184+
185+
expect(stdout).toContain('Run instructions');
186+
expect(stdout).toContain(
187+
`Installing template from ${outOfTreePlatformName}@latest`,
188+
);
189+
190+
// make sure we don't leave garbage
191+
expect(fs.readdirSync(DIR)).toContain('custom');
192+
193+
let dirFiles = fs.readdirSync(path.join(DIR, PROJECT_NAME));
194+
195+
expect(dirFiles.length).toBeGreaterThan(0);
196+
});

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,5 +47,10 @@ export default {
4747
description:
4848
'Inits a project with a custom package name (Android) and bundle ID (iOS), e.g. com.example.app',
4949
},
50+
{
51+
name: '--platform-name <string>',
52+
description:
53+
'Name of out of tree platform to be used for ex. react-native-macos. This flag is optional as it should be passed automatically by out of tree platform. It needs to match the name of the platform declared in package.json',
54+
},
5055
],
5156
};

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ type Options = {
4444
version?: string;
4545
packageName?: string;
4646
installPods?: string | boolean;
47+
platformName?: string;
4748
};
4849

4950
interface TemplateOptions {
@@ -261,14 +262,17 @@ function createTemplateUri(options: Options, version: string): string {
261262
const isTypescriptTemplate =
262263
options.template === 'react-native-template-typescript';
263264

265+
// This allows to correctly retrieve template uri for out of tree platforms.
266+
const platform = options.platformName || 'react-native';
267+
264268
if (isTypescriptTemplate) {
265269
logger.warn(
266270
"Ignoring custom template: 'react-native-template-typescript'. Starting from React Native v0.71 TypeScript is used by default.",
267271
);
268-
return 'react-native';
272+
return platform;
269273
}
270274

271-
return options.template || `react-native@${version}`;
275+
return options.template || `${platform}@${version}`;
272276
}
273277

274278
//remove quotes from object keys to match the linter rules of the template

packages/cli/src/index.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -134,15 +134,16 @@ function attachCommand<C extends Command<boolean>>(
134134
}
135135
}
136136

137-
async function run() {
137+
// Platform name is optional argument passed by out of tree platforms.
138+
async function run(platformName?: string) {
138139
try {
139-
await setupAndRun();
140+
await setupAndRun(platformName);
140141
} catch (e) {
141142
handleError(e as Error);
142143
}
143144
}
144145

145-
async function setupAndRun() {
146+
async function setupAndRun(platformName?: string) {
146147
// Commander is not available yet
147148

148149
// when we run `config`, we don't want to output anything to the console. We
@@ -210,7 +211,14 @@ async function setupAndRun() {
210211
}
211212
}
212213

213-
program.parse(process.argv);
214+
const argv = [...process.argv];
215+
216+
// If out of tree platform specifices custom platform name by passing it to , we need to pass it to argv array.
217+
if (platformName) {
218+
argv.push('--platform-name', platformName);
219+
}
220+
221+
program.parse(argv);
214222
}
215223

216224
const bin = require.resolve('./bin');

0 commit comments

Comments
 (0)