forked from lucaperret/kap-now
-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
89 lines (81 loc) · 2.24 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
77
78
79
80
81
82
83
84
85
86
87
88
89
"use strict";
const { createHash } = require("crypto");
const path = require("path");
const through2 = require("through2");
const { readFile } = require("fs-extra");
const ejs = require("ejs");
const action = async (context) => {
const uploadEndpoint = "https://api.vercel.com/v2/now/files";
const deploymentEndpoint = "https://api.vercel.com/v12/now/deployments";
const Authorization = "Bearer " + context.config.get("token");
const filePath = await context.filePath();
const data = await readFile(filePath);
const sha = createHash("sha1").update(data).digest("hex");
const stream = through2();
stream.write(data);
stream.end();
context.setProgress("Uploading…");
await context.request(uploadEndpoint, {
headers: {
"Content-Type": "application/octet-stream",
"x-now-digest": sha,
"x-now-size": data.length,
Authorization,
},
body: stream,
});
context.setProgress("Generating Landing Page…");
const page = await ejs.renderFile(
path.join(__dirname, "template.ejs"),
{ title: context.defaultFileName, format: context.format },
{ async: true }
);
context.setProgress("Creating Vercel deployment…");
const deploymentResponse = await context.request(deploymentEndpoint, {
headers: {
Authorization,
},
json: true,
body: {
name: context.config.get("name"),
files: [
{
file: context.defaultFileName,
sha,
size: data.length,
},
{
file: "index.html",
data: page,
},
],
projectSettings: {
framework: null,
},
},
});
context.copyToClipboard(`https://${deploymentResponse.body.url}`);
context.notify(
`URL to the ${context.prettyFormat} has been copied to the clipboard`
);
};
const vercel = {
title: "Share on Vercel",
formats: ["gif", "mp4", "webm", "apng"],
action,
config: {
token: {
title: "Vercel Personal Access Token",
description: "Create one here https://vercel.com/account/tokens",
type: "string",
required: true,
},
name: {
title: "Name used in the deployment URL",
type: "string",
default: "kapture",
required: true,
},
},
};
exports.shareServices = [vercel];