-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcli.js
executable file
·226 lines (197 loc) · 6.58 KB
/
cli.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
#!/usr/bin/env node
//TOGGLED Command Line Interface. ZYMONO LICENSE
// Copyright (c) 2024 Zymono
const { program } = require("commander");
const fs = require("fs");
const https = require("https");
const inquirer = require("inquirer");
const temp = require("temp");
// Create a temporary directory
// temp.track();
// const tempDir = temp.mkdirSync();
const tempFilePath = '.toggled/temp.json';
try {
const directoryPath = './.toggled';
fs.mkdir(directoryPath, (err) => {
if (err) {
// console.error(`Error creating directory: ${err}`);
} else {
// console.log(`Directory created successfully at: ${directoryPath}`);
}
});
} catch {}
const auth = {};
// Function to read the JSON file and parse it
function readToggledJson() {
try {
// Read the contents of toggled.json synchronously
const jsonData = fs.readFileSync("toggled.json", "utf-8");
// Parse the JSON data into a JavaScript object
const jsonObject = JSON.parse(jsonData);
return jsonObject;
} catch (error) {
console.error("Error reading or parsing toggled.json:", error.message);
return null;
}
}
program
.command("init <type>")
.description("Initialize the toggled.json file")
.action((type) => {
const filePath = "toggled.json"; // Replace with your desired file path
const fileContents = `{
"name": "Toggled",
"version": "1.0.0",
"description": "Design something great.",
"main": "index.js",
"type": "${type}"
}
`;
// Use fs.writeFile to create the file with the specified contents
fs.writeFile(filePath, fileContents, (err) => {
if (err) {
console.error("Error creating file:", err);
} else {
console.log(`Successfully created toggled.json with type: ${type}`);
}
});
});
program
.command("package")
.description("Publish a Toggled package to the Toggled Package Registry")
.action(() => {
try {
const toggledData = readToggledJson();
if (!fs.readFileSync(tempFilePath, "utf-8")) {
console.log(
"You must sign in to use this command. Sign in by running: toggled login.",
);
} else {
if (toggledData.type !== "package") {
console.log("ERROR: The specified project is not a package.");
} else {
console.log("Publishing package to the Toggled Package Registry...");
const blocks = fs.readFileSync(toggledData.main, "utf-8");
toggledData.code = blocks;
toggledData.author = fs.readFileSync(tempFilePath, "utf-8");
// Perform the necessary actions to publish the package to the Toggled Package Registry
const data = JSON.stringify(toggledData);
const options = {
hostname: "toggled.tech",
port: 443,
path: "/api/v1/packages/publish",
method: "POST",
headers: {
"Content-Type": "application/json",
"Content-Length": data.length,
},
};
const req = https.request(options, (res) => {
console.log(`STATUS: ${res.statusCode}`);
res.setEncoding("utf8");
res.on("data", (chunk) => {
if (res.statusCode === 200 && !chunk.includes('You are not authorized')) {
console.log(
"Sucessfully published package to the Toggled Package Registry.",
);
}
});
});
req.on("error", (e) => {
console.error(`Problem with request: ${e.message}`);
});
// Write data to request body
req.write(data);
req.end();
}
}
} catch (error) {
console.log(
"You must sign in to use this command. Sign in by running: toggled login. " + error,
);
}
});
program
.command("login")
.description("Login to the Toggled Package Registry")
.action(() => {
try {
const data = JSON.stringify({});
const options = {
hostname:
"toggled.tech",
port: 443,
path: "/api/v1/auth",
method: "POST",
headers: {
"Content-Type": "application/json",
"Content-Length": data.length,
},
};
const req = https.request(options, (res) => {
console.log(`STATUS: ${res.statusCode}`);
res.setEncoding("utf8");
res.on("data", (chunk) => {
if (res.statusCode === 200) {
console.log(
`Please sign into your Toggled account at ${
JSON.parse(chunk).url
}`,
);
const prompt = inquirer.createPromptModule();
prompt([
{
type: "input",
name: "auth",
message: "Please enter the code you were given:",
},
]).then((answers) => {
const code = answers.auth;
https
.get(
`https://toggled.tech/api/v1/auth/${code}`,
(res) => {
let data = "";
res.on("data", (chunk) => {
data += chunk;
});
res.on("end", () => {
try {
const json = data;
if (!String(json).includes("firebase") && !String(json).includes("html")) {
const filePath = './.toggled/temp.json';;
// Create a file and write content to it
fs.writeFile(filePath, json, (err) => {
if (err) {
console.log("Authentication failed! " + err);
} else {
console.log("Authentication successful");
}
});
} else {
console.log("Authentication failed!");
}
} catch (e) {
console.error("Error parsing JSON:", e);
}
});
},
)
.on("error", (e) => {
console.error(`HTTPS GET request failed: ${e.message}`);
});
});
}
});
});
req.on("error", (e) => {
console.error(`Problem with request: ${e.message}`);
});
// Write data to request body
req.write(data);
req.end();
} catch (error) {
console.error("There was an error:", error.message);
}
});
program.parse(process.argv);