-
Notifications
You must be signed in to change notification settings - Fork 0
/
extension.js
147 lines (138 loc) · 4.54 KB
/
extension.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
// Dependencies
const vscode = require("vscode");
const { Web3Storage } = require("web3.storage");
const dotenv = require("dotenv");
const { selectFolder, getCurrentWorkspaceFolderPath } = require("./options");
const { getFilesFromPath } = require("files-from-path");
const fs = require("fs");
// Environment variable paths
const folderPath = __dirname + "/.folderPath.env";
dotenv.config({ path: folderPath });
const apiToken = __dirname + "/.env";
dotenv.config({ path: apiToken });
// web3.storage API token
function getToken() {
return process.env.API_TOKEN;
}
// Get files from path
async function getFiles(path) {
const files = await getFilesFromPath(path);
files.forEach((file) => {
if (file.name.charAt(0) === "/") {
file.name = file.name.split("/").slice(2).join("/");
} else {
file.name = file.name.split("/").slice(1).join("/");
}
});
return files;
}
// Validate web3.storage API token
function isValidWeb3StorageApiKey(apiKey) {
const pattern = /^eyJ[a-zA-Z0-9-_]+\.[a-zA-Z0-9-_]+\.[a-zA-Z0-9-_]+$/;
return pattern.test(apiKey);
}
// Set web3.storage API token
async function setApiToken() {
const userToken = await vscode.window.showInputBox({
placeHolder: "Enter your web3.storage API token",
prompt: "API Token",
ignoreFocusOut: true,
});
if (userToken && isValidWeb3StorageApiKey(userToken)) {
process.env.API_TOKEN = userToken;
try {
fs.writeFileSync(apiToken, `API_TOKEN=${userToken}`);
vscode.window.showInformationMessage("API token saved!");
// Set web3.storage API token
const client = new Web3Storage({ token: getToken() });
} catch (e) {
console.log(e);
}
} else {
vscode.window.showWarningMessage(
"Invalid API key. Please enter a valid web3.storage API key."
);
}
}
/**
* @param {vscode.ExtensionContext} context
*/
function activate(context) {
// Set web3.storage API token
context.subscriptions.push(
vscode.commands.registerCommand("dhost.token", async () => {
await setApiToken();
})
);
// Check if the folderPath.env file exists, and if so, set the folder path from it.
if (fs.existsSync(folderPath)) {
dotenv.config({ path: folderPath });
}
// If VSCode workspace folder is already defined, set it as the default folder path.
const defaultFolderPath = getCurrentWorkspaceFolderPath();
if (defaultFolderPath) {
process.env.FOLDER_PATH = defaultFolderPath;
}
// Select folder
context.subscriptions.push(
vscode.commands.registerCommand("dhost.select", async () => {
await selectFolder();
})
);
// Publish the website to IPFS
context.subscriptions.push(
vscode.commands.registerCommand("dhost.publish", async () => {
let path = process.env.FOLDER_PATH;
vscode.window.withProgress(
{
location: vscode.ProgressLocation.Window,
cancellable: false,
title: "Publishing the website to IPFS...",
},
async (progress) => {
progress.report({ increment: 0 });
let publish = async () => {
// Get files
const files = await getFiles(path);
// Set web3.storage API token
const client = new Web3Storage({ token: getToken() });
// Upload to IPFS and return a CID
try {
const cid = await client.put(files);
progress.report({ increment: 100 });
const result = await vscode.window.showInformationMessage(
`Successfully published! Here's the IPFS CID of your website: ${cid}`,
"Open the website"
);
if (result === "Open the website") {
vscode.env.openExternal(
vscode.Uri.parse(`https://w3s.link/ipfs/${cid}/`)
);
}
} catch (e) {
const message =
"Please submit your valid web3.storage API token using the 'dhost.token' command before publishing the website. Instructions are provided on the extension homepage.";
const action = {
title: "Set API token",
command: "dhost.token",
};
const selectedAction = await vscode.window.showWarningMessage(
message,
action
);
if (selectedAction === action) {
await vscode.commands.executeCommand("dhost.token");
}
}
};
await publish();
}
);
})
);
}
function deactivate() {}
module.exports = {
activate,
deactivate,
};