From 47a5a4fa33ced8cb91faed4e3055549cd9f5b172 Mon Sep 17 00:00:00 2001 From: Mimi <1119186082@qq.com> Date: Mon, 31 Oct 2022 14:12:53 +0800 Subject: [PATCH 1/2] refactor: migrate typescript --- .eslintignore | 3 ++- .eslintrc | 8 ++++++-- .gitignore | 4 +++- lib/{i18n.js => i18n.ts} | 13 +++++++++---- package.json | 22 ++++++++++++++++------ test/.eslintrc | 5 ++++- test/index.js | 2 +- tsconfig.json | 19 +++++++++++++++++++ 8 files changed, 60 insertions(+), 16 deletions(-) rename lib/{i18n.js => i18n.ts} (92%) create mode 100644 tsconfig.json diff --git a/.eslintignore b/.eslintignore index f6b9638..f15eb05 100644 --- a/.eslintignore +++ b/.eslintignore @@ -1,3 +1,4 @@ node_modules/ coverage/ -tmp/ \ No newline at end of file +tmp/ +dist/ \ No newline at end of file diff --git a/.eslintrc b/.eslintrc index 91288aa..6b545e6 100644 --- a/.eslintrc +++ b/.eslintrc @@ -1,4 +1,8 @@ { - "extends": "hexo", - "root": true + "root": true, + "extends": "hexo/ts.js", + "parserOptions": { + "sourceType": "module", + "ecmaVersion": 2020 + } } \ No newline at end of file diff --git a/.gitignore b/.gitignore index 7f8dde4..a611977 100644 --- a/.gitignore +++ b/.gitignore @@ -3,4 +3,6 @@ node_modules/ tmp/ *.log .idea/ -coverage \ No newline at end of file +coverage +dist +package-lock.json \ No newline at end of file diff --git a/lib/i18n.js b/lib/i18n.ts similarity index 92% rename from lib/i18n.js rename to lib/i18n.ts index 02d3a13..12419c4 100644 --- a/lib/i18n.js +++ b/lib/i18n.ts @@ -1,9 +1,14 @@ -'use strict'; +import { vsprintf } from 'sprintf-js'; -const { vsprintf } = require('sprintf-js'); +interface Options { + languages?: string[]; +} class i18n { - constructor(options = {}) { + data: object; + languages: string[]; + + constructor(options: Options = {}) { this.data = {}; this.languages = options.languages || ['default']; @@ -110,4 +115,4 @@ function flattenObject(data, obj = {}, parent = '') { return obj; } -module.exports = i18n; +export = i18n; diff --git a/package.json b/package.json index 753e33b..2110af1 100644 --- a/package.json +++ b/package.json @@ -2,18 +2,23 @@ "name": "hexo-i18n", "version": "1.0.0", "description": "i18n module for Hexo.", - "main": "lib/i18n.js", + "main": "dist/i18n.js", "scripts": { + "prepublish ": "npm run clean && npm run build", + "build": "tsc -b", + "clean": "tsc -b --clean", "eslint": "eslint .", - "test": "mocha test/index.js", + "pretest": "npm run clean && npm run build", + "test": "mocha test/index.js --require ts-node/register", "test-cov": "c8 --reporter=lcovonly npm run test" }, "directories": { "lib": "./lib" }, "files": [ - "lib/" + "dist/**" ], + "types": "./dist/front_matter.d.ts", "engines": { "node": ">=14" }, @@ -30,10 +35,15 @@ "sprintf-js": "^1.1.2" }, "devDependencies": { + "@types/node": "^18.11.8", + "@typescript-eslint/eslint-plugin": "^5.41.0", + "@typescript-eslint/parser": "^5.41.0", "c8": "^7.12.0", - "chai": "^4.3.4", - "eslint": "^8.6.0", + "chai": "^4.3.6", + "eslint": "^8.26.0", "eslint-config-hexo": "^5.0.0", - "mocha": "^10.1.0" + "mocha": "^10.1.0", + "ts-node": "^10.9.1", + "typescript": "^4.8.4" } } diff --git a/test/.eslintrc b/test/.eslintrc index 82825e5..852f031 100644 --- a/test/.eslintrc +++ b/test/.eslintrc @@ -1,3 +1,6 @@ { - "extends": "hexo/test" + "extends": "hexo/test", + "rules": { + "@typescript-eslint/no-var-requires": 0 + } } \ No newline at end of file diff --git a/test/index.js b/test/index.js index fac80a3..8cfe097 100644 --- a/test/index.js +++ b/test/index.js @@ -3,7 +3,7 @@ const should = require('chai').should(); describe('i18n', () => { - const Ctor = require('../lib/i18n'); + const Ctor = require('../dist/i18n'); const i18n = new Ctor({ languages: ['zh-TW', 'en'] diff --git a/tsconfig.json b/tsconfig.json new file mode 100644 index 0000000..4db8461 --- /dev/null +++ b/tsconfig.json @@ -0,0 +1,19 @@ +{ + "compilerOptions": { + "module": "commonjs", + "target": "es6", + "sourceMap": true, + "outDir": "dist", + "declaration": true, + "esModuleInterop": true, + "types": [ + "node" + ] + }, + "include": [ + "lib/i18n.ts" + ], + "exclude": [ + "node_modules" + ] +} \ No newline at end of file From 228525d775e675851f715692635380a7ff6f8e72 Mon Sep 17 00:00:00 2001 From: Mimi <1119186082@qq.com> Date: Mon, 31 Oct 2022 14:15:24 +0800 Subject: [PATCH 2/2] dep: install @types/sprintf-js --- package.json | 1 + 1 file changed, 1 insertion(+) diff --git a/package.json b/package.json index 2110af1..56c6ed1 100644 --- a/package.json +++ b/package.json @@ -36,6 +36,7 @@ }, "devDependencies": { "@types/node": "^18.11.8", + "@types/sprintf-js": "^1.1.2", "@typescript-eslint/eslint-plugin": "^5.41.0", "@typescript-eslint/parser": "^5.41.0", "c8": "^7.12.0",