vaderjs 1.4.1-ui7iuy47 → 1.4.2-kml56

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.
@@ -0,0 +1,98 @@
1
+ import { Glob } from 'bun'
2
+ import fs from 'fs'
3
+ const glob = new Glob("/**/*.{ts,tsx,js,jsx}", {
4
+ absolute: true,
5
+ });
6
+ /**
7
+ * @description This function generates cloudflare functions from the routes folder
8
+ */
9
+ async function generate(){
10
+ let config = await import(process.cwd() + '/vader.config.js').then((config) => { return config.default })
11
+ let start = Date.now()
12
+ for(var i of glob.scanSync({cwd: process.cwd() + '/routes', absolute: true})){
13
+ let data = await Bun.file(i).text()
14
+ let method = ''
15
+ i = i.replaceAll('\\', '/').replace(process.cwd(), '')
16
+ if(!data.includes(data.match(new RegExp('export\\s+default')))){
17
+ throw new Error('File must have a default export')
18
+ }
19
+ data.split('\n').forEach((line, index) => {
20
+ if(line.includes('GET') || line.includes('POST') || line.includes('PUT') || line.includes('DELETE') && line.includes('function')){
21
+ line = line.replace(/export\s+default\s+async\s+function/g, line.includes('async') ? 'async function' : 'function')
22
+ method = line.split('function')[1].split('(')[0].trim()
23
+ data = data.replace(data.split('\n')[index], line)
24
+ data = data + `\nexport async function onRequest${method.toLowerCase().charAt(0).toUpperCase() + method.toLowerCase().slice(1)}(request){
25
+ let req = {
26
+ url: request.request.url,
27
+ method: request.request.method,
28
+ headers: request.request.headers,
29
+ }
30
+ let res = {
31
+ params: (param) => {
32
+ return request.params[param]
33
+ },
34
+ query: (param) => {
35
+ let url = new URL(request.request.url)
36
+ return url.searchParams.get(param)
37
+ }
38
+ }
39
+ let response = await ${method}({req, res})
40
+ return response
41
+ }
42
+ `
43
+ }
44
+ });
45
+
46
+ let env = `globalThis.env = {
47
+
48
+ ${
49
+ Object.keys(config?.env).map((key) => {
50
+ return `${key}:"${config.env[key]}",`
51
+ } ).join('\n')
52
+ }
53
+ ${Object.keys(process.env).map((key) => {
54
+ let value = process.env[key].replace(/"/g, '\\"')
55
+ value = value.replace(/\\n/g, '\\n') // remove new lines
56
+ value = value.replace(/\\r/g, '\\r') // remove carriage returns
57
+ value = value.replace(/\\t/g, '\\t') // remove tabs
58
+ value = value.replace('/\/g', '\\\\') // remove octal escape sequences
59
+ // remove octal escape sequences
60
+ value = value.replace(/\\[0-7]{1,3}/g, '');
61
+ return `${key}:"${value}"`
62
+ } ).join(',\n')
63
+ }
64
+ };
65
+ `
66
+ // make env all on one line
67
+ env = env.replace(/\n/g, '')
68
+ data = env + data
69
+
70
+
71
+ fs.mkdirSync(process.cwd() + '/build/functions' + i.split('/routes')[1].split('/').slice(0, -1).join('/'), { recursive: true })
72
+ fs.writeFileSync(process.cwd() + '/build/functions/' + i.split('/routes')[1].replace('.ts', '.js').replace('.tsx', '.js').replace('.jsx', '.js'), data)
73
+
74
+ let nodeModules = process.cwd() + '/node_modules/vaderjs/plugins/cloudflare/toCopy'
75
+ let glb = new Glob('**/*', {
76
+ absolute: true,
77
+ cwd: nodeModules
78
+ })
79
+ for(var file of glb.scanSync({cwd: nodeModules, absolute: true})){
80
+ file = file.replaceAll('\\', '/').replace(nodeModules, '')
81
+ if(fs.existsSync('build/' + file.split('/toCopy')[1])) continue
82
+ let data = await Bun.file(file).text()
83
+ let path = file.split('/toCopy')[1]
84
+ Bun.write(process.cwd() + '/build/' + path, data)
85
+ }
86
+ }
87
+ console.log(`\x1b[32msuccess \x1b[0m - Cloudflare Functions Compiled in: ${Date.now() - start}ms`)
88
+ }
89
+ export default {
90
+ name: 'Cloudflare Functions Plugin',
91
+ description: 'This plugin utilizes cloudflare functios for server side rendering',
92
+ version: '0.0.1',
93
+ type: "SSR",
94
+ init: async (path, options) => {
95
+ console.log(`\x1b[32mevent \x1b[0m - Cloudflare Functions Plugin Initialized`)
96
+ await generate()
97
+ }
98
+ }