rajt 0.0.54 → 0.0.56

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "rajt",
3
3
  "description": "A serverless bundler layer, fully typed for AWS Lambda (Node.js and LLRT) and Cloudflare Workers.",
4
- "version": "0.0.54",
4
+ "version": "0.0.56",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",
7
7
  "exports": {
@@ -19,17 +19,22 @@
19
19
  ],
20
20
  "scripts": {
21
21
  "dev": "tsx watch src/dev.ts",
22
- "local": "bun run --silent build && bun run --silent sam:local",
23
- "build": "bun run --silent cache:routes && bun run --silent export && bun run --silent clean:temp",
24
- "build:watch": "chokidar \"../../{actions,configs,models,utils}/**/*.ts\" -c \"bun run --silent build\" --initial",
25
- "export": "node src/esbuild.mjs",
26
- "package": "bun run --silent build && bun run --silent sam:package",
27
- "deploy": "bun run --silent build && bun run --silent sam:package && bun run --silent sam:deploy",
28
- "update": "bun run --silent build && bun run --silent zip && bun run --silent sam:update",
22
+ "aws:build": "bun run --silent cache:routes && bun run --silent aws:export && bun run --silent clean:temp",
23
+ "cf:build": "bun run --silent cache:routes && bun run --silent cf:export && bun run --silent clean:temp",
24
+ "aws:build:watch": "chokidar \"../../{actions,configs,enums,locales,middlewares,models,utils}/**/*.ts\" -c \"bun run --silent aws:build\" --initial",
25
+ "cf:build:watch": "chokidar \"../../{actions,configs,enums,locales,middlewares,models,utils}/**/*.ts\" -c \"bun run --silent cf:build\" --initial",
26
+ "aws:export": "node src/esbuild.mjs aws",
27
+ "cf:export": "node src/esbuild.mjs cf",
28
+ "aws:local": "bun run --silent aws:build && bun run --silent sam:local",
29
+ "aws:package": "bun run --silent aws:build && bun run --silent sam:package",
30
+ "aws:deploy": "bun run --silent aws:build && bun run --silent sam:package && bun run --silent sam:deploy",
31
+ "aws:update": "bun run --silent aws:build && bun run --silent zip && bun run --silent sam:update",
29
32
  "sam:local": "sam local start-api --warm-containers LAZY --debug --template-file ../../template-dev.yaml",
30
33
  "sam:package": "sam package --template-file ../../template-prod.yaml --output-template-file ../../packaged.yaml",
31
34
  "sam:deploy": "sam deploy --template-file ../../packaged.yaml --stack-name rajt-llrt --capabilities CAPABILITY_IAM",
32
35
  "sam:update": "source ../../.env.prod && aws lambda update-function-code --function-name $AWS_NAME --zip-file fileb://../../lambda.zip --region $AWS_REGION --no-cli-pager 2>&1 >/dev/null",
36
+ "cf:local": "bun run --silent cf:build && cd ../../dist && bunx wrangler dev",
37
+ "cf:deploy": "bun run --silent cf:build && cd ../../dist && bunx wrangler deploy",
33
38
  "cache:routes": "tsx src/scripts/cache-routes.ts",
34
39
  "ensure-dirs": "rm -rf ../../dist ../../tmp && mkdir -p ../../tmp && chmod 755 ../../tmp && mkdir -p ../../dist && chmod 755 ../../dist",
35
40
  "clean": "rm -rf ../../dist ../../tmp",
@@ -45,7 +50,7 @@
45
50
  "@hono/zod-validator": "^0.4.3",
46
51
  "@types/node": "^20.11.0",
47
52
  "chokidar-cli": "^3.0.0",
48
- "cripta": "0.1.8",
53
+ "cripta": "^0.1",
49
54
  "dotenv": "^16.5.0",
50
55
  "esbuild": "^0.25.2",
51
56
  "hono": "^4.7.6",
@@ -88,4 +93,4 @@
88
93
  "bun",
89
94
  "nodejs"
90
95
  ]
