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
7 changes: 7 additions & 0 deletions .changeset/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Changesets

Each Markdown file in this folder describes one change for users of the package. `npx changeset` writes one; the
release pull request turns them into the next version and its `CHANGELOG.md` entry and deletes them.

How to choose between `minor` and `patch`, and how a release is made, is in
[CONTRIBUTING.md](../CONTRIBUTING.md#changesets).
7 changes: 7 additions & 0 deletions .changeset/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"$schema": "https://unpkg.com/@changesets/config@4/schema.json",
"changelog": ["@changesets/changelog-github", { "repo": "Huldoser/code-inventory", "disableThanks": true }],
"commit": false,
"access": "public",
"baseBranch": "main"
}
4 changes: 4 additions & 0 deletions .github/ISSUE_TEMPLATE/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
contact_links:
- name: Report a vulnerability
url: https://github.com/Huldoser/code-inventory/security/advisories/new
about: Privately, as SECURITY.md describes, not in a public issue.
50 changes: 50 additions & 0 deletions .github/ISSUE_TEMPLATE/scan-result-wrong.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
name: The scan reports something wrong
description: A reference, call, caller, entry path or changed declaration is missing, extra or in the wrong place.
labels: [bug]
body:
- type: markdown
attributes:
value: >-
A small project that shows the problem is the most useful part of a report. Rename what you can't share, but
keep the imports, exports and calls that lead to the wrong result.
- type: textarea
id: source
attributes:
label: Source files
description: >-
The smallest set of files that shows the problem, each with its path, and the tsconfig.json or package.json
that the scan reads, if it matters.
render: ts
validations:
required: true
- type: input
id: command
attributes:
label: Command
description: The command you ran, or the library call and its options.
placeholder: npx code-inventory . --symbol normalize --format json
validations:
required: true
- type: textarea
id: output
attributes:
label: What the scan reports
description: The part of the output that is wrong, and any diagnostics the scan printed.
render: json
validations:
required: true
- type: textarea
id: expected
attributes:
label: What you expected
description: The reference, call or path you expected, and why, for example the line that calls the declaration.
validations:
required: true
- type: input
id: versions
attributes:
label: Versions
description: Of code-inventory and Node.js.
placeholder: code-inventory 0.4.0, Node.js 26
validations:
required: true
10 changes: 10 additions & 0 deletions .github/pull_request_template.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
## What changed

<!-- What the change does and why. Link the issue it closes. -->

## Checklist

- [ ] `npm run check` passes, and tests cover the change.
- [ ] The docs describe the change: the README, or the guide or reference pages in `docs/`.
- [ ] No docs change is needed.
- [ ] A changeset describes the change for users (`npx changeset`), or the change does not affect them.
43 changes: 43 additions & 0 deletions AGENTS.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# AGENTS.md

Notes for coding agents working in this repository. People should read CONTRIBUTING.md.

## Commands

- `npm ci --ignore-scripts --no-audit` to install, with the Node.js version in `.node-version`.
- `npm run check` runs every project check: typecheck, build, lint, tests and formatting. It must pass.
- `npm test` alone needs a fresh `npm run build`: the CLI tests run `dist/cli.js`.
- There is no format script; rewrite a file with `npx prettier --write <file>` and review the diff.

## Layout

- `src/index.ts`: the public library API. `src/cli.ts`: the command line, a thin layer over the library.
- `src/model.ts`: the inventory format. `docs/report-schema.json` is its JSON Schema and ships with the package.
- `src/compiler.ts`: builds the inventory with the TypeScript compiler API. `src/projects.ts` and
`src/discovery.ts`: TypeScript projects and source files.
- `src/query.ts`, `src/reachability.ts`, `src/change-impact.ts`: usage queries, entry paths and the `--changed-since`
report. `src/package-entries.ts` and `src/next-routes.ts`: entry points from manifests, workspaces and Next.js routes.
- `test/fixtures/`: projects the tests scan; never run, linted or formatted. `test/fixtures/saved-reports/`: reports
written by published versions, which the current library must still read.
- `docs/`: the static documentation website (HTML and CSS, relative links), published with the package.

## Conventions that are easy to get wrong

