Skip to content

Commit

Permalink
feat: initial implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
RebeccaStevens committed Jul 6, 2023
1 parent 5bde193 commit 3547c44
Show file tree
Hide file tree
Showing 11 changed files with 879 additions and 31 deletions.
7 changes: 7 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,13 @@
"project": ["./tsconfig.json"]
},
"ignorePatterns": ["/coverage/", "/dist/"],
"rules": {
"@typescript-eslint/naming-convention": "off",
"@typescript-eslint/restrict-plus-operands": "off",
"@typescript-eslint/no-unsafe-return": "off",
"functional/functional-parameters": "off",
"import/extensions": "off"
},
"overrides": [
{
"files": ["**/*.test.ts"],
Expand Down
9 changes: 8 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,14 @@
"type": "module",
"exports": {
".": {
"default": "./src/base/index.ts",
"types": {
"import": "./dist/index.d.mts",
"require": "./dist/index.d.cts"
}
},
"./functions": {
"default": "./src/functions/index.ts",
"types": {
"import": "./dist/functions.d.mts",
"require": "./dist/functions.d.cts"
Expand All @@ -39,6 +41,7 @@
"require": "./dist/functions.cjs"
},
"./functions/higher-order": {
"default": "./src/functions-ho/index.ts",
"types": {
"import": "./dist/functions-ho.d.mts",
"require": "./dist/functions-ho.d.cts"
Expand All @@ -47,12 +50,14 @@
"require": "./dist/functions-ho.cjs"
},
"./si-units": {
"default": "./src/si-units/index.ts",
"types": {
"import": "./dist/si-units.d.mts",
"require": "./dist/si-units.d.cts"
}
},
"./si-units/converters": {
"default": "./src/si-units/converters/index.ts",
"types": {
"import": "./dist/si-units-converters.d.mts",
"require": "./dist/si-units-converters.d.cts"
Expand Down Expand Up @@ -82,7 +87,9 @@
"lint:prettier-fix": "prettier \"**/*\" --ignore-unknown --write",
"lint:spelling": "cspell \"**\" \".github/**/*\"",
"prepare": "husky install",
"release": "semantic-release",
"release": "pnpm run release:pre && pnpm run release:semantic",
"release:pre": "node --no-warnings=ExperimentalWarning --loader=ts-paths-esm-loader/transpile-only --experimental-specifier-resolution=node ./scripts/pre-release.ts",
"release:semantic": "semantic-release",
"test": "pnpm run test:js",
"test:js": "c8 ava",
"type-check": "tsc --noEmit"
Expand Down
58 changes: 40 additions & 18 deletions rollup.config.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import assert from "node:assert/strict";
import path from "node:path";

import rollupPluginNodeResolve from "@rollup/plugin-node-resolve";
import rollupPluginTypescript from "@rollup/plugin-typescript";
import { defineConfig, type Plugin } from "rollup";
Expand All @@ -6,20 +9,39 @@ import rollupPluginDts from "rollup-plugin-dts";

import pkg from "./package.json" assert { type: "json" };

const entries = [
["src/base/index.ts", pkg.exports["."]],
["src/functions/index.ts", pkg.exports["./functions"]],
["src/functions-ho/index.ts", pkg.exports["./functions/higher-order"]],
["src/si-units/index.ts", pkg.exports["./si-units"]],
["src/si-units/converters/index.ts", pkg.exports["./si-units/converters"]],
] as const;

const common = defineConfig({
output: {
sourcemap: false,
},

external: [],
external: (
source: string,
importer: string | undefined,
isResolved: boolean,
) => {
if (!isResolved || importer === undefined) {
return null;
}

assert(path.isAbsolute(source));
const relativeSource = path.relative(process.cwd(), source);
const relativeImporter = path.relative(process.cwd(), importer);

if (!relativeSource.startsWith("src/")) {
return null;
}

const sourceSubproject = relativeSource.slice(
4,
Math.max(0, relativeSource.indexOf("/", 4)),
);
const importerSubproject = relativeImporter.slice(
4,
Math.max(0, relativeImporter.indexOf("/", 4)),
);

return sourceSubproject !== importerSubproject;
},

treeshake: {
annotations: true,
Expand Down Expand Up @@ -52,20 +74,20 @@ const types = defineConfig({
] as Plugin[],
});

export default entries
.flatMap(([entry, output]) => [
"import" in output
export default Object.values(pkg.exports)
.flatMap((pkgExports) => [
"import" in pkgExports
? {
...runtimes,
input: entry,
input: pkgExports.default,
output: [
{
file: output.import,
file: pkgExports.import,
format: "esm",
sourcemap: false,
},
{
file: output.require,
file: pkgExports.require,
format: "cjs",
sourcemap: false,
},
Expand All @@ -74,15 +96,15 @@ export default entries
: null,
{
...types,
input: entry,
input: pkgExports.default,
output: [
{
file: output.types.import,
file: pkgExports.types.import,
format: "esm",
sourcemap: false,
},
{
file: output.types.require,
file: pkgExports.types.require,
format: "cjs",
sourcemap: false,
},
Expand Down
42 changes: 42 additions & 0 deletions scripts/pre-release.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import childProcess from "node:child_process";
import fs from "node:fs/promises";
import util from "node:util";

import pkg from "../package.json" assert { type: "json" };

const exec = util.promisify(childProcess.exec);

if (
process.env["CI"] === undefined ||
process.env["CI"] === "" ||
process.env["CI"] === "0"
) {
throw new Error("Not in CI Environment");
}

const releasePkg = Object.fromEntries(
Object.entries(pkg).map(([key, value]): [string, unknown] => {
if (key === "exports") {
return [
key,
Object.fromEntries(
Object.entries(value).map(
([exportsKey, exportsValue]): [string, unknown] => {
const { default: _, ...updatedExportsValue } = exportsValue;

Check warning on line 25 in scripts/pre-release.ts

View workflow job for this annotation

GitHub Actions / lint_js / lint_js

Unsafe assignment of an `any` value
return [exportsKey, updatedExportsValue];
},
),
),
];
}

return [key, value];
}),
);

await fs.writeFile("./package.json", JSON.stringify(releasePkg, undefined, 2), {
encoding: "utf8",
});

await exec("git add package.json");
await exec('git commit -m "build: package.json"');
27 changes: 27 additions & 0 deletions src/base/core.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { type BrandUnit } from "./utils";

export type Exponent = -6 | -5 | -4 | -3 | -2 | -1 | 0 | 1 | 2 | 3 | 4 | 5 | 6;

Check warning on line 3 in src/base/core.ts

View workflow job for this annotation

GitHub Actions / lint_js / lint_js

Missing JSDoc comment

export type UnitFull<T extends Record<string, UnitValue>> = number &

Check warning on line 5 in src/base/core.ts

View workflow job for this annotation

GitHub Actions / lint_js / lint_js

Missing JSDoc comment
Readonly<BrandUnit<T>>;

export type UnitValue = { exponent: Exponent } & Record<string, Exponent>;

Check warning on line 8 in src/base/core.ts

View workflow job for this annotation

GitHub Actions / lint_js / lint_js

Missing JSDoc comment

/**
* A unit of measurement.
*/
export type Unit<T extends Record<string, Exponent | UnitValue>> = UnitFull<{
[K in keyof T]: T[K] extends Exponent
? {
exponent: T[K];
}
: T[K] extends UnitValue
? T[K]
: never;
}>;

export type UnknownUnit = number &

Check warning on line 23 in src/base/core.ts

View workflow job for this annotation

GitHub Actions / lint_js / lint_js

Missing JSDoc comment
Readonly<{
__exactKeys: string;
__uom_types: Record<string, {}>;
}>;
5 changes: 2 additions & 3 deletions src/base/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
/* eslint-disable unicorn/no-empty-file */

// Write me
export type { Unit, UnknownUnit } from "./core";
export type { Multiply, Divide, Inverse } from "./utils";

0 comments on commit 3547c44

Please sign in to comment.