-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharboCrafter.js
More file actions
143 lines (98 loc) · 3.98 KB
/
arboCrafter.js
File metadata and controls
143 lines (98 loc) · 3.98 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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
import fs from 'fs';
import path from 'path';
/**
* @description Build a filetree from a filetree structure definition
* @param {Object} fileTreeStructure Describes the filetree to build
* @param {String} outputPath The path to the output filetree
* @return {Void}
*/
export function buildFileTree(fileTreeStructure, outputPath) {
try {
// If the output folder does not exist, create it
if (!fs.existsSync(outputPath)) {
fs.mkdirSync(outputPath);
}
for (const [distFilename, distContent] of Object.entries(fileTreeStructure)) {
const distFilePath = path.join(outputPath, distFilename);
// If the content is a string : create a file
if (typeof distContent === 'string') {
// Get content from external source
if (distContent.startsWith("@")) {
const sanitizedContent = distContent.replaceAll("@", "").trim();
const srcFilePath = path.resolve(sanitizedContent);
if(fileExists(srcFilePath)){
// Check if it's a file or a folder
const stats = fs.statSync(srcFilePath)
// Copy whole folder
if (stats.isDirectory()) {
copyFolder(srcFilePath, distFilePath)
// Copy the file
} else {
copyFile(srcFilePath, distFilePath)
}
}else{throw new Error(`File '${srcFilePath}' not found`)}
} else {fs.writeFileSync(distFilePath, distContent, 'utf-8');} // Write the string as file content
}
// If the content is an object : create a folder
else if (typeof distContent === 'object') {
fs.mkdirSync(distFilePath, { recursive: true });
buildFileTree(distContent, distFilePath);
}
}
} catch (error) {throw new Error(`Error while building file tree`, { cause: error })}
}
/**
* @description Copy a file from a path to another
* @param {String} filePath
* @param {String} distFilePath
* @returns {Void}
*/
function copyFile(filePath, distFilePath) {
try {
fs.copyFileSync(filePath, distFilePath)
} catch (error) {throw new Error(`Error while copying file ${filePath} to ${distFilePath}`, { cause: error })}
}
/**
* @description Copy a folder from a path to another
* @param {String} folderPath
* @param {String} distFolderPath
* @returns {Void}
*/
function copyFolder(folderPath, distFolderPath,keepStructure = true) {
try {
// Check if folder exists
if (!fileExists(folderPath)) throw new Error(`Folder ${folderPath} not found`)
// Check if dist folder exists
if (!fileExists(distFolderPath)) {
fs.mkdirSync(distFolderPath, { recursive: true });
}
if(!keepStructure){
// List all file and subfolders files in folderPath
const files = fs.readdirSync(folderPath, { withFileTypes: true, recursive: true });
for (const file of files) {
// Copy file
if (file.isFile()) {
const srcPath = path.join(file.parentPath,file.name)
const distPath = path.join(distFolderPath, file.name)
copyFile(srcPath, distPath)
}
}
}else{
// Just copy the whole folder
fs.copyFileSync(folderPath, distFolderPath)
}
} catch (error) {throw new Error(`Error while copying folder ${folderPath} to ${distFolderPath}`, { cause: error })}
}
/**
* @description Check if a file exists
* @param {String} filePath
* @returns {Boolean}
*/
function fileExists(filePath) {
try {
fs.accessSync(filePath, fs.constants.F_OK);
return true;
} catch {
return false;
}
}