Skip to content

Commit 2fdaf7d

Browse files
committed
fix: npm-app deploy
1 parent 057ef91 commit 2fdaf7d

File tree

6 files changed

+35
-27
lines changed

6 files changed

+35
-27
lines changed

backend/tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
"baseUrl": ".",
1616
"paths": {
1717
"@/env": ["../env.ts"],
18-
"common/*": ["../common/src/*"],
18+
"common/*": ["./src/common/*", "../common/src/*"],
1919
"@/*": ["./src/*"],
2020
"@codebuff/billing": ["../packages/billing/src"],
2121
"@codebuff/bigquery": ["../packages/bigquery/src"],

common/tsconfig.json

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,25 @@
1111
"module": "CommonJS",
1212
"moduleResolution": "node",
1313
"target": "esnext",
14-
"typeRoots": ["./node_modules/@types", "../node_modules/@types", "./src/types"],
14+
"incremental": true,
15+
"isolatedModules": true,
16+
"skipLibCheck": true,
17+
"types": ["node"],
18+
"typeRoots": [
19+
"./node_modules/@types",
20+
"../node_modules/@types",
21+
"./src/types"
22+
],
1523
"baseUrl": ".",
1624
"outDir": "./dist",
17-
"skipLibCheck": true,
18-
"types": [],
25+
"rootDir": "./src",
1926
"paths": {
2027
"@/env": ["../env.ts"]
2128
}
2229
},
2330
"include": ["src/**/*"],
2431
"references": [{ "path": "../tsconfig.env.json" }],
32+
2533
"ts-node": {
2634
"require": ["tsconfig-paths/register"]
2735
}

npm-app/clean-package.js

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
const fs = require('fs')
22
const path = require('path')
33

4-
// Set production environment for the build
5-
process.env.NEXT_PUBLIC_CB_ENVIRONMENT = 'prod'
6-
74
const packageJsonPath = path.join(__dirname, 'package.json')
85
const tempPackageJsonPath = path.join(__dirname, 'temp.package.json')
96
const indexJsPath = path.join(__dirname, 'dist', 'index.js')
@@ -22,12 +19,8 @@ for (const depType of ['dependencies', 'optionalDependencies']) {
2219
if (typeof version === 'string' && version.startsWith('workspace:')) {
2320
if (pkgName === 'common') {
2421
delete packageJson[depType][pkgName]
25-
console.log(
26-
`Removed dependency on ${pkgName} because it's now bundled.`
27-
)
2822
} else {
2923
packageJson[depType][pkgName] = '1.0.0'
30-
console.warn(`No version found for ${pkgName}, defaulting to 1.0.0`)
3124
}
3225
}
3326
}
@@ -44,11 +37,25 @@ fs.writeFileSync(packageJsonPath, JSON.stringify(packageJson, null, 2))
4437
if (fs.existsSync(indexJsPath)) {
4538
let indexJsContent = fs.readFileSync(indexJsPath, 'utf8')
4639

40+
// Dynamically get client-side environment variables from env.ts
41+
const envTsContent = fs.readFileSync(
42+
path.join(__dirname, '..', 'env.ts'),
43+
'utf8'
44+
)
45+
const clientKeysMatch = envTsContent.match(/client: {([^]*?)}/s)
46+
let clientKeys = []
47+
if (clientKeysMatch) {
48+
clientKeys =
49+
clientKeysMatch[1]
50+
.split('\n')
51+
.map((line) => line.match(/^\s*(\w+):/))
52+
.filter(Boolean)
53+
.map((match) => match[1]) ?? []
54+
}
55+
4756
// Get all environment variables that start with NEXT_PUBLIC_ or are needed for the npm package
4857
const envVarsToInject = Object.entries(process.env)
49-
.filter(
50-
([key, value]) => key.startsWith('NEXT_PUBLIC_') && value !== undefined
51-
)
58+
.filter(([key, value]) => clientKeys.includes(key) && value !== undefined)
5259
.map(([key, value]) => `process.env.${key} = '${value}';`)
5360

5461
const lines = indexJsContent.split('\n')
@@ -57,12 +64,9 @@ if (fs.existsSync(indexJsPath)) {
5764
indexJsContent = lines.join('\n')
5865
fs.writeFileSync(indexJsPath, indexJsContent)
5966
console.log(
60-
`Injected ${envVarsToInject.length} environment variables into index.js`
67+
`Injected ${envVarsToInject.length} environment variables into index.js:`
6168
)
69+
envVarsToInject.forEach((envVar) => console.log(`- ${envVar}`))
6270
} else {
6371
console.error('index.js not found in the dist directory')
6472
}
65-
66-
console.log(
67-
'package.json has been cleaned for publishing and index.js has been updated.'
68-
)

npm-app/package.json

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
"codebuff": "dist/index.js"
1010
},
1111
"scripts": {
12-
"prepublishOnly": "nx build npm-app && bun run clean-package.js",
12+
"prepublishOnly": "nx clean npm-app && nx build npm-app && infisical run --env=prod --log-level=warn -- bun run clean-package.js",
1313
"postpublish": "echo \"const fs=require('fs');fs.copyFileSync('temp.package.json','package.json');fs.unlinkSync('temp.package.json');\" | bun run -",
1414
"format": "prettier --write \"**/*.{ts,tsx,json,md}\"",
1515
"test": "jest",
@@ -81,10 +81,5 @@
8181
"trustedDependencies": [
8282
"@homebridge/node-pty-prebuilt-multiarch",
8383
"@vscode/ripgrep"
84-
],
85-
"devDependencies": {
86-
"eslint-config-prettier": "^10.1.2",
87-
"globals": "^16.0.0",
88-
"typescript-eslint": "^8.32.0"
89-
}
84+
]
9085
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
"start-nushell": "bun --cwd npm-app start-nushell",
3434
"format": "prettier --write \"**/*.{ts,tsx,json,md}\"",
3535
"bump": "bun run scripts/bump-version.ts",
36-
"deploy:npm-app": "infisical run --env=prod --log-level=warn -- bun run bump && cd npm-app && npm publish",
36+
"deploy:npm-app": "bun run bump && cd npm-app && npm publish",
3737
"test:patch": "bun test test/__src__/patch.test.ts",
3838
"clean": "nx run-many --target=clean --all",
3939
"build": "nx reset && nx run-many --target=build --all",

tsconfig.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
"files": [],
33
"references": [
44
{ "path": "./tsconfig.env.json" },
5+
{ "path": "common" },
56
{ "path": "backend" },
67
{ "path": "common" },
78
{ "path": "npm-app" },

0 commit comments

Comments
 (0)