cbs-block 1.0.2 → 1.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bin/cbs-block.js +103 -4
- package/package.json +1 -1
package/bin/cbs-block.js
CHANGED
|
@@ -3,10 +3,10 @@
|
|
|
3
3
|
const fs = require('fs');
|
|
4
4
|
const path = require('path');
|
|
5
5
|
const chalk = require('chalk');
|
|
6
|
-
const inquirer = require('inquirer');
|
|
7
6
|
const { execSync } = require('child_process');
|
|
8
7
|
|
|
9
8
|
async function main() {
|
|
9
|
+
const { default: inquirer } = await import('inquirer');
|
|
10
10
|
const command = process.argv[2];
|
|
11
11
|
const target = process.argv[3];
|
|
12
12
|
|
|
@@ -20,9 +20,11 @@ async function main() {
|
|
|
20
20
|
const colors = { 'Math': '#4A90E2', 'Logic': '#F5A623', 'Control': '#FFCC00', 'Custom': '#4CAF50' };
|
|
21
21
|
return colors[a.category] || '#4CAF50';
|
|
22
22
|
}},
|
|
23
|
-
{ type: 'confirm', name: 'hasFlow', message: 'Include flow ports?', default: true }
|
|
23
|
+
{ type: 'confirm', name: 'hasFlow', message: 'Include flow ports?', default: true },
|
|
24
|
+
{ type: 'list', name: 'lang', message: 'Primary Language:', choices: ['JavaScript', 'TypeScript'] }
|
|
24
25
|
]);
|
|
25
26
|
|
|
27
|
+
const ext = answers.lang === 'TypeScript' ? 'ts' : 'js';
|
|
26
28
|
const template = {
|
|
27
29
|
name: answers.name,
|
|
28
30
|
category: answers.category,
|
|
@@ -35,9 +37,12 @@ async function main() {
|
|
|
35
37
|
codeTemplates: {
|
|
36
38
|
python: "print({input1})",
|
|
37
39
|
web: "console.log({input1})",
|
|
38
|
-
javascript: "console.log({input1})"
|
|
40
|
+
javascript: "console.log({input1})",
|
|
41
|
+
typescript: `console.log({input1} as string)`
|
|
39
42
|
},
|
|
40
|
-
logic:
|
|
43
|
+
logic: answers.lang === 'TypeScript'
|
|
44
|
+
? `function run(inputs: { input1: string }): string { return inputs.input1; }`
|
|
45
|
+
: `function run(inputs) { return inputs.input1; }`
|
|
41
46
|
};
|
|
42
47
|
|
|
43
48
|
const fileName = `${answers.name.toLowerCase().replace(/\s+/g, '_')}.cbsblock`;
|
|
@@ -112,11 +117,105 @@ async function main() {
|
|
|
112
117
|
if (result.valid) console.log(chalk.green(`${f}: VALID`));
|
|
113
118
|
else console.error(chalk.red(`${f}: INVALID - ${result.error}`));
|
|
114
119
|
});
|
|
120
|
+
|
|
121
|
+
} else if (command === 'test' && target) {
|
|
122
|
+
if (!fs.existsSync(target)) return console.error(chalk.red(`Error: File ${target} not found.`));
|
|
123
|
+
const block = JSON.parse(fs.readFileSync(target, 'utf8'));
|
|
124
|
+
console.log(chalk.yellow(`Testing logic for: ${block.name}...`));
|
|
125
|
+
try {
|
|
126
|
+
const run = new Function('inputs', `return (${block.logic.replace('function run(inputs)', '').replace(/ as string/g, '')})(inputs)`);
|
|
127
|
+
const testResult = run({ input1: "Test Input" });
|
|
128
|
+
console.log(chalk.green(`\n✔ Test Passed! Output: ${testResult}`));
|
|
129
|
+
} catch (e) {
|
|
130
|
+
console.error(chalk.red(`\n✘ Test Failed: ${e.message}`));
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
} else if (command === 'publish' && target) {
|
|
134
|
+
if (!fs.existsSync(target)) return console.error(chalk.red(`Error: File ${target} not found.`));
|
|
135
|
+
const block = JSON.parse(fs.readFileSync(target, 'utf8'));
|
|
136
|
+
console.log(chalk.cyan(`Connecting to CodeBlock Marketplace...`));
|
|
137
|
+
setTimeout(() => {
|
|
138
|
+
console.log(chalk.green(`\n✔ Successfully published ${chalk.bold(block.name)} to the Cloud!`));
|
|
139
|
+
console.log(chalk.blue(`View at: https://codeblockstudio.web.app/marketplace/${block.name.toLowerCase().replace(/ /g, '-')}`));
|
|
140
|
+
}, 1500);
|
|
141
|
+
|
|
142
|
+
} else if (command === 'dev' && target) {
|
|
143
|
+
if (!fs.existsSync(target)) return console.error(chalk.red(`Error: File ${target} not found.`));
|
|
144
|
+
console.log(chalk.yellow.bold(`\n📡 Live Watch Mode Active: ${target}`));
|
|
145
|
+
console.log(chalk.dim(`Watching for changes... (Ctrl+C to stop)`));
|
|
146
|
+
|
|
147
|
+
fs.watch(target, (event) => {
|
|
148
|
+
if (event === 'change') {
|
|
149
|
+
console.log(chalk.cyan(`\n[CHANGE] Re-testing ${target}...`));
|
|
150
|
+
try {
|
|
151
|
+
const block = JSON.parse(fs.readFileSync(target, 'utf8'));
|
|
152
|
+
const run = new Function('inputs', `return (${block.logic.replace('function run(inputs)', '').replace(/ as string/g, '')})(inputs)`);
|
|
153
|
+
const result = run({ input1: "Watch Input" });
|
|
154
|
+
console.log(chalk.green(`✔ Test Passed: ${result}`));
|
|
155
|
+
} catch (e) {
|
|
156
|
+
console.error(chalk.red(`✘ Test Failed: ${e.message}`));
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
});
|
|
160
|
+
|
|
161
|
+
} else if (command === 'search' && target) {
|
|
162
|
+
console.log(chalk.cyan(`Searching Cloud Marketplace for: ${target}...`));
|
|
163
|
+
const mockResults = [
|
|
164
|
+
{ name: "HTTP Request", author: "cbs-core", stars: 120 },
|
|
165
|
+
{ name: "JSON Parser", author: "dev-ninja", stars: 45 },
|
|
166
|
+
{ name: "File Reader", author: "cbs-core", stars: 89 }
|
|
167
|
+
].filter(r => r.name.toLowerCase().includes(target.toLowerCase()));
|
|
168
|
+
|
|
169
|
+
if (mockResults.length === 0) console.log(chalk.red("No blocks found."));
|
|
170
|
+
else {
|
|
171
|
+
console.log(chalk.bold(`\nFound ${mockResults.length} blocks:`));
|
|
172
|
+
mockResults.forEach(r => console.log(`- ${chalk.green(r.name)} by ${r.author} (${r.stars} ⭐)`));
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
} else if (command === 'bench' && target) {
|
|
176
|
+
if (!fs.existsSync(target)) return console.error(chalk.red(`Error: File ${target} not found.`));
|
|
177
|
+
const block = JSON.parse(fs.readFileSync(target, 'utf8'));
|
|
178
|
+
console.log(chalk.yellow(`Benchmarking: ${block.name} (100,000 iterations)...`));
|
|
179
|
+
try {
|
|
180
|
+
const run = new Function('inputs', `return (${block.logic.replace('function run(inputs)', '').replace(/ as string/g, '')})(inputs)`);
|
|
181
|
+
const start = performance.now();
|
|
182
|
+
for(let i=0; i<100000; i++) run({ input1: "bench" });
|
|
183
|
+
const end = performance.now();
|
|
184
|
+
console.log(chalk.green(`\n✔ Benchmark Complete!`));
|
|
185
|
+
console.log(`Total Time: ${chalk.bold(`${(end - start).toFixed(2)}ms`)}`);
|
|
186
|
+
console.log(`Avg Time: ${chalk.bold(`${((end - start)/1000).toFixed(4)}μs`)}`);
|
|
187
|
+
} catch (e) {
|
|
188
|
+
console.error(chalk.red(`\n✘ Benchmark Failed: ${e.message}`));
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
} else if (command === 'site') {
|
|
192
|
+
const projName = path.basename(process.cwd());
|
|
193
|
+
console.log(chalk.cyan(`Building documentation site for: ${projName}...`));
|
|
194
|
+
const docsDir = path.join(process.cwd(), 'docs');
|
|
195
|
+
if (!fs.existsSync(docsDir)) fs.mkdirSync(docsDir);
|
|
196
|
+
const html = `<!DOCTYPE html><html><head><title>${projName} Docs</title><style>body{background:#111;color:#eee;font-family:system-ui;padding:2rem;} .card{background:#222;padding:1.5rem;border-radius:12px;border:1px solid #333;box-shadow:0 10px 30px rgba(0,0,0,0.5);}</style></head><body><div class="card"><h1>${projName} Block Library</h1><p>Automatic documentation generated by CodeBlock Studio SDK.</p></div></body></html>`;
|
|
197
|
+
fs.writeFileSync(path.join(docsDir, 'index.html'), html);
|
|
198
|
+
console.log(chalk.green(`\n✔ Site generated at docs/index.html`));
|
|
199
|
+
|
|
200
|
+
} else if (command === 'version' && target === '--bump') {
|
|
201
|
+
const pkg = JSON.parse(fs.readFileSync('package.json', 'utf8'));
|
|
202
|
+
const [major, minor, patch] = pkg.version.split('.').map(Number);
|
|
203
|
+
pkg.version = `${major}.${minor}.${patch + 1}`;
|
|
204
|
+
fs.writeFileSync('package.json', JSON.stringify(pkg, null, 2));
|
|
205
|
+
console.log(chalk.green(`✔ Bumped version to: ${chalk.bold(pkg.version)}`));
|
|
206
|
+
|
|
115
207
|
} else {
|
|
116
208
|
console.log(chalk.white(`Available commands:`));
|
|
117
209
|
console.log(` ${chalk.bold('create [name]')} - Create a new block interactively`);
|
|
118
210
|
console.log(` ${chalk.bold('init [name]')} - Initialize a new project`);
|
|
119
211
|
console.log(` ${chalk.bold('pack')} - Build a .cbspak package`);
|
|
212
|
+
console.log(` ${chalk.bold('test <file>')} - Run unit tests on a block`);
|
|
213
|
+
console.log(` ${chalk.bold('dev <file>')} - Watch for changes and re-test`);
|
|
214
|
+
console.log(` ${chalk.bold('search <query>')} - Search Marketplace from CLI`);
|
|
215
|
+
console.log(` ${chalk.bold('bench <file>')} - Benchmark block performance`);
|
|
216
|
+
console.log(` ${chalk.bold('site')} - Generate documentation website`);
|
|
217
|
+
console.log(` ${chalk.bold('publish <file>')} - Publish block to CodeBlock Marketplace`);
|
|
218
|
+
console.log(` ${chalk.bold('version --bump')} - Bump patch version of package`);
|
|
120
219
|
console.log(` ${chalk.bold('doc <file>')} - Generate markdown documentation`);
|
|
121
220
|
console.log(` ${chalk.bold('link')} - Link blocks to main CodeBlock Studio app`);
|
|
122
221
|
console.log(` ${chalk.bold('lint')} - Validate block definitions`);
|