diff --git a/.gitignore b/.gitignore index 51212ff..6fc64df 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,4 @@ /node_modules -lib +/@types npm-debug.log .DS_Store diff --git a/src/index.d.ts b/index.d.ts similarity index 90% rename from src/index.d.ts rename to index.d.ts index ceeb996..522d3ee 100644 --- a/src/index.d.ts +++ b/index.d.ts @@ -1,8 +1,9 @@ import { Transform } from "readable-stream"; + import {Buffer} from "safe-buffer"; export declare module 'encoding' { - export function convert(buf: Buffer, charset: string): Buffer; + export function convert(buf: Buffer, toCharset: string, fromCharset: string): Buffer; } export interface Compiler { @@ -37,13 +38,13 @@ export interface parserOptions { validation?: boolean; } -export interface PoParser { +export interface po { parse: (buffer: Buffer | string, defaultCharset?: string) => GetTextTranslations; compile: (table: GetTextTranslations, options?: parserOptions) => Buffer; createParseStream: (options?: parserOptions, transformOptions?: import('readable-stream').TransformOptions) => Transform; } -export interface MoParser { +export interface mo { parse: (buffer: Buffer | string, defaultCharset?: string) => GetTextTranslations; compile: (table: GetTextTranslations, options?: parserOptions) => Buffer; } diff --git a/src/index.js b/index.js similarity index 59% rename from src/index.js rename to index.js index d5deb01..99aafda 100644 --- a/src/index.js +++ b/index.js @@ -1,13 +1,13 @@ -import * as poParser from './poparser.js'; -import poCompiler from './pocompiler.js'; -import moParser from './moparser.js'; -import moCompiler from './mocompiler.js'; +import * as poParser from './lib/poparser.js'; +import poCompiler from './lib/pocompiler.js'; +import moParser from './lib/moparser.js'; +import moCompiler from './lib/mocompiler.js'; /** * Translation parser and compiler for PO files * @see https://www.gnu.org/software/gettext/manual/html_node/PO.html * - * @type {import("./index.js").PoParser} po + * @type {import("./index.d.ts").po} po */ export const po = { parse: poParser.parse, @@ -19,9 +19,9 @@ export const po = { * Translation parser and compiler for PO files * @see https://www.gnu.org/software/gettext/manual/html_node/MO.html * - * @type {import("./index.js").MoParser} mo + * @type {import("./index.d.ts").mo} mo */ -export const mo= { +export const mo = { parse: moParser, compile: moCompiler }; diff --git a/src/mocompiler.js b/lib/mocompiler.js similarity index 91% rename from src/mocompiler.js rename to lib/mocompiler.js index 98ee909..9e8747d 100644 --- a/src/mocompiler.js +++ b/lib/mocompiler.js @@ -1,13 +1,13 @@ import { Buffer } from 'safe-buffer'; import encoding from 'encoding'; -import { compareMsgid, formatCharset, generateHeader, HEADERS } from './shared.js'; +import { HEADERS, formatCharset, generateHeader, compareMsgid } from './shared.js'; import contentType from 'content-type'; /** * Exposes general compiler function. Takes a translation * object as a parameter and returns binary MO object * - * @param {import('./index.js').gettextTranslations} table Translation object + * @param {import('./index.d.ts').GetTextTranslations} table Translation object * @return {Buffer} Compiled binary MO object */ export default function (table) { @@ -20,10 +20,10 @@ export default function (table) { * Creates a MO compiler object. * * @constructor - * @param {import('./index.js').gettextTranslations} table Translation table as defined in the README - * @return {import('./index.js').Compiler} Compiler + * @param {import('./index.d.ts').GetTextTranslations} table Translation table as defined in the README + * @return {import('./index.d.ts').Compiler} Compiler */ -function Compiler (table ) { +function Compiler (table = {}) { this._table = table; let { headers = {}, translations = {} } = this._table; @@ -71,8 +71,6 @@ function Compiler (table ) { this._writeFunc = 'writeUInt32LE'; this._handleCharset(); - - return this; } /** @@ -151,7 +149,7 @@ Compiler.prototype._generateList = function () { /** * Calculate buffer size for the final binary object * - * @param {import('./index.js').GettextTranslations} list An array of translation strings from _generateList + * @param {import('./index.d.ts').GetTextTranslations} list An array of translation strings from _generateList * @return {Object} Size data of {msgid, msgstr, total} */ Compiler.prototype._calculateSize = function (list) { @@ -186,7 +184,7 @@ Compiler.prototype._calculateSize = function (list) { /** * Generates the binary MO object from the translation list * - * @param {import('./index.js').GettextTranslations} list translation list + * @param {import('./index.d.ts').GetTextTranslations} list translation list * @param {Object} size Byte size information * @return {Buffer} Compiled MO object */ @@ -240,7 +238,7 @@ Compiler.prototype._build = function (list, size) { }; /** - * Compiles the translation object into a binary MO object + * Compiles translation object into a binary MO object * * @return {Buffer} Compiled MO object */ diff --git a/src/moparser.js b/lib/moparser.js similarity index 100% rename from src/moparser.js rename to lib/moparser.js diff --git a/src/pocompiler.js b/lib/pocompiler.js similarity index 98% rename from src/pocompiler.js rename to lib/pocompiler.js index 61a229f..dd15ef4 100644 --- a/src/pocompiler.js +++ b/lib/pocompiler.js @@ -1,6 +1,8 @@ -import { compareMsgid, foldLine, formatCharset, generateHeader, HEADERS } from './shared.js'; -import contentType from 'content-type'; +import { Buffer } from 'safe-buffer'; import encoding from 'encoding'; +import { HEADERS, foldLine, compareMsgid, formatCharset, generateHeader } from './shared.js'; +import contentType from 'content-type'; + /** * Exposes general compiler function. Takes a translation diff --git a/src/poparser.js b/lib/poparser.js similarity index 100% rename from src/poparser.js rename to lib/poparser.js diff --git a/src/shared.js b/lib/shared.js similarity index 100% rename from src/shared.js rename to lib/shared.js diff --git a/package.json b/package.json index 08cadbe..90d8e11 100644 --- a/package.json +++ b/package.json @@ -17,24 +17,15 @@ "engines": { "node": ">=18" }, - "files": [ - "lib", - "test", - "*.json", - "*.md", - "license.txt" - ], "scripts": { - "start": "tsc --watch", - "build": "tsc", - "lint": "eslint src/*.js test/*.js", + "lint": "eslint lib/*.js test/*.js index.js", "test-generate-mo": "msgfmt test/fixtures/latin13.po -o test/fixtures/latin13.mo & msgfmt test/fixtures/utf8.po -o test/fixtures/utf8.mo & msgfmt test/fixtures/obsolete.po -o test/fixtures/obsolete.mo", "test": "mocha", "preversion": "npm run lint && npm test", - "postversion": "git push && git push --tags" + "postversion": "git push && git push --tags", + "prepublish": "tsc && npm run lint && npm run test" }, - "main": "./lib/index.js", - "types": "./lib/index.d.ts", + "main": "./index.js", "license": "MIT", "dependencies": { "content-type": "^1.0.5", @@ -46,15 +37,16 @@ "@types/chai": "latest", "@types/content-type": "^1.1.8", "@types/mocha": "latest", + "@types/readable-stream": "^4.0.11", "@typescript-eslint/eslint-plugin": "^6.18.1", "@typescript-eslint/parser": "^6.14.0", - "chai": "^5.1.0", + "chai": "^5.0.3", "eslint": "^8.56.0", "eslint-config-standard": "^17.1.0", "eslint-plugin-import": "^2.29.1", "eslint-plugin-n": "^16.6.2", "eslint-plugin-promise": "^6.1.1", - "mocha": "^10.4.0", + "mocha": "^10.3.0", "typescript": "^5.4.4" }, "keywords": [ diff --git a/test/mo-compiler-test.js b/test/mo-compiler-test.js index 6405d7a..4796425 100644 --- a/test/mo-compiler-test.js +++ b/test/mo-compiler-test.js @@ -1,7 +1,7 @@ import * as chai from 'chai'; import { promisify } from 'util'; import path from 'path'; -import { mo } from '../src/index.js'; +import { mo } from '../index.js'; import { readFile as fsReadFile } from 'fs'; import { fileURLToPath } from 'url'; diff --git a/test/mo-parser-test.js b/test/mo-parser-test.js index cf569dc..723368a 100644 --- a/test/mo-parser-test.js +++ b/test/mo-parser-test.js @@ -1,7 +1,7 @@ import * as chai from 'chai'; import { promisify } from 'util'; import path from 'path'; -import { mo } from '../src/index.js'; +import { mo } from '../index.js'; import { readFile as fsReadFile } from 'fs'; import { fileURLToPath } from 'url'; diff --git a/test/module.mjs b/test/module.mjs index b1b48b0..1796fd6 100644 --- a/test/module.mjs +++ b/test/module.mjs @@ -1,5 +1,5 @@ import { expect } from 'chai'; -import { po, mo } from '../src/index.js'; +import { po, mo } from '../index.js'; describe('esm module', () => { it('should allow named imports', () => { diff --git a/test/po-compiler-test.js b/test/po-compiler-test.js index 07000a9..18ad149 100644 --- a/test/po-compiler-test.js +++ b/test/po-compiler-test.js @@ -3,7 +3,7 @@ import { promisify } from 'util'; import path from 'path'; import { readFile as fsReadFile } from 'fs'; import * as chai from 'chai'; -import { po } from '../src/index.js'; +import { po } from '../index.js'; import { fileURLToPath } from 'url'; const __filename = fileURLToPath(import.meta.url); diff --git a/test/po-obsolete-test.js b/test/po-obsolete-test.js index 10ec59d..f0e696f 100644 --- a/test/po-obsolete-test.js +++ b/test/po-obsolete-test.js @@ -3,7 +3,7 @@ import * as chai from 'chai'; import { promisify } from 'util'; import path from 'path'; import fs from 'fs'; -import * as gettextParser from '../src/index.js'; +import * as gettextParser from '../index.js'; import { fileURLToPath } from 'url'; const __filename = fileURLToPath(import.meta.url); diff --git a/test/po-parser-test.js b/test/po-parser-test.js index 89b9e95..3a006c1 100644 --- a/test/po-parser-test.js +++ b/test/po-parser-test.js @@ -2,7 +2,7 @@ import * as chai from 'chai'; import { promisify } from 'util'; import path from 'path'; import fs from 'fs'; -import * as gettextParser from '../src/index.js'; +import * as gettextParser from '../index.js'; import { fileURLToPath } from 'url'; const __filename = fileURLToPath(import.meta.url); diff --git a/test/shared.js b/test/shared.js index 7ca0a81..c40dfd6 100644 --- a/test/shared.js +++ b/test/shared.js @@ -3,7 +3,7 @@ import * as chai from 'chai'; import { promisify } from 'util'; import path from 'path'; -import { formatCharset, parseHeader, generateHeader, foldLine, parseNPluralFromHeadersSafely } from '../src/shared.js'; +import { formatCharset, parseHeader, generateHeader, foldLine, parseNPluralFromHeadersSafely } from '../lib/shared.js'; import { readFile as fsReadFile } from 'fs'; import { fileURLToPath } from 'url'; diff --git a/tsconfig.json b/tsconfig.json index 1b7d1bd..1564f0f 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -2,36 +2,35 @@ "compilerOptions": { "noImplicitAny": true, "removeComments": false, - "module": "ESNext", - "moduleResolution": "Node", - "target": "ES2021", - "declaration": true, - "emitDeclarationOnly": false, - "preserveConstEnums": true, - "sourceMap": true, - "strict": false, + "module": "Node16", + "moduleResolution": "Node16", + "target": "ES2015", + + // Strict mode + "strict": true, + + // Allow javascript files "allowJs": true, - "downlevelIteration": true, - "esModuleInterop": true, - "skipLibCheck": true, - "allowSyntheticDefaultImports": true, - "experimentalDecorators": true, - "rootDir": "src", - "outDir": "lib", + + // Check js files for errors + "checkJs": false, + + // Output d.ts files to @types + "outDir": "@types", + + // Generate d.ts files + "declaration": true, + + // This compiler run should + // only output d.ts files + "emitDeclarationOnly": true, // Minify "pretty": false, - // types - "typeRoots": [ - "src/types.d.ts", - "node_modules/@types", - "@types" - ], - "lib": [ - "dom", - "ESNext" - ] + // Skip lib check when compiling + "skipLibCheck": true }, "include": [ - "./src/**/*" + "lib/**/*.js", + "index.d.ts" ] }