From 6e8f2f17a41064976b84170acda4e47b90d77599 Mon Sep 17 00:00:00 2001 From: Masafumi Koba <473530+ybiquitous@users.noreply.github.com> Date: Sat, 8 Aug 2020 11:48:26 +0900 Subject: [PATCH] feat(types): publish type declarations --- index.js | 3 --- lib/index.js | 3 +++ lib/init.js | 6 ++---- lib/logger.js | 6 ++++-- package.json | 11 +++++++---- test/tsconfig.test.json | 6 ++++++ test/types.test.ts | 6 ++++++ tsconfig.json | 2 +- types/ybiq.d.ts | 9 +++++---- 9 files changed, 34 insertions(+), 18 deletions(-) delete mode 100644 index.js create mode 100644 lib/index.js create mode 100644 test/tsconfig.test.json create mode 100644 test/types.test.ts diff --git a/index.js b/index.js deleted file mode 100644 index 89d46097..00000000 --- a/index.js +++ /dev/null @@ -1,3 +0,0 @@ -const { init } = require("./lib/init"); - -module.exports = { init }; diff --git a/lib/index.js b/lib/index.js new file mode 100644 index 00000000..f9bfc8b3 --- /dev/null +++ b/lib/index.js @@ -0,0 +1,3 @@ +const { init } = require("./init"); + +module.exports.init = init; diff --git a/lib/init.js b/lib/init.js index f95a54c6..e0d9d134 100644 --- a/lib/init.js +++ b/lib/init.js @@ -17,7 +17,7 @@ const packagePath = (elem, ...elems) => path.join(...[__dirname, "..", elem, ... /** * @param {string} baseDir - * @param {import("ybiq").Logger} logger + * @param {import("../types/ybiq").Logger} logger */ // eslint-disable-next-line max-lines-per-function const initCommand = (baseDir, logger) => { @@ -113,9 +113,7 @@ const initCommand = (baseDir, logger) => { }; }; -/** - * @param {import("ybiq").CommandParams} params - */ +/** @type {import("../types/ybiq").InitCommand} */ async function init({ cwd = process.cwd(), logger = defaultLogger } = {}) { const cmd = initCommand(cwd, logger); diff --git a/lib/logger.js b/lib/logger.js index a1c5b3ed..1f642344 100644 --- a/lib/logger.js +++ b/lib/logger.js @@ -1,4 +1,6 @@ const { EOL } = require("os"); -/** @type {import("ybiq").Logger} */ -module.exports.defaultLogger = (msg) => process.stdout.write(`${msg}${EOL}`); +/** @type {import("../types/ybiq").Logger} */ +module.exports.defaultLogger = (msg) => { + process.stdout.write(`${msg}${EOL}`); +}; diff --git a/package.json b/package.json index b7c69ddb..fcd6eab8 100644 --- a/package.json +++ b/package.json @@ -11,11 +11,13 @@ "utility", "tool" ], - "main": "index.js", + "main": "lib/index.js", "bin": "bin/cli.js", + "types": "types/ybiq.d.ts", "files": [ "bin", "lib", + "types", ".editorconfig", ".remarkignore", ".github/workflows/commitlint.yml", @@ -52,9 +54,10 @@ "typescript": "^3.9.7" }, "scripts": { - "test": "nyc --check-coverage --lines 100 --branches 90 tape \"test/**/*.test.js\"", + "test": "nyc --check-coverage --lines 100 --branches 90 tape \"test/**/*.test.js\" && npm run test:types", "test:watch": "nodemon --ext js,json --watch . --exec \"tape test/**/*.test.js\"", "test:coverage": "nyc report --reporter=html", + "test:types": "tsc --project test/tsconfig.test.json --noEmit", "lint:js": "eslint .", "lint:js:fix": "npm run lint:js -- --fix", "lint:md": "remark . --frail", @@ -132,7 +135,7 @@ "release", "remark", "standard-version", - "travis" + "types" ] ] } @@ -140,7 +143,7 @@ "eslintConfig": { "root": true, "ignorePatterns": [ - "*.d.ts" + "*.ts" ], "extends": [ "ybiquitous/node" diff --git a/test/tsconfig.test.json b/test/tsconfig.test.json new file mode 100644 index 00000000..a1448ff0 --- /dev/null +++ b/test/tsconfig.test.json @@ -0,0 +1,6 @@ +{ + "compilerOptions": { + "strict": true + }, + "include": ["./**/*.test.ts"] +} diff --git a/test/types.test.ts b/test/types.test.ts new file mode 100644 index 00000000..b12cdf05 --- /dev/null +++ b/test/types.test.ts @@ -0,0 +1,6 @@ +import * as ybiq from ".."; + +ybiq.init().then(() => console.log("none")); +ybiq.init({ cwd: "lib" }).then(() => console.log("none")); +ybiq.init({ logger: (msg: string) => {} }).then(() => console.log("none")); +ybiq.init({ cwd: "lib", logger: (msg: string) => {} }).then(() => console.log("none")); diff --git a/tsconfig.json b/tsconfig.json index 066e7e2f..d748cc90 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -11,5 +11,5 @@ "forceConsistentCasingInFileNames": true, "resolveJsonModule": true }, - "include": ["lib", "types"] + "include": ["lib/**/*", "types/**/*"] } diff --git a/types/ybiq.d.ts b/types/ybiq.d.ts index 2e488814..e20b93e8 100644 --- a/types/ybiq.d.ts +++ b/types/ybiq.d.ts @@ -1,4 +1,5 @@ -declare module "ybiq" { - type Logger = (msg: string) => any; - type CommandParams = { cwd?: string; logger?: Logger }; -} +export type Logger = (msg: string) => void; +export type CommandParams = { cwd?: string; logger?: Logger }; + +export declare function init(params?: CommandParams): Promise; +type InitCommand = typeof init;