-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuildStaging.ts
More file actions
112 lines (92 loc) · 3.29 KB
/
Copy pathbuildStaging.ts
File metadata and controls
112 lines (92 loc) · 3.29 KB
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import path from 'path';
import { fileURLToPath } from "url";
import fs from "fs";
import { rmStaging } from './rmStaging';
import config from "./config.json";
const __filename = fileURLToPath(import.meta.url);
// @ts-ignore
const __dirname = path.dirname(__filename);
/**
* copy files so that linux moment doesn't happen
*/
async function copyFileStream(src: string, dest: string) {
await new Promise((resolve, reject) => {
const readStream = fs.createReadStream(src);
const writeStream = fs.createWriteStream(dest);
readStream.on("error", reject);
writeStream.on("error", reject);
writeStream.on("close", resolve);
readStream.pipe(writeStream);
});
}
/**
* copy directory to new directory
*/
export async function copyDir(sourceDir: string, newDir: string):Promise<void> {
// Get dirs in the project folder.
var dirs = fs.readdirSync(sourceDir, { withFileTypes: true });
if (!fs.existsSync(newDir)) fs.mkdirSync(newDir);
// now we finna do something with all of these dirs
for (let entry of dirs) {
// Folders & files that we AIN'T ALLOWIN'!
if (
entry.name === '.git' ||
entry.name === '.github' ||
entry.name === 'psds' ||
entry.name === 'node_modules' ||
entry.name === 'dist' ||
entry.name === 'build.js' ||
entry.name === 'package-lock.json' ||
entry.name === 'package.json' ||
entry.name === '.ignore'
) continue;
// Paths
var sourcePath = path.join(sourceDir, entry.name);
var newPath = path.join(newDir, entry.name);
switch (entry.name) {
case "tobethereadme":
newPath = path.join(newDir, "README");
break;
}
// If the files passed the vibe check, we go.
if (entry.isDirectory()) {
await copyDir(sourcePath, newPath);
} else {
await copyFileStream(sourcePath, newPath);
}
}
}
export async function moveDirs(ogdir: string, dist: string) {
ogdir = `${config.stagingHtmlDir}/${ogdir}`;
dist = `${config.stagingHtmlDir}/${dist}`;
const ogExists = fs.existsSync(ogdir);
if (!ogExists) {
console.error(`${ogdir}`);
throw new Error('og dir doesn\'t exist');
}
const distExists = fs.existsSync(dist);
if (!distExists) fs.mkdirSync(dist);
await copyDir(ogdir, dist);
fs.rmSync(ogdir, {
recursive: true,
force: true,
maxRetries: 10,
retryDelay: 100
});
}
export async function buildStaging():Promise<void> {
return new Promise<void>((resolve) => {
// copy current html folder
if (fs.existsSync(config.stagingHtmlDir)) rmStaging();
console.log(`🔃 copying current "${config.baseHtmlDir}" folder to a "${config.stagingHtmlDir}" for Vite to compile...`);
copyDir(config.baseHtmlDir, config.stagingHtmlDir).then(async () => {
console.log('✅ copy complete!');
// move bfprof html
console.log('🔃 moving dirs that needs to be moved...');
await moveDirs('bfprofeditor/html', 'bfprofeditor');
console.log('✅ moving complete!');
// end
resolve();
});
});
}