-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
Copy pathbootstrapProject.ts
74 lines (61 loc) · 2.04 KB
/
bootstrapProject.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import { cpSync, existsSync, readdirSync, rmSync } from 'fs';
import { basename, join } from 'path';
import { rewriteTemplateFiles } from '../../src/lib/rewriteTemplateFiles';
export type ProjectPaths = {
// Project paths
projectRoot: string;
projectPackageJson: string;
// Template paths
templateName: string;
templateSource: string;
templateRoot: string;
};
/**
* Path and Directory utils
*/
const testDir = join(__dirname, '..');
const createFuelsDir = join(__dirname, '../..');
const sourceTemplatesDir = join(__dirname, '../../../../templates');
export const bootstrapProject = (testFilepath: string, template: string = 'vite'): ProjectPaths => {
// Unique name
const testFilename = basename(testFilepath.replace(/\./g, '-'));
// Project paths
const projectName = `__temp__project_${testFilename}_${new Date().getTime()}`;
const projectRoot = join(testDir, projectName);
const projectPackageJson = join(projectRoot, 'package.json');
// Template paths
const templateName = `__temp__template_${template}_${testFilename}_${new Date().getTime()}`;
const templateSource = join(sourceTemplatesDir, template);
const templateRoot = join(createFuelsDir, 'templates', templateName);
return {
// Project paths
projectRoot,
projectPackageJson,
// Template paths
templateName,
templateSource,
templateRoot,
};
};
export const copyTemplate = (srcDir: string, destDir: string, shouldRewrite: boolean = true) => {
if (!existsSync(destDir)) {
cpSync(srcDir, destDir, { recursive: true });
}
if (shouldRewrite) {
rewriteTemplateFiles(destDir);
}
};
export const resetFilesystem = (dirPath: string) => {
if (existsSync(dirPath)) {
rmSync(dirPath, { recursive: true });
}
};
export const cleanupFilesystem = (dirPaths: string[] = [testDir, createFuelsDir]) => {
dirPaths
.flatMap((dirPath) => {
const dirsInDir = readdirSync(dirPath);
return dirsInDir.map((dir) => join(dirPath, dir));
})
.filter((dir) => dir.startsWith('__temp__'))
.forEach(resetFilesystem);
};