-
Notifications
You must be signed in to change notification settings - Fork 0
/
replaceSingleQuotesForRequire.js
109 lines (70 loc) · 1.66 KB
/
replaceSingleQuotesForRequire.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
103
104
105
106
107
108
109
const fs = require("fs");
const nodePath = require("path");
if(
!fs.existsSync(
nodePath.join( process.cwd(), "./package.json" )
)
||
require(
nodePath.join( process.cwd(), "./package.json" )
).name != "node-rules-system"
) {
throw new Error("This script should be run in the main project directory!");
}
const backupDirName = "backup";
const projectBDir = nodePath.join( process.cwd(), "./" + backupDirName + "/" );
function start(dir, nestedPaths) {
nestedPaths = nestedPaths || "";
const ls = fs.readdirSync(dir);
for (let i = 0; i < ls.length; ++i) {
const obj = ls[i];
const path = nodePath.join(dir, obj);
if( fs.lstatSync(path).isDirectory() ) {
if(
obj == "node_modules"
||
obj == ".git"
||
obj == ".idea"
||
obj == "dirty"
||
obj == "lib"
||
obj == "@types"
||
obj == backupDirName
) {
continue;
}
let bDir = nestedPaths;
if(nestedPaths) {
nestedPaths = nodePath.join( nestedPaths, "./" + obj );
} else {
nestedPaths = obj;
}
start(path, nestedPaths);
nestedPaths = bDir;
} else {
if( obj.endsWith(".js") ) {
const content = fs.readFileSync(path).toString();
if( content.match ( /require\('.+?'/ ) ) {
const newContent = content
.replace( /require\('(.+?)'/g , 'require("$1"' );
if(projectBDir) {
fs.mkdirSync(
nodePath.join( projectBDir, nestedPaths ),
{ recursive: true }
);
}
fs.writeFileSync(
nodePath.join( projectBDir, nodePath.join( nestedPaths, "./" + obj ) ),
content
);
fs.writeFileSync(path, newContent);
}
}
}
}
}
start(__dirname);