-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcli.js
More file actions
executable file
Β·170 lines (148 loc) Β· 4.92 KB
/
Copy pathcli.js
File metadata and controls
executable file
Β·170 lines (148 loc) Β· 4.92 KB
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
#!/usr/bin/env node
"use strict";
const { execSync } = require("child_process");
const fs = require("fs");
const path = require("path");
const inquirer = require("inquirer");
const tar = require("tar");
const { promisify } = require("util");
const { pipeline } = require("stream");
const pipelineAsync = promisify(pipeline);
const init = async () => {
try {
// DATA
const owner = "sovbiz";
const repoName = "Cypher-Nostr-Edition";
const apiUrl = `https://api.github.com/repos/${owner}/${repoName}/tarball/main`;
const tmpDir = path.join(__dirname, `${owner}_tmp`);
const tarPath = path.join(tmpDir, "template.tar");
/** @type {string | undefined} */
let projectDir;
/** @type {{[name:string]: number | string | {[name:string]: any }} | undefined} */
let projectPackage;
/** @type {{[name:string]: number | string | {[name:string]: any }} | undefined} */
let projectSetupConfig;
console.log(`π ${owner} Template initialization... please have your Npub ready !`);
/** @type {{ name?: string; blog: boolean; store: boolean; nostradmin?: string; btcadress?: string; orderwebhook>: string; finance: boolean; albytoken?: string }} */
const answers = await inquirer.prompt([
{
type: "input",
name: "name",
message: "π What is the name of your project: ",
default: owner,
},
{
type: "confirm",
name: "blog",
message: "π Do you want to enable a blog: ",
default: true,
},
{
type: "confirm",
name: "store",
message: "π Do you want to enable a store: ",
default: true,
},
{
type: "input",
name: "nostradmin",
message: "π Enter your Npub ",
},
{
type: "input",
name: "orderwebhook",
message: "π Set your order webhook (discord): ",
},
]);
projectDir = path.resolve(process.cwd(), answers.name);
try {
console.log(
"\x1b[36m%s\x1b[0m",
`π Fetching 'cypher-space' template into ${answers.name}...`
);
fs.mkdirSync(tmpDir, { recursive: true });
const response = await fetch(apiUrl, {
headers: {
"User-Agent": "cypher-space",
Accept: "application/vnd.github.v3.raw",
},
});
if (!response.ok)
throw `π Failed to fetch repository. Status code: ${response.status}`;
await pipelineAsync(response.body, fs.createWriteStream(tarPath));
await tar.extract({
file: tarPath,
cwd: tmpDir,
sync: true,
});
fs.unlinkSync(tarPath);
const extractedFolderName = fs.readdirSync(tmpDir)[0];
fs.renameSync(path.join(tmpDir, extractedFolderName), projectDir);
fs.rmSync(tmpDir, { recursive: true });
} catch (_) {
console.log(_);
throw typeof _ === "string" ? _ : "π Unable to install the template";
}
try {
projectPackage = require(path.join(projectDir, "package.json"));
projectSetupConfig = require(path.join(projectDir, "config/setup.json"));
if (!projectDir || !projectPackage || !projectSetupConfig)
throw new Error();
} catch (_) {
throw "π Unable to load metadata";
}
try {
console.log("\x1b[36m%s\x1b[0m", `π Configuring template...`);
projectPackage.name = answers.name;
projectSetupConfig.name = answers.name ?? projectSetupConfig.name;
projectSetupConfig.textlogo = answers.name ?? projectSetupConfig.name;
projectSetupConfig.blog = answers.blog ?? projectSetupConfig.blog;
projectSetupConfig.shop = answers.shop ?? projectSetupConfig.shop;
projectSetupConfig.nostradmin = answers.nostradmin ?? projectSetupConfig.nostradmin;
projectSetupConfig.finance =
answers.finance ?? projectSetupConfig.finance;
if (answers.albytoken) projectSetupConfig.albytoken = answers.albytoken;
fs.writeFileSync(
path.join(projectDir, "package.json"),
JSON.stringify(projectPackage, null, 2)
);
fs.writeFileSync(
path.join(projectDir, "config/setup.json"),
JSON.stringify(projectSetupConfig, null, 2)
);
} catch (_) {
throw "π Unable to configure project's package";
}
try {
console.log(
"\x1b[36m%s\x1b[0m",
`π Installing dependencies in ${answers.name}...`
);
execSync("npm install", { cwd: answers.name, stdio: "inherit" });
} catch (_) {
throw "π Unable to install dependencies";
}
console.log(
"\x1b[32m%s\x1b[0m",
`β
Template initialized successfully! \nType 'cd ${answers.name}' to go into your new template and \nType 'npm run dev' β¨`
);
console.log(
"\x1b[32m%s\x1b[0m",
`π You can modify settings at any times in '${answers.name}/config/setup.json'`
);
} catch (err) {
try {
fs.unlinkSync(tarPath);
} catch (_) {}
try {
fs.rmSync(tmpDir, { recursive: true });
} catch (_) {}
console.warn(
"\x1b[31m%s\x1b[0m",
`π§ Something went wrong: ${
typeof err !== "string" ? "π Unable resolve the prompt" : err
}`
);
}
};
init();