Skip to content

Commit eb2dc4d

Browse files
committed
refactor: extract applytemplate
1 parent 48b3c66 commit eb2dc4d

File tree

2 files changed

+71
-51
lines changed

2 files changed

+71
-51
lines changed

packages/create-react-native-library/src/index.ts

Lines changed: 26 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import path from 'path';
22
import fs from 'fs-extra';
3-
import ejs from 'ejs';
43
import dedent from 'dedent';
54
import kleur from 'kleur';
65
import yargs from 'yargs';
@@ -16,17 +15,13 @@ import { spawn } from './utils/spawn';
1615
import { version } from '../package.json';
1716
import { addCodegenBuildScript } from './exampleApp/addCodegenBuildScript';
1817
import { createInitialGitCommit } from './utils/initialCommit';
19-
import { assertNpx, assertOptions } from './utils/assert';
18+
import { assertAnswers, assertNpx } from './utils/assert';
2019
import { resolveBobVersionWithFallback } from './utils/promiseWithFallback';
2120
import { generateTemplateConfiguration } from './template/config';
21+
import { applyTemplate } from './template/applyTemplate';
2222

2323
const FALLBACK_BOB_VERSION = '0.32.0';
2424

25-
const BINARIES = [
26-
/(gradlew|\.(jar|keystore|png|jpg|gif))$/,
27-
/\$\.yarn(?![a-z])/,
28-
];
29-
3025
const COMMON_FILES = path.resolve(__dirname, '../templates/common');
3126
const COMMON_EXAMPLE_FILES = path.resolve(
3227
__dirname,
@@ -465,7 +460,7 @@ async function create(_argv: yargs.Arguments<any>) {
465460
});
466461
}
467462

468-
assertOptions(questions, argv);
463+
assertAnswers(questions, argv);
469464

470465
const singleChoiceAnswers: Partial<Answers> = {};
471466
const finalQuestions: Question[] = [];
@@ -516,7 +511,7 @@ async function create(_argv: yargs.Arguments<any>) {
516511
...promptAnswers,
517512
} as Required<Answers>;
518513

519-
assertOptions(questions, answers);
514+
assertAnswers(questions, answers);
520515

521516
const bobVersion = await resolveBobVersion();
522517

@@ -526,35 +521,6 @@ async function create(_argv: yargs.Arguments<any>) {
526521
answers,
527522
});
528523

529-
async function applyTemplate(source: string, destination: string) {
530-
await fs.mkdirp(destination);
531-
532-
const files = await fs.readdir(source);
533-
534-
for (const f of files) {
535-
const target = path.join(
536-
destination,
537-
ejs.render(f.replace(/^\$/, ''), config, {
538-
openDelimiter: '{',
539-
closeDelimiter: '}',
540-
})
541-
);
542-
543-
const file = path.join(source, f);
544-
const stats = await fs.stat(file);
545-
546-
if (stats.isDirectory()) {
547-
await applyTemplate(file, target);
548-
} else if (!BINARIES.some((r) => r.test(file))) {
549-
const content = await fs.readFile(file, 'utf8');
550-
551-
await fs.writeFile(target, ejs.render(content, config));
552-
} else {
553-
await fs.copyFile(file, target);
554-
}
555-
}
556-
}
557-
558524
await fs.mkdirp(folder);
559525

