Skip to content

Commit

Permalink
chore: Upgrade dev dependencies, add changesets (#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
manzt committed Aug 11, 2023
1 parent a40985b commit 374f273
Show file tree
Hide file tree
Showing 17 changed files with 286 additions and 234 deletions.
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@2.3.1/schema.json",
"changelog": "@changesets/cli/changelog",
"commit": false,
"access": "public",
"baseBranch": "main"
}
37 changes: 37 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
name: CI

on:
push:
branches: main
pull_request:
workflow_dispatch:

concurrency:
group: test-${{ github.head_ref }}
cancel-in-progress: true

jobs:

Test:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3
- uses: pnpm/action-setup@v2
with:
version: 8
- run: |
pnpm install
pnpm test
Lint:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3
- uses: pnpm/action-setup@v2
with:
version: 8
- run: |
pnpm install
pnpm lint
27 changes: 27 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
name: Release

on:
push:
branches:
- main

concurrency: ${{ github.workflow }}-${{ github.ref }}

jobs:
release:
name: Release
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: 20.x

- run: pnpm install

- name: Create Release Pull Request or Publish to npm
id: changesets
uses: changesets/action@v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
23 changes: 0 additions & 23 deletions .github/workflows/test.yml

This file was deleted.

3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
node_modules
types
package-lock.json
package-lock.json
pnpm-lock.yaml
18 changes: 5 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ ReferenceStore.fromJSON(await res.json(), { target: 'http://localhost:8080/data.

This package is written as a pure ES Module and is intended for use in various JavaScript
runtimes. It can be imported from a CDN via URL directly, or installed as a dependency
from `npm` for use in Node.js or via a bundler.
from `pnpm` for use in Node.js or via a bundler.

```javascript
// Browser or Deno via a CDN
Expand All @@ -124,23 +124,15 @@ I welcome any input, feedback, bug reports, and contributions. This project requ

```bash
cd reference-spec-reader
npm install
pnpm install
```

### Running tests

```bash
npm test # runs unit tests only
npm run test:ci # runs CI test suite (type-checking, linting, and unit tests)
pnpm test # runs unit tests only
pnpm run test:ci # runs CI test suite (type-checking, linting, and unit tests)
```

> NOTE: If making a PR, please run `npm run format` to auto-format your changes. The linting
> NOTE: If making a PR, please run `pnpm fmt` to auto-format your changes. The linting
> check will fail for unformatted code.
### Publishing to `npm`

```bash
npm version [<new version> | major | minor | patch]
npm publish --dry-run # make sure correct files are included in tarball
npm publish
```
46 changes: 18 additions & 28 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,41 +1,31 @@
{
"name": "reference-spec-reader",
"version": "0.1.3",
"description": "",
"main": "src/index.js",
"author": "manzt <trevor.j.manz@gmail.com>",
"license": "MIT",
"type": "module",
"types": "types/index.d.ts",
"files": [
"src",
"types",
"types.d.ts"
"dist"
],
"exports": "./src/index.js",
"engines": {
"node": "^12.20.0 || ^14.13.1 || >=16.0.0"
},
"devDependencies": {
"nunjucks": "^3.2.3",
"prettier": "^2.3.2",
"typescript": "^4.3.5",
"uvu": "^0.5.1"
},
"scripts": {
"test:ci": "npm run lint && tsc && npm test",
"test": "uvu test",
"prepare": "tsc",
"format": "prettier --write src/**/*.js test/**/*.js",
"lint": "prettier --check src/**/*.js test/**/*.js"
"prepare": "tsc --emitDeclarationOnly",
"typecheck": "tsc --noEmit",
"fmt": "prettier --use-tabs --write src/**/* test/**/*",
"lint": "prettier --use-tabs --check src/**/* test/**/*"
},
"keywords": [],
"author": "manzt <trevor.j.manz@gmail.com>",
"license": "MIT",
"prettier": {
"singleQuote": true,
"printWidth": 120,
"arrowParens": "avoid",
"trailingComma": "all",
"useTabs": true,
"tabWidth": 4
"exports": {
".": {
"types": "./types/index.d.ts",
"import": "./src/index.js"
}
},
"devDependencies": {
"nunjucks": "^3.2.4",
"prettier": "^3.0.1",
"typescript": "^5.1.6",
"uvu": "^0.5.6"
}
}
4 changes: 2 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export { parse } from './parse.js';
export { ReferenceStore } from './store.js';
export { parse } from "./parse.js";
export { ReferenceStore } from "./store.js";
51 changes: 25 additions & 26 deletions src/parse.js
Original file line number Diff line number Diff line change
@@ -1,38 +1,35 @@
// @ts-check
/// <reference lib="esnext" />

import { render } from './render.js';
import { render } from "./render.js";

/**
* @param {import('../types').ReferencesV0 | import('../types').ReferencesV1} spec
* @param {import('../types').RenderFn=} renderString
* @param {import('./types.js').ReferencesV0 | import('./types.js').ReferencesV1} spec
* @param {import('./types.js').RenderFn=} renderString
*/
export function parse(spec, renderString = render) {
// @ts-ignore
return 'version' in spec ? parseV1(spec, renderString) : parseV0(spec);
return "version" in spec ? parseV1(spec, renderString) : parseV0(spec);
}

/**
* @param {import('../types').ReferencesV0} spec
* @returns {Map<string, import('../types').Ref>}
* @param {import('./types.js').ReferencesV0} spec
* @returns {Map<string, import('./types.js').Ref>}
*/
function parseV0(spec) {
return new Map(Object.entries(spec));
}

/**
* @param {import('../types').ReferencesV1} spec
* @param {import('../types').RenderFn} renderString
* @returns {Map<string, import('../types').Ref>}
* @param {import('./types.js').ReferencesV1} spec
* @param {import('./types.js').RenderFn} renderString
* @returns {Map<string, import('./types.js').Ref>}
*/
function parseV1(spec, renderString) {
/** @type {import('../types').RenderContext} */
/** @type {import('./types.js').RenderContext} */
const context = {};
for (const [key, template] of Object.entries(spec.templates ?? {})) {
// TODO: better check for whether a template or not
if (template.includes('{{')) {
if (template.includes("{{")) {
// Need to register filter in environment
context[key] = ctx => renderString(template, ctx);
context[key] = (ctx) => renderString(template, ctx);
} else {
context[key] = template;
}
Expand All @@ -43,14 +40,14 @@ function parseV1(spec, renderString) {
return renderString(t, { ...context, ...o });
};

/** @type {Map<string, import('../types').Ref>} */
/** @type {Map<string, import('./types.js').Ref>} */
const refs = new Map();

for (const [key, ref] of Object.entries(spec.refs ?? {})) {
if (typeof ref === 'string') {
if (typeof ref === "string") {
refs.set(key, ref);
} else {
const url = ref[0]?.includes('{{') ? render(ref[0]) : ref[0];
const url = ref[0]?.includes("{{") ? render(ref[0]) : ref[0];
refs.set(key, ref.length === 1 ? [url] : [url, ref[1], ref[2]]);
}
}
Expand All @@ -59,7 +56,7 @@ function parseV1(spec, renderString) {
for (const dims of iterDims(g.dimensions)) {
const key = render(g.key, dims);
const url = render(g.url, dims);
if ('offset' in g && 'length' in g) {
if ("offset" in g && "length" in g) {
// [url, offset, length]
const offset = render(g.offset, dims);
const length = render(g.length, dims);
Expand All @@ -75,12 +72,14 @@ function parseV1(spec, renderString) {
}

/**
* @param {Record<string, import('../types').Range | number[]>} dimensions
* @param {Record<string, import('./types.js').Range | number[]>} dimensions
* @returns {Generator<Record<string, number>>}
*/
function* iterDims(dimensions) {
const keys = Object.keys(dimensions);
const iterables = Object.values(dimensions).map(i => (Array.isArray(i) ? i : [...range(i)]));
const iterables = Object.values(dimensions).map((i) =>
Array.isArray(i) ? i : [...range(i)],
);
for (const values of product(...iterables)) {
yield Object.fromEntries(keys.map((key, i) => [key, values[i]]));
}
Expand All @@ -92,10 +91,10 @@ function* product(...iterables) {
return;
}
// make a list of iterators from the iterables
const iterators = iterables.map(it => it[Symbol.iterator]());
const results = iterators.map(it => it.next());
if (results.some(r => r.done)) {
throw new Error('Input contains an empty iterator.');
const iterators = iterables.map((it) => it[Symbol.iterator]());
const results = iterators.map((it) => it.next());
if (results.some((r) => r.done)) {
throw new Error("Input contains an empty iterator.");
}
for (let i = 0; ; ) {
if (results[i].done) {
Expand All @@ -114,7 +113,7 @@ function* product(...iterables) {
}
}

/** @param {import('../types').Range} rng */
/** @param {import('./types.js').Range} rng */
function* range({ stop, start = 0, step = 1 }) {
for (let i = start; i < stop; i += step) {
yield i;
Expand Down
Loading

0 comments on commit 374f273

Please sign in to comment.