forked from kriasoft/graphql-starter-kit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeploy-proxy.js
67 lines (59 loc) · 1.88 KB
/
deploy-proxy.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
/**
* Deploys application bundle to Cloudflare. Usage:
*
* $ yarn deploy [--version=#0] [--env=#1] [--no-download]
*
* @see https://developers.cloudflare.com/workers/
* @see https://api.cloudflare.com/#worker-script-upload-worker
* @copyright 2016-present Kriasoft (https://git.io/vMINh)
*/
require("env");
const fs = require("fs");
const got = require("got");
const path = require("path");
const spawn = require("cross-spawn");
const minimist = require("minimist");
const { name } = require("../proxy/package.json");
const env = process.env;
const root = path.resolve(__dirname, "..");
const args = minimist(process.argv.slice(2), {
default: { env: "dev", version: env.VERSION, download: true },
});
const source = `gs://${env.PKG_BUCKET}/${name}_${args.version}.js`;
const script = `${name}${args.env === "prod" ? "" : `-${args.env}`}`;
// Optionally, download the bundle from GCS bucket
if (args.download) {
const p = spawn.sync("gsutil", ["cp", source, `dist/${name}.js`], {
cwd: root,
stdio: "inherit",
});
if (p.error) console.error(p.error);
if (p.status !== 0) process.exit(p.status || 1);
console.log(`Deploying ${source} to workers/scripts/${script}...`);
} else {
console.log(`Deploying dist/${name}.js to workers/scripts/${script}...`);
}
const cf = got.extend({
prefixUrl: `https://api.cloudflare.com/client/v4/accounts/${env.CLOUDFLARE_ACCOUNT_ID}/`,
headers: {
Authorization: `Bearer ${env.CLOUDFLARE_API_TOKEN}`,
"Content-Type": "application/javascript",
},
responseType: "json",
});
cf.put({
url: `workers/scripts/${script}`,
body: fs.readFileSync(path.resolve(root, `dist/${name}.js`), "utf-8"),
})
.then(({ body }) => {
if (body.success) {
delete body.result.script;
console.log(body.result);
} else {
throw new Error(body.errors[0]);
}
})
.catch((err) => {
console.error(err);
process.exit(1);
});