From 27f3f856810580b0736a6f5c2a170e0bb9068d71 Mon Sep 17 00:00:00 2001 From: Mark Lee Date: Fri, 27 Mar 2020 16:07:51 -0700 Subject: [PATCH] feat: add TypeScript definition (#92) --- .eslintrc.typescript.js | 16 ++++++++++++++++ index.d.ts | 21 +++++++++++++++++++++ index.test-d.ts | 26 ++++++++++++++++++++++++++ package.json | 23 ++++++++++++++++++----- 4 files changed, 81 insertions(+), 5 deletions(-) create mode 100644 .eslintrc.typescript.js create mode 100644 index.d.ts create mode 100644 index.test-d.ts diff --git a/.eslintrc.typescript.js b/.eslintrc.typescript.js new file mode 100644 index 0000000..702934e --- /dev/null +++ b/.eslintrc.typescript.js @@ -0,0 +1,16 @@ +const { eslintConfig } = require('./package.json') + +eslintConfig.parser = '@typescript-eslint/parser' +eslintConfig.extends.push( + 'plugin:@typescript-eslint/eslint-recommended', + 'plugin:@typescript-eslint/recommended' +) + +eslintConfig.rules = { + 'node/no-unsupported-features/es-syntax': 'off', + 'comma-dangle': ['error', 'only-multiline'], + semi: ['error', 'always'], + 'space-before-function-paren': ['error', 'never'] +} + +module.exports = eslintConfig diff --git a/index.d.ts b/index.d.ts new file mode 100644 index 0000000..6dbcc80 --- /dev/null +++ b/index.d.ts @@ -0,0 +1,21 @@ +// Based on the type definitions for extract-zip 1.6 +// Definitions by: Mizunashi Mana +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped/blob/e69b58e/types/extract-zip/index.d.ts + +import { Entry, ZipFile } from 'yauzl'; + +declare namespace extract { + interface Options { + dir: string; + defaultDirMode?: number; + defaultFileMode?: number; + onEntry?: (entry: Entry, zipfile: ZipFile) => void; + } +} + +declare function extract( + zipPath: string, + opts: extract.Options, +): Promise; + +export = extract; diff --git a/index.test-d.ts b/index.test-d.ts new file mode 100644 index 0000000..62fc01d --- /dev/null +++ b/index.test-d.ts @@ -0,0 +1,26 @@ +import { Entry, ZipFile } from 'yauzl'; +import * as extract from '.'; + +const zip = '/path/to/zip'; +const dir = '/path/to/dir'; +const defaultFileMode = 0o600; + +let options: extract.Options = { + dir, +}; +options = { + dir, + defaultDirMode: 0o700, + defaultFileMode, + onEntry: (entry: Entry, zipfile: ZipFile): void => { + console.log(entry); + console.log(zipfile); + } +}; + +try { + await extract(zip, options); + console.log('done'); +} catch (err) { + console.error(err); +} diff --git a/package.json b/package.json index 3be6b9d..3620832 100644 --- a/package.json +++ b/package.json @@ -3,17 +3,22 @@ "version": "1.7.0", "description": "unzip a zip file into a directory using 100% javascript", "main": "index.js", + "types": "index.d.ts", "bin": { "extract-zip": "cli.js" }, "scripts": { "ava": "ava", "coverage": "nyc ava", - "lint": "eslint .", - "test": "npm run lint && ava" + "lint": "yarn lint:js && yarn lint:ts && yarn tsd", + "lint:js": "eslint .", + "lint:ts": "eslint --config .eslintrc.typescript.js --ext .ts .", + "test": "yarn lint && ava", + "tsd": "tsd" }, "files": [ - "*.js" + "*.js", + "index.d.ts" ], "author": "max ogden", "license": "BSD-2-Clause", @@ -31,7 +36,12 @@ "get-stream": "^5.1.0", "yauzl": "^2.10.0" }, + "optionalDependencies": { + "@types/yauzl": "^2.9.1" + }, "devDependencies": { + "@typescript-eslint/eslint-plugin": "^2.25.0", + "@typescript-eslint/parser": "^2.25.0", "ava": "^3.5.1", "eslint": "^6.8.0", "eslint-config-standard": "^14.1.1", @@ -43,7 +53,9 @@ "fs-extra": "^9.0.0", "husky": "^4.2.3", "lint-staged": "^10.0.9", - "nyc": "^15.0.0" + "nyc": "^15.0.0", + "tsd": "^0.11.0", + "typescript": "^3.8.3" }, "eslintConfig": { "extends": [ @@ -62,6 +74,7 @@ } }, "lint-staged": { - "*.js": "eslint --fix" + "*.js": "yarn lint:js --fix", + "*.ts": "yarn lint:ts --fix" } }