generated from usdigitalresponse/template-public-code
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun-airtable-script.ts
More file actions
105 lines (94 loc) · 2.86 KB
/
run-airtable-script.ts
File metadata and controls
105 lines (94 loc) · 2.86 KB
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import * as vm from 'node:vm'
import * as fs from 'fs'
import type { Collaborator } from './sdk/globals/collaborator'
import type { DefaultCursor } from './sdk/globals/cursor'
import type { Mutation } from './sdk/globals/mutations'
import {
consoleAggregator,
ConsoleAggregator,
ConsoleMessage,
} from './console-aggregator'
type DefaultDateLocale = 'friendly' | 'us' | 'european' | 'iso'
type Output = [string, number, boolean] | { key: string; value: string }[]
type RunScriptOptions = {
script: string
base: { base: unknown } | unknown
inAutomation?: boolean
defaultCursor?: DefaultCursor | false
currentUser?: Collaborator | false
mockInput?: unknown
// eslint-disable-next-line @typescript-eslint/no-unsafe-function-type
mockFetch?: Function | false
defaultDateLocale?: DefaultDateLocale
}
type RunScriptResult = {
output: Output
mutations: Mutation[]
console: ConsoleMessage[]
}
type RunContext = {
__base: unknown
__inAutomation: boolean
__output?: unknown[]
__mutations?: Mutation[]
__defaultCursor: DefaultCursor | false
__currentUser: Collaborator | false
__isAirtableScriptTestEnvironment: boolean
__mockInput?: unknown
// eslint-disable-next-line @typescript-eslint/no-unsafe-function-type
__mockFetch?: Function | false
__input?: unknown
__defaultDateLocale: DefaultDateLocale
console: ConsoleAggregator
}
let sdkScript: string | null = null
/**
* Runs a given Airtable script against a base fixture. Full definition is in src/environment/index.ts
*/
const runAirtableScript = async ({
script,
base,
inAutomation = false,
defaultCursor = false,
currentUser = false,
mockInput = undefined,
mockFetch = false,
defaultDateLocale = 'us',
}: RunScriptOptions): Promise<RunScriptResult> => {
if (!sdkScript) {
// The path is dynamically rewritten in the build script
const sdkScriptPath = process.env.JEST_AIRTABLE_TS_DEV
? './src/environment/sdk.js'
: __dirname + '/sdk.js'
sdkScript = fs.readFileSync(sdkScriptPath, 'utf8').toString()
}
const context: RunContext = {
// @ts-ignore
__base: base.base ? { ...base.base } : base,
__inAutomation: inAutomation,
__defaultCursor: defaultCursor,
__currentUser: currentUser,
__isAirtableScriptTestEnvironment: true,
__mockInput: mockInput,
__mockFetch: mockFetch,
__defaultDateLocale: defaultDateLocale,
console: consoleAggregator(),
}
vm.createContext(context)
vm.runInContext(sdkScript, context)
// We need to run the script in an async function so that we can use await
// directly inside the script.
vm.runInContext(
`;(async () => {
${script}
})()`,
context
)
return {
output: (context.__output as Output) || [],
mutations: context.__mutations || [],
console: context.console._getMessages(),
}
}
export default runAirtableScript
export { RunScriptOptions, RunScriptResult }