Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/ts-package-scripts.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'markdown-magic-package-scripts': minor
---

TypeScript: ship type declarations
23 changes: 13 additions & 10 deletions packages/package-scripts/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ yarn add -D markdown-magic markdown-magic-package-scripts

## Adding the plugin

See `example.js` for usage.
See `example.ts` for usage.

<!-- AUTO-GENERATED-CONTENT:START (CODE:src=./example.js) -->
<!-- AUTO-GENERATED-CONTENT:START (CODE:src=./example.ts) -->

```js
```ts
import path from 'path';
import { markdownMagic } from 'markdown-magic';
import SCRIPTS from './index.js';
import SCRIPTS from './index.ts';

const config = {
matchWord: 'AUTO-GENERATED-CONTENT',
Expand All @@ -36,11 +36,14 @@ await markdownMagic(markdownPath, config);

<!-- AUTO-GENERATED-CONTENT:START (SCRIPTS) -->

| Script | Description |
| -------- | -------------------------------- |
| `docs` | generate docs |
| `empty` | `echo "this is just an example"` |
| `format` | format code |
| `test` | `vitest run` |
| Script | Description |
| ---------- | ----------------------------------- |
| `prebuild` | `rm -rf dist` |
| `build` | `tsc --project tsconfig.build.json` |
| `docs` | generate docs |
| `empty` | `echo "this is just an example"` |
| `format` | format code |
| `prepack` | `pnpm build` |
| `test` | `vitest run` |

<!-- AUTO-GENERATED-CONTENT:END -->
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@
exports[`markdown-magic-package-scripts > renders a table of scripts from the nearest package.json 1`] = `
"| Script | Description |
|--------|-------------|
| \`docs\` | \`node example.js && prettier --write README.md\` |
| \`prebuild\` | \`rm -rf dist\` |
| \`build\` | \`tsc --project tsconfig.build.json\` |
| \`docs\` | \`node example.ts && prettier --write README.md\` |
| \`empty\` | \`echo "this is just an example"\` |
| \`format\` | \`prettier --write .\` |
| \`prepack\` | \`pnpm build\` |
| \`test\` | \`vitest run\` |"
`;
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import path from 'path';
import { markdownMagic } from 'markdown-magic';
import SCRIPTS from './index.js';
import SCRIPTS from './index.ts';

const config = {
matchWord: 'AUTO-GENERATED-CONTENT',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
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');

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@ import { readFileSync } from 'fs';
import path from 'path';
import { findUpSync } from 'find-up';
import sortScripts from 'sort-scripts';
import type { TransformArgs } from './types.ts';

function findPkg(dir) {
export type { TransformArgs, TransformOptions } from './types.ts';

function findPkg(dir: string): string {
const pkgPath = findUpSync('package.json', { cwd: dir });

if (!pkgPath) {
Expand All @@ -13,8 +16,12 @@ function findPkg(dir) {
return pkgPath;
}

export default function SCRIPTS({ content, options = {}, srcPath }) {
let pkgPath;
export default function SCRIPTS({
content,
options = {},
srcPath,
}: TransformArgs): string {
let pkgPath: string;

if (options && options.pkg) {
pkgPath = path.resolve(path.dirname(srcPath), options.pkg);
Expand All @@ -32,18 +39,23 @@ export default function SCRIPTS({ content, options = {}, srcPath }) {
.map((s) => s.trim());
const headerIndex = rows.findIndex((row) => row.startsWith('|-'));

const details = (headerIndex !== -1 ? rows.slice(headerIndex + 1) : rows)
const details: Record<string, string | undefined> = (
headerIndex !== -1 ? rows.slice(headerIndex + 1) : rows
)
.map((row) =>
row
.split('|')
.map((s) => s.trim())
.filter((s) => !!s),
)
.filter(([script]) => !!script)
.reduce((obj, [script, description]) => {
obj[script.replace(/`/g, '')] = description;
return obj;
}, {});
.reduce(
(obj: Record<string, string | undefined>, [script, description]) => {
obj[script.replace(/`/g, '')] = description;
return obj;
},
{},
);

return ['| Script | Description |', '|--------|-------------|']
.concat(
Expand Down
14 changes: 10 additions & 4 deletions packages/package-scripts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,21 @@
"version": "2.0.0",
"description": "Print list of scripts in package.json with descriptions",
"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",
"empty": "echo \"this is just an example\"",
"format": "prettier --write ."
},
Expand All @@ -40,15 +44,17 @@
"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",
"bugs": {
"url": "https://github.com/camacho/markdown-magic-plugins/issues"
},
"files": [
"index.js"
"dist"
]
}
10 changes: 10 additions & 0 deletions packages/package-scripts/sort-scripts-shim.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// Ambient shim for `sort-scripts`. Upstream ships no type declarations and
// no `@types/sort-scripts` package exists — smallest correct local
// declaration for the single default export actually used here (verified
// against node_modules/sort-scripts@1.0.1/index.js: takes a scripts map,
// returns `[name, script]` pairs sorted with pre/post ordering).
declare module 'sort-scripts' {
export default function sortScripts(
scripts: Record<string, string>,
): Array<[string, string]>;
}
18 changes: 18 additions & 0 deletions packages/package-scripts/tsconfig.build.json
Original file line number Diff line number Diff line change
@@ -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"]
}
14 changes: 14 additions & 0 deletions packages/package-scripts/types.ts
Original file line number Diff line number Diff line change
@@ -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 `pkg` option.
export interface TransformOptions {
pkg?: string;
[key: string]: unknown;
}

export interface TransformArgs {
content: string;
options: TransformOptions;
srcPath: string;
}
6 changes: 6 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading