rajt 0.0.83 → 0.0.85

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.83",
4
+ "version": "0.0.85",
5
5
  "type": "module",
6
6
  "main": "./src/index.ts",
7
7
  "bin": {
@@ -39,7 +39,7 @@
39
39
  },
40
40
  "dependencies": {
41
41
  "cripta": "^0.1",
42
- "forj": "^0.0.7",
42
+ "forj": "^0.0.9",
43
43
  "t0n": "^0.1",
44
44
  "@hono/node-server": "^1.19.9",
45
45
  "@hono/standard-validator": "^0.2.2",
@@ -1,9 +1,9 @@
1
1
  import { defineCommand } from 'citty'
2
2
  import { gray } from '../../utils/colors'
3
- import { build, normalizePlatform, platformError } from './utils'
3
+ import { build, normalizePlatform, platformError } from '../utils'
4
4
  import { wait, error, rn } from '../../utils/log'
5
5
 
6
- import { platforms } from './constants'
6
+ import { platforms } from '../constants'
7
7
 
8
8
  export default defineCommand({
9
9
  meta: {
@@ -1,9 +1,9 @@
1
1
  import { spawn } from 'node:child_process'
2
2
  import { defineCommand } from 'citty'
3
3
 
4
- import { _root, normalizePlatform, platformError, getRuntime } from './utils'
4
+ import { _root, normalizePlatform, platformError, getRuntime } from '../utils'
5
5
  import { error } from '../../utils/log'
6
- import { platforms } from './constants'
6
+ import { platforms } from '../constants'
7
7
 
8
8
  import build from './build'
9
9
 
@@ -7,7 +7,7 @@ import {
7
7
  _root, build, wait, watch, normalizePlatform, platformError, getRuntime,
8
8
  wranglerConfig, createMiniflare, localflareManifest,
9
9
  getDockerHost
10
- } from './utils'
10
+ } from '../utils'
11
11
  import { error, event, log, rn, warn } from '../../utils/log'
12
12
  import { withPort } from '../../utils/port'
13
13
  import shutdown from '../../utils/shutdown'
@@ -1,8 +1,9 @@
1
1
  import { defineCommand } from 'citty'
2
2
  import { join, relative } from 'node:path'
3
3
  import { Migrator } from 'forj'
4
- import { _root, makeFile, hasExt, camelCase, kebabCase } from './utils'
4
+ import { _root, makeFile, hasExt, camelCase, kebabCase } from '../utils'
5
5
  import { event, error } from '../../utils/log'
6
+ import * as stub from '../stubs'
6
7
 
7
8
  export default defineCommand({
8
9
  meta: {
@@ -31,32 +32,20 @@ export default defineCommand({
31
32
  case 'endpoint':
32
33
  fileName = path(kebabCase(name))
33
34
  if (!fileName.endsWith('.ts')) fileName += '.ts'
34
- const className = camelCase(name)
35
- makeFile(fileName, `import { Action } from 'rajt'
36
- import { IRequest, IResponse } from 'rajt/types'
37
-
38
- export default class ${className} extends Action {
39
- static async handle(req: IRequest, res: IResponse) {
40
- return res.ok('${className}')
41
- }
42
- }
43
- `)
35
+ makeFile(fileName, stub.replace(stub.route, { R_NAME: camelCase(name) }))
44
36
  break
45
37
  case 'migrate':
46
38
  case 'migration':
47
- fileName = path(Migrator.fileName(name), 'migration')
39
+ fileName = Migrator.fileName(name)
40
+ const [table, create] = Migrator.guess(fileName)
41
+ fileName = path(fileName, 'migration')
48
42
  if (!fileName.endsWith('.ts')) fileName += '.ts'
49
- makeFile(fileName, `import { Migration, Schema, Blueprint } from 'rajt/db'
50
-
51
- export default class ${Migrator.className(name)} extends Migration {
52
- static async run() {
53
- Schema.${name.includes('create') ? 'create' : 'table'}('users', (table: Blueprint) => {
54
- table.id()
55
- table.timestamps()
56
- })
57
- }
58
- }
59
- `)
43
+ makeFile(fileName, stub.replace(stub.migration, {
44
+ M_NAME: Migrator.className(name),
45
+ S_NAME: create ? 'create' : 'table',
46
+ T_NAME: table || 'TABLE_NAME',
47
+ M_CONTENT: create ? stub.migrationCreate : '//',
48
+ }))
60
49
  break
61
50
  case 'model':
62
51
  error('Action not yet implemented, contact the webmaster')
@@ -2,7 +2,7 @@ import { defineCommand } from 'citty'
2
2
  import { spawn } from 'node:child_process'
3
3
  import { Migrator } from 'forj'
4
4
  import { gray } from '../../utils/colors'
5
- import { _root, getRuntime } from './utils'
5
+ import { _root, getRuntime, cleanDir, d1Path, wait as WAIT } from '../utils'
6
6
  import { wait, info, event, rn, error, log } from '../../utils/log'
7
7
 
8
8
  export default defineCommand({
@@ -34,10 +34,17 @@ export default defineCommand({
34
34
  switch (action) {
35
35
  case 'migrate':
36
36
  case 'apply':
37
+ case 'fresh':
38
+ case 'refresh':
37
39
  if (!pending?.length)
38
40
  log('Nothing to compile')
39
41
  wait('Running migrations')
40
42
 
43
+ if (action.includes('fresh')) {
44
+ cleanDir(d1Path)
45
+ await WAIT(2000)
46
+ }
47
+
41
48
  await Migrator.compile([...migrated, ...pending])
42
49
 
43
50
  const child = spawn(
@@ -50,11 +57,8 @@ export default defineCommand({
50
57
  )
51
58
 
52
59
  child.on('exit', code => process.exit(code ?? 0))
53
- .on('message', msg => {
54
- process.send && process.send(msg)
55
- }).on('disconnect', () => {
56
- process.disconnect && process.disconnect()
57
- })
60
+ .on('message', msg => process.send && process.send(msg))
61
+ .on('disconnect', () => process.disconnect && process.disconnect())
58
62
 
59
63
  pending.forEach(item => event(item.name))
60
64
  break
@@ -3,7 +3,7 @@ import { defineCommand } from 'citty'
3
3
  import { inspectRoutes } from 'hono/dev'
4
4
  import { IMPORT } from 't0n'
5
5
  import { gray, purple, red, yellow } from '../../utils/colors'
6
- import { __rajt } from './utils'
6
+ import { __rajt } from '../utils'
7
7
  import { rn } from '../../utils/log'
8
8
 
9
9
  export default defineCommand({
@@ -0,0 +1,42 @@
1
+ export const replace = (str: string, map: Record<string, string>) => {
2
+ if (!str || typeof str != 'string' || !map || typeof map != 'object') return str
3
+ const entries = Object.entries(map)
4
+ const length = entries?.length || 0
5
+ if (!length) return str
6
+
7
+ str = str.trimStart()
8
+ if (length == 1)
9
+ return str.replaceAll(entries[0][0], entries[0][1])
10
+
11
+ return entries.reduce(
12
+ (acc, entry) => acc.replace(new RegExp(entry[0].replace(/[.*+?^${}()|[\]\\]/g, '\\$&'), 'g'), entry[1]),
13
+ str
14
+ )
15
+ }
16
+
17
+ export const route = `
18
+ import { Action } from 'rajt'
19
+ import { IRequest, IResponse } from 'rajt/types'
20
+
21
+ export default class R_NAME extends Action {
22
+ static async handle(req: IRequest, res: IResponse) {
23
+ return res.ok('R_NAME')
24
+ }
25
+ }
26
+ `
27
+
28
+ export const migration = `
29
+ import { Migration, Schema, Blueprint } from 'rajt/db'
30
+
31
+ export default class M_NAME extends Migration {
32
+ static async run() {
33
+ Schema.S_NAME('T_NAME', (table: Blueprint) => {
34
+ M_CONTENT
35
+ })
36
+ }
37
+ }
38
+ `
39
+ export const migrationCreate = `
40
+ table.id()
41
+ table.timestamps()
42
+ `.trim()
@@ -1,6 +1,6 @@
1
1
  import esbuild from 'esbuild'
2
2
  import { Miniflare } from 'miniflare'
3
- import { mkdirSync, existsSync, readdirSync, rmSync, copyFileSync, writeFileSync } from 'node:fs'
3
+ import { mkdirSync, existsSync, statSync, readdirSync, rmSync, unlinkSync, copyFileSync, writeFileSync } from 'node:fs'
4
4
  import { readFile, stat, writeFile } from 'node:fs/promises'
5
5
  import { basename, dirname, join, relative } from 'node:path'
6
6
 
@@ -8,14 +8,14 @@ import { findWranglerConfig, parseWranglerConfig, WRANGLER_CONFIG_FILES } from '
8
8
  import type { WranglerConfig, LocalflareManifest } from 'localflare-core'
9
9
 
10
10
  import chokidar from 'chokidar'
11
- import { gray, red } from '../../utils/colors'
11
+ import { gray, red } from '../utils/colors'
12
12
  import type { ChokidarEventName, Platform } from './types'
13
13
 
14
- import { cacheRoutes } from '../../routes'
15
- import { substep, event, error, wait as wwait, warn, log } from '../../utils/log'
14
+ import { cacheRoutes } from '../routes'
15
+ import { substep, event, error, wait as wwait, warn, log } from '../utils/log'
16
16
  import { platforms } from './constants'
17
17
 
18
- export const _root = join(dirname(new URL(import.meta.url).pathname), '../../../../../')
18
+ export const _root = join(dirname(new URL(import.meta.url).pathname), '../../../../')
19
19
  export const __rajt = join(_root, 'node_modules/rajt/src')
20
20
 
21
21
  export function normalizePlatform(platform: Platform) {
@@ -73,11 +73,7 @@ export const build = async (platform: Platform) => {
73
73
  const startTime = Date.now()
74
74
 
75
75
  const isCF = platform == 'cf'
76
- const distDir = join(_root, dist)
77
-
78
- existsSync(distDir)
79
- ? readdirSync(distDir).forEach(file => rmSync(join(distDir, file), { recursive: true, force: true }))
80
- : mkdirSync(distDir, { recursive: true })
76
+ cleanDir(join(_root, dist))
81
77
 
82
78
  if (isCF) {
83
79
  for (let file of WRANGLER_CONFIG_FILES) {
@@ -181,6 +177,24 @@ export const build = async (platform: Platform) => {
181
177
  )
182
178
  }
183
179
 
180
+ export function cleanDir(path: string) {
181
+ try {
182
+ if (!existsSync(path))
183
+ return mkdirSync(path, {recursive: true})
184
+
185
+ const files = readdirSync(path)
186
+
187
+ for (const file of files) {
188
+ const filePath = join(path, file)
189
+ const stat = statSync(filePath)
190
+
191
+ stat.isDirectory()
192
+ ? rmSync(filePath, {recursive: true, force: true})
193
+ : unlinkSync(filePath)
194
+ }
195
+ } catch {}
196
+ }
197
+
184
198
  export function wranglerConfig(file?: string) {
185
199
  file ??= findWranglerConfig(_root)
186
200
  if (!file) {
@@ -237,6 +251,12 @@ export function localflareManifest(opts: WranglerConfig): LocalflareManifest {
237
251
  }
238
252
  }
239
253
 
254
+ const wranglerPath = '.wrangler/state/v3'
255
+ export const kvPath = join(_root, wranglerPath, 'kv')
256
+ export const d1Path = join(_root, wranglerPath, 'd1')
257
+ export const r2Path = join(_root, wranglerPath, 'r2')
258
+ export const doPath = join(_root, wranglerPath, 'durable_objects')
259
+
240
260
  export function createMiniflare(opts = {}) {
241
261
  const entry = join(_root, opts?.main || opts?.script)
242
262
  return new Miniflare({
@@ -264,11 +284,11 @@ export function createMiniflare(opts = {}) {
264
284
  // r2Buckets: Object.fromEntries((opts.r2_buckets || []).map(r2 => [r2.binding, r2.bucket_name])),
265
285
  // durableObjects: Object.fromEntries((opts.durable_objects?.bindings || []).map(do => [do.name, do.class_name])),
266
286
 
267
- kvPersist: join(_root, '.wrangler/state/v3/kv'),
268
- cachePersist: join(_root, '.wrangler/state/v3/cache'),
269
- d1Persist: join(_root, '.wrangler/state/v3/d1'),
270
- r2Persist: join(_root, '.wrangler/state/v3/r2'),
271
- durablesPersist: join(_root, '.wrangler/state/v3/durable_objects'),
287
+ kvPersist: kvPath,
288
+ cachePersist: join(_root, wranglerPath, 'cache'),
289
+ d1Persist: d1Path,
290
+ r2Persist: r2Path,
291
+ durablesPersist: doPath,
272
292
 
273
293
  verbose: false,
274
294
 
File without changes
File without changes