- Scanning reads files and never runs the scanned project's code. Only `--changed-since` runs a command, `git`.
- The CLI never overwrites a file: `--output` must name a new path.
- Output must be deterministic: no timestamps or absolute paths, paths relative to the scanned root with `/`, and
stable sorting.
- Reports are `schemaVersion` 1. Readers follow the compatibility rules in `docs/reference.html#compatibility`; a
change that breaks them is a breaking change, and a JSON shape change also updates `docs/report-schema.json`.
- Code problems in the scanned project become diagnostics and make the result `partial`; `--strict` turns that into
exit code 2. Don't throw for them; errors are for invalid options and inputs the scan can't use at all.
- Relative imports in `src/` end in `.ts`; the build rewrites them. Type-only imports use `import type`.
- `no-console` is on; the CLI writes with `process.stdout.write`.
- The only runtime dependency is the TypeScript 6 compiler under the `typescript` alias. Every dependency is pinned to
an exact version. Ask before adding one, and list it in `THIRD_PARTY.md`.
- A change users can see updates `README.md` and the matching page in `docs/` in the same pull request.
- Changesets: a change that affects users carries one (`npx changeset`). Breaking changes are `minor` before 1.0;
never `major`.
- Releases follow CONTRIBUTING.md, Releases: a release pull request made with `npx changeset version`, then the manual
"Publish to npm" workflow. Prepare or publish a release only when a maintainer asks for it, and if a step fails,
stop and report rather than publishing by hand.
53 changes: 53 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# code-inventory

## 0.4.0

### Minor Changes

