From 65f1d4be3d4b7b12289d0ac95d5772b9bfa38476 Mon Sep 17 00:00:00 2001 From: Will Ernst Date: Fri, 14 Jun 2024 15:56:27 -0400 Subject: [PATCH] add validation of generated types to packages --- packages/cli/package.json | 5 +- packages/client/package.json | 5 +- packages/client/src/client/triplit-client.ts | 3 +- packages/db/package.json | 5 +- packages/db/src/index.ts | 1 + packages/react/package.json | 5 +- packages/react/src/use-query.ts | 52 +++++++++++--- packages/server-core/package.json | 5 +- packages/server/package.json | 5 +- packages/svelte/package.json | 5 +- packages/types/package.json | 5 +- ...ck-for-local-references-in-declarations.js | 69 +++++++++++++++++++ 12 files changed, 139 insertions(+), 26 deletions(-) create mode 100644 scripts/check-for-local-references-in-declarations.js diff --git a/packages/cli/package.json b/packages/cli/package.json index d55ed471..3a651ca9 100644 --- a/packages/cli/package.json +++ b/packages/cli/package.json @@ -3,13 +3,14 @@ "version": "0.3.68", "type": "module", "scripts": { - "build": "tsc", + "build": "tsc && yarn validate:types", "build:watch": "tsc -w", "lint": "tsc --noEmit", "lint:strict": "tsc --noEmit --strict", "test": "vitest run", "test:watch": "vitest watch", - "publish-pkg": "node ../../scripts/npm-check-version-and-publish.js" + "publish-pkg": "node ../../scripts/npm-check-version-and-publish.js", + "validate:types": "node ../../scripts/check-for-local-references-in-declarations.js dist" }, "bin": { "triplit": "./dist/index.js" diff --git a/packages/client/package.json b/packages/client/package.json index 46d5d018..5e9f3cfa 100644 --- a/packages/client/package.json +++ b/packages/client/package.json @@ -39,7 +39,7 @@ } }, "scripts": { - "build": "tsc && yarn build-worker", + "build": "tsc && yarn validate:types && yarn build-worker", "build:watch": "tsc -w", "lint:build": "npx publint", "lint": "tsc --noEmit", @@ -47,7 +47,8 @@ "test:unit": "vitest run", "typecheck": "vitest --typecheck.only --no-watch", "publish-pkg": "node ../../scripts/npm-check-version-and-publish.js", - "build-worker": "esbuild ./src/worker-client/worker-client-operator.ts --bundle --minify --platform=browser --outfile=./dist/worker-client/worker-client-operator.js --format=esm --sourcemap" + "build-worker": "esbuild ./src/worker-client/worker-client-operator.ts --bundle --minify --platform=browser --outfile=./dist/worker-client/worker-client-operator.js --format=esm --sourcemap", + "validate:types": "node ../../scripts/check-for-local-references-in-declarations.js dist" }, "files": [ "/dist" diff --git a/packages/client/src/client/triplit-client.ts b/packages/client/src/client/triplit-client.ts index ca585aa2..85790cc6 100644 --- a/packages/client/src/client/triplit-client.ts +++ b/packages/client/src/client/triplit-client.ts @@ -17,6 +17,7 @@ import { TupleValue, schemaToJSON, Unalias, + SchemaJSON, } from '@triplit/db'; import { getUserId } from '../token.js'; import { UnrecognizedFetchPolicyError } from '../errors.js'; @@ -260,7 +261,7 @@ export class TriplitClient { }); } - async getSchema() { + async getSchema(): Promise { return schemaToJSON(await this.db.getSchema()); } diff --git a/packages/db/package.json b/packages/db/package.json index a3c8ae91..e1d57f62 100644 --- a/packages/db/package.json +++ b/packages/db/package.json @@ -33,7 +33,7 @@ }, "type": "module", "scripts": { - "build": "tsc --build --pretty", + "build": "tsc --build --pretty && yarn validate:types", "build:watch": "tsc -w", "lint:build": "npx publint", "lint": "tsc --noEmit", @@ -46,7 +46,8 @@ "typecheck": "vitest --typecheck.only --no-watch", "bench": "vitest bench", "coverage": "stryker run", - "publish-pkg": "node ../../scripts/npm-check-version-and-publish.js" + "publish-pkg": "node ../../scripts/npm-check-version-and-publish.js", + "validate:types": "node ../../scripts/check-for-local-references-in-declarations.js dist" }, "files": [ "/dist" diff --git a/packages/db/src/index.ts b/packages/db/src/index.ts index 9c1a02d7..f623c106 100644 --- a/packages/db/src/index.ts +++ b/packages/db/src/index.ts @@ -84,6 +84,7 @@ export type { CollectionsDefinition, QueryAttributeDefinition, UserTypeOptions, + SchemaDefinition as SchemaJSON, } from './data-types/serialization.js'; export { timestampCompare } from './timestamp.js'; export type { Timestamp } from './timestamp.js'; diff --git a/packages/react/package.json b/packages/react/package.json index 52a41ef5..fa2c33fa 100644 --- a/packages/react/package.json +++ b/packages/react/package.json @@ -16,11 +16,12 @@ } }, "scripts": { - "build": "tsc", + "build": "tsc && yarn validate:types", "build:watch": "tsc -w", "lint:build": "npx publint", "lint": "tsc --noEmit", - "publish-pkg": "node ../../scripts/npm-check-version-and-publish.js" + "publish-pkg": "node ../../scripts/npm-check-version-and-publish.js", + "validate:types": "node ../../scripts/check-for-local-references-in-declarations.js dist" }, "files": [ "/dist" diff --git a/packages/react/src/use-query.ts b/packages/react/src/use-query.ts index fa9af548..0ad47b20 100644 --- a/packages/react/src/use-query.ts +++ b/packages/react/src/use-query.ts @@ -10,20 +10,54 @@ import { } from '@triplit/client'; import type { WorkerClient } from '@triplit/client/worker-client'; -export function useQuery< +type useQueryPayload< M extends Models | undefined, Q extends ClientQuery ->( - client: TriplitClient | WorkerClient, - query: ClientQueryBuilder, - options?: Partial -): { +> = { + results: Unalias> | undefined; fetching: boolean; fetchingLocal: boolean; fetchingRemote: boolean; + error: any; +}; + +type usePaginatedQueryPayload< + M extends Models | undefined, + Q extends ClientQuery +> = { + results: Unalias> | undefined; + fetching: boolean; + fetchingPage: boolean; + error: any; + hasNextPage: boolean; + hasPreviousPage: boolean; + nextPage: () => void; + prevPage: () => void; + disconnect: () => void; +}; + +type useInfiniteQueryPayload< + M extends Models | undefined, + Q extends ClientQuery +> = { results: Unalias> | undefined; + fetching: boolean; + fetchingRemote: boolean; + fetchingMore: boolean; error: any; -} { + hasMore: boolean; + loadMore: () => void; + disconnect: () => void; +}; + +export function useQuery< + M extends Models | undefined, + Q extends ClientQuery +>( + client: TriplitClient | WorkerClient, + query: ClientQueryBuilder, + options?: Partial +): useQueryPayload { const [results, setResults] = useState< Unalias> | undefined >(undefined); @@ -103,7 +137,7 @@ export function usePaginatedQuery< client: TriplitClient | WorkerClient, query: ClientQueryBuilder, options?: Partial -) { +): usePaginatedQueryPayload { const builtQuery = useMemo(() => query.build(), [query]); const [hasNextPage, setHasNextPage] = useState(false); const [hasPreviousPage, setHasPreviousPage] = useState(false); @@ -178,7 +212,7 @@ export function useInfiniteQuery< client: TriplitClient | WorkerClient, query: ClientQueryBuilder, options?: Partial -) { +): useInfiniteQueryPayload { const builtQuery = useMemo(() => query.build(), [query]); const stringifiedQuery = builtQuery && JSON.stringify(builtQuery); const [hasMore, setHasMore] = useState(false); diff --git a/packages/server-core/package.json b/packages/server-core/package.json index 92385508..334cba0e 100644 --- a/packages/server-core/package.json +++ b/packages/server-core/package.json @@ -50,12 +50,13 @@ } }, "scripts": { - "build": "tsc", + "build": "tsc && yarn validate:types", "build:watch": "tsc -w", "test": "vitest", "lint": "tsc --noEmit", "lint:build": "npx publint", - "publish-pkg": "node ../../scripts/npm-check-version-and-publish.js" + "publish-pkg": "node ../../scripts/npm-check-version-and-publish.js", + "validate:types": "node ../../scripts/check-for-local-references-in-declarations.js dist" }, "dependencies": { "@triplit/db": "workspace:^", diff --git a/packages/server/package.json b/packages/server/package.json index 396c8dfe..564441f2 100644 --- a/packages/server/package.json +++ b/packages/server/package.json @@ -18,13 +18,14 @@ "dist" ], "scripts": { - "build": "tsc", + "build": "tsc && yarn validate:types", "build:watch": "tsc -w", "lint": "tsc --noEmit", "lint:build": "npx publint", "util:gentoken": "node ./dev/scripts/createToken.cjs", "publish-pkg": "node ../../scripts/npm-check-version-and-publish.js && yarn sentry:sourcemaps", - "sentry:sourcemaps": "sentry-cli sourcemaps inject --org aspen --project triplit-server ./dist && sentry-cli sourcemaps upload --release=$npm_package_version --org aspen --project triplit-server ./dist" + "sentry:sourcemaps": "sentry-cli sourcemaps inject --org aspen --project triplit-server ./dist && sentry-cli sourcemaps upload --release=$npm_package_version --org aspen --project triplit-server ./dist", + "validate:types": "node ../../scripts/check-for-local-references-in-declarations.js dist" }, "dependencies": { "@sentry/cli": "^2.31.2", diff --git a/packages/svelte/package.json b/packages/svelte/package.json index a2a9b756..eeca1e24 100644 --- a/packages/svelte/package.json +++ b/packages/svelte/package.json @@ -17,11 +17,12 @@ } }, "scripts": { - "build": "svelte-package -t=false && tsc", + "build": "svelte-package -t=false && tsc && yarn validate:types", "build:watch": "svelte-package -w", "lint:build": "npx publint", "publish-pkg": "node ../../scripts/npm-check-version-and-publish.js", - "check": "svelte-check --tsconfig ./tsconfig.json" + "check": "svelte-check --tsconfig ./tsconfig.json", + "validate:types": "node ../../scripts/check-for-local-references-in-declarations.js dist" }, "files": [ "/dist" diff --git a/packages/types/package.json b/packages/types/package.json index 0ff88398..b4b2c0e3 100644 --- a/packages/types/package.json +++ b/packages/types/package.json @@ -2,8 +2,9 @@ "name": "@triplit/types", "packageManager": "yarn@3.4.1", "scripts": { - "build": "tsc", - "build:watch": "tsc -w" + "build": "tsc && yarn validate:types", + "build:watch": "tsc -w", + "validate:types": "node ../../scripts/check-for-local-references-in-declarations.js dist" }, "exports": { "./*": { diff --git a/scripts/check-for-local-references-in-declarations.js b/scripts/check-for-local-references-in-declarations.js new file mode 100644 index 00000000..f0615fde --- /dev/null +++ b/scripts/check-for-local-references-in-declarations.js @@ -0,0 +1,69 @@ +const fs = require('fs'); +const path = require('path'); + +// Function to recursively search through directories +function searchInDirectory(dir, searchString) { + let stringFound = false; + let files; + try { + files = fs.readdirSync(dir); + } catch (e) { + console.error(`Unable to scan directory: ${e}`); + throw e; + } + + for (const file of files) { + const filePath = path.join(dir, file); + try { + const stats = fs.statSync(filePath); + if (stats.isDirectory()) { + stringFound = stringFound || searchInDirectory(filePath, searchString); // Recursively search in subdirectory + } else if (filePath.endsWith('.d.ts')) { + stringFound = stringFound || searchInFile(filePath, searchString); + } + } catch (e) { + console.error(`Unable to get stats of file: ${e}`); + throw e; + } + } + + return stringFound; +} + +// Function to search for the string in a file +function searchInFile(file, searchString) { + try { + const data = fs.readFileSync(file, 'utf8'); + if (data.includes(searchString)) { + console.log(`Found "${searchString}" in file: ${file}`); + return true; + } + return false; + } catch (e) { + console.error(`Unable to read file: ${e}`); + throw e; + } +} + +// Main function +function main() { + const args = process.argv.slice(2); + if (args.length !== 1) { + console.error('invalid args'); + return; + } + + const directory = args[0]; + const localImportPrefix = 'import("packages'; + + const isMatch = searchInDirectory(directory, localImportPrefix); + if (isMatch) { + console.log( + 'Found local references in declarations. Please remove them before publishing.' + ); + process.exit(1); + } +} + +// Run the main function +main();