91
- }
96
+ }
package/src/dev.ts CHANGED
@@ -6,6 +6,9 @@ import { registerHandler, registerMiddleware } from './register'
6
6
  import { Ability } from './auth'
7
7
  import { getAvailablePort } from './utils/port'
8
8
  import jsonImport from './utils/json-import'
9
+ import { setEnv, detectEnvironment } from './utils/environment'
10
+
11
+ setEnv(detectEnvironment())
9
12
 
10
13
  config({ path: '../../.env.dev' })
11
14
 
package/src/esbuild.mjs CHANGED
@@ -1,7 +1,20 @@
1
1
  import esbuild from 'esbuild'
2
- import { dirname, join, relative } from 'path'
2
+ import { basename, dirname, join, relative } from 'node:path'
3
3
  import { fileURLToPath } from 'url'
4
- import { readFile, stat, writeFile } from 'fs/promises'
4
+ import { mkdirSync, existsSync, readdirSync, rmSync, copyFileSync } from 'node:fs'
5
+ import { readFile, stat, writeFile } from 'node:fs/promises'
6
+
7
+ const fail = (e) => {
8
+ console.error('❌ Build failed' + (e ? ':' : ''), e || '')
9
+ process.exit(1)
10
+ }
11
+
12
+ const args = process.argv.slice(2)
13
+ const platform = args[0] || ''
14
+
15
+ const platforms = ['aws', 'cf']
16
+ if (!platform || !platforms.includes(platform))
17
+ fail()
5
18
 
6
19
  const __dirname = dirname(fileURLToPath(import.meta.url))
7
20
 
@@ -16,7 +29,7 @@ const formatTime = (ms) => {
16
29
  }
17
30
 
18
31
  const buildOptions = {
19
- entryPoints: [join(__dirname, 'prod.ts')],
32
+ entryPoints: [join(__dirname, `prod-${platform}.ts`)],
20
33
  bundle: true,
21
34
  minify: true,
22
35
  outfile: join(__dirname, '../../../dist/index.js'),
@@ -69,21 +82,33 @@ const buildOptions = {
69
82
  ]
70
83
  }
71
84
 
