-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathfile-manager.js
More file actions
179 lines (155 loc) · 4.15 KB
/
file-manager.js
File metadata and controls
179 lines (155 loc) · 4.15 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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
import fs from "fs-extra";
import * as path from "path";
import { fileURLToPath } from "url";
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
/**
* checks if a directory is empty
*/
export function isDirectoryEmpty(dir) {
try {
return fs.readdirSync(dir).length === 0;
} catch (error) {
console.log(error);
return false;
}
}
/**
* empty a directory
*/
export function emptyDirectory(path, cb = null) {
// check if node module folder exist and remove firstly
if (fs.existsSync(`${path}/node_modules`)) {
fs.removeSync(`${path}/node_modules`);
}
fs.emptyDirSync(path);
cb && cb();
}
/**
* update file content using template string {{sample}}
* it replaces with data passed in the datas argument .
* this would delete any {{sample}} that is not sent in datas
*/
export function updateFileContent(filePath, newContent, datas) {
try {
// Replace placeholders like {{template_string}} with values from datas
const updatedContent = newContent.replace(/{{\s*[\w.]+\s*}}/g, (match) => {
const key = match.replace(/[{}]/g, "").trim();
if (datas[key] !== undefined) {
return datas[key];
}
return match; // If the dynamic data doesn't exist, keep the placeholder
});
// Write the updated content back to the file
fs.writeFileSync(filePath, updatedContent, "utf8");
} catch (err) {
console.error(`Error updating file '${filePath}': ${err}`);
}
}
/**
* removes a file
* @param {*} filePath
*/
export function removeFile(filePath) {
try {
fs.unlink(filePath);
} catch (err) {
console.error(`Error removing file '${filePath}': ${err}`);
}
}
/**
* removes a folder
* @param {*} folderPath
*/
export function removeFolder(folderPath) {
try {
fs.remove(folderPath);
} catch (err) {
console.error(`Error removing folder '${folderPath}': ${err}`);
}
}
/**
* add content to a file
* @param {*} filePath
* @param {*} content
*/
export function writeToFile(filePath, content) {
try {
fs.writeFileSync(filePath, content, "utf8");
} catch (err) {
console.log(`writeToFile error ${filePath}, error: ${err}`);
}
}
/**
* copy file content to another file
* @param {*} source
* @param {*} destination
*/
export function copyFile(source, destination) {
// destination = path.join(process.cwd(), destination);
fs.copySync(source, destination, {
filter: (src, dest) => {
const relativePath = path.relative(source, src);
if (relativePath.startsWith("node_modules")) {
return false;
}
return true;
},
});
}
/**
* create file if not exist and add content
* @param {*} path
* @param {*} content
*/
export function createAndUpdateFile(path, content) {
fs.outputFile(path, content, (err) => {
if (err) return console.log(err);
});
}
/**
* create folder
* @param {*} path
*/
export function createFolder(path) {
fs.ensureDir(path, (err) => {
if (err) {
console.log(`createFolder success`);
}
});
}
/**
* get template directory
* @param filePath
* @returns {string}
*/
export const getTemplateDir = (filePath) => {
return path.join(__dirname, "..", "../templates", filePath);
};
export const handleAppConfigs = async (path, projectName) => {
if (projectName.includes(" ")) {
projectName = projectName.split(" ").join("-");
}
//app.json
// const path = "./templates/app/react-native";
const pathToAppJson = path + "/app.json"
let appJson = await fs.readJson(pathToAppJson);
appJson.expo.name = projectName;
appJson.expo.slug = projectName;
//package.lock.json
const pathToPackageLock = path + "/package-lock.json"
let PackageLock = await fs.readJson(pathToPackageLock);
PackageLock.name = projectName;
PackageLock.packages[""].name = projectName;
//package.json
const pathToPackage = path + "/package.json"
let Package = await fs.readJson(pathToPackage);
Package.name = projectName;
//save the edited config
saveAppConfig(pathToAppJson, appJson);
saveAppConfig(pathToPackage, Package);
saveAppConfig(pathToPackageLock, PackageLock);
}
const saveAppConfig = (path, config) => {
fs.writeFile(path, JSON.stringify(config, null, 4));
}