forked from obytes/react-native-template-obytes
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathproject-files-manager.js
102 lines (88 loc) · 2.21 KB
/
project-files-manager.js
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
const fs = require('fs-extra');
const path = require('path');
class ProjectFilesManager {
#projectName;
constructor(
/**
* @type {string}
*/
projectName
) {
this.#projectName = projectName;
}
static withProjectName(
/**
* @type {string}
*/
projectName
) {
return new this(projectName);
}
getAbsoluteFilePath(
/**
* A file path relative to project's root path
* @type {string}
*/
relativeFilePath
) {
return path.join(process.cwd(), `${this.#projectName}/${relativeFilePath}`);
}
removeFiles(
/**
* An array of file paths relative to project's root path
* @type {Array<string>}
*/
files
) {
files.forEach((fileName) => {
const absoluteFilePath = this.getAbsoluteFilePath(fileName);
fs.removeSync(absoluteFilePath);
});
}
renameFiles(
/**
* An array of objects containing the old and the new file names
* relative to project's root path
*
* @type {Array<{
* oldFileName: string;
* newFileName: string;
* }>}
*/
files
) {
files.forEach(({ oldFileName, newFileName }) => {
const oldAbsoluteFilePath = this.getAbsoluteFilePath(oldFileName);
const newAbsoluteFilePath = this.getAbsoluteFilePath(newFileName);
fs.renameSync(oldAbsoluteFilePath, newAbsoluteFilePath);
});
}
replaceFilesContent(
/**
* An array of objects containing the file name relative to project's
* root path and the replacement patterns to be applied
*
* @type {Array<{
* fileName: string;
* replacements: Array<{
* searchValue: string;
* replaceValue: string;
* }>
* }>}
*/
files
) {
files.forEach(({ fileName, replacements }) => {
const absoluteFilePath = this.getAbsoluteFilePath(fileName);
const contents = fs.readFileSync(absoluteFilePath, {
encoding: 'utf-8',
});
let replaced = contents;
replacements.forEach(({ searchValue, replaceValue }) => {
replaced = replaced.replace(searchValue, replaceValue);
});
fs.writeFileSync(absoluteFilePath, replaced, { spaces: 2 });
});
}
}
module.exports = ProjectFilesManager;