560526
if (answers.reactNativeVersion != null) {
@@ -593,52 +559,61 @@ async function create(_argv: yargs.Arguments<any>) {
593559
spinner.text = 'Copying files';
594560

595561
if (local) {
596-
await applyTemplate(COMMON_LOCAL_FILES, folder);
562+
await applyTemplate(config, COMMON_LOCAL_FILES, folder);
597563
} else {
598-
await applyTemplate(COMMON_FILES, folder);
564+
await applyTemplate(config, COMMON_FILES, folder);
599565

600566
if (config.example !== 'none') {
601-
await applyTemplate(COMMON_EXAMPLE_FILES, folder);
567+
await applyTemplate(config, COMMON_EXAMPLE_FILES, folder);
602568
}
603569
}
604570

605571
if (answers.languages === 'js') {
606-
await applyTemplate(JS_FILES, folder);
607-
await applyTemplate(EXPO_FILES, folder);
572+
await applyTemplate(config, JS_FILES, folder);
573+
await applyTemplate(config, EXPO_FILES, folder);
608574
} else {
609-
await applyTemplate(NATIVE_COMMON_FILES, folder);
575+
await applyTemplate(config, NATIVE_COMMON_FILES, folder);
610576

611577
if (config.example !== 'none') {
612-
await applyTemplate(NATIVE_COMMON_EXAMPLE_FILES, folder);
578+
await applyTemplate(config, NATIVE_COMMON_EXAMPLE_FILES, folder);
613579
}
614580

615581
if (config.project.module) {
616582
await applyTemplate(
583+
config,
617584
NATIVE_FILES[`module_${config.project.arch}`],
618585
folder
619586
);
620587
} else {
621-
await applyTemplate(NATIVE_FILES[`view_${config.project.arch}`], folder);
588+
await applyTemplate(
589+
config,
590+
NATIVE_FILES[`view_${config.project.arch}`],
591+
folder
592+
);
622593
}
623594

624595
if (config.project.swift) {
625-
await applyTemplate(SWIFT_FILES[`module_legacy`], folder);
596+
await applyTemplate(config, SWIFT_FILES[`module_legacy`], folder);
626597
} else {
627598
if (config.project.module) {
628-
await applyTemplate(OBJC_FILES[`module_common`], folder);
599+
await applyTemplate(config, OBJC_FILES[`module_common`], folder);
629600
} else {
630-
await applyTemplate(OBJC_FILES[`view_${config.project.arch}`], folder);
601+
await applyTemplate(
602+
config,
603+
OBJC_FILES[`view_${config.project.arch}`],
604+
folder
605+
);
631606
}
632607
}
633608

634609
const templateType = `${config.project.module ? 'module' : 'view'}_${
635610
config.project.arch
636611
}` as const;
637612

638-
await applyTemplate(KOTLIN_FILES[templateType], folder);
613+
await applyTemplate(config, KOTLIN_FILES[templateType], folder);
639614

640615
if (config.project.cpp) {
641-
await applyTemplate(CPP_FILES, folder);
616+
await applyTemplate(config, CPP_FILES, folder);
642617
await fs.remove(path.join(folder, 'ios', `${config.project.name}.m`));
643618
}
644619
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import path from 'path';
2+
import fs from 'fs-extra';
3+
import ejs from 'ejs';
4+
import type { TemplateConfiguration } from './config';
5+
6+
const BINARIES = [
7+
/(gradlew|\.(jar|keystore|png|jpg|gif))$/,
8+
/\$\.yarn(?![a-z])/,
9+
];
10+
11+
/**
12+
* This copies the template files and renders them via ejs
13+
*/
14+
export async function applyTemplate(
15+
config: TemplateConfiguration,
16+
source: string,
17+
destination: string
18+
) {
19+
await fs.mkdirp(destination);
20+
21+
const files = await fs.readdir(source);
22+
23+
for (const f of files) {
24+
const target = path.join(
25+
destination,
26+
ejs.render(f.replace(/^\$/, ''), config, {
27+
openDelimiter: '{',
28+
closeDelimiter: '}',
29+
})
30+
);
31+
32+
const file = path.join(source, f);
33+
const stats = await fs.stat(file);
34+
35+
if (stats.isDirectory()) {
36+
await applyTemplate(config, file, target);
37+
} else if (!BINARIES.some((r) => r.test(file))) {
38+
const content = await fs.readFile(file, 'utf8');
39+
40+
await fs.writeFile(target, ejs.render(content, config));
41+
} else {
42+
await fs.copyFile(file, target);
43+
}
44+
}
45+
}

0 commit comments

Comments
 (0)