-
Notifications
You must be signed in to change notification settings - Fork 1
/
makefile.js
executable file
·287 lines (261 loc) · 7.46 KB
/
makefile.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
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
#!/usr/bin/env node
// @ts-check
/**
* @typedef {import("node:readline").Interface} Interface
*/
const { argv, chdir, env, exit, stdin, stdout } = require("node:process")
const { copyFile, readFile, rm, writeFile } = require("node:fs/promises")
const { createInterface } = require("node:readline")
const { error, log, warn } = require("node:console")
const { execSync } = require("node:child_process")
const { existsSync } = require("node:fs")
const { join } = require("node:path")
const { build } = require("esbuild")
const sade = require("sade")
const pack = require("./package.json")
const root = __dirname
const dist = join(root, "dist")
const app = sade("./makefile.js")
/**
* @typedef {Object} Version
* @property {string} Version
* @property {string} Deployment
*/
/**
* @param {string} command
* @returns {string}
*/
function exec(command) {
return execSync(command, { encoding: "utf-8", stdio: "inherit" })
}
/**
* @param {Interface} line
* @param {string} message
* @returns {Promise<string>}
*/
function question(line, message) {
return new Promise((resolve) => {
line.question(message, resolve)
})
}
/**
* @param {string} directory
* @returns {Promise<void>}
*/
async function clearDirectory(directory) {
if (existsSync(directory)) {
await rm(directory, { force: true, recursive: true })
}
}
/**
* @param {string} input
* @param {string} output
* @returns {Promise<void>}
*/
async function buildEntry(input, output) {
const entry = join(input, "index.js")
await build({
bundle: true,
entryPoints: [entry],
external: ["zapier-platform-core"],
minify: true,
outdir: output,
platform: "node",
target: "node18"
})
}
/**
* @param {typeof pack} pack
* @param {string} directory
* @returns {Promise<void>}
*/
async function buildPackage(pack, directory) {
const object = {
dependencies: {
"zapier-platform-core": pack.dependencies["zapier-platform-core"]
},
main: pack.main,
name: pack.name,
type: pack.type,
version: pack.version
}
const content = JSON.stringify(object, undefined, 2)
const file = join(directory, "package.json")
await writeFile(file, content)
}
/**
* @param {string} directory
* @returns {Promise<void>}
*/
async function loadEnvironment(directory) {
const variables = [
"DOC_SPACE_BASE_URL",
"DOC_SPACE_USERNAME",
"DOC_SPACE_PASSWORD",
"ZAPIER_DEPLOY_KEY"
]
const file = join(directory, ".env")
if (!existsSync(file)) {
log("info: loading environment variables from the process")
variables.forEach((key) => {
const value = env[key]
if (!value) {
warn(`warn: the ${key} isn't set or has an empty value`)
return
}
})
return
}
log("info: loading environment variables from the dot file")
const content = await readFile(file, { encoding: "utf-8" })
variables.forEach((key) => {
const pattern = new RegExp(`^${key}=([\\S\\s]*?)$`, "m")
const result = content.match(pattern)
if (!result) {
warn(`warn: the ${key} isn't set or has an empty value`)
return
}
const [, value] = result
if (!value) {
warn(`warn: the ${key} isn't set or has an empty value`)
return
}
env[key] = value
})
}
/**
* @param {string} directory
* @returns {Promise<string>}
*/
async function generateNotes(directory) {
const file = join(directory, "CHANGELOG.md")
const content = await readFile(file, { encoding: "utf-8" })
const version = pack.version.replace(/\./g, "\\.")
const pattern = new RegExp(`^## ${version} \\(\\d{4}-\\d{2}-\\d{2}\\)([\\S\\s](?!(?:##)))*$`, "m")
const result = content.match(pattern)
if (!result) {
return ""
}
const [notes] = result
if (!notes) {
return ""
}
return notes
}
/**
* @param {string} input
* @param {string} output
* @returns {Promise<void>}
*/
async function copyMeta(input, output) {
const from = join(input, ".zapierapprc")
const to = join(output, ".zapierapprc")
await copyFile(from, to)
}
app
.command("all")
.describe("Run an audit of the app and then build it")
.action(() => {
exec("pnpm check")
exec("pnpm lint")
exec("pnpm tt")
exec("pnpm build")
})
app
.command("build")
.describe("Build the app")
.action(async () => {
await clearDirectory(dist)
await buildEntry(root, dist)
await buildPackage(pack, dist)
await copyMeta(root, dist)
chdir(dist)
// We don't use the `--frozen-lockfile` option here because we assume that
// all dependencies, except for the `zapier-platform-core`, have already
// been bundled.
exec("pnpm install --prod")
exec("zapier build")
})
app
.command("install-lefthook")
.describe("Install Lefthook locally using pnpm")
.action(() => {
exec("pnpm lefthook install")
})
app
.command("install-zapier")
.describe("Install Zapier CLI globally using pnpm")
.action(() => {
const version = pack.dependencies["zapier-platform-core"]
exec(`pnpm install --global "zapier-platform-cli@${version}"`)
})
app
.command("promote")
.describe("Promote the current version of the app")
.action(() => {
exec(`zapier promote ${pack.version} --yes`)
})
app
.command("release")
.describe("Release the current version of the app")
.action(async () => {
const notes = await generateNotes(root)
exec(`gh release create "${pack.version}" --notes "${notes}"`)
const build = join(dist, "build/build.zip")
const source = join(dist, "build/source.zip")
exec(`gh release upload "${pack.version}" "${build}" "${source}"`)
})
app
.command("setup-env")
.describe("Setup environment dot file")
.action(async () => {
const line = createInterface({
input: stdin,
output: stdout
})
/* eslint-disable @stylistic/max-len */
const DOC_SPACE_BASE_URL = await question(line, "The base URL of DocSpace for testing purposes (DOC_SPACE_BASE_URL): ")
const DOC_SPACE_USERNAME = await question(line, "The username of DocSpace user for testing purposes (DOC_SPACE_USERNAME): ")
const DOC_SPACE_PASSWORD = await question(line, "The password of DocSpace user for testing purposes (DOC_SPACE_PASSWORD): ")
const ZAPIER_DEPLOY_KEY = await question(line, "The deploy key from developer.zapier.com for testing purposes (ZAPIER_DEPLOY_KEY): ")
/* eslint-enable @stylistic/max-len */
line.close()
const file = join(root, ".env")
const content =
`DOC_SPACE_BASE_URL=${DOC_SPACE_BASE_URL}\n` +
`DOC_SPACE_USERNAME=${DOC_SPACE_USERNAME}\n` +
`DOC_SPACE_PASSWORD=${DOC_SPACE_PASSWORD}\n` +
`ZAPIER_DEPLOY_KEY=${ZAPIER_DEPLOY_KEY}`
await writeFile(file, content)
})
app
.command("tt")
.describe("Run validation with tests")
.action(async () => {
await loadEnvironment(root)
exec("zapier test")
})
app
.command("upload")
.describe("Upload the current version of the app")
.option("--force")
.action((options) => {
if (!options.force) {
const rawVersions = execSync("zapier versions --format json", { encoding: "utf-8" })
/** @type {Version[]} */
const versions = JSON.parse(rawVersions)
const current = versions.find((version) => (
version.Version === pack.version
))
if (current) {
warn(`warn: the app version ${pack.version} has already been uploaded`)
if (current.Deployment !== "non-production") {
error(`error: the app version ${pack.version} has already been promoted`)
exit(1)
}
}
}
chdir(dist)
exec("zapier upload")
})
app.parse(argv)