- [#20](https://github.com/Huldoser/code-inventory/pull/20) - Trace the tests affected by a change. With `--changed-since` and `--format json`, the change-impact report is versioned and lists direct references, notes on the diff, and one shortest chain of call sites from each candidate test to each changed declaration. The library exports it as `changeImpact`.

- [#20](https://github.com/Huldoser/code-inventory/pull/20) - Add `--workspace-entries`, which traces the explicit package entries of npm workspaces and reports the manifest that selected each file, and `--next-routes`, which traces the HTTP handlers exported by Next.js route files. The library exports them as `workspaceEntries` and `nextRouteEntries`.

## 0.3.3

### Patch Changes

- [#19](https://github.com/Huldoser/code-inventory/pull/19) - Fix two wrong paths in entry reports: a callback parameter named `require` no longer loads a module, and inline type-only imports that the compiler erases no longer trace module initialization.

## 0.3.2

### Patch Changes

- [#17](https://github.com/Huldoser/code-inventory/pull/17) - No change to the CLI or the library. Reports saved by 0.2.1 and 0.3.0 are now checked against the current schema and replayed with the current library in every test run.

## 0.3.1

### Patch Changes

- [#15](https://github.com/Huldoser/code-inventory/pull/15) - The package includes the compatibility reference for saved inventories, symbol queries and entry-path reports, and documents the CLI exit codes for successful, failed and strict partial scans.

## 0.3.0

### Minor Changes

- [#14](https://github.com/Huldoser/code-inventory/pull/14) - Add `--changed-since <revision>`. In a Git checkout it lists the declarations that tracked changes touch, their exports, and their references grouped by caller or test file, with source locations for calls and callback reads. Deleted files and removed base lines appear separately. The query uses the current checkout and accepts the summary format.

## 0.2.1

### Patch Changes

- [#13](https://github.com/Huldoser/code-inventory/pull/13) - Usage and entry-point reports follow explicit relative JavaScript imports to their implementation when a matching TypeScript declaration file is present. This recovers calls through namespace exports, including calls in tests. TypeScript diagnostics still use the declared types, and 0.2.0 snapshots remain readable.

## 0.2.0

### Minor Changes

- [#11](https://github.com/Huldoser/code-inventory/pull/11) - Trace paths from package entry points with `--package-entries`. The CLI reads explicit `exports`, `main` and `bin` targets and, for compiled packages, maps output files back to source through the selected TypeScript configuration. The library exports `packageEntries` and `entryPath`, the reference includes a JSON Schema for inventory, symbol and entry reports, and the scan summary groups diagnostics by cause.

- [#12](https://github.com/Huldoser/code-inventory/pull/12) - Resolve wildcard package exports, honoring private and remapped subpaths. Select entry files with `--entry-glob` and model an external call with `--entry-symbol`: a file entry traces module initialization, and a selected handler traces the calls from that declaration. Saved reports record the analyzer version of the installed package.

## 0.1.0

### Minor Changes

- [#8](https://github.com/Huldoser/code-inventory/pull/8) - First release on npm. Inventory the declarations of a JavaScript or TypeScript project and inspect their references, direct calls, construction sites and distinct callers. Select TypeScript projects and project references, exclude files, and label production, test and tooling code. Trace paths from explicit source entry points, and write terminal, Markdown or versioned JSON reports. Scanning reads the project without running its code.
115 changes: 115 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
# Contributing

Thanks for helping. Bug reports with a short source file that shows the problem are the most useful contribution.

## Setup

Use the Node.js version in [.node-version](.node-version), which CI uses too, then:

```sh
npm ci --ignore-scripts --no-audit
npm run check
```

No dependency needs an install script, so installs skip them.

| Command | Does |
| -------------------------------------------------------- | ---------------------------------------------------------------------------------------- |
| `npm run check` | Everything CI checks in the project: typecheck, build, lint, tests and formatting. |
| `npm run typecheck` | Type checks `src/`, the tests and the Vitest config. |
| `npm run build` | Compiles `src/` to `dist/`. |
| `npm run lint` | Checks ESLint rules on `src/` and the tests. |
| `npm test` | Runs the Vitest suite. The CLI tests run `dist/cli.js`, so build first. |
| `npm run format:check` | Checks formatting without rewriting files. `npx prettier --write <files>` rewrites them. |
| `npm pack --dry-run --ignore-scripts` | Lists the files the package would publish, as CI does. |
| `osv-scanner scan source --offline -L package-lock.json` | Checks the lockfile for known vulnerabilities, if you have osv-scanner installed. |
| `npx changeset` | Writes a changeset for your change. |

CI also checks the workflows in `.github/workflows/` with actionlint and zizmor.

## How the code is laid out

- `src/index.ts` is the library's public API and `src/cli.ts` the command line. `src/model.ts` defines the inventory
format; `docs/report-schema.json` is its JSON Schema and is published with the package.
- `src/discovery.ts` finds source files, `src/projects.ts` loads TypeScript projects, and `src/compiler.ts` builds the
inventory with the TypeScript compiler API.
- `src/query.ts` answers usage queries and `src/reachability.ts` traces paths from entry points.
`src/package-entries.ts` and `src/next-routes.ts` select entry points from package manifests, npm workspaces and
Next.js route files. `src/change-impact.ts` reports the declarations a Git diff touches.
- `src/report.ts` writes the summary and Markdown reports.
- `test/*.test.ts` are the tests. `test/fixtures/` holds the projects they scan; the scanner reads them and never runs
them. `test/fixtures/saved-reports/` holds reports written by published versions, which the current library must
still read.
- `docs/` is the documentation website: static HTML and CSS with relative links, published with the package.

## Tests

- Name tests by the behaviour they check. A project that a test scans belongs in `test/fixtures/`; a test that needs
to change files, such as a Git history, builds them in a temporary directory.
- A change to the report format keeps the saved reports in `test/fixtures/saved-reports/` readable, as the
[compatibility rules](docs/reference.html#compatibility) describe, and keeps every report valid against
`docs/report-schema.json`.

## Docs

A change that users can see updates the docs with it: `README.md`, and the guide or reference page in `docs/` that
describes the behaviour. Search both for the old behaviour, such as an option, a default, a diagnostic code or a
version, and update each place, not only one. The docs describe the code on `main`.

## Changesets

Every change that affects users of the package carries a changeset. Run `npx changeset`, select `code-inventory`, and
describe the change in a sentence or two, for users rather than for reviewers. Changes to tests, CI or development
dependencies don't need one.

Before 1.0:

- A breaking change is `minor`. Never choose `major`: it would release 1.0.0. The
[compatibility rules](docs/reference.html#compatibility) say which changes to reports and exit codes are breaking.
- A new feature is `minor`.
- A fix is `patch`.

## Releases

Releases are made from `main`. Changesets collect there until a maintainer releases them; merging a pull request
publishes nothing.

1. Read each pending changeset in `.changeset/` and check that the docs describe it. Fix what's missing in a pull
request first.
2. From an up-to-date `main`, make a branch such as `release-0.5.0` and run
`GITHUB_TOKEN=$(gh auth token) npx changeset version`. It raises the version in `package.json`, writes the
`CHANGELOG.md` entry with a link to each pull request, and deletes the changesets it used. The token is set for this
one command and stored nowhere.
3. Run `npm install --package-lock-only --ignore-scripts --no-audit` so that `package-lock.json` has the new version
too, then `npm run check`.
4. Commit as "Release x.y.z", push, and open a pull request. Review the `CHANGELOG.md` entry and the version, and
squash merge it when the checks pass.
5. Run the "Publish to npm" workflow from the Actions tab on `main`, with the bootstrap box left unticked. It builds,
checks and packs the package, then publishes that tarball from the `npm` environment, which only `main` may use and
which waits for a maintainer's approval. npm's trusted publisher for the package names this repository,
`release.yml` and the `npm` environment, so no npm token is involved.
6. Check that npm lists the version with `npm view code-inventory@x.y.z --prefer-online`; it can take a few minutes.
Then create the `vx.y.z` tag and the GitHub release on the release commit, with the version's `CHANGELOG.md` entry
as its notes.

### When a release goes wrong

- **The workflow fails before publishing.** Nothing reached npm. Fix the problem in a pull request and run the workflow
again.
- **A release is broken.** Deprecate it first, so that new installs warn, with a message that says what is wrong and
which version to use: `npm deprecate code-inventory@x.y.z "<message>"`. Then release the fix as a patch.
- **Unpublishing** is only possible for 72 hours after a publish, and a version number can never be used again, even
after it is unpublished. Prefer deprecating.

## Dependencies

Every dependency in `package.json` is pinned to an exact version, so a version changes only in a pull request. Update
them after reading their changelogs, with `npm install --save-exact --ignore-scripts`, and run `npm audit`. The only runtime
dependency is the TypeScript 6 compiler, installed as an npm alias; ask before adding another one.

When a dependency is added, add it to [THIRD_PARTY.md](THIRD_PARTY.md) with its license.

## Pull requests

Keep a pull request to one change and fill in the template. Pull requests are squash merged, and the title becomes the
commit message, so write it as a sentence that says what the change does.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,6 @@ osv-scanner scan source --offline -L package-lock.json

The project check type-checks source and tests, builds the CLI and library, runs ESLint and Vitest, and checks formatting. CI also validates its workflows with actionlint and zizmor.

[CONTRIBUTING.md](CONTRIBUTING.md) explains the layout, changesets and releases. Changes in each version are listed in [CHANGELOG.md](CHANGELOG.md). Report vulnerabilities privately as [SECURITY.md](SECURITY.md) describes.

[MIT license](LICENSE) · [Dependency licenses](THIRD_PARTY.md)
21 changes: 21 additions & 0 deletions SECURITY.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Security policy

## Supported versions

Security fixes go into the latest release on npm. Before 1.0 there are no maintenance branches, so a fix comes out as
a patch of the latest minor version.

## Reporting a vulnerability

Report it privately through
[GitHub's private vulnerability reporting](https://github.com/Huldoser/code-inventory/security/advisories/new), not
in a public issue. Say which version is affected and how to reproduce it; a small project that triggers it helps most.

code-inventory reads the source and configuration files it scans and never runs them; `--changed-since` only runs
`git` to read the diff. A scanned project that makes a scan run its code or another command, write a file other than
the requested output, overwrite an existing file, or reach the network is a vulnerability, and so is a problem in a
dependency that a scan exposes. A wrong count, path or diagnostic is a bug; report it with the
[issue form](https://github.com/Huldoser/code-inventory/issues/new?template=scan-result-wrong.yml) instead.

You get a first reply within 14 days. Once the problem is confirmed, the fix is released as soon as it is ready, with a
GitHub security advisory that credits you unless you prefer not to be named.
1 change: 1 addition & 0 deletions THIRD_PARTY.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
| vitest and its workspace packages | MIT | https://github.com/vitest-dev/vitest |
| prettier | MIT | https://github.com/prettier/prettier |
| eslint and typescript-eslint | MIT | https://github.com/eslint/eslint; https://github.com/typescript-eslint/typescript-eslint |
| @changesets/cli and @changesets/changelog-github | MIT | https://github.com/changesets/changesets |

The lockfile records exact versions, integrity hashes and licenses for transitive dependencies. Installation uses
`--ignore-scripts`. The optional macOS-only fsevents package declares an install script and is not installed on Linux.
Expand Down
Loading