-
Notifications
You must be signed in to change notification settings - Fork 0
/
createProject.ts
55 lines (45 loc) · 1.49 KB
/
createProject.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
43
44
45
46
47
48
49
50
51
52
53
54
55
import * as fs from 'node:fs';
import * as path from 'node:path';
import { printError } from './utils/print.js';
import { MESSAGES, SKIP_FILENAMES } from './config.js';
import { saveDirectoryWithContent, saveFile } from './fs.js';
import { OptionsType } from './types.js';
export const createDirectoryContents = (
options: OptionsType,
relativePath = '',
skipFileNames = SKIP_FILENAMES
): void => {
const buildRelativePath = (parentPath: string) =>
path.join(parentPath, relativePath);
const projectBuildPath = buildRelativePath(options.buildDir);
const fullTemplatePath = buildRelativePath(options.templatePath);
const entriesToCreate = fs.readdirSync(fullTemplatePath);
entriesToCreate.forEach((entryName) => {
if (skipFileNames.includes(entryName)) {
return;
}
const entryPath = path.join(fullTemplatePath, entryName);
const stats = fs.statSync(entryPath);
const isDirectory = stats.isDirectory();
const saveProps = {
entryPath: isDirectory ? relativePath : entryPath,
options,
entryName,
projectBuildPath,
};
if (isDirectory) {
return saveDirectoryWithContent(saveProps);
}
if (stats.isFile()) {
return saveFile(saveProps);
}
});
};
export const createProject = (options: OptionsType): void => {
if (fs.existsSync(options.buildDir)) {
printError(MESSAGES.buildDirExists);
throw new Error(MESSAGES.buildDirExists);
}
fs.mkdirSync(options.buildDir);
createDirectoryContents(options);
};