Skip to content

Commit 2b2f5e2

Browse files
committed
Refactoring 🔧
1 parent 5a3cc29 commit 2b2f5e2

File tree

4 files changed

+61
-70
lines changed

4 files changed

+61
-70
lines changed

packages/create-typink/src/tasks/copyTemplateFiles.ts

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,33 +15,32 @@ export async function copyTemplateFiles(options: Options, templatesDir: string,
1515
throw new Error(`Preset directory not found: ${templateDir}`);
1616
}
1717

18-
fs.cpSync(templateDir, targetDir, { recursive: true });
18+
await fs.promises.cp(templateDir, targetDir, { recursive: true });
1919

2020
const packageJsonPath = `${targetDir}/package.json`;
2121
const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf-8'));
2222

2323
packageJson.name = projectName;
24+
await fs.promises.writeFile(packageJsonPath, JSON.stringify(packageJson, null, 2));
2425

25-
fs.writeFileSync(packageJsonPath, JSON.stringify(packageJson, null, 2));
26-
27-
processTemplate(options, targetDir);
26+
processTemplateFiles(options, targetDir);
2827

2928
if (!noGit) {
3029
await execa('git', ['init'], { cwd: targetDir });
3130
await execa('git', ['checkout', '-b', 'main'], { cwd: targetDir });
3231
}
3332
}
3433

35-
export async function processTemplate(rawOptions: Options, targetDir: string) {
34+
export async function processTemplateFiles(rawOptions: Options, targetDir: string) {
3635
const options = {
3736
...rawOptions,
3837
networks: rawOptions.networks?.map(stringCamelCase),
3938
};
4039

41-
await _processTemplate(options, targetDir);
40+
await processTemplateFilesRecursive(options, targetDir);
4241
}
4342

44-
export async function _processTemplate(options: any, dir: string) {
43+
async function processTemplateFilesRecursive(options: any, dir: string) {
4544
if (IS_IGNORE_FILES.test(dir)) {
4645
return;
4746
}
@@ -52,7 +51,7 @@ export async function _processTemplate(options: any, dir: string) {
5251
const filePath = path.join(dir, file.name);
5352

5453
if (file.isDirectory()) {
55-
await _processTemplate(options, filePath);
54+
await processTemplateFilesRecursive(options, filePath);
5655
} else {
5756
if (IS_TEMPLATE_FILE.test(filePath)) {
5857
const content = fs.readFileSync(filePath, 'utf-8');

packages/create-typink/src/tasks/createProject.ts

Lines changed: 0 additions & 53 deletions
This file was deleted.
Lines changed: 51 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,51 @@
1-
export * from './createFirstCommit.js';
2-
export * from './copyTemplateFiles.js';
3-
export * from './createProjectDirectory.js';
4-
export * from './installPackages.js';
5-
export * from './prettierFormat.js';
6-
export * from './createProject.js';
1+
import { Options } from '../types.js';
2+
import { Listr } from 'listr2';
3+
import { fileURLToPath } from 'url';
4+
import * as path from 'path';
5+
import chalk from 'chalk';
6+
import { prettierFormat } from './prettierFormat.js';
7+
import { createFirstCommit } from './createFirstCommit.js';
8+
import { installPackages } from './installPackages.js';
9+
import { createProjectDirectory } from './createProjectDirectory.js';
10+
import { copyTemplateFiles } from './copyTemplateFiles.js';
11+
12+
export async function createProject(options: Options) {
13+
const { projectName, skipInstall, noGit } = options;
14+
15+
const __filename = fileURLToPath(import.meta.url);
16+
const __dirname = path.dirname(__filename);
17+
18+
const rootDirectory = path.resolve(__dirname, '../../');
19+
const templateDirectory = path.resolve(__dirname, '../../templates');
20+
const targetDirectory = path.resolve(process.cwd(), projectName!);
21+
22+
const tasks = new Listr(
23+
[
24+
{
25+
title: `📁 Create project directory ${targetDirectory}`,
26+
task: () => createProjectDirectory(projectName!),
27+
},
28+
{
29+
title: `🚀 Creating a new Typink app in ${chalk.green.bold(projectName)}`,
30+
task: () => copyTemplateFiles(options, templateDirectory, targetDirectory),
31+
},
32+
{
33+
title: '📦 Installing dependencies with yarn, this could take a while',
34+
task: () => installPackages(targetDirectory),
35+
skip: skipInstall,
36+
},
37+
{
38+
title: '🧹 Formatting the code with Prettier',
39+
task: () => prettierFormat(rootDirectory, targetDirectory, options),
40+
},
41+
{
42+
title: `🚨 Create the very first Git commit`,
43+
task: () => createFirstCommit(targetDirectory),
44+
skip: noGit,
45+
},
46+
],
47+
{ rendererOptions: { suffixSkips: true }, exitOnError: true },
48+
);
49+
50+
await tasks.run();
51+
}

packages/create-typink/src/tasks/prettierFormat.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,18 @@ export async function prettierFormat(rootDir: string, targetDir: string, options
1010
} else {
1111
const prettierConfig = await prettier.resolveConfig(path.resolve(rootDir, './.prettierrc.js'));
1212

13-
await prettierFormatWithConfig(targetDir, prettierConfig);
13+
await prettierFormatRecursive(targetDir, prettierConfig);
1414
}
1515
}
1616

17-
export async function prettierFormatWithConfig(dir: string, config: prettier.Options | null) {
17+
async function prettierFormatRecursive(dir: string, config: prettier.Options | null) {
1818
const files = await fs.readdir(dir, { withFileTypes: true });
1919

2020
for (const file of files) {
2121
const filePath = path.join(dir, file.name);
2222

2323
if (file.isDirectory()) {
24-
await prettierFormatWithConfig(filePath, config);
24+
await prettierFormatRecursive(filePath, config);
2525
} else {
2626
const fileInfo = await prettier.getFileInfo(filePath);
2727

0 commit comments

Comments
 (0)