Skip to content

Commit 1bb7e62

Browse files
committed
refactor: improve template selection
1 parent 8ae6a79 commit 1bb7e62

File tree

5 files changed

+49
-15
lines changed

5 files changed

+49
-15
lines changed

packages/create-app/package.json

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
{
22
"name": "@callstack/create-rnef-app",
33
"version": "0.0.1",
4+
"type": "commonjs",
5+
"main": "./src/index.js",
6+
"typings": "./src/index.d.ts",
7+
"bin": {
8+
"create-rnef-app": "./src/bin.js"
9+
},
410
"dependencies": {
511
"@clack/prompts": "^0.7.0",
612
"minimist": "^1.2.8",
713
"tslib": "^2.3.0"
814
},
915
"devDependencies": {
1016
"@types/minimist": "^1.2.5"
11-
},
12-
"type": "commonjs",
13-
"main": "./src/index.js",
14-
"typings": "./src/index.d.ts"
17+
}
1518
}

packages/create-app/project.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
"executor": "@nx/js:tsc",
1818
"outputs": ["{options.outputPath}"],
1919
"options": {
20-
"outputPath": "dist/packages/create-app",
20+
"outputPath": "packages/create-app/dist",
2121
"main": "packages/create-app/src/index.ts",
2222
"tsConfig": "packages/create-app/tsconfig.lib.json",
2323
"assets": ["packages/create-app/*.md"]

packages/create-app/src/lib/create-app.ts

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,10 @@ import {
1010
promptProjectName,
1111
printWelcomeMessage,
1212
printByeMessage,
13+
promptTemplate,
1314
} from './prompts';
1415
import { copyDir, isEmptyDir, removeDir, resolveAbsolutePath } from './fs';
15-
import { parseCliOptions } from './parse-args';
16+
import { parseCliOptions } from './parse-cli-options';
1617

1718
const TEMPLATES = ['default'];
1819

@@ -32,7 +33,7 @@ async function create() {
3233
printWelcomeMessage();
3334

3435
const projectName =
35-
(options.dir || options.projectName) ?? (await promptProjectName());
36+
(options.dir || options.name) ?? (await promptProjectName());
3637
const { targetDir } = parsePackageInfo(projectName);
3738
const absoluteTargetDir = resolveAbsolutePath(targetDir);
3839

@@ -49,11 +50,17 @@ async function create() {
4950

5051
removeDir(absoluteTargetDir);
5152

52-
const templateName = TEMPLATES[0];
53-
// TODO: figure monorepo workaround
54-
const srcDir = path.join(__dirname, '..', `rnef-template-${templateName}`);
53+
const templateName = options.template ?? (await promptTemplate(TEMPLATES));
54+
const srcDir = path.join(
55+
__dirname,
56+
// Workaround for getting the template from within the monorepo
57+
// TODO: implement downloading templates from NPM
58+
'../../../../../templates',
59+
`rnef-template-${templateName}`
60+
);
61+
5562
if (!fs.existsSync(srcDir)) {
56-
throw new Error(`Invalid input: template "${templateName}" not found.`);
63+
throw new Error(`Invalid template: template "${templateName}" not found.`);
5764
}
5865

5966
copyDir(srcDir, absoluteTargetDir);

packages/create-app/src/lib/parse-args.ts renamed to packages/create-app/src/lib/parse-cli-options.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import minimist from 'minimist';
22

33
export type CliOptions = {
4-
projectName?: string;
4+
name?: string;
5+
template?: string;
56
help?: boolean;
67
version?: boolean;
78
dir?: string;
@@ -14,7 +15,8 @@ export function parseCliOptions(argv: string[]): CliOptions {
1415
});
1516

1617
return {
17-
projectName: options._[0],
18+
name: options._[0],
19+
template: options.template,
1820
help: options.help,
1921
version: options.version,
2022
dir: options.dir,

packages/create-app/src/lib/prompts.ts

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,12 @@ export function printByeMessage(targetDir: string) {
4545
process.env['npm_config_user_agent']
4646
);
4747

48+
const pkgManagerCommand = pkgManager?.name ?? 'npm';
49+
4850
const nextSteps = [
4951
`cd ${targetDir}`,
50-
`${pkgManager} install`,
51-
`${pkgManager} run start`,
52+
`${pkgManagerCommand} install`,
53+
`${pkgManagerCommand} run start`,
5254
].join('\n');
5355

5456
note(nextSteps, 'Next steps');
@@ -64,6 +66,26 @@ export async function promptProjectName() {
6466
);
6567
}
6668

69+
export async function promptTemplate(templates: string[]): Promise<string> {
70+
if (templates.length === 0) {
71+
throw new Error('No templates found');
72+
}
73+
74+
if (templates.length === 1) {
75+
return templates[0];
76+
}
77+
78+
return checkCancel<string>(
79+
await select({
80+
message: 'Select a template:',
81+
options: templates.map((template) => ({
82+
value: template,
83+
label: template,
84+
})),
85+
})
86+
);
87+
}
88+
6789
export async function confirmOverrideFiles(targetDir: string) {
6890
const option = checkCancel<string>(
6991
await select({

0 commit comments

Comments
 (0)