pug-site-core 2.0.7 → 2.0.9

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.
Files changed (2) hide show
  1. package/lib/generate.js +35 -14
  2. package/package.json +2 -2
package/lib/generate.js CHANGED
@@ -105,12 +105,23 @@ export async function generateGetDataFn() {
105
105
  const getDataFile = await fse.readFile(getDataPath, "utf8");
106
106
  const pagesPugFilePathArr = await getPagesPugFilePathArr(true);
107
107
 
108
- // 注入公共数据获取函数
109
- if (!getDataFile.includes("get_common_data")) {
108
+ // 使用正则表达式检查init函数是否存在
109
+ const initFunctionRegex = /export\s+async\s+function\s+init\s*\(\s*\)/;
110
+ if (!initFunctionRegex.test(getDataFile)) {
111
+ const initFn = `export async function init() {
112
+ //初始化操作,在获取页面数据前执行
113
+ }`;
114
+ await fse.appendFile(getDataPath, initFn + "\n");
115
+ }
116
+
117
+ // 使用正则表达式检查公共数据获取函数是否存在
118
+ const commonDataFnRegex =
119
+ /export\s+async\s+function\s+get_common_data\s*\(\s*language\s*\)/;
120
+ if (!commonDataFnRegex.test(getDataFile)) {
110
121
  const commonDataFn = `export async function get_common_data(language) {
111
- return {}
112
- }\n`;
113
- await fse.appendFile(getDataPath, commonDataFn);
122
+ return {}
123
+ }`;
124
+ await fse.appendFile(getDataPath, commonDataFn + "\n");
114
125
  }
115
126
 
116
127
  // 为每个页面注入数据获取函数
@@ -118,12 +129,14 @@ export async function generateGetDataFn() {
118
129
  const funName =
119
130
  "get_" + fileName.split(pathSymbol).join("_").slice(0, -4) + "_data";
120
131
 
121
- if (!getDataFile.includes(funName)) {
122
- const template = config.getDataFnTemplate
123
- .toString()
124
- .replace("template", funName);
125
- const dataFn = `\nexport async ${template}`;
126
- await fse.appendFile(getDataPath, dataFn);
132
+ // 使用正则表达式检查特定的数据获取函数是否存在
133
+ const dataFnRegex = new RegExp(
134
+ `export\\s+async\\s+function\\s+${funName}\\s*\\(\\s*language\\s*\\)`
135
+ );
136
+ if (!dataFnRegex.test(getDataFile)) {
137
+ const templateFn = config.getDataFnTemplate.toString();
138
+ const dataFn = `export async ${templateFn.replace("template", funName)}`;
139
+ await fse.appendFile(getDataPath, dataFn + "\n");
127
140
  }
128
141
  });
129
142
  } catch (error) {
@@ -197,6 +210,16 @@ export async function fetchDataToJsonFile(args) {
197
210
  }
198
211
 
199
212
  const getData = await import(paths.getData);
213
+
214
+ // 执行初始化函数
215
+ if (getData.init && typeof getData.init === "function") {
216
+ console.log("开始执行init初始化函数");
217
+ const initStartTime = Date.now();
218
+ await getData.init();
219
+ const initCostTime = (Date.now() - initStartTime) / 1000;
220
+ console.log("初始化函数执行完成,耗时:", initCostTime, "s");
221
+ }
222
+
200
223
  const pugFilePathList = await getPagesPugFilePathArr();
201
224
 
202
225
  const { languageList, customBuildData, fetchDataConcurrencyLimit } = config;
@@ -392,9 +415,7 @@ export async function buildFn() {
392
415
  let totalCommonData = {};
393
416
  totalCommonData.langCommon = config.commonData;
394
417
  await async.each(config.languageList, async (lang) => {
395
- let commonData = await fse.readJSON(
396
- paths.resolveRoot("jsonData", lang, "_common.json")
397
- );
418
+ let commonData = await (await import(paths.getData))["get_common_data"](lang);
398
419
  totalCommonData[lang] = commonData;
399
420
  });
400
421
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pug-site-core",
3
- "version": "2.0.7",
3
+ "version": "2.0.9",
4
4
  "main": "index.js",
5
5
  "type": "module",
6
6
  "scripts": {
@@ -37,7 +37,7 @@
37
37
  "axios": "^1.7.7"
38
38
  },
39
39
  "license": "ISC",
40
- "description": "增加获取数据的log",
40
+ "description": "优化注入函数的判断、添加init函数",
41
41
  "files": [
42
42
  "lib/",
43
43
  "index.js"