-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathindex.js
More file actions
executable file
·196 lines (160 loc) · 8.01 KB
/
index.js
File metadata and controls
executable file
·196 lines (160 loc) · 8.01 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
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
#!/usr/bin/env node
// Import the custom console logger
require('./lib/console');
const { Command } = require('commander');
const program = new Command();
const packageJson = require('./package.json');
const fs = require('node:fs');
const path = require('node:path');
const { getRuntime } = require('./lib/bins');
const chalk = require('chalk');
const readlineSync = require('readline-sync')
const { parseTomlConfig } = require('./lib/config'); // Import parseTomlConfig
// checks
const isLinked = require('node:fs').existsSync(require('node:path').join(__dirname, 'node_modules', '.bin'));
const version = isLinked ? `${packageJson.version}-dev` : packageJson.version;
const blessnetDir = path.join(require('node:os').homedir(), '.blessnet');
const runtimePath = path.join(blessnetDir, 'bin', `bls-runtime${process.platform === 'win32' ? '.exe' : ''}`);
const authTokenPath = path.join(blessnetDir, 'auth_token');
const isLoggedIn = fs.existsSync(authTokenPath);
// commands
const initCommand = require('./commands/init');
const walletCommand = require('./commands/wallet');
const buildCommand = require('./commands/build');
const previewCommand = require('./commands/preview');
const manageCommand = require('./commands/manage');
const deployCommand = require('./commands/deploy');
const registryCommand = require('./commands/registry');
const accountCommand = require('./commands/account'); // Import the account command
const Box = require("cli-box");
async function main() {
const cwd = process.cwd();
const blsTomlPath = path.join(cwd, 'bls.toml');
const isVersionCommand = process.argv.includes('version') || process.argv.includes('-v');
const isOptionsCommand = process.argv.includes('options');
const isBuildCommand = process.argv.includes('build');
const isInitCommand = process.argv.includes('init');
const isHelpCommand = process.argv.includes('help') || process.argv.includes('-h') || process.argv.includes('--help');
const isPreviewCommand = process.argv.includes('preview');
const isManageCommand = process.argv.includes('manage');
const isDeployCommand = process.argv.includes('deploy');
const isRegistryCommand = process.argv.includes('registry');
const hasDeployTarget = isDeployCommand && process.argv.length > 3;
// Handle runtime installation first
if (!isVersionCommand && !isOptionsCommand && !isBuildCommand && !fs.existsSync(runtimePath)) {
const answer = readlineSync.question(chalk.yellow("BLESS environment not found. Do you want to install it? (yes/no): "));
if (answer.toLowerCase() !== 'yes' && answer.toLowerCase() !== 'y') {
process.exit(1);
}
const install = async () => {
await getRuntime().catch(err => {
console.error('Failed to download bls-runtime:', err);
process.exit(1);
});
console.log('BLESS environment installed successfully.');
console.log('You can now use the `blessnet` command.');
process.exit(0);
}
await install();
}
// Handle init command before bls.toml check
if (isInitCommand) {
const argv = process.argv;
await initCommand.parseAsync(["node", "init"].concat(argv.slice(-1)));
return;
}
// Skip bls.toml check if we're doing a targeted deploy
if (!isVersionCommand && !isOptionsCommand && !isBuildCommand && fs.existsSync(blsTomlPath) && !hasDeployTarget) {
if (!isHelpCommand && !isPreviewCommand && !isManageCommand && !isDeployCommand) {
const blsToml = parseTomlConfig(cwd, 'bls.toml'); // Use parseTomlConfig
const info = [{
"Project Name": blsToml.name,
"Version": blsToml.version,
"Type": blsToml.type,
}];
console.table(info);
if (blsToml.deployments && blsToml.deployments.length > 0) {
const deployment = blsToml.deployments[0];
const createdDate = new Date(deployment.created).toLocaleString();
console.log(chalk.yellow(`Deployment Status: ${chalk.green("Deployed")}`));
console.log(chalk.yellow(`CID: ${deployment.cid}`));
console.log(`${chalk.yellow("Created:")} ${createdDate}\n`);
if (deployment.host) {
console.log(`${chalk.yellow("Web2 Host:")} https://${deployment.host}\n`);
}
} else {
console.log(chalk.yellow("Deployment Status: Not Deployed\n"));
}
console.log("Deploy this project to the BLESS network using the command:");
console.log(chalk.green("blessnet deploy\n"));
console.log("Preview this project using the command:");
console.log(`${chalk.green("blessnet preview\n")} or ${chalk.green("blessnet preview serve\n")}`);
console.log("Change the project settings using the command:");
console.log(chalk.green("blessnet manage\n"));
console.log("Need more help?:");
console.log(chalk.green("blessnet help\n"));
console.log(`\nvisit ${chalk.blue('https://docs.bless.network')} for more information.\nyou are currently ${isLoggedIn ? chalk.green('logged in') : chalk.red('logged out')} to ${chalk.yellow('bless.network')}
${!isLoggedIn ? `\nTo log in, run ${chalk.blue('npx blessnet options account login')}` : ''}`);
console.log(`To log out,run ${chalk.blue('npx blessnet options account logout\n')}`);
process.exit(0);
}
} else {
if (!isVersionCommand && !isHelpCommand && !isOptionsCommand && !isRegistryCommand && !isBuildCommand && !hasDeployTarget) {
const answer = readlineSync.question(`Run ${chalk.blue("blessnet help")} for more information.\n\n${chalk.red("No bls.toml file detected in the current directory.")} \n${chalk.yellow("Initialize project? (yes/no): ")} `);
if (answer.toLowerCase() !== 'yes' && answer.toLowerCase() !== 'y') {
process.exit(1);
}
await initCommand.parseAsync(['node', 'init']);
return;
}
}
// Set custom marks
const b2 = new Box({
w: process.stdout.columns
, h: 12
, stretch: true
, stringify: false
}, `
${chalk.yellow("To scaffold a new project, run:")}
npx blessnet init < project - name >
${chalk.yellow("If you already have a project set up and would \nlike to add, remove, or update its structure, run:")}
npx blessnet manage
${chalk.yellow("Preview your project results in the terminal or web:")}
npx blessnet preview ${chalk.yellow("[serve]")}
`);
program.addHelpText(
'before',
b2.stringify(),
);
program.addHelpText(
'after',
`\nvisit ${chalk.blue('https://docs.bless.network')} for more information.
you are currently ${isLoggedIn ? chalk.green('logged in') : chalk.red('logged out')} to ${chalk.yellow('bless.network')} \n
${!isLoggedIn ? `\nTo login, run ${chalk.blue('npx blessnet options account login')}\n` : ''}
\n`,
);
program
.name(packageJson.name)
.description(packageJson.description)
.version(version);
// Register commands - init should always be available
program.addCommand(initCommand);
program.addCommand(previewCommand);
program.addCommand(manageCommand);
program.addCommand(deployCommand);
program.addCommand(registryCommand);
// Create 'options' command and add 'wallet', 'account', and 'build' as subcommands
const optionsCommand = new Command('options');
optionsCommand.addCommand(walletCommand);
optionsCommand.addCommand(accountCommand);
optionsCommand.addCommand(buildCommand); // Move the build command under options
program.addCommand(optionsCommand);
program
.command('version')
.description('Show the current version')
.action(() => {
console.log(`Current version: ${packageJson.version} `);
});
await program.parseAsync(process.argv);
}
main()