-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcli.js
201 lines (167 loc) · 4.7 KB
/
cli.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
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
#!/usr/bin/env node
// Makes the script crash on unhandled rejections instead of silently
// ignoring them. In the future, promise rejections that are not handled will
// terminate the Node.js process with a non-zero exit code.
process.on("unhandledRejection", (err) => {
throw err;
});
const watch = require("node-watch");
const fs = require("fs");
const findFiles = require("file-regex");
const printMessage = require("print-message");
const replace = require("replace-in-file");
//config constants
const REQUIRED_FIELDS = [
"source_path",
"target_path",
"begin_target_mark_comment",
"end_target_mark_comment",
];
const OPTIONAL_FIELDS = ["file_filter", "depth", "quotes_style"];
const OPTIONAL_FIELDS_DEFAULTS = {
file_filter: "^init.ts",
depth: 30,
quotes_style: "double",
};
const config = getConfig();
const isConfigValid = validateConfig(config);
if (!isConfigValid) {
throw "Invalid config file";
}
(async function main() {
const foundFiles = await findFiles(
config.source_path,
new RegExp(config.file_filter || OPTIONAL_FIELDS_DEFAULTS.file_filter),
config.depth || OPTIONAL_FIELDS_DEFAULTS.depth
);
const sourcePart = config.source_path.replace("./", "");
const paths = foundFiles.map(
({ dir, file }) =>
`${dir.replace(`${process.env.PWD}/${sourcePart}`, "")}/${file}`
);
doReplaceImports(makeImportsString(paths));
startWatchChanges(paths);
})();
function startWatchChanges(paths) {
watch(
config.source_path,
{
recursive: true,
filter: (f) => {
const chunks = f.split("/");
const length = chunks.length;
const res = new RegExp(
config.file_filter || OPTIONAL_FIELDS_DEFAULTS.file_filter
).test(chunks[length - 1]);
return res;
},
},
function (evt, name) {
const sourcePart = config.source_path.replace("./", "");
const filePath = name.replace(`${sourcePart}`, "");
if (evt === "remove") {
paths = paths.filter((p) => p !== `${filePath}`);
}
if (evt === "update") {
const isPresent = paths.find((p) => p === `${filePath}`);
if (!isPresent) {
paths.push(`${filePath}`);
}
}
doReplaceImports(makeImportsString(paths));
}
);
}
function doReplaceImports(replacement) {
const results = replace.sync({
files: config.target_path,
from: new RegExp(
`(?<=${config.begin_target_mark_comment})(.*)(?=${config.end_target_mark_comment})`,
"gms"
),
to: `${replacement}\n`,
});
}
function makeImportsString(pathsArray) {
const quotesStyle =
config.quotes_style || OPTIONAL_FIELDS_DEFAULTS.quotes_style;
const quote = quotesStyle === "single" ? `'` : `"`;
const reducer = (acc, curVal) => `${acc}\nimport ${quote}.${curVal}${quote}`;
return pathsArray.reduce(reducer, "");
}
function getConfig() {
let config = "";
const fileConfigSources = [
"./effector-importer.config.json",
"./configs/effector-importer.config.json",
"./config/effector-importer.config.json",
];
let fileConfig = "";
for (let index = 0; index < fileConfigSources.length; index++) {
try {
const conf = fs.readFileSync(fileConfigSources[index], "utf8");
if (conf) {
fileConfig = conf;
}
break;
} catch (e) {
// continue
}
}
if (fileConfig) {
try {
config = JSON.parse(fileConfig);
} catch (e) {
printMessage([`Effector importer`, "", "Invalid config json"], {
borderColor: "red",
});
throw e;
}
} else {
try {
const pjConf = fs.readFileSync("./package.json", "utf8");
config = JSON.parse(pjConf)["effector-importer"];
} catch (e) {
printMessage([`Effector importer`, "", "Couldn't read package.json"], {
borderColor: "red",
});
throw e;
}
}
return config;
}
function validateConfig(config) {
let hasRequiredFieldErr = false;
REQUIRED_FIELDS.forEach((field) => {
const res = Object.prototype.hasOwnProperty.call(config, field);
if (!res) {
printMessage(
[
`Effector importer`,
"",
`Required field '${field}' is absent in present config`,
],
{
borderColor: "red",
}
);
hasRequiredFieldErr = true;
}
});
OPTIONAL_FIELDS.forEach((field) => {
const res = Object.prototype.hasOwnProperty.call(config, field);
if (!res) {
printMessage(
[
`Effector importer`,
"",
`Optional field '${field}' is absent in present config. It will be used default '${OPTIONAL_FIELDS_DEFAULTS[field]}' value.`,
],
{
borderColor: "yellow",
}
);
}
});
return !hasRequiredFieldErr;
}