diff --git a/package.json b/package.json index 87ee663..7ce000f 100644 --- a/package.json +++ b/package.json @@ -4,6 +4,7 @@ "description": "兰亭文存", "type": "module", "scripts": { + "tsnode": "ts-node", "lint": "umi g tmp && npm run lint:js && npm run lint:prettier", "lint:fix": "eslint --fix --cache --ext .js,.jsx,.ts,.tsx --format=pretty ./src", "lint:js": "eslint --cache --ext .js,.jsx,.ts,.tsx --format=pretty ./src", @@ -42,9 +43,11 @@ "@ant-design/pro-descriptions": "^1.0.16", "@ant-design/pro-layout": "^6.4.16", "@ant-design/pro-table": "^2.7.2", + "@types/node": "^20.4.9", "@umijs/route-utils": "^1.0.32", "antd": "^4.6.3", "classnames": "^2.2.6", + "esm": "^3.2.25", "lodash": "^4.17.11", "omit.js": "^2.0.2", "qs": "^6.9.0", @@ -55,6 +58,7 @@ "react-highlight-words": "^0.16.0", "react-markdown": "^5.0.3", "remark-gfm": "^1.0.0", + "tslib": "^2.6.1", "umi": "^3.2.14", "umi-request": "^1.0.8", "use-merge-value": "^1.0.1" @@ -101,7 +105,7 @@ "pro-download": "1.0.1", "puppeteer-core": "^5.0.0", "ts-node": "^10.9.1", - "typescript": "^3.9.7" + "typescript": "^5.1.6" }, "engines": { "node": ">=14.0.0" diff --git a/scripts/compile-archives.ts b/scripts/compile-archives.ts index 230acc0..4ba6a83 100644 --- a/scripts/compile-archives.ts +++ b/scripts/compile-archives.ts @@ -1,6 +1,7 @@ /* eslint-disable no-console */ /* eslint-disable no-unused-vars */ /* eslint-disable @typescript-eslint/no-unused-vars */ + import * as fs from 'fs'; import { dirname } from 'path'; import { fileURLToPath } from 'url'; @@ -10,7 +11,7 @@ import mysql, { Connection } from 'mysql'; import { execSync } from 'child_process'; import axios from 'axios'; import { createHmac } from 'crypto'; -import { Archive, Archives } from './data'; +import { Archive, Archives, FieldFreqMap } from './data'; import secrets from './secrets.json' assert { type: "json" }; import { exec } from './migrate-backend2'; @@ -96,11 +97,12 @@ function endConnection() { connection.end(); } -function setField(archive: Archive, field: string, fileContent: string, archives: Archives) { +function setField(archive: Archive, field: keyof Archive, fileContent: string, archives: Archives) { const regexArr = toFieldRegex(field).exec(fileContent); if (regexArr && regexArr[1]) { - const fieldVal = getFieldFromFieldLine(field, regexArr[1]); - archive[field] = fieldVal; + const fieldVal: string | string[] = getFieldFromFieldLine(field, regexArr[1]); + + (archive[field] as any) = fieldVal; fileContent = fileContent.replace(regexArr[0], ''); updateFreqMap(field, fieldVal, archives); @@ -112,11 +114,12 @@ function setField(archive: Archive, field: string, fileContent: string, archives function updateFreqMap(field: string, fieldVal: string | string[], archives: Archives) { if (['author', 'publisher', 'date', 'tag'].includes(field)) { + const validfield = field as keyof FieldFreqMap; if (typeof fieldVal === 'string') { - archives.fieldFreqMap[field][fieldVal] = (archives.fieldFreqMap[field][fieldVal] || 0) + 1; + archives.fieldFreqMap[validfield][fieldVal] = (archives.fieldFreqMap[validfield][fieldVal] || 0) + 1; } else { (fieldVal as string[]).forEach((v) => { - archives.fieldFreqMap[field][v] = (archives.fieldFreqMap[field][v] || 0) + 1; + archives.fieldFreqMap[validfield][v] = (archives.fieldFreqMap[validfield][v] || 0) + 1; }); } } @@ -425,9 +428,10 @@ async function compileArchives( const archive = new Archive(); archive.id = getIdFromCommentFilename(f); + console.log(f); let fileContent: string = fs.readFileSync(`${ARCHIVE_DIR}/comments/${f}`, 'utf-8'); - ['title', 'author', 'publisher', 'date', 'chapter', 'tag'].forEach((field) => { + (['title', 'author', 'publisher', 'date', 'chapter', 'tag'] as (keyof Archive)[]).forEach((field) => { fileContent = setField(archive, field, fileContent, compiledArchives); }); archive.remarks = fileContent.replace('# remarks', ''); @@ -449,7 +453,7 @@ async function compileArchives( compiledArchives.archives = {}; archives.forEach((a) => { - compiledArchives.archives[a.id] = a; + compiledArchives.archives[parseInt(a.id)] = a; }); if (isCompleteTask) { diff --git a/scripts/package.json b/scripts/package.json index bb9c10d..5299108 100644 --- a/scripts/package.json +++ b/scripts/package.json @@ -8,5 +8,9 @@ "@types/ali-oss": "^6.0.8", "ali-oss": "^6.15.2", "axios": "^0.21.1" + }, + "ts-node": { + "esm": true, + "experimentalSpecifierResolution": "node" } } diff --git a/scripts/test.ts b/scripts/test.ts index 86be1fa..7365be9 100644 --- a/scripts/test.ts +++ b/scripts/test.ts @@ -1,3 +1,17 @@ -import * as fs from 'fs'; -console.log(fs.readdirSync('./')); +class Archive { + title: string = ''; + author: string[] = []; + + publisher: string = ''; + + origs: false | string[] = false; + + likes: number = 0; +} + +const archive = new Archive(); +const keys: (keyof Archive)[] = ['title', 'author', 'publisher', 'origs', 'likes']; +let field: keyof Archive = keys[Math.floor(Math.random() * keys.length)]; + +archive[field] = 123; diff --git a/tsconfig.json b/tsconfig.json index 471eaec..8af0d70 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -12,7 +12,6 @@ "moduleResolution": "node", "forceConsistentCasingInFileNames": true, "noImplicitReturns": true, - "suppressImplicitAnyIndexErrors": true, "noUnusedLocals": false, "allowJs": true, "skipLibCheck": true,