Skip to content

Commit 159a38b

Browse files
authored
Merge pull request #296 from mrzachnugent/@zach/init-new-project
feat(cli): init new project
2 parents a3cd9cd + da218e0 commit 159a38b

File tree

10 files changed

+268
-259
lines changed

10 files changed

+268
-259
lines changed

apps/cli/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@react-native-reusables/cli",
3-
"version": "0.2.2",
3+
"version": "0.3.0",
44
"description": "Add react-native-reusables to your project.",
55
"publishConfig": {
66
"access": "public"

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

Lines changed: 2 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@ import { existsSync, promises as fs } from 'fs';
44
import path from 'path';
55
import { COMPONENTS } from '../src/items/components';
66
import { TEMPLATES } from '../src/items/templates';
7+
import { copyFolder } from '../src/utils/copy-folder';
78

89
async function main() {
910
for (const template of TEMPLATES) {
10-
await copyFolder(template.path);
11+
await copyFolder(template.path, path.join('__generated', template.name));
1112
}
1213
for (const comp of COMPONENTS) {
1314
if (Array.isArray(comp.paths)) {
@@ -34,33 +35,3 @@ async function writeFiles(paths: Array<{ from: string; to: { folder: string; fil
3435
}
3536
}
3637
}
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/add.ts

Lines changed: 3 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,19 @@
1-
import {
2-
Config,
3-
DEFAULT_COMPONENTS,
4-
DEFAULT_LIB,
5-
getConfig,
6-
rawConfigSchema,
7-
resolveConfigPaths,
8-
} from '@/src/utils/get-config';
1+
import { Config, getConfig } from '@/src/utils/get-config';
92
import { getPackageManager } from '@/src/utils/get-package-manager';
103
import { handleError } from '@/src/utils/handle-error';
114
import { logger } from '@/src/utils/logger';
5+
import { promptForConfig } from '@/src/utils/prompt-for-config';
126
import chalk from 'chalk';
137
import { Command } from 'commander';
148
import { execa } from 'execa';
159
import { existsSync, promises as fs } from 'fs';
1610
import ora, { Ora } from 'ora';
1711
import path from 'path';
1812
import prompts from 'prompts';
13+
import { fileURLToPath } from 'url';
1914
import { z } from 'zod';
2015
import { Component, INVALID_COMPONENT_ERROR, getAllComponentsToWrite } from '../items';
2116
import { COMPONENTS } from '../items/components';
22-
import { fileURLToPath } from 'url';
2317

2418
const filePath = fileURLToPath(import.meta.url);
2519
const fileDir = path.dirname(filePath);
@@ -242,47 +236,3 @@ function fixImports(rawfile: string, componentsAlias: string, libAlias: string)
242236
.replaceAll('../../components', componentsAlias)
243237
.replaceAll('../../lib', libAlias);
244238
}
245-
246-
async function promptForConfig(cwd: string) {
247-
const highlight = (text: string) => chalk.cyan(text);
248-
249-
const options = await prompts([
250-
{
251-
type: 'text',
252-
name: 'components',
253-
message: `Configure the import alias for ${highlight('components')}:`,
254-
initial: DEFAULT_COMPONENTS,
255-
},
256-
{
257-
type: 'text',
258-
name: 'lib',
259-
message: `Configure the import alias for ${highlight('lib')}:`,
260-
initial: DEFAULT_LIB,
261-
},
262-
]);
263-
264-
const config = rawConfigSchema.parse({
265-
aliases: {
266-
lib: options.lib,
267-
components: options.components,
268-
},
269-
});
270-
271-
const { proceed } = await prompts({
272-
type: 'confirm',
273-
name: 'proceed',
274-
message: `Write configuration to ${highlight('components.json')}. Proceed?`,
275-
initial: true,
276-
});
277-
278-
if (proceed) {
279-
// Write to file.
280-
logger.info('');
281-
const spinner = ora(`Writing components.json...`).start();
282-
const targetPath = path.resolve(cwd, 'components.json');
283-
await fs.writeFile(targetPath, JSON.stringify(config, null, 2), 'utf8');
284-
spinner.succeed();
285-
}
286-
287-
return await resolveConfigPaths(cwd, config);
288-
}

0 commit comments

Comments
 (0)