From 7fad3d13f1efdc37b904945dffe7cffe32f729aa Mon Sep 17 00:00:00 2001 From: JellyO1 Date: Fri, 10 Nov 2023 15:11:47 +0000 Subject: [PATCH] Add typescript type generation to build --- index.d.ts | 7 ------- lib/index.d.ts | 43 +++++++++++++++++++++++++++++++++++++++++++ lib/index.d.ts.map | 1 + lib/index.js | 2 +- lib/utils.d.ts | 6 ++++++ lib/utils.d.ts.map | 1 + package-lock.json | 22 +++++++++++++++++++++- package.json | 5 +++-- src/index.js | 1 + tsconfig.json | 21 +++++++++++++++++++++ 10 files changed, 98 insertions(+), 11 deletions(-) delete mode 100644 index.d.ts create mode 100644 lib/index.d.ts create mode 100644 lib/index.d.ts.map create mode 100644 lib/utils.d.ts create mode 100644 lib/utils.d.ts.map create mode 100644 tsconfig.json diff --git a/index.d.ts b/index.d.ts deleted file mode 100644 index 410fe7f..0000000 --- a/index.d.ts +++ /dev/null @@ -1,7 +0,0 @@ -import { BackendModule } from "i18next"; - -export function mainBindings(ipcMain: Electron.IpcMain, browserWindow: Electron.BrowserWindow, fs: any): any; -export function clearMainBindings(ipcMain: Electron.IpcMain): any; -export function preloadBindings(ipcRenderer: Electron.IpcRenderer, process: NodeJS.Process): any; - -export default {} as BackendModule; diff --git a/lib/index.d.ts b/lib/index.d.ts new file mode 100644 index 0000000..a1e6a42 --- /dev/null +++ b/lib/index.d.ts @@ -0,0 +1,43 @@ +export const readFileRequest: "ReadFile-Request"; +export const writeFileRequest: "WriteFile-Request"; +export const readFileResponse: "ReadFile-Response"; +export const writeFileResponse: "WriteFile-Response"; +export const changeLanguageRequest: "ChangeLanguage-Request"; +export function preloadBindings(ipcRenderer: any, process: any): { + send: (channel: any, data: any) => void; + onReceive: (channel: any, func: any) => void; + onLanguageChange: (func: any) => void; + clientOptions: { + environment: any; + platform: any; + resourcesPath: string; + }; +}; +export function mainBindings(ipcMain: any, browserWindow: any, fs: any): void; +export function clearMainBindings(ipcMain: any): void; +export default Backend; +declare class Backend { + constructor(services: any, backendOptions?: {}, i18nextOptions?: {}); + readCallbacks: {}; + writeCallbacks: {}; + writeTimeout: any; + writeQueue: any[]; + writeQueueOverflow: any[]; + useOverflow: boolean; + init(services: any, backendOptions: any, i18nextOptions: any): void; + services: any; + backendOptions: any; + i18nextOptions: any; + mainLog: string; + rendererLog: string; + setupIpcBindings(): void; + write(writeQueue: any): void; + requestFileRead(filename: any, callback: any): void; + read(language: any, namespace: any, callback: any): void; + readMulti(_languages: any, _namespaces: any, _callback: any): void; + create(languages: any, namespace: any, key: any, fallbackValue: any, callback: any): void; +} +declare namespace Backend { + let type: string; +} +//# sourceMappingURL=index.d.ts.map \ No newline at end of file diff --git a/lib/index.d.ts.map b/lib/index.d.ts.map new file mode 100644 index 0000000..572b3b4 --- /dev/null +++ b/lib/index.d.ts.map @@ -0,0 +1 @@ +{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.js"],"names":[],"mappings":"AAiBA,iDAAkD;AAClD,mDAAoD;AACpD,mDAAoD;AACpD,qDAAsD;AACtD,6DAA8D;AAIvD;;;;;;;;;EA0BN;AAIM,8EAoCN;AAIM,sDAGN;;AAID;IACI,qEAQC;IANG,kBAAuB;IACvB,mBAAwB;IACxB,kBAA6B;IAC7B,kBAAoB;IACpB,0BAA4B;IAC5B,qBAAwB;IAG5B,oEAgCC;IApBG,cAAwB;IACxB,oBAIC;IACD,oBAAoC;IAIpC,gBAAqC;IACrC,oBAA6C;IAajD,yBAyEC;IAGD,6BAoDC;IAGD,oDAkBC;IAGD,yDAaC;IAGD,mEAEC;IAGD,0FA8DC;CACJ"} \ No newline at end of file diff --git a/lib/index.js b/lib/index.js index d0e6e0b..53ac4ee 100644 --- a/lib/index.js +++ b/lib/index.js @@ -1 +1 @@ -"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.writeFileResponse=exports.writeFileRequest=exports.readFileResponse=exports.readFileRequest=exports.preloadBindings=exports.mainBindings=exports["default"]=exports.clearMainBindings=exports.changeLanguageRequest=void 0;var _lodash=require("lodash"),_utils=require("./utils");function _typeof(a){"@babel/helpers - typeof";return _typeof="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(a){return typeof a}:function(a){return a&&"function"==typeof Symbol&&a.constructor===Symbol&&a!==Symbol.prototype?"symbol":typeof a},_typeof(a)}function ownKeys(a,b){var c=Object.keys(a);if(Object.getOwnPropertySymbols){var d=Object.getOwnPropertySymbols(a);b&&(d=d.filter(function(b){return Object.getOwnPropertyDescriptor(a,b).enumerable})),c.push.apply(c,d)}return c}function _objectSpread(a){for(var b,c=1;c"),this.rendererLog="".concat("[i18next-electron-fs-backend:","renderer]=>"),"undefined"==typeof this.backendOptions.i18nextElectronBackend||this.setupIpcBindings()}},{key:"setupIpcBindings",value:function setupIpcBindings(){var a=this,b=this.backendOptions.i18nextElectronBackend;b.onReceive(readFileResponse,function(b){if("undefined"!=typeof a.readCallbacks[b.key]){var c;if(b.error)c=a.readCallbacks[b.key].callback,delete a.readCallbacks[b.key],null!==c&&"function"==typeof c&&c(null,{});else{var d;b.data=b.data.replace(/^\uFEFF/,"");try{d=JSON.parse(b.data)}catch(d){return d.message="Error parsing '".concat(b.filename,"'. Message: '").concat(d,"'."),c=a.readCallbacks[b.key].callback,delete a.readCallbacks[b.key],void(null!==c&&"function"==typeof c&&c(d))}c=a.readCallbacks[b.key].callback,delete a.readCallbacks[b.key],null!==c&&"function"==typeof c&&c(null,d)}}}),b.onReceive(writeFileResponse,function(b){for(var c,d=b.keys,e=0;e"),this.rendererLog="".concat("[i18next-electron-fs-backend:","renderer]=>"),"undefined"==typeof this.backendOptions.i18nextElectronBackend||this.setupIpcBindings()}},{key:"setupIpcBindings",value:function setupIpcBindings(){var a=this,b=this.backendOptions.i18nextElectronBackend;b.onReceive(readFileResponse,function(b){if("undefined"!=typeof a.readCallbacks[b.key]){var c;if(b.error)c=a.readCallbacks[b.key].callback,delete a.readCallbacks[b.key],null!==c&&"function"==typeof c&&c(null,{});else{var d;b.data=b.data.replace(/^\uFEFF/,"");try{d=JSON.parse(b.data)}catch(d){return d.message="Error parsing '".concat(b.filename,"'. Message: '").concat(d,"'."),c=a.readCallbacks[b.key].callback,delete a.readCallbacks[b.key],void(null!==c&&"function"==typeof c&&c(d))}c=a.readCallbacks[b.key].callback,delete a.readCallbacks[b.key],null!==c&&"function"==typeof c&&c(null,d)}}}),b.onReceive(writeFileResponse,function(b){for(var c,d=b.keys,e=0;e=4" } }, + "node_modules/typescript": { + "version": "5.2.2", + "resolved": "https://registry.npmjs.org/typescript/-/typescript-5.2.2.tgz", + "integrity": "sha512-mI4WrpHsbCIcwT9cF4FZvr80QUeKvsUsUvKDoR+X/7XHQH98xYD8YHZg7ANtz2GtZt/CBq2QJ0thkGJMHfqc1w==", + "dev": true, + "bin": { + "tsc": "bin/tsc", + "tsserver": "bin/tsserver" + }, + "engines": { + "node": ">=14.17" + } + }, "node_modules/unicode-canonical-property-names-ecmascript": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/unicode-canonical-property-names-ecmascript/-/unicode-canonical-property-names-ecmascript-2.0.0.tgz", @@ -5032,6 +5046,12 @@ "integrity": "sha512-/OaKK0xYrs3DmxRYqL/yDc+FxFUVYhDlXMhRmv3z915w2HF1tnN1omB354j8VUGO/hbRzyD6Y3sA7v7GS/ceog==", "dev": true }, + "typescript": { + "version": "5.2.2", + "resolved": "https://registry.npmjs.org/typescript/-/typescript-5.2.2.tgz", + "integrity": "sha512-mI4WrpHsbCIcwT9cF4FZvr80QUeKvsUsUvKDoR+X/7XHQH98xYD8YHZg7ANtz2GtZt/CBq2QJ0thkGJMHfqc1w==", + "dev": true + }, "unicode-canonical-property-names-ecmascript": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/unicode-canonical-property-names-ecmascript/-/unicode-canonical-property-names-ecmascript-2.0.0.tgz", diff --git a/package.json b/package.json index 468028f..f87c90a 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,7 @@ "description": "An i18next backend plugin that allows you to use local translation files for your electron app, following electron's best-security practices.", "main": "lib/index.js", "scripts": { - "build": "npx babel ./src -d ./lib" + "build": "npx tsc && npx babel ./src -d ./lib" }, "repository": { "type": "git", @@ -21,7 +21,8 @@ "@babel/cli": "^7.23.0", "@babel/core": "^7.23.2", "@babel/preset-env": "^7.23.2", - "babel-preset-minify": "^0.5.2" + "babel-preset-minify": "^0.5.2", + "typescript": "^5.2.2" }, "dependencies": { "lodash.clonedeep": "^4.5.0", diff --git a/src/index.js b/src/index.js index d5dba06..e7543d9 100644 --- a/src/index.js +++ b/src/index.js @@ -6,6 +6,7 @@ import { mergeNested, groupByArray } from "./utils"; +import '../index'; // CONFIGS const defaultOptions = { diff --git a/tsconfig.json b/tsconfig.json new file mode 100644 index 0000000..98d8ea5 --- /dev/null +++ b/tsconfig.json @@ -0,0 +1,21 @@ +{ + // Change this to match your project + "include": ["src/**/*"], + "compilerOptions": { + // Tells TypeScript to read JS files, as + // normally they are ignored as source files + "allowJs": true, + // Generate d.ts files + "declaration": true, + // This compiler run should + // only output d.ts files + "emitDeclarationOnly": true, + // Types should go into this directory. + // Removing this would place the .d.ts files + // next to the .js files + "outDir": "lib", + // go to js file when using IDE functions like + // "Go to Definition" in VSCode + "declarationMap": true + } + } \ No newline at end of file