Skip to content
Open
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
38 changes: 17 additions & 21 deletions cli/build-cmd.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import { PackageJson, Version, buildJsrConf, getVersion, patchPackageJson } from
import { expect, it } from "vitest";

const mock = {
readJSON: async (): Promise<unknown> => {
return {
readJSON: (): Promise<PackageJson> => {
return Promise.resolve({
name: "@fireproof/core-cli",
version: "0.0.0",
description: "Live ledger for the web.",
Expand All @@ -25,7 +25,10 @@ const mock = {
"@fireproof/core-cli": "workspace:0.0.0",
vitest: "^3.2.4",
},
};
exports: {
".": "./index.js",
},
});
},
};

Expand All @@ -50,8 +53,8 @@ it("should only use prefix version in dependencies", async () => {
it("should only use prefix version in dependencies", async () => {
const version = new Version("0.0.0-smoke", "^");
const { patchedPackageJson } = await patchPackageJson("package.json", version, undefined, mock);
const originalPackageJson = (await mock.readJSON()) as unknown as PackageJson;
const jsrConf = await buildJsrConf({ originalPackageJson, patchedPackageJson }, version.prefixedVersion);
const originalPackageJson = await mock.readJSON();
const jsrConf = buildJsrConf({ originalPackageJson, patchedPackageJson }, version.prefixedVersion);
expect(jsrConf.version).toBe("0.0.0-smoke");
expect(jsrConf.imports).toHaveProperty("@fireproof/vendor", "jsr:@fireproof/vendor@^0.0.0-smoke");
});
Expand All @@ -70,8 +73,8 @@ it("ILLEGAL Version with gitref", async () => {
xenv: { GITHUB_REF: "a/b/cdkdkdkdk" },
xfs: {
existsSync: () => false,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} as any,
readFile: (_x: string, _y = "utf-8") => Promise.resolve(""),
},
}),
).toContain("0.0.0-smoke-");
});
Expand All @@ -83,8 +86,7 @@ it("GITHUB_REF set and file", async () => {
xfs: {
existsSync: () => true,
readFile: (_x: string, _y = "utf-8") => Promise.resolve("v0.0.48-smoke"),
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} as any,
},
}),
).toBe("0.0.48-smoke");
});
Expand All @@ -96,8 +98,7 @@ it("GITHUB_REF set and not exist file", async () => {
xfs: {
existsSync: () => false,
readFile: (_x: string, _y = "utf-8") => Promise.resolve("v0.0.48-smoke"),
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} as any,
},
}),
).toBe("0.0.45-xx");
});
Expand All @@ -108,8 +109,7 @@ it("getVersion file with v", async () => {
xfs: {
existsSync: () => true,
readFile: (_x: string, _y = "utf-8") => Promise.resolve("v0.0.48-smoke"),
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} as any,
},
xenv: {},
}),
).toContain("0.0.48-smoke");
Expand All @@ -121,8 +121,7 @@ it("getVersion file without ", async () => {
xfs: {
existsSync: () => true,
readFile: (_x: string, _y = "utf-8") => Promise.resolve("0.0.48-smoke"),
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} as any,
},
xenv: {},
}),
).toContain("0.0.48-smoke");
Expand All @@ -134,8 +133,7 @@ it("getVersion file with scope", async () => {
xfs: {
existsSync: () => true,
readFile: (_x: string, _y = "utf-8") => Promise.resolve("[email protected]"),
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} as any,
},
xenv: {},
}),
).toContain("0.0.48-smoke");
Expand All @@ -147,8 +145,7 @@ it("getVersion file with scope and v", async () => {
xfs: {
existsSync: () => true,
readFile: (_x: string, _y = "utf-8") => Promise.resolve("[email protected]"),
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} as any,
},
xenv: {},
}),
).toContain("0.0.48-smoke");
Expand All @@ -160,8 +157,7 @@ it("getVersion file with ref", async () => {
xfs: {
existsSync: () => true,
readFile: (_x: string, _y = "utf-8") => Promise.resolve("d/d/[email protected]"),
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} as any,
},
xenv: {},
}),
).toContain("0.0.48-smoke");
Expand Down
91 changes: 41 additions & 50 deletions cli/build-cmd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ function getEnvVersion(version = "refs/tags/v0.0.0-smoke", xenv = process.env) {
}
if (reEndVersion.test(wversion)) {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
wversion = wversion.match(reEndVersion)![1];
wversion = reEndVersion.exec(wversion)![1];
}
const calculatedVersion = wversion.replace(reScopedVersion, "$1").replace(reVersionAlphaStart, "$1");
try {
Expand All @@ -43,16 +43,16 @@ export async function getVersion(
{ xfs, xenv }: Partial<Mock> = {
xfs: {
existsSync: fs.existsSync,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
readFile: fs.readFile as any,
readFile: async (path: string, encoding: string) => {
const buffer = await fs.readFile(path);
return buffer.toString(encoding as BufferEncoding);
},
},
xenv: process.env as Record<string, string>,
},
) {
let top = await findUp("tsconfig.dist.json");
if (!top) {
top = process.cwd();
}
top ??= process.cwd();
if (fpVersionFname && xfs) {
const fpVersionFile = path.join(path.dirname(top), fpVersionFname);
if (xfs.existsSync(fpVersionFile)) {
Expand All @@ -65,8 +65,8 @@ export async function getVersion(
}

export class Version {
#version: string;
#versionPrefix: string;
readonly #version: string;
readonly #versionPrefix: string;

constructor(version: string, versionPrefix: string) {
this.#version = version;
Expand All @@ -90,9 +90,6 @@ export class Version {
}

function patchDeps(dep: Record<string, string>, version: string) {
if (typeof dep !== "object" || !dep) {
return;
}
for (const i of Object.keys(dep)) {
const val = dep[i];
if (val.startsWith("workspace:")) {
Expand All @@ -104,7 +101,7 @@ function patchDeps(dep: Record<string, string>, version: string) {

export interface PackageJson {
name: string;
private?: "true" | string | boolean;
private?: string | boolean;
license: string;
version: string;
scripts: Record<string, string>;
Expand All @@ -118,8 +115,8 @@ export async function patchPackageJson(
version: Version,
changeScope?: string,
mock: {
readJSON: typeof fs.readJson;
} = { readJSON: fs.readJson },
readJSON: (path: string) => Promise<PackageJson>;
} = { readJSON: fs.readJson as (path: string) => Promise<PackageJson> },
): Promise<{
patchedPackageJson: PackageJson;
originalPackageJson: PackageJson;
Expand All @@ -137,44 +134,44 @@ export async function patchPackageJson(
}
// patchedPackageJson.name = changeScope ? : patchedPackageJson.name;
patchedPackageJson.version = version.version;
delete patchedPackageJson.scripts["pack"];
delete patchedPackageJson.scripts["publish"];
patchedPackageJson.dependencies = patchDeps(patchedPackageJson.dependencies, version.prefixedVersion);
patchedPackageJson.devDependencies = patchDeps(patchedPackageJson.devDependencies, version.prefixedVersion);
delete patchedPackageJson.scripts.pack;
delete patchedPackageJson.scripts.publish;
patchDeps(patchedPackageJson.dependencies, version.prefixedVersion);
patchDeps(patchedPackageJson.devDependencies, version.prefixedVersion);

return { patchedPackageJson, originalPackageJson };
}

async function updateTsconfig(srcTsConfig: string, dstTsConfig: string) {
const tsconfig = await fs.readJSONSync(srcTsConfig);
const tsconfig = (await fs.readJSONSync(srcTsConfig)) as Record<string, unknown>;
tsconfig.extends = [await findUp("tsconfig.dist.json")];
tsconfig.compilerOptions = {
...tsconfig.compilerOptions,
...(tsconfig.compilerOptions as Record<string, unknown>),
noEmit: false,
outDir: "../npm/",
};
tsconfig.include = tsconfig.include || [];
tsconfig.include.push("**/*");
tsconfig.exclude = tsconfig.exclude || [];
tsconfig.exclude.push("node_modules", "dist", ".git", ".vscode");
tsconfig.include = (tsconfig.include as string[] | undefined) ?? [];
(tsconfig.include as string[]).push("**/*");
tsconfig.exclude = (tsconfig.exclude as string[] | undefined) ?? [];
(tsconfig.exclude as string[]).push("node_modules", "dist", ".git", ".vscode");

console.log("tsconfig", tsconfig);
await fs.writeJSONSync(dstTsConfig, tsconfig, { spaces: 2 });
fs.writeJSONSync(dstTsConfig, tsconfig, { spaces: 2 });
// {
// "extends": "../../tsconfig.json",
// "compilerOptions": {
// "outDir": "./dist"
// }
}
function toDenoExports(exports: Record<string, string | Record<string, string>>) {
return Object.entries(exports ?? {}).reduce(
return Object.entries(exports).reduce<Record<string, string>>(
(acc, [k, v]) => {
if (typeof v === "string") {
acc[k] = v.replace(/\.(js|mjs|cjs)$/, ".ts").replace(/\.(jsx|mjsx|cjsx)$/, ".tsx");
} else {
const x = Object.entries(v).reduce((acc, [_, v]) => {
if (acc === "") {
if (v.match(/\.(js|mjs|cjs|)$/)) {
if (/\.(js|mjs|cjs|)$/.exec(v)) {
return v.replace(/\.(js|mjs|cjs)$/, ".ts");
}
}
Expand All @@ -186,26 +183,23 @@ function toDenoExports(exports: Record<string, string | Record<string, string>>)
}
return acc;
},
{ ".": "./index.ts" } as Record<string, string>,
{ ".": "./index.ts" },
);
}

function toDenoDeps(deps: Record<string, string>, version: string) {
return Object.entries(deps).reduce(
(acc, [k, v]) => {
if (v.startsWith("workspace:")) {
acc[k] = `jsr:${k}@${version}`;
return acc;
}
if (k.startsWith("@adviser/cement")) {
acc[k] = `jsr:${k}@${v.replace("npm:", "")}`;
return acc;
}
acc[k] = `npm:${k}@${v.replace("npm:", "")}`;
return Object.entries(deps).reduce<Record<string, string>>((acc, [k, v]) => {
if (v.startsWith("workspace:")) {
acc[k] = `jsr:${k}@${version}`;
return acc;
},
{} as Record<string, string>,
);
}
if (k.startsWith("@adviser/cement")) {
acc[k] = `jsr:${k}@${v.replace("npm:", "")}`;
return acc;
}
acc[k] = `npm:${k}@${v.replace("npm:", "")}`;
return acc;
}, {});
}
interface JsrConfig {
name: string;
Expand All @@ -227,16 +221,13 @@ interface JsrConfig {
}

function isPrivate(p: PackageJson) {
return (
p.private &&
((typeof p.private === "boolean" && p.private) || (typeof p.private === "string" && p.private.toLocaleLowerCase() === "true"))
);
return p.private && (typeof p.private === "boolean" || (typeof p.private === "string" && p.private.toLowerCase() === "true"));
}

export async function buildJsrConf(
export function buildJsrConf(
pj: { originalPackageJson: PackageJson; patchedPackageJson: PackageJson },
version: string,
): Promise<Partial<JsrConfig>> {
): Partial<JsrConfig> {
if (isPrivate(pj.originalPackageJson)) {
return {};
}
Expand Down Expand Up @@ -464,7 +455,7 @@ export function buildCmd(sthis: SuperThis) {
await fs.writeJSON("package.json", packageJson.patchedPackageJson, { spaces: 2 });
// await $`pnpm version ${args.version}`;

fs.copy(".", "../npm", {
await fs.copy(".", "../npm", {
filter: (src: string, _dst: string) => {
if (src.endsWith(".ts") || src.endsWith(".tsx")) {
return false;
Expand All @@ -480,7 +471,7 @@ export function buildCmd(sthis: SuperThis) {
}

if (args.publishJsr) {
const jsrConf = await buildJsrConf(packageJson, version.prefixedVersion);
const jsrConf = buildJsrConf(packageJson, version.prefixedVersion);
await fs.writeJSON("jsr.json", jsrConf, { spaces: 2 });
if (!isPrivate(packageJson.originalPackageJson)) {
await $`pnpm exec deno publish --allow-dirty ${args.doPack ? "--dry-run" : ""}`;
Expand Down
2 changes: 1 addition & 1 deletion cli/cloud-token-key-cmd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export function keyCmd(sthis: SuperThis) {
{
const key = await rt.sts.env2jwk(args.ourToJWK, "ES256", sthis).then((jwk) => jwk);
// eslint-disable-next-line no-console
console.log(`${JSON.stringify(await exportJWK(key))}`);
console.log(JSON.stringify(await exportJWK(key)));
}
break;
case args.generatePair:
Expand Down
1 change: 0 additions & 1 deletion cli/main.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { ensureSuperThis } from "@fireproof/core-runtime";
import { run, subcommands } from "cmd-ts";

import { dotenv } from "zx";
import { buildCmd } from "./build-cmd.js";
import { setDependenciesCmd, setScriptsCmd } from "./set-scripts-cmd.js";
Expand Down
7 changes: 4 additions & 3 deletions cli/pre-signed-url.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ export function preSignedUrlCmd(sthis: SuperThis) {
accessKeyId: option({
long: "accessKeyId",
type: string,
defaultValue: () => sthis.env.get("ACCESS_KEY_ID") || "accessKeyId",
defaultValue: () => sthis.env.get("ACCESS_KEY_ID") ?? "accessKeyId",
defaultValueIsSerializable: true,
}),
secretAccessKey: option({
long: "secretAccessKey",
type: string,
defaultValue: () => sthis.env.get("SECRET_ACCESS_KEY") || "secretAccessKey",
defaultValue: () => sthis.env.get("SECRET_ACCESS_KEY") ?? "secretAccessKey",
defaultValueIsSerializable: true,
}),
region: option({
Expand All @@ -46,7 +46,7 @@ export function preSignedUrlCmd(sthis: SuperThis) {
storageURL: option({
long: "storageURL",
type: string,
defaultValue: () => sthis.env.get("STORAGE_URL") || "https://bucket.example.com/db/main",
defaultValue: () => sthis.env.get("STORAGE_URL") ?? "https://bucket.example.com/db/main",
defaultValueIsSerializable: true,
}),
path: option({
Expand All @@ -64,6 +64,7 @@ export function preSignedUrlCmd(sthis: SuperThis) {
now: option({
long: "now",
type: {
// eslint-disable-next-line @typescript-eslint/require-await
async from(str): Promise<string> {
const decoded = new Date(str);
if (isNaN(decoded.getTime())) {
Expand Down
Loading