Skip to content

Commit 5efeb07

Browse files
authored
ENG-847 include, do not build dependencies. (#431)
* ENG-847 compile roam against db ts * Also make utils and ui co-compile rather than build. Adjust turbo dependencies. * ENG-821 Rewrite createEnv.mts to be more resilient. (#434) Specifically, allows it to be called in a non-dev environment.
1 parent bed3ec2 commit 5efeb07

File tree

5 files changed

+92
-119
lines changed

5 files changed

+92
-119
lines changed

packages/database/package.json

Lines changed: 7 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -4,55 +4,33 @@
44
"private": true,
55
"license": "Apache-2.0",
66
"exports": {
7-
"./lib/*": {
8-
"types": "./dist/src/lib/*.d.ts",
9-
"require": "./dist/src/lib/*.js",
10-
"default": "./dist/src/lib/*.js"
11-
},
7+
"./lib/*": "./src/lib/*.ts",
128
"./dbDotEnv": {
139
"types": "./types/dbDotEnv.d.ts",
1410
"import": "./src/dbDotEnv.mjs",
15-
"require": "./src/dbDotEnv.mjs",
1611
"default": "./src/dbDotEnv.mjs"
1712
},
18-
"./dbTypes": {
19-
"types": "./dist/src/dbTypes.d.ts",
20-
"require": "./dist/src/dbTypes.js",
21-
"default": "./dist/src/dbTypes.js"
22-
},
23-
"./inputTypes": {
24-
"types": "./dist/src/inputTypes.d.ts",
25-
"default": "./dist/src/inputTypes.d.ts"
26-
}
13+
"./dbTypes": "./src/dbTypes.ts",
14+
"./inputTypes": "./src/inputTypes.ts"
2715
},
2816
"typesVersions": {
2917
"*": {
30-
"lib/*": [
31-
"dist/src/lib/*.d.ts"
32-
],
3318
"./dbDotEnv": [
34-
"src/dbDotEnv.d.ts"
35-
],
36-
"./dbTypes": [
37-
"dist/src/dbTypes.d.ts"
38-
],
39-
"./inputTypes": [
40-
"dist/src/inputTypes.d.ts"
19+
"types/dbDotEnv.d.ts"
4120
]
4221
}
4322
},
4423
"scripts": {
4524
"init": "supabase login",
4625
"dev": "tsx scripts/dev.ts",
47-
"compile": "tsc",
4826
"serve": "supabase start && tsx scripts/createEnv.mts && supabase functions serve",
4927
"stop": "supabase stop",
50-
"check-types": "tsc --emitDeclarationOnly --skipLibCheck",
28+
"check-types": "tsc --noEmit --skipLibCheck",
5129
"check-schema": "tsx scripts/lint.ts && supabase stop && npm run dbdiff",
5230
"lint": "eslint . && tsx scripts/lint.ts",
5331
"lint:fix": "tsx scripts/lint.ts -f",
54-
"build": "tsc",
55-
"build-schema": "tsx scripts/build.ts && npm run genenv -- local",
32+
"build": "tsx scripts/createEnv.mts && tsc",
33+
"build-schema": "tsx scripts/build.ts && tsx scripts/createEnv.mts -- local",
5634
"test": "tsc && cucumber-js",
5735
"genenv": "tsx scripts/createEnv.mts",
5836
"gentypes:production": "supabase start && supabase gen types typescript --project-id \"$SUPABASE_PROJECT_ID\" --schema public > src/dbTypes.ts",

packages/database/scripts/createEnv.mts

Lines changed: 67 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,32 @@
11
import { execSync } from "node:child_process";
2-
import { appendFileSync, writeFileSync } from "fs";
3-
import { join, dirname } from "path";
4-
import { fileURLToPath } from "url";
2+
import { appendFileSync, writeFileSync } from "node:fs";
3+
import { join, dirname } from "node:path";
4+
import { fileURLToPath } from "node:url";
55
import dotenv from "dotenv";
66
import { Vercel } from "@vercel/sdk";
77

88
const __dirname = dirname(fileURLToPath(import.meta.url));
99
const projectRoot = join(__dirname, "..");
10+
const baseParams: Record<string, string> = {};
1011

11-
if (process.env.HOME === "/vercel") process.exit(0);
12-
13-
dotenv.config();
14-
15-
const variant =
16-
(process.argv.length === 3
17-
? process.argv[2]
18-
: process.env["SUPABASE_USE_DB"]) || "local";
19-
20-
if (["local", "branch", "production", "all"].indexOf(variant) === -1) {
21-
console.error("Invalid variant: " + variant);
22-
process.exit(-1);
12+
enum Variant {
13+
local = "local",
14+
branch = "branch",
15+
production = "production",
16+
all = "all",
17+
none = "none",
2318
}
2419

2520
// option to override in .env, but otherwise use our values
2621
const projectIdOrName: string =
2722
process.env["VERCEL_PROJECT_ID"] ||
2823
process.env["VERCEL_PROJECT_NAME"] ||
2924
"discourse-graph";
30-
const baseParams: Record<string, string> = {};
31-
const vercelToken = process.env["VERCEL_TOKEN"];
25+
26+
const getVercelToken = () => {
27+
dotenv.config();
28+
return process.env["VERCEL_TOKEN"];
29+
};
3230

3331
const makeLocalEnv = () => {
3432
const stdout = execSync("supabase status -o env", {
@@ -49,7 +47,7 @@ const makeLocalEnv = () => {
4947
);
5048
};
5149

52-
const makeBranchEnv = async (vercel: Vercel) => {
50+
const makeBranchEnv = async (vercel: Vercel, vercelToken: string) => {
5351
let branch: string;
5452
if (process.env.SUPABASE_GIT_BRANCH) {
5553
// allow to override current branch
@@ -88,7 +86,7 @@ const makeBranchEnv = async (vercel: Vercel) => {
8886
appendFileSync(".env.branch", `NEXT_API_ROOT="${url}/api"\n`);
8987
};
9088

91-
const makeProductionEnv = async (vercel: Vercel) => {
89+
const makeProductionEnv = async (vercel: Vercel, vercelToken: string) => {
9290
const result = await vercel.deployments.getDeployments({
9391
...baseParams,
9492
projectId: projectIdOrName,
@@ -100,38 +98,62 @@ const makeProductionEnv = async (vercel: Vercel) => {
10098
throw new Error("No production deployment found");
10199
}
102100
const url = result.deployments[0]!.url;
103-
console.log(url);
104101
execSync(
105102
`vercel -t ${vercelToken} env pull --environment production .env.production`,
106103
);
107104
appendFileSync(".env.production", `NEXT_API_ROOT="${url}/api"\n`);
108105
};
109106

110-
try {
111-
if (variant === "local" || variant === "all") {
112-
makeLocalEnv();
113-
if (variant === "local") process.exit(0);
114-
}
115-
if (!vercelToken) {
116-
console.error("Missing VERCEL_TOKEN in .env");
117-
process.exit(-1);
118-
}
119-
// option to override in .env, but otherwise use our values
120-
const teamId = process.env["VERCEL_TEAM_ID"];
121-
const teamSlug = process.env["VERCEL_TEAM_SLUG"] || "discourse-graphs";
122-
if (teamId) {
123-
baseParams.teamId = teamId;
124-
} else {
125-
baseParams.slug = teamSlug;
126-
}
127-
const vercel = new Vercel({ bearerToken: vercelToken });
128-
if (variant === "branch" || variant === "all") {
129-
await makeBranchEnv(vercel);
107+
const main = async (variant: Variant) => {
108+
// Do not execute in deployment or github action.
109+
if (
110+
process.env.HOME === "/vercel" ||
111+
process.env.GITHUB_ACTIONS !== undefined
112+
)
113+
return;
114+
115+
if (variant === Variant.none) return;
116+
117+
try {
118+
if (variant === Variant.local || variant === Variant.all) {
119+
makeLocalEnv();
120+
if (variant === Variant.local) return;
121+
}
122+
const vercelToken = getVercelToken();
123+
if (!vercelToken) {
124+
throw Error("Missing VERCEL_TOKEN in .env");
125+
}
126+
// option to override in .env, but otherwise use our values
127+
const teamId = process.env["VERCEL_TEAM_ID"];
128+
const teamSlug = process.env["VERCEL_TEAM_SLUG"] || "discourse-graphs";
129+
if (teamId) {
130+
baseParams.teamId = teamId;
131+
} else {
132+
baseParams.slug = teamSlug;
133+
}
134+
const vercel = new Vercel({ bearerToken: vercelToken });
135+
if (variant === Variant.branch || variant === Variant.all) {
136+
await makeBranchEnv(vercel, vercelToken);
137+
}
138+
if (variant === Variant.production || variant === Variant.all) {
139+
await makeProductionEnv(vercel, vercelToken);
140+
}
141+
} catch (err) {
142+
console.error("variant ", variant, " error ", err);
143+
throw err;
130144
}
131-
if (variant === "production" || variant === "all") {
132-
await makeProductionEnv(vercel);
145+
};
146+
147+
if (fileURLToPath(import.meta.url) === process.argv[1]) {
148+
const variantS: string =
149+
(process.argv.length === 3
150+
? process.argv[2]
151+
: process.env["SUPABASE_USE_DB"]) || "none";
152+
153+
const variant = (Variant as Record<string, Variant>)[variantS];
154+
if (variant === undefined) {
155+
throw Error("Invalid variant: " + variant);
133156
}
134-
} catch (err) {
135-
console.error(err);
136-
throw err;
157+
console.log(variant);
158+
await main(variant);
137159
}

packages/ui/package.json

Lines changed: 4 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -7,41 +7,16 @@
77
"./globals.css": "./src/globals.css",
88
"./postcss.config": "./postcss.config.mjs",
99
"./tailwind.config": "./tailwind.config.ts",
10-
"./lib/*": {
11-
"types": "./dist/src/lib/*.d.ts",
12-
"require": "./dist/src/lib/*.js",
13-
"default": "./dist/src/lib/*.js"
14-
},
15-
"./hooks/*": {
16-
"types": "./dist/src/hooks/*.d.ts",
17-
"require": "./dist/src/hooks/*.js",
18-
"default": "./dist/src/hooks/*.js"
19-
},
20-
"./components/*": {
21-
"types": "./dist/src/components/*.d.ts",
22-
"require": "./dist/src/components/*.js",
23-
"default": "./dist/src/components/*.js"
24-
}
25-
},
26-
"typesVersions": {
27-
"*": {
28-
"lib/*": [
29-
"dist/lib/*.d.ts"
30-
],
31-
"hooks/*": [
32-
"dist/hooks/*.d.ts"
33-
],
34-
"components/*": [
35-
"dist/components/*.d.ts"
36-
]
37-
}
10+
"./lib/*": "./src/lib/*.ts",
11+
"./hooks/*": "./src/hooks/*.ts",
12+
"./components/*": "./src/components/*.tsx"
3813
},
3914
"scripts": {
4015
"lint": "eslint .",
4116
"build": "tsc",
4217
"lint:fix": "eslint . --fix",
4318
"generate:component": "turbo gen react-component",
44-
"check-types": "tsc --emitDeclarationOnly --skipLibCheck",
19+
"check-types": "tsc --noEmit --skipLibCheck",
4520
"ui": "npx shadcn@latest"
4621
},
4722
"devDependencies": {

packages/utils/package.json

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,11 @@
44
"private": true,
55
"license": "Apache-2.0",
66
"exports": {
7-
"./*": {
8-
"types": "./dist/src/*.d.ts",
9-
"require": "./dist/src/*.js",
10-
"default": "./dist/src/*.js"
11-
}
12-
},
13-
"typesVersions": {
14-
"*": {
15-
"*": [
16-
"dist/src/*.d.ts"
17-
]
18-
}
7+
"./*": "./src/*.ts"
198
},
209
"scripts": {
2110
"build": "tsc",
22-
"check-types": "tsc --emitDeclarationOnly --skipLibCheck",
11+
"check-types": "tsc --noEmit --skipLibCheck",
2312
"lint": "eslint ."
2413
}
2514
}

turbo.json

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,11 @@
4040
],
4141
"tasks": {
4242
"build": {
43-
"dependsOn": ["^build"],
43+
"dependsOn": ["@repo/database#genenv"],
4444
"inputs": ["$TURBO_DEFAULT$", ".env*"],
4545
"outputs": [".next/**", "!.next/cache/**", "dist/**", "src/dbTypes.ts"]
4646
},
4747
"build-schema": {
48-
"dependsOn": ["^build-schema"],
4948
"inputs": ["$TURBO_DEFAULT$", ".env*"],
5049
"outputs": ["src/dbTypes.ts"]
5150
},
@@ -60,7 +59,17 @@
6059
"cache": false,
6160
"persistent": true,
6261
"inputs": ["$TURBO_DEFAULT$", ".env*"],
63-
"dependsOn": ["^build"]
62+
"dependsOn": ["@repo/database#genenv"]
63+
},
64+
"roam#dev": {
65+
"with": ["@repo/database#dev"]
66+
},
67+
"website#dev": {
68+
"with": ["@repo/database#dev"]
69+
},
70+
"genenv": {
71+
"env": ["SUPABASE_USE_DB"],
72+
"outputs": [".env*"]
6473
},
6574
"deploy": {
6675
"cache": false,

0 commit comments

Comments
 (0)