-
Notifications
You must be signed in to change notification settings - Fork 34
/
copy.ts
42 lines (37 loc) · 1.48 KB
/
copy.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import path from 'path';
import {existsSync} from 'fs';
import fs from 'fs/promises';
import {globby} from 'globby';
import ora from 'ora';
import {dirFromImportMeta} from '@reskript/core';
import {UserOptions} from '../interface.js';
export default async (cwd: string, options: UserOptions) => {
const spinner = ora('Copying initial files');
spinner.start();
const templateDirectory = path.join(dirFromImportMeta(import.meta.url), '..', '..', 'templates', 'normal-app');
const files = await globby(`${templateDirectory}/**`, {dot: true});
const copyFile = async (file: string) => {
const relative = path.relative(templateDirectory, file);
const destination = path.join(cwd, relative);
await fs.mkdir(path.dirname(destination), {recursive: true});
const content = await fs.readFile(file, 'utf-8');
await fs.writeFile(
destination,
content.replace(/\{\{(\w+)\}\}/g, (match, key) => (options as any)[key].toString())
);
};
await Promise.all(files.map(copyFile));
const gitignoreExists = existsSync(path.join(cwd, '.gitignore'));
if (gitignoreExists) {
const data = await fs.readFile(path.join(cwd, 'gitignore'));
await fs.appendFile(path.join(cwd, '.gitignore'), data);
await fs.unlink(path.join(cwd, 'gitignore'));
}
else {
await fs.rename(
path.join(cwd, 'gitignore'),
path.join(cwd, '.gitignore')
);
}
spinner.succeed();
};