Skip to content

Commit 6fba4bd

Browse files
committed
fix: generate starter-base file for npm package
1 parent 69516ec commit 6fba4bd

File tree

3 files changed

+59
-18
lines changed

3 files changed

+59
-18
lines changed

apps/cli/scripts/generate-source-files.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,12 @@
33
import { existsSync, promises as fs } from 'fs';
44
import path from 'path';
55
import { COMPONENTS } from '../src/items/components';
6+
import { TEMPLATES } from '../src/items/templates';
67

78
async function main() {
9+
for (const template of TEMPLATES) {
10+
await copyFolder(template.path);
11+
}
812
for (const comp of COMPONENTS) {
913
if (Array.isArray(comp.paths)) {
1014
await writeFiles(comp.paths);
@@ -30,3 +34,33 @@ async function writeFiles(paths: Array<{ from: string; to: { folder: string; fil
3034
}
3135
}
3236
}
37+
38+
async function copyFolder(src: string, destPath?: string) {
39+
if (!existsSync(src)) {
40+
throw new Error(`Source folder does not exist: ${src}`);
41+
}
42+
43+
const paths = src.split('/');
44+
const folderName = paths[paths.length - 1];
45+
46+
const dest = destPath ?? path.join('__generated', folderName);
47+
48+
if (!existsSync(dest)) {
49+
await fs.mkdir(dest, { recursive: true });
50+
}
51+
52+
const entries = await fs.readdir(src, { withFileTypes: true });
53+
54+
for (const entry of entries) {
55+
const srcPath = path.join(src, entry.name);
56+
const destPath = path.join(dest, entry.name);
57+
58+
if (entry.isDirectory()) {
59+
// Recursively copy subdirectories
60+
await copyFolder(srcPath, destPath);
61+
} else if (entry.isFile()) {
62+
// Copy files
63+
await fs.copyFile(srcPath, destPath);
64+
}
65+
}
66+
}

apps/cli/src/commands/init.ts

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,26 @@
1-
import { existsSync } from 'fs';
2-
import { promises as fs } from 'fs';
3-
import { Command } from 'commander';
4-
import { execa } from 'execa';
5-
import ora, { Ora } from 'ora';
6-
import path from 'path';
7-
import { z } from 'zod';
8-
import { handleError } from '@/src/utils/handle-error';
9-
import { logger } from '@/src/utils/logger';
10-
import chalk from 'chalk';
11-
import prompts from 'prompts';
12-
import glob from 'fast-glob';
13-
import { createRequire } from 'module';
14-
import { execSync } from 'child_process';
151
import {
2+
DEFAULT_COMPONENTS,
3+
DEFAULT_LIB,
164
getConfig,
175
rawConfigSchema,
186
resolveConfigPaths,
19-
DEFAULT_COMPONENTS,
20-
DEFAULT_LIB,
217
} from '@/src/utils/get-config';
8+
import { handleError } from '@/src/utils/handle-error';
9+
import { logger } from '@/src/utils/logger';
10+
import chalk from 'chalk';
11+
import { execSync } from 'child_process';
12+
import { Command } from 'commander';
13+
import { execa } from 'execa';
14+
import glob from 'fast-glob';
15+
import { existsSync, promises as fs } from 'fs';
16+
import ora, { Ora } from 'ora';
17+
import path from 'path';
18+
import prompts from 'prompts';
19+
import { fileURLToPath } from 'url';
20+
import { z } from 'zod';
21+
22+
const filePath = fileURLToPath(import.meta.url);
23+
const fileDir = path.dirname(filePath);
2224

2325
const initOptionsSchema = z.object({
2426
cwd: z.string(),
@@ -29,7 +31,6 @@ const REQUIRED_DEPENDENCIES = [
2931
'nativewind',
3032
'expo-navigation-bar',
3133
'tailwindcss-animate',
32-
'@react-native-async-storage/async-storage',
3334
'class-variance-authority',
3435
'clsx',
3536
'tailwind-merge',
@@ -282,7 +283,7 @@ async function initializeProject(cwd: string, overwrite: boolean) {
282283
spinner.start();
283284
}
284285

285-
const templatesDir = path.dirname(createRequire(import.meta.url).resolve('@rnr/starter-base'));
286+
const templatesDir = path.join(fileDir, '../__generated/starter-base');
286287

287288
await installDependencies(cwd, spinner);
288289
await updateTsConfig(cwd, config, spinner);

apps/cli/src/items/templates.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
export const TEMPLATES = [
2+
{
3+
name: 'starter-base',
4+
path: './node_modules/@rnr/starter-base',
5+
},
6+
];

0 commit comments

Comments
 (0)