pug-site-core 1.0.1 → 1.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.js +15 -2
- package/lib/devServer.js +8 -2
- package/lib/generate.js +236 -218
- package/lib/translate.js +13 -3
- package/lib/watchFile.js +5 -4
- package/package.json +9 -2
package/index.js
CHANGED
|
@@ -1,4 +1,17 @@
|
|
|
1
1
|
import { startDevServer } from "./lib/devServer.js";
|
|
2
|
-
|
|
3
|
-
|
|
2
|
+
import {
|
|
3
|
+
generateGetDataFn,
|
|
4
|
+
compilePagesPugToFn,
|
|
5
|
+
fetchDataToJsonFile,
|
|
6
|
+
buildFn,
|
|
7
|
+
buildStatic
|
|
8
|
+
} from "./lib/generate.js";
|
|
9
|
+
|
|
10
|
+
export const pugSiteCore = {
|
|
11
|
+
startDevServer,
|
|
12
|
+
generateGetDataFn,
|
|
13
|
+
compilePagesPugToFn,
|
|
14
|
+
fetchDataToJsonFile,
|
|
15
|
+
buildFn,
|
|
16
|
+
buildStatic
|
|
4
17
|
};
|
package/lib/devServer.js
CHANGED
|
@@ -13,7 +13,8 @@ import {
|
|
|
13
13
|
} from "./utils.js";
|
|
14
14
|
import http from "http";
|
|
15
15
|
import WebSocket, { WebSocketServer } from "ws";
|
|
16
|
-
import { pathToFileURL } from "url";
|
|
16
|
+
import { pathToFileURL, fileURLToPath } from "url";
|
|
17
|
+
import { Worker } from "worker_threads";
|
|
17
18
|
|
|
18
19
|
const projectRoot = process.cwd();
|
|
19
20
|
const configPath = pathToFileURL(path.resolve(projectRoot, "config.js")).href;
|
|
@@ -228,5 +229,10 @@ export async function startDevServer() {
|
|
|
228
229
|
);
|
|
229
230
|
|
|
230
231
|
server.listen(port);
|
|
231
|
-
|
|
232
|
+
|
|
233
|
+
// 获取当前文件的目录路径
|
|
234
|
+
const currentDir = path.dirname(fileURLToPath(import.meta.url));
|
|
235
|
+
// 构建 watchFile.js 的完整路径
|
|
236
|
+
const watchFilePath = path.join(currentDir, "watchFile.js");
|
|
237
|
+
new Worker(watchFilePath);
|
|
232
238
|
}
|
package/lib/generate.js
CHANGED
|
@@ -11,12 +11,15 @@ import {
|
|
|
11
11
|
obfuscateJavaScript
|
|
12
12
|
} from "./utils.js";
|
|
13
13
|
import _ from "lodash";
|
|
14
|
-
import { config } from "./config.js";
|
|
15
14
|
import async from "async";
|
|
16
15
|
import UglifyJS from "uglify-js";
|
|
16
|
+
import { pathToFileURL } from "url";
|
|
17
17
|
|
|
18
|
-
const
|
|
19
|
-
const
|
|
18
|
+
const projectRoot = process.cwd();
|
|
19
|
+
const configPath = pathToFileURL(path.resolve(projectRoot, "config.js")).href;
|
|
20
|
+
const { config } = await import(configPath);
|
|
21
|
+
|
|
22
|
+
const pugRootPath = path.join(projectRoot, "/template/pages");
|
|
20
23
|
|
|
21
24
|
/**
|
|
22
25
|
* 将pages目录下的pug模板编译为JS函数
|
|
@@ -28,10 +31,11 @@ export async function compilePagesPugToFn(pugPath) {
|
|
|
28
31
|
try {
|
|
29
32
|
// 获取所有需要编译的pug文件路径
|
|
30
33
|
const pagesPugFilePathArr = await getPagesPugFilePathArr();
|
|
31
|
-
const fnRootPath = path.join(
|
|
34
|
+
const fnRootPath = path.join(projectRoot, "/pagesPugFn");
|
|
32
35
|
|
|
33
36
|
// 读取pug运行时代码作为基础代码
|
|
34
|
-
const
|
|
37
|
+
const pugRuntimePath = path.join(projectRoot, "pugRuntime.js");
|
|
38
|
+
const lastPugFnStr = await fse.readFile(pugRuntimePath, "utf8");
|
|
35
39
|
|
|
36
40
|
// 验证指定路径是否存在
|
|
37
41
|
if (pugPath && !fse.pathExistsSync(path.join(pugRootPath, pugPath))) {
|
|
@@ -55,7 +59,7 @@ export async function compilePagesPugToFn(pugPath) {
|
|
|
55
59
|
const pugValue = await fse.readFile(filePath, "utf8");
|
|
56
60
|
const fnStr = pug.compileClient(pugValue, {
|
|
57
61
|
filename: filePath,
|
|
58
|
-
basedir: path.join(
|
|
62
|
+
basedir: path.join(projectRoot, "/template"),
|
|
59
63
|
compileDebug: true,
|
|
60
64
|
name: funName,
|
|
61
65
|
filters: getCompilePugFilter()
|
|
@@ -103,7 +107,8 @@ export async function compilePagesPugToFn(pugPath) {
|
|
|
103
107
|
*/
|
|
104
108
|
export async function generateGetDataFn() {
|
|
105
109
|
try {
|
|
106
|
-
const
|
|
110
|
+
const getDataPath = path.join(projectRoot, "getData.js");
|
|
111
|
+
const getDataFile = await fse.readFile(getDataPath, "utf8");
|
|
107
112
|
const pagesPugFilePathArr = await getPagesPugFilePathArr(true);
|
|
108
113
|
|
|
109
114
|
// 注入公共数据获取函数
|
|
@@ -111,7 +116,7 @@ export async function generateGetDataFn() {
|
|
|
111
116
|
const commonDataFn = `export async function get_common_data(language) {
|
|
112
117
|
return {}
|
|
113
118
|
}\n`;
|
|
114
|
-
await fse.appendFile(
|
|
119
|
+
await fse.appendFile(getDataPath, commonDataFn);
|
|
115
120
|
}
|
|
116
121
|
|
|
117
122
|
// 为每个页面注入数据获取函数
|
|
@@ -124,7 +129,7 @@ export async function generateGetDataFn() {
|
|
|
124
129
|
.toString()
|
|
125
130
|
.replace("template", funName);
|
|
126
131
|
const dataFn = `\nexport async ${template}`;
|
|
127
|
-
await fse.appendFile(
|
|
132
|
+
await fse.appendFile(getDataPath, dataFn);
|
|
128
133
|
}
|
|
129
134
|
});
|
|
130
135
|
} catch (error) {
|
|
@@ -138,261 +143,274 @@ export async function generateGetDataFn() {
|
|
|
138
143
|
* @param {string[]} args - 命令行参数,用于过滤模板和语言
|
|
139
144
|
*/
|
|
140
145
|
export async function fetchDataToJsonFile(args) {
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
if (
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
146
|
+
try {
|
|
147
|
+
const JsonRootPath = path.join(projectRoot, "/jsonData");
|
|
148
|
+
|
|
149
|
+
// 解析过滤参数
|
|
150
|
+
let filterFun = [];
|
|
151
|
+
let filterLang = [];
|
|
152
|
+
args.forEach((item) => {
|
|
153
|
+
const [key, value] = item.split("=");
|
|
154
|
+
if (value) {
|
|
155
|
+
if (key === "f") {
|
|
156
|
+
filterFun = value.split(",");
|
|
157
|
+
}
|
|
158
|
+
if (key === "c") {
|
|
159
|
+
filterLang = value.split(",");
|
|
160
|
+
}
|
|
154
161
|
}
|
|
162
|
+
});
|
|
163
|
+
|
|
164
|
+
// 如果没有过滤条件,清空输出目录
|
|
165
|
+
if (!filterFun.length && !filterLang.length) {
|
|
166
|
+
await fse.remove(JsonRootPath);
|
|
155
167
|
}
|
|
156
|
-
});
|
|
157
168
|
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
169
|
+
const getDataPath = pathToFileURL(
|
|
170
|
+
path.resolve(projectRoot, "getData.js")
|
|
171
|
+
).href;
|
|
172
|
+
const getData = await import(getDataPath);
|
|
173
|
+
let arrPagesPugFilePathArr = await getPagesPugFilePathArr();
|
|
174
|
+
let pagesPugFilePathArr = await getPagesPugFilePathArr(true);
|
|
175
|
+
let filterFinishArr = arrPagesPugFilePathArr.filter(
|
|
176
|
+
(item) => !pagesPugFilePathArr.includes(item)
|
|
177
|
+
);
|
|
178
|
+
const { languageList, fileMapTable, fetchDataLangLimit } = config;
|
|
179
|
+
let starTime = Date.now();
|
|
162
180
|
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
181
|
+
await async.eachLimit(
|
|
182
|
+
languageList,
|
|
183
|
+
fetchDataLangLimit,
|
|
184
|
+
async (language) => {
|
|
185
|
+
// 语言过滤
|
|
186
|
+
if (filterLang.length && !filterLang.includes(language)) {
|
|
187
|
+
return;
|
|
188
|
+
}
|
|
171
189
|
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
}
|
|
190
|
+
// 清空指定语言的数据目录
|
|
191
|
+
if (filterLang.includes(language) && !filterFun.length) {
|
|
192
|
+
await fse.remove(path.join(JsonRootPath, language));
|
|
193
|
+
}
|
|
177
194
|
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
195
|
+
// 处理公共数据
|
|
196
|
+
const commonFuncName = "get_common_data";
|
|
197
|
+
if (!filterFun.length || filterFun.includes(commonFuncName)) {
|
|
198
|
+
const commonData = await getData[commonFuncName](language);
|
|
199
|
+
console.log(language, commonFuncName, "开始写入json文件");
|
|
200
|
+
await fse.outputJSON(
|
|
201
|
+
path.join(JsonRootPath, language, "_common.json"),
|
|
202
|
+
commonData
|
|
203
|
+
);
|
|
204
|
+
}
|
|
182
205
|
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
206
|
+
// 处理文件映射表数据
|
|
207
|
+
if (fileMapTable?.length) {
|
|
208
|
+
await async.each(fileMapTable, async (obj) => {
|
|
209
|
+
if (
|
|
210
|
+
obj.getDataFn &&
|
|
211
|
+
!obj.pugPath &&
|
|
212
|
+
obj.outPutPath &&
|
|
213
|
+
obj.outPutPath.endsWith(".json")
|
|
214
|
+
) {
|
|
215
|
+
if (
|
|
216
|
+
obj.languageList &&
|
|
217
|
+
obj.languageList.length > 0 &&
|
|
218
|
+
!obj.languageList.includes(language)
|
|
219
|
+
) {
|
|
220
|
+
return Promise.resolve();
|
|
221
|
+
}
|
|
222
|
+
let dataFn = getData[obj.getDataFn];
|
|
223
|
+
if (!dataFn || typeof dataFn !== "function") {
|
|
224
|
+
return Promise.reject(dataFn + "获取数据函数不存在!");
|
|
225
|
+
}
|
|
193
226
|
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
227
|
+
if (filterFun.length && !filterFun.includes(funName)) {
|
|
228
|
+
return Promise.resolve();
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
let data = await dataFn(language);
|
|
232
|
+
if (!data) {
|
|
233
|
+
return Promise.reject(dataFn + "获取的数据为null!");
|
|
234
|
+
}
|
|
235
|
+
console.log(language, obj.getDataFn, "开始写入json文件");
|
|
236
|
+
let outPutPath = obj.outPutPath.split("/").join(pathSymbol);
|
|
237
|
+
let jsonFilePath;
|
|
238
|
+
if (Array.isArray(data)) {
|
|
239
|
+
let name = outPutPath
|
|
240
|
+
.split(pathSymbol)
|
|
241
|
+
.pop()
|
|
242
|
+
.replace(/\..*$/, "");
|
|
243
|
+
const regex = /^\[.+\]$/;
|
|
244
|
+
if (regex.test(name)) {
|
|
245
|
+
let property = name.slice(1, -1);
|
|
246
|
+
for (let index = 0; index < data.length; index++) {
|
|
247
|
+
const dataItem = data[index];
|
|
248
|
+
let fileName = dataItem[property];
|
|
249
|
+
if (
|
|
250
|
+
fileName === null ||
|
|
251
|
+
fileName === undefined ||
|
|
252
|
+
fileName === ""
|
|
253
|
+
) {
|
|
254
|
+
return Promise.reject(
|
|
255
|
+
dataFn +
|
|
256
|
+
"获取的数据中期望以" +
|
|
257
|
+
property +
|
|
258
|
+
`命名但是${index}下标中对象属性不存在`
|
|
259
|
+
);
|
|
260
|
+
}
|
|
261
|
+
jsonFilePath = path.join(
|
|
262
|
+
JsonRootPath,
|
|
263
|
+
language,
|
|
264
|
+
outPutPath.replace(name, fileName)
|
|
265
|
+
);
|
|
266
|
+
await fse.remove(jsonFilePath);
|
|
267
|
+
await fse.outputJson(jsonFilePath, dataItem);
|
|
268
|
+
}
|
|
269
|
+
} else {
|
|
270
|
+
jsonFilePath = path.join(JsonRootPath, language, outPutPath);
|
|
271
|
+
await fse.remove(jsonFilePath);
|
|
272
|
+
await fse.outputJson(jsonFilePath, data);
|
|
273
|
+
}
|
|
274
|
+
} else if (typeof data === "object") {
|
|
275
|
+
jsonFilePath = path.join(JsonRootPath, language, outPutPath);
|
|
276
|
+
await fse.remove(jsonFilePath);
|
|
277
|
+
await fse.outputJson(jsonFilePath, data);
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
});
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
await async.each(pagesPugFilePathArr, async (fileName) => {
|
|
284
|
+
let funName =
|
|
285
|
+
"get_" +
|
|
286
|
+
fileName.split(pathSymbol).join("_").slice(0, -4) +
|
|
287
|
+
"_data";
|
|
214
288
|
|
|
289
|
+
let jsonFilePath = fileName.slice(0, -4).split(pathSymbol);
|
|
290
|
+
if (!getData[funName] || typeof getData[funName] !== "function") {
|
|
291
|
+
console.log(funName, "获取数据函数不存在!");
|
|
292
|
+
return Promise.reject(funName + "获取数据函数不存在!");
|
|
293
|
+
}
|
|
215
294
|
if (filterFun.length && !filterFun.includes(funName)) {
|
|
216
295
|
return Promise.resolve();
|
|
217
296
|
}
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
for (let index = 0; index < data.length; index++) {
|
|
232
|
-
const dataItem = data[index];
|
|
233
|
-
let fileName = dataItem[property];
|
|
234
|
-
if (
|
|
235
|
-
fileName === null ||
|
|
236
|
-
fileName === undefined ||
|
|
237
|
-
fileName === ""
|
|
238
|
-
) {
|
|
239
|
-
return Promise.reject(
|
|
240
|
-
dataFn +
|
|
241
|
-
"获取的数据中期望以" +
|
|
242
|
-
property +
|
|
243
|
-
`命名但是${index}下标中对象属性不存在`
|
|
244
|
-
);
|
|
245
|
-
}
|
|
246
|
-
jsonFilePath = path.join(
|
|
297
|
+
let data = await getData[funName](language);
|
|
298
|
+
if (Array.isArray(data) && data.length > 0) {
|
|
299
|
+
console.log(language, funName, "开始写入json文件");
|
|
300
|
+
await async.eachOfLimit(data, 1000, async (item, index) => {
|
|
301
|
+
if (typeof item !== "object" || Array.isArray(item)) {
|
|
302
|
+
let type = Array.isArray(item) ? "array" : typeof item;
|
|
303
|
+
return Promise.reject(
|
|
304
|
+
funName + "返回的数据不为对象数组得到类型" + type + "[]"
|
|
305
|
+
);
|
|
306
|
+
}
|
|
307
|
+
let lastJsonFilePath;
|
|
308
|
+
if (item.page_name && item.page_name.length > 0) {
|
|
309
|
+
lastJsonFilePath = path.join(
|
|
247
310
|
JsonRootPath,
|
|
248
311
|
language,
|
|
249
|
-
|
|
312
|
+
...jsonFilePath.slice(0, -1),
|
|
313
|
+
item.page_name
|
|
250
314
|
);
|
|
251
|
-
|
|
252
|
-
|
|
315
|
+
} else {
|
|
316
|
+
lastJsonFilePath =
|
|
317
|
+
path.join(JsonRootPath, language, ...jsonFilePath) +
|
|
318
|
+
"_" +
|
|
319
|
+
++index +
|
|
320
|
+
".json";
|
|
253
321
|
}
|
|
322
|
+
let templateArr = filterFinishArr.filter(
|
|
323
|
+
(item) => pagesPathFilter(item) === fileName
|
|
324
|
+
);
|
|
325
|
+
if (fse.pathExistsSync(path.join(pugRootPath, fileName))) {
|
|
326
|
+
templateArr.unshift(fileName);
|
|
327
|
+
}
|
|
328
|
+
item._template = templateArr;
|
|
329
|
+
await fse.remove(lastJsonFilePath);
|
|
330
|
+
await fse.outputJson(lastJsonFilePath, item);
|
|
331
|
+
});
|
|
332
|
+
} else if (data && typeof data === "object" && !Array.isArray(data)) {
|
|
333
|
+
console.log(language, funName, "开始写入json文件");
|
|
334
|
+
if (data.page_name && data.page_name.length > 0) {
|
|
335
|
+
jsonFilePath = path.join(
|
|
336
|
+
JsonRootPath,
|
|
337
|
+
language,
|
|
338
|
+
...jsonFilePath.slice(0, -1),
|
|
339
|
+
data.page_name
|
|
340
|
+
);
|
|
254
341
|
} else {
|
|
255
|
-
jsonFilePath =
|
|
256
|
-
|
|
257
|
-
await fse.outputJson(jsonFilePath, data);
|
|
342
|
+
jsonFilePath =
|
|
343
|
+
path.join(JsonRootPath, language, ...jsonFilePath) + ".json";
|
|
258
344
|
}
|
|
259
|
-
|
|
260
|
-
|
|
345
|
+
let templateArr = filterFinishArr.filter(
|
|
346
|
+
(item) => pagesPathFilter(item) === fileName
|
|
347
|
+
);
|
|
348
|
+
if (fse.pathExistsSync(path.join(pugRootPath, fileName))) {
|
|
349
|
+
templateArr.unshift(fileName);
|
|
350
|
+
}
|
|
351
|
+
data._template = templateArr;
|
|
261
352
|
await fse.remove(jsonFilePath);
|
|
262
353
|
await fse.outputJson(jsonFilePath, data);
|
|
263
|
-
}
|
|
264
|
-
}
|
|
265
|
-
});
|
|
266
|
-
}
|
|
267
|
-
|
|
268
|
-
await async.each(pagesPugFilePathArr, async (fileName) => {
|
|
269
|
-
let funName =
|
|
270
|
-
"get_" + fileName.split(pathSymbol).join("_").slice(0, -4) + "_data";
|
|
271
|
-
|
|
272
|
-
let jsonFilePath = fileName.slice(0, -4).split(pathSymbol);
|
|
273
|
-
if (!getData[funName] || typeof getData[funName] !== "function") {
|
|
274
|
-
console.log(funName, "获取数据函数不存在!");
|
|
275
|
-
return Promise.reject(funName + "获取数据函数不存在!");
|
|
276
|
-
}
|
|
277
|
-
if (filterFun.length && !filterFun.includes(funName)) {
|
|
278
|
-
return Promise.resolve();
|
|
279
|
-
}
|
|
280
|
-
let data = await getData[funName](language);
|
|
281
|
-
if (Array.isArray(data) && data.length > 0) {
|
|
282
|
-
console.log(language, funName, "开始写入json文件");
|
|
283
|
-
await async.eachOfLimit(data, 1000, async (item, index) => {
|
|
284
|
-
if (typeof item !== "object" || Array.isArray(item)) {
|
|
285
|
-
let type = Array.isArray(item) ? "array" : typeof item;
|
|
286
|
-
return Promise.reject(
|
|
287
|
-
funName + "返回的数据不为对象数组得到类型" + type + "[]"
|
|
288
|
-
);
|
|
289
|
-
}
|
|
290
|
-
let lastJsonFilePath;
|
|
291
|
-
if (item.page_name && item.page_name.length > 0) {
|
|
292
|
-
lastJsonFilePath = path.join(
|
|
293
|
-
JsonRootPath,
|
|
294
|
-
language,
|
|
295
|
-
...jsonFilePath.slice(0, -1),
|
|
296
|
-
item.page_name
|
|
297
|
-
);
|
|
298
354
|
} else {
|
|
299
|
-
|
|
300
|
-
path.join(JsonRootPath, language, ...jsonFilePath) +
|
|
301
|
-
"_" +
|
|
302
|
-
++index +
|
|
303
|
-
".json";
|
|
355
|
+
console.log(language, funName, "期望返回数组、对象类型返回:", data);
|
|
304
356
|
}
|
|
305
|
-
let templateArr = filterFinishArr.filter(
|
|
306
|
-
(item) => pagesPathFilter(item) === fileName
|
|
307
|
-
);
|
|
308
|
-
if (fse.pathExistsSync(path.join(pugRootPath, fileName))) {
|
|
309
|
-
templateArr.unshift(fileName);
|
|
310
|
-
}
|
|
311
|
-
item._template = templateArr;
|
|
312
|
-
await fse.remove(lastJsonFilePath);
|
|
313
|
-
await fse.outputJson(lastJsonFilePath, item);
|
|
314
357
|
});
|
|
315
|
-
} else if (data && typeof data === "object" && !Array.isArray(data)) {
|
|
316
|
-
console.log(language, funName, "开始写入json文件");
|
|
317
|
-
if (data.page_name && data.page_name.length > 0) {
|
|
318
|
-
jsonFilePath = path.join(
|
|
319
|
-
JsonRootPath,
|
|
320
|
-
language,
|
|
321
|
-
...jsonFilePath.slice(0, -1),
|
|
322
|
-
data.page_name
|
|
323
|
-
);
|
|
324
|
-
} else {
|
|
325
|
-
jsonFilePath =
|
|
326
|
-
path.join(JsonRootPath, language, ...jsonFilePath) + ".json";
|
|
327
|
-
}
|
|
328
|
-
let templateArr = filterFinishArr.filter(
|
|
329
|
-
(item) => pagesPathFilter(item) === fileName
|
|
330
|
-
);
|
|
331
|
-
if (fse.pathExistsSync(path.join(pugRootPath, fileName))) {
|
|
332
|
-
templateArr.unshift(fileName);
|
|
333
|
-
}
|
|
334
|
-
data._template = templateArr;
|
|
335
|
-
await fse.remove(jsonFilePath);
|
|
336
|
-
await fse.outputJson(jsonFilePath, data);
|
|
337
|
-
} else {
|
|
338
|
-
console.log(language, funName, "期望返回数组、对象类型返回:", data);
|
|
339
358
|
}
|
|
340
|
-
|
|
341
|
-
})
|
|
342
|
-
|
|
359
|
+
);
|
|
360
|
+
} catch (error) {
|
|
361
|
+
console.error("获取数据失败:", error);
|
|
362
|
+
throw error;
|
|
363
|
+
}
|
|
343
364
|
}
|
|
344
365
|
|
|
345
366
|
export async function buildFn() {
|
|
346
|
-
|
|
367
|
+
const jsonDataPath = path.join(projectRoot, "jsonData");
|
|
368
|
+
if (!fse.pathExistsSync(jsonDataPath)) {
|
|
347
369
|
return Promise.reject(
|
|
348
|
-
|
|
370
|
+
jsonDataPath,
|
|
349
371
|
"目录不存在请先执行npm run getData生成数据!"
|
|
350
372
|
);
|
|
351
373
|
}
|
|
352
374
|
console.log("开始打包...");
|
|
353
375
|
let starTime = Date.now();
|
|
354
|
-
let outputPath = path.join(
|
|
376
|
+
let outputPath = path.join(projectRoot, config.fnOutput);
|
|
355
377
|
await fse.remove(outputPath);
|
|
356
378
|
await sleep(0);
|
|
357
379
|
await compilePagesPugToFn();
|
|
358
380
|
await fse.copy(
|
|
359
|
-
path.join(
|
|
381
|
+
path.join(projectRoot, "pagesPugFn/index.js"),
|
|
360
382
|
path.join(outputPath, "page/pages.js")
|
|
361
383
|
);
|
|
362
384
|
|
|
363
|
-
|
|
385
|
+
const routerPath = path.join(projectRoot, "router.js");
|
|
386
|
+
if (!fse.pathExistsSync(routerPath)) {
|
|
364
387
|
return Promise.reject("router.js文件不存在!");
|
|
365
388
|
}
|
|
366
389
|
|
|
390
|
+
await fse.copy(routerPath, path.join(outputPath, "page/router.js"));
|
|
367
391
|
await fse.copy(
|
|
368
|
-
path.join(
|
|
369
|
-
path.join(outputPath, "page
|
|
392
|
+
path.join(projectRoot, "public"),
|
|
393
|
+
path.join(outputPath, "page")
|
|
370
394
|
);
|
|
371
395
|
|
|
372
|
-
await fse.copy(path.join(__dirname, "public"), path.join(outputPath, "page"));
|
|
373
|
-
|
|
374
396
|
let totalCommonData = {};
|
|
375
397
|
totalCommonData.langCommon = config.commonData;
|
|
376
398
|
await async.each(config.languageList, async (lang) => {
|
|
377
399
|
let commonData = await fse.readJSON(
|
|
378
|
-
path.join(
|
|
400
|
+
path.join(jsonDataPath, lang, "_common.json")
|
|
379
401
|
);
|
|
380
402
|
totalCommonData[lang] = commonData;
|
|
381
403
|
});
|
|
382
404
|
|
|
383
|
-
await fse.copy(
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
filter: (src, dest) => {
|
|
388
|
-
// 排除_common.json 文件
|
|
389
|
-
return !src.endsWith("_common.json");
|
|
390
|
-
}
|
|
405
|
+
await fse.copy(path.join(jsonDataPath), path.join(outputPath, "data"), {
|
|
406
|
+
filter: (src, dest) => {
|
|
407
|
+
// 排除_common.json 文件
|
|
408
|
+
return !src.endsWith("_common.json");
|
|
391
409
|
}
|
|
392
|
-
);
|
|
410
|
+
});
|
|
393
411
|
|
|
394
412
|
await fse.copy(
|
|
395
|
-
path.join(
|
|
413
|
+
path.join(projectRoot, "/template/static"),
|
|
396
414
|
path.join(outputPath, "page/static"),
|
|
397
415
|
{
|
|
398
416
|
filter: (src, dest) => {
|
|
@@ -403,7 +421,7 @@ export async function buildFn() {
|
|
|
403
421
|
if (config.buildStaticDirArr && config.buildStaticDirArr.length > 0) {
|
|
404
422
|
return !!config.buildStaticDirArr.find((item) => {
|
|
405
423
|
return src.startsWith(
|
|
406
|
-
path.join(
|
|
424
|
+
path.join(projectRoot, "/template/static", item)
|
|
407
425
|
);
|
|
408
426
|
});
|
|
409
427
|
}
|
|
@@ -430,7 +448,7 @@ export async function buildFn() {
|
|
|
430
448
|
|
|
431
449
|
//html文件打包 不维护了
|
|
432
450
|
export async function buildStatic() {
|
|
433
|
-
let jsonDataPath = path.join(
|
|
451
|
+
let jsonDataPath = path.join(projectRoot, "jsonData");
|
|
434
452
|
|
|
435
453
|
if (!fse.pathExistsSync(jsonDataPath)) {
|
|
436
454
|
return Promise.reject(
|
|
@@ -439,13 +457,13 @@ export async function buildStatic() {
|
|
|
439
457
|
}
|
|
440
458
|
console.log("开始打包...");
|
|
441
459
|
let starTime = Date.now();
|
|
442
|
-
let distOutputPath = path.join(
|
|
460
|
+
let distOutputPath = path.join(projectRoot, config.staticOutput);
|
|
443
461
|
await fse.remove(distOutputPath);
|
|
444
462
|
await sleep(0);
|
|
445
|
-
await fse.copy(path.join(
|
|
463
|
+
await fse.copy(path.join(projectRoot, "public"), distOutputPath);
|
|
446
464
|
|
|
447
465
|
await fse.copy(
|
|
448
|
-
path.join(
|
|
466
|
+
path.join(projectRoot, "/template/static"),
|
|
449
467
|
path.join(distOutputPath, "static"),
|
|
450
468
|
{
|
|
451
469
|
filter: (src, dest) => {
|
|
@@ -456,7 +474,7 @@ export async function buildStatic() {
|
|
|
456
474
|
if (config.buildStaticDirArr && config.buildStaticDirArr.length > 0) {
|
|
457
475
|
return !!config.buildStaticDirArr.find((item) => {
|
|
458
476
|
return src.startsWith(
|
|
459
|
-
path.join(
|
|
477
|
+
path.join(projectRoot, "/template/static", item)
|
|
460
478
|
);
|
|
461
479
|
});
|
|
462
480
|
}
|
|
@@ -478,7 +496,7 @@ export async function buildStatic() {
|
|
|
478
496
|
return;
|
|
479
497
|
}
|
|
480
498
|
let commonData = await fse.readJSON(
|
|
481
|
-
path.join(
|
|
499
|
+
path.join(projectRoot, "jsonData", lang, "_common.json")
|
|
482
500
|
);
|
|
483
501
|
commonData = _.merge(commonData, config.commonData);
|
|
484
502
|
|
|
@@ -554,7 +572,7 @@ export async function buildStatic() {
|
|
|
554
572
|
outPutPath.replace(name, fileName)
|
|
555
573
|
);
|
|
556
574
|
html = pug.compileFile(pugPath, {
|
|
557
|
-
basedir: path.join(
|
|
575
|
+
basedir: path.join(projectRoot, "/template"),
|
|
558
576
|
compileDebug: true,
|
|
559
577
|
filters: getCompilePugFilter()
|
|
560
578
|
})({
|
|
@@ -567,14 +585,14 @@ export async function buildStatic() {
|
|
|
567
585
|
}
|
|
568
586
|
} else {
|
|
569
587
|
htmlPath = path.join(
|
|
570
|
-
|
|
588
|
+
projectRoot,
|
|
571
589
|
"template",
|
|
572
590
|
lang,
|
|
573
591
|
devicePrefix,
|
|
574
592
|
outPutPath
|
|
575
593
|
);
|
|
576
594
|
html = pug.compileFile(pugPath, {
|
|
577
|
-
basedir: path.join(
|
|
595
|
+
basedir: path.join(projectRoot, "/template"),
|
|
578
596
|
compileDebug: true,
|
|
579
597
|
filters: getCompilePugFilter()
|
|
580
598
|
})({
|
|
@@ -593,7 +611,7 @@ export async function buildStatic() {
|
|
|
593
611
|
outPutPath
|
|
594
612
|
);
|
|
595
613
|
html = pug.compileFile(pugPath, {
|
|
596
|
-
basedir: path.join(
|
|
614
|
+
basedir: path.join(projectRoot, "/template"),
|
|
597
615
|
compileDebug: true,
|
|
598
616
|
filters: getCompilePugFilter()
|
|
599
617
|
})({
|
package/lib/translate.js
CHANGED
|
@@ -1,9 +1,17 @@
|
|
|
1
1
|
import { v2 } from "@google-cloud/translate";
|
|
2
2
|
import fse from "fs-extra";
|
|
3
|
-
import languageData from "./languageData.js";
|
|
4
3
|
import async from "async";
|
|
5
|
-
import { config } from "../config.js";
|
|
6
4
|
import path from "path";
|
|
5
|
+
import { pathToFileURL } from "url";
|
|
6
|
+
|
|
7
|
+
const projectRoot = process.cwd();
|
|
8
|
+
const configPath = pathToFileURL(path.resolve(projectRoot, "config.js")).href;
|
|
9
|
+
const { config } = await import(configPath);
|
|
10
|
+
|
|
11
|
+
const languageDataPath = pathToFileURL(
|
|
12
|
+
path.resolve(projectRoot, "languageData.js")
|
|
13
|
+
).href;
|
|
14
|
+
const { default: languageData } = await import(languageDataPath);
|
|
7
15
|
|
|
8
16
|
const key = "AIzaSyAbhPvlSAAJnH_IIRbdwjXOX9c0plvlH_k";
|
|
9
17
|
const projectId = "3nm Game Site";
|
|
@@ -88,8 +96,10 @@ async function main() {
|
|
|
88
96
|
}
|
|
89
97
|
}
|
|
90
98
|
});
|
|
99
|
+
|
|
100
|
+
// 修改输出路径为项目根目录
|
|
91
101
|
fse.writeFileSync(
|
|
92
|
-
path.join(
|
|
102
|
+
path.join(projectRoot, "languageData.js"),
|
|
93
103
|
"export default" + JSON.stringify(languageData)
|
|
94
104
|
);
|
|
95
105
|
console.log("翻译完成花费:", (Date.now() - startTime) / 1000 + "s");
|
package/lib/watchFile.js
CHANGED
|
@@ -1,10 +1,9 @@
|
|
|
1
1
|
import chokidar from "chokidar";
|
|
2
|
-
import fse from "fs-extra";
|
|
3
2
|
import path from "path";
|
|
4
3
|
import { exec } from "child_process";
|
|
5
4
|
import { debounce } from "./utils.js";
|
|
6
|
-
|
|
7
|
-
const
|
|
5
|
+
|
|
6
|
+
const projectRoot = process.cwd();
|
|
8
7
|
|
|
9
8
|
/**
|
|
10
9
|
* 更改/template刷新网页
|
|
@@ -13,7 +12,9 @@ function watchTemplate() {
|
|
|
13
12
|
const refreshPagFn = debounce(() => {
|
|
14
13
|
exec(`curl http://${process.env._localIp}:${process.env._port}/_refresh`);
|
|
15
14
|
}, 300);
|
|
16
|
-
|
|
15
|
+
|
|
16
|
+
// 使用 path.join 来确保跨平台兼容性
|
|
17
|
+
let watch = chokidar.watch(path.join(projectRoot, "template"), {
|
|
17
18
|
persistent: true
|
|
18
19
|
// ignored: [/node_modules/, /\.git/]
|
|
19
20
|
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "pug-site-core",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.2",
|
|
4
4
|
"main": "index.js",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"scripts": {
|
|
@@ -30,5 +30,12 @@
|
|
|
30
30
|
"ws": "^8.18.0"
|
|
31
31
|
},
|
|
32
32
|
"license": "ISC",
|
|
33
|
-
"description": ""
|
|
33
|
+
"description": "",
|
|
34
|
+
"files": [
|
|
35
|
+
"lib/",
|
|
36
|
+
"index.js"
|
|
37
|
+
],
|
|
38
|
+
"exports": {
|
|
39
|
+
".": "./index.js"
|
|
40
|
+
}
|
|
34
41
|
}
|