72
- try {
73
- const startTime = Date.now()
74
- const result = await esbuild.build(buildOptions)
85
+ const startTime = Date.now()
86
+ const cwd = join(__dirname, '../../..')
75
87
 
76
- const cwd = join(__dirname, '../../..')
88
+ const distDir = join(cwd, 'dist')
89
+ existsSync(distDir)
90
+ ? readdirSync(distDir).forEach(file => rmSync(join(distDir, file), { recursive: true, force: true }))
91
+ : mkdirSync(distDir, { recursive: true })
77
92
 
78
- const outputFile = buildOptions.outfile
79
- const stats = await stat(outputFile)
80
- const size = formatSize(stats.size)
93
+ for (const file of await readdirSync(distDir))
94
+ await rmSync(join(distDir, file))
81
95
 
82
- console.log(`\n⚡️ Done in ${formatTime(Date.now() - startTime)}`)
83
- console.log(` ${relative(join(cwd, 'node_modules/rajt/src'), buildOptions.entryPoints[0])} → ${relative(cwd, outputFile)}`)
84
- console.log(` Size: ${size}`)
85
- console.log(` Files: ${Object.keys(result.metafile.outputs).length}`)
86
- } catch (error) {
87
- console.error('❌ Build failed:', error)
88
- process.exit(1)
96
+ for (let file of [
97
+ 'wrangler.toml',
98
+ ]) {
99
+ file = join(cwd, file)
100
+ if (existsSync(file))
101
+ copyFileSync(file, join(cwd, 'dist', basename(file)))
89
102
  }
103
+
104
+ esbuild.build(buildOptions)
105
+ .then(async result => {
106
+ const outputFile = buildOptions.outfile
107
+ const stats = await stat(outputFile)
108
+ const size = formatSize(stats.size)
109
+
110
+ console.log(`\n⚡️ Done in ${formatTime(Date.now() - startTime)}`)
111
+ console.log(` ${relative(join(cwd, 'node_modules/rajt/src'), buildOptions.entryPoints[0])} → ${relative(cwd, outputFile)}`)
112
+ console.log(` Size: ${size}`)
113
+ console.log(` Files: ${Object.keys(result.metafile.outputs).length}`)
114
+ }).catch(fail)
@@ -16,5 +16,4 @@ Ability.fromRoutes(routes)
16
16
  // @ts-ignore
17
17
  const app = createApp({ routes })
18
18
 
19
- // export default app // AWS Lambda & Cloudflare Workers
20
19
  export const handler = handle(app) // AWS Lambda (LLRT)
package/src/prod-cf.ts ADDED
@@ -0,0 +1,18 @@
1
+ import createApp from './create-app'
2
+ import { Ability } from './auth'
3
+
4
+ // @ts-ignore
5
+ await import('../../../tmp/import-routes.mjs')
6
+
7
+ // @ts-ignore
8
+ const routes = (await import('../../../tmp/routes.json')).default
9
+
10
+ // @ts-ignore
11
+ Ability.roles = (await import('../../../roles.json')).default
12
+ // @ts-ignore
13
+ Ability.fromRoutes(routes)
14
+
15
+ // @ts-ignore
16
+ const app = createApp({ routes })
17
+
18
+ export default app
@@ -23,7 +23,7 @@ async function cacheRoutes() {
23
23
  const iPath = '../../tmp/import-routes.mjs'
24
24
  ensureDir(iPath)
25
25
  writeFileSync(iPath, `// AUTO-GENERATED FILE - DO NOT EDIT
26
- ${env.map(([key, val]) => `process.env.${key} = ${JSON.stringify(val)}`).join('\n')}
26
+ ${env?.length ? `import { Envir } from '../node_modules/t0n/dist/index'\nEnvir.add({${env.map(([key, val]) => key + ':' + JSON.stringify(val)).join(',')}})` : ''}
27
27
 
28
28
  import { registerHandler, registerMiddleware } from '../node_modules/rajt/src/register'
29
29
 
@@ -1,19 +1,33 @@
1
- export default function getEnvironment() {
1
+ const prd = Symbol('prd')
2
+ const dev = Symbol('dev')
3
+
4
+ let env = prd
5
+
6
+ export const getEnv = () => env
7
+ export const setEnv = (e: symbol) => env = e
8
+
9
+ export function detectEnvironment() {
2
10
  try {
3
11
  if (
4
- process.env?.npm_lifecycle_event === 'dev'
12
+ process.env?.npm_lifecycle_event == 'dev'
5
13
  || process.env?.AWS_SAM_LOCAL
6
- || process?.argv?.includes('--dev')
14
+ // || process?.argv?.includes('--dev')
15
+ || process?.argv?.some(arg => ['--dev', '--development', '--watch'].includes(arg))
7
16
  || process?.execArgv?.includes('--watch')
8
17
  || import.meta.url?.includes('localhost')
9
18
  ) {
10
- return 'dev'
19
+ return dev
11
20
  }
12
21
  } catch (e) { }
13
22
 
14
- return 'prod'
23
+ return prd
15
24
  }
16
25
 
17
- export const isDev = () => getEnvironment() === 'dev'
18
- export const isProd = () => getEnvironment() === 'prod'
26
+ export const isEnv = (e: symbol) => env == e
27
+
28
+ export const isDev = () => env == dev
29
+ export const isProd = () => env == prd
30
+
31
+ export const isDevelopment = isDev
32
+ export const isProduction = isProd
19
33
  export const isPrd = isProd
@@ -13,7 +13,7 @@ export default function getLastCommitHash(path: string = '.git') {
13
13
 
14
14
  return headContent
15
15
  } catch (e) {
16
- console.error('Error reading HEAD file: ', e?.message)
16
+ // console.error('Error reading HEAD file: ', e?.message)
17
17
  return null
18
18
  }
19
19
  }