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
8 changes: 5 additions & 3 deletions .github/workflows/test-and-release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,18 @@ jobs:
with:
node-version: ">=24.18.1 <25"
cache: "npm"
registry-url: "https://registry.npmjs.org"

- name: Install NPM version specified in package.json
uses: ./.github/actions/install-npm

- name: Install dependencies
run: npm ci --no-audit

- name: Compile TypeScript
run: npx tsc

- run: npm test

- name: Semantic release
run: |
npm install -D @sebbo2002/semantic-release-jsr
npx semantic-release
run: npx semantic-release
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
node_modules/
npm/
50 changes: 50 additions & 0 deletions .npm/compile.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* Compile source for NPM
*/

import swc from '@swc/core'
import { mkdirSync, rmSync, writeFileSync } from 'node:fs'
import { glob } from 'node:fs/promises'
import path, { dirname } from 'node:path'
import { fileURLToPath } from 'node:url'
import tsconfig from './tsconfig.npm.json' with { type: 'json' }
import { updateImports } from './updateImports.ts'

const __dirname = dirname(fileURLToPath(import.meta.url))

const outDir = path.join(__dirname, '..', 'npm')

try {
rmSync(outDir, { recursive: true, force: true })
} catch {
// pass
}

for await (const file of glob(
tsconfig.include.map((include) => path.join('.npm', include)),
)) {
if (file.endsWith('.spec.ts') || file.endsWith('.test.ts')) continue
let compiled = (
await swc.transformFile(file, {
jsc: {
parser: {
syntax: 'typescript',
},
target: 'es2024',
},
module: {
type: 'es6',
},
})
).code

compiled = updateImports(compiled)

const targetFile = path.join(outDir, file.replace(/\.ts$/, '.js'))

mkdirSync(dirname(targetFile), { recursive: true })

writeFileSync(targetFile, compiled, 'utf8')

console.log(file)
}
11 changes: 11 additions & 0 deletions .npm/tsconfig.npm.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"extends": "../tsconfig.json",
"compilerOptions": {
"emitDeclarationOnly": true,
"declaration": true,
"noEmit": false,
"rootDir": ".."
},
"include": ["../*.ts"],
"exclude": ["../*.spec.ts", "../*.test.ts"]
}
11 changes: 11 additions & 0 deletions .npm/updateImports.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import assert from 'node:assert'
import { describe, it } from 'node:test'
import { updateImports } from './updateImports.ts'

void describe('updateImports', () => {
void it('replaces .ts with .js in relative imports', () => {
const input = `import { foo } from './bar.ts';`
const expected = `import { foo } from './bar.js';`
assert.equal(updateImports(input), expected)
})
})
2 changes: 2 additions & 0 deletions .npm/updateImports.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export const updateImports = (source: string): string =>
source.replace(/from ['"](.+?)\.ts['"]/g, "from '$1.js'")
24 changes: 19 additions & 5 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# Contributing to `@nrfcloud/fetch-with-debug`

Thanks for contributing! This is a published library; new versions are released
to [JSR](https://jsr.io/@nrfcloud/fetch-with-debug) automatically on merge to
`main`.
to [NPM](https://www.npmjs.com/package/@nrfcloud/fetch-with-debug) automatically
on merge to `main`.

## Development setup

Expand All @@ -15,8 +15,22 @@ to [JSR](https://jsr.io/@nrfcloud/fetch-with-debug) automatically on merge to

## Testing

Run `npm test` for the unit tests. They run the `*.spec.ts` files directly with
the Node.js built-in test runner.
1. Run `npx tsc` to type-check the project.
1. Run `npm test` for the unit tests. They run the `*.spec.ts` files directly
with the Node.js built-in test runner.

## Building the NPM package

The package is published as compiled JavaScript with type declarations in the
`npm/` folder, which is created by the `prepublishOnly` hook:

1. [`.npm/compile.ts`](.npm/compile.ts) transpiles the TypeScript sources using
[`@swc/core`](https://www.npmjs.com/package/@swc/core) and rewrites the `.ts`
import specifiers to `.js`.
1. [TypeScript 7](https://www.npmjs.com/package/typescript) emits the type
declarations, using [`.npm/tsconfig.npm.json`](.npm/tsconfig.npm.json).

Run `npm run prepublishOnly` to build it locally.

## Releasing a new version

Expand All @@ -29,7 +43,7 @@ the Node.js built-in test runner.
1. Once approved and CI passes, rebase or squash away!
1. [`semantic-release` in the Test&Release workflow](.github/workflows/test-and-release.yaml)
takes care of creating a new GitHub release and publishing the package to
[JSR](https://jsr.io/@nrfcloud/fetch-with-debug).
[NPM](https://www.npmjs.com/package/@nrfcloud/fetch-with-debug).

Once the new version is published, consumers can bump their dependency on
`@nrfcloud/fetch-with-debug` to pick it up.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
# `@nrfcloud/fetch-with-debug`

<https://jsr.io/@nrfcloud/fetch-with-debug>
<https://www.npmjs.com/package/@nrfcloud/fetch-with-debug>

Simple wrapper around fetch that logs request and response.

## Install with NPM

```bash
npx jsr add (--save-prod|--save-dev) @nrfcloud/fetch-with-debug
npm i (--save-prod|--save-dev) @nrfcloud/fetch-with-debug
```

## Usage
Expand Down
7 changes: 0 additions & 7 deletions jsr.json

This file was deleted.

Loading