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: 2 additions & 3 deletions .github/workflows/test-and-release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ 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
Expand All @@ -33,6 +34,4 @@ jobs:
- 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'")
17 changes: 15 additions & 2 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

`@nrfcloud/lambda-helpers` is a published library: it provides helper functions
for AWS Lambda functions used in nRF Cloud and is published to
[JSR](https://jsr.io/@nrfcloud/lambda-helpers).
[NPM](https://www.npmjs.com/package/@nrfcloud/lambda-helpers).

## Development setup

Expand All @@ -18,6 +18,19 @@ for AWS Lambda functions used in nRF Cloud and is published to
1. Run `npx tsc` to type-check the project.
1. Run `npm test` to run the unit tests.

## 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

1. Finally, create a commit that packages up all the changes.
Expand All @@ -29,7 +42,7 @@ for AWS Lambda functions used in nRF Cloud and is published to
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/lambda-helpers).
[NPM](https://www.npmjs.com/package/@nrfcloud/lambda-helpers).

Now that you have a new version of `@nrfcloud/lambda-helpers` published, you can
bump the dependency in the projects that consume it.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
# `@nrfcloud/lambda-helpers`

<https://jsr.io/@nrfcloud/lambda-helpers>
<https://www.npmjs.com/package/@nrfcloud/lambda-helpers>

Helper functions for AWS Lambda functions used in nRF Cloud.

## Install with NPM

```bash
npx jsr add (--save-prod|--save-dev) @nrfcloud/lambda-helpers
npm i (--save-prod|--save-dev) @nrfcloud/lambda-helpers
```

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

This file was deleted.

Loading