-
Notifications
You must be signed in to change notification settings - Fork 8
/
index.js
76 lines (70 loc) · 2.14 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
let {
compileProject,
compileHandler,
getTsConfig,
} = require('./_compile')
let handlers = require('./handlers')
module.exports = {
set: {
runtimes: function ({ inventory }) {
let { arc } = inventory.inv._project
let build = '.build'
let baseRuntime = 'nodejs20.x'
if (arc.typescript) {
let settings = Object.fromEntries(arc.typescript)
if (settings.build && typeof settings.build === 'string') {
build = settings.build
}
if (settings['base-runtime'] && typeof settings['base-runtime'] === 'string') {
baseRuntime = settings['base-runtime']
}
}
return {
name: 'typescript',
type: 'transpiled',
build,
baseRuntime,
}
}
},
create: {
handlers: ({ lambda: { pragma } }) => {
let body = handlers[pragma] ?? handlers.custom
return { filename: 'index.ts', body }
}
},
deploy: {
// TODO: add support for custom TS check commands (e.g. `tsc -p .`)?
start: compileProject
},
sandbox: {
start: compileProject,
watcher: async function (params) {
let { filename, /* event, */ inventory } = params
if (filename.endsWith('.ts') || filename.endsWith('.tsx')) {
let { lambdasBySrcDir, shared, views } = inventory.inv
// Second pass filter by shared dirs
if (filename.startsWith(shared?.src) ||
filename.startsWith(views?.src)) {
await compileProject(params)
return
}
// Second pass filter by Lambda dir
let lambda = Object.values(lambdasBySrcDir).find(({ src }) => filename.startsWith(src))
if (!lambda) { return }
let start = Date.now()
let { name, pragma } = lambda
let { cwd } = inventory.inv._project
let globalTsConfig = getTsConfig(cwd)
console.log(`Recompiling handler: @${pragma} ${name}`)
try {
await compileHandler({ inventory, lambda, globalTsConfig })
console.log(`Compiled in ${(Date.now() - start) / 1000}s\n`)
}
catch (err) {
console.log('esbuild error:', err)
}
}
}
},
}