diff --git a/.changeset/ts-last-modified.md b/.changeset/ts-last-modified.md new file mode 100644 index 0000000..82a4091 --- /dev/null +++ b/.changeset/ts-last-modified.md @@ -0,0 +1,5 @@ +--- +'markdown-magic-last-modified': minor +--- + +TypeScript: ship type declarations diff --git a/packages/last-modified/README.md b/packages/last-modified/README.md index b605c8a..d3d6f10 100644 --- a/packages/last-modified/README.md +++ b/packages/last-modified/README.md @@ -10,14 +10,14 @@ npm i markdown-magic markdown-magic-last-modified --save-dev ## Adding the plugin -See `example.js` for usage. +See `example.ts` for usage. - + -```js +```ts import path from 'path'; import { markdownMagic } from 'markdown-magic'; -import LASTMODIFIED from './index.js'; +import LASTMODIFIED from './index.ts'; const config = { matchWord: 'AUTO-GENERATED-CONTENT', diff --git a/packages/last-modified/example.js b/packages/last-modified/example.ts similarity index 87% rename from packages/last-modified/example.js rename to packages/last-modified/example.ts index 86e130a..2c7ad01 100644 --- a/packages/last-modified/example.js +++ b/packages/last-modified/example.ts @@ -1,6 +1,6 @@ import path from 'path'; import { markdownMagic } from 'markdown-magic'; -import LASTMODIFIED from './index.js'; +import LASTMODIFIED from './index.ts'; const config = { matchWord: 'AUTO-GENERATED-CONTENT', diff --git a/packages/last-modified/index.spec.js b/packages/last-modified/index.spec.ts similarity index 96% rename from packages/last-modified/index.spec.js rename to packages/last-modified/index.spec.ts index 7d41ab2..46bd709 100644 --- a/packages/last-modified/index.spec.js +++ b/packages/last-modified/index.spec.ts @@ -1,7 +1,7 @@ import { mkdirSync, rmSync, writeFileSync } from 'fs'; import path from 'path'; import { describe, expect, it } from 'vitest'; -import format from './index.js'; +import format from './index.ts'; const srcPath = path.join(import.meta.dirname, 'README.md'); diff --git a/packages/last-modified/index.js b/packages/last-modified/index.ts similarity index 71% rename from packages/last-modified/index.js rename to packages/last-modified/index.ts index 297d6f9..92b698d 100644 --- a/packages/last-modified/index.js +++ b/packages/last-modified/index.ts @@ -1,8 +1,15 @@ -import path from 'path'; import { execFileSync } from 'child_process'; +import path from 'path'; +import type { TransformArgs } from './types.ts'; + +export type { TransformArgs, TransformOptions } from './types.ts'; -export default function LASTMODIFIED({ content, options = {}, srcPath }) { - let filePath; +export default function LASTMODIFIED({ + content, + options = {}, + srcPath, +}: TransformArgs): string { + let filePath: string; if (options.file) { filePath = path.resolve(path.dirname(srcPath), options.file); diff --git a/packages/last-modified/package.json b/packages/last-modified/package.json index 5750a9f..cbeaba3 100644 --- a/packages/last-modified/package.json +++ b/packages/last-modified/package.json @@ -3,17 +3,21 @@ "version": "2.0.0", "description": "Print the last modified date using git for a given file", "type": "module", - "main": "index.js", + "main": "dist/index.js", + "types": "dist/index.d.ts", "exports": { - ".": "./index.js", + ".": "./dist/index.js", "./package.json": "./package.json" }, "engines": { "node": ">=22.18.0" }, "scripts": { + "prebuild": "rm -rf dist", + "build": "tsc --project tsconfig.build.json", + "prepack": "pnpm build", "test": "vitest run", - "docs": "node example.js && prettier --write README.md", + "docs": "node example.ts && prettier --write README.md", "format": "prettier --write ." }, "license": "MIT", @@ -32,8 +36,10 @@ "markdown-magic": "^4" }, "devDependencies": { + "@types/node": "^24.13.2", "markdown-magic": "catalog:", "prettier": "catalog:", + "typescript": "^6.0.3", "vitest": "catalog:" }, "homepage": "https://github.com/camacho/markdown-magic-plugins#readme", @@ -41,6 +47,6 @@ "url": "https://github.com/camacho/markdown-magic-plugins/issues" }, "files": [ - "index.js" + "dist" ] } diff --git a/packages/last-modified/tsconfig.build.json b/packages/last-modified/tsconfig.build.json new file mode 100644 index 0000000..d7ec333 --- /dev/null +++ b/packages/last-modified/tsconfig.build.json @@ -0,0 +1,18 @@ +{ + "extends": "../../tsconfig.json", + "compilerOptions": { + "noEmit": false, + "outDir": "dist", + "rootDir": ".", + "types": ["node"] + }, + "exclude": [ + "node_modules", + "dist", + "*.spec.ts", + "example.ts", + "__fixtures__", + "__snapshots__" + ], + "include": ["*.ts"] +} diff --git a/packages/last-modified/types.ts b/packages/last-modified/types.ts new file mode 100644 index 0000000..64c1458 --- /dev/null +++ b/packages/last-modified/types.ts @@ -0,0 +1,14 @@ +// Transform interface shared by markdown-magic plugins. Adapted from +// format-package's scripts/markdown-transformers.ts TransformArgs/TransformOptions +// (content: unknown -> content: string, the plugins' actual contract), plus +// this package's own `file` option. +export interface TransformOptions { + file?: string; + [key: string]: unknown; +} + +export interface TransformArgs { + content: string; + options: TransformOptions; + srcPath: string; +} diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 12eb245..181f5c5 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -160,12 +160,18 @@ importers: packages/last-modified: devDependencies: + '@types/node': + specifier: ^24.13.2 + version: 24.13.2 markdown-magic: specifier: 'catalog:' version: 4.10.5 prettier: specifier: 'catalog:' version: 3.9.1 + typescript: + specifier: ^6.0.3 + version: 6.0.3 vitest: specifier: 'catalog:' version: 4.1.9(@types/node@24.13.2)(vite@8.1.0(@types/node@24.13.2))