feat: scripts
This commit is contained in:
86
scripts/createrunjs.js
Normal file
86
scripts/createrunjs.js
Normal file
@@ -0,0 +1,86 @@
|
||||
#!/usr/bin/env runjs
|
||||
|
||||
var consolejs = require('component-console.js');
|
||||
var filesystemjs = require('component-filesystem.js');
|
||||
|
||||
var RUNJS_HEAD_LINE = '#!/usr/bin/env runjs';
|
||||
var RUNJS_COMPONENT_CONFIG_URL = 'https://hatter.ink/script/get_scripts.json?type=runjs-component';
|
||||
|
||||
var main = () => {
|
||||
var runjsFn = $$.file(consolejs.readLineEx('Input your runjs filename: '));
|
||||
if (runjsFn.exists()) {
|
||||
xprintln('[ERROR] File exists: ' + runjsFn);
|
||||
return;
|
||||
}
|
||||
var componentsJSON = JSON.parse($$.httpRequest().skipCertCheck().url(RUNJS_COMPONENT_CONFIG_URL).get().string());
|
||||
var components = [];
|
||||
componentsJSON.scripts.forEach((c, i) => {
|
||||
if (!(['component-colorprint.js', 'component-diffutil.js', 'component-dingtalkrobot.js',
|
||||
'component-filechooser.js', 'component-gpg.js', 'component-helloworld.js',
|
||||
'component-helloworld2.js', 'component-httpserver.js', 'component-json.js',
|
||||
'component-shannonentropy.js', 'component-sqlitedataaccess.js', 'component-swing.js'].contains(c))) {
|
||||
components.push(c); // old js components
|
||||
}
|
||||
});
|
||||
|
||||
xprintln('[INFO] Components: ');
|
||||
components.forEach((c, i) => {
|
||||
var ln = repeat(' ', (3 - $STR(i).length)) + i + ': ' + c;
|
||||
print(ln + repeat(' ', 60 - ln.length));
|
||||
if (((i + 1) % 2) == 0) { println(); }
|
||||
});
|
||||
if (components.length % 2 != 0) { println(); }
|
||||
|
||||
var selectedComponents = consolejs.readLine('Select components(split with \',\'): ');
|
||||
|
||||
var componentIds = [];
|
||||
$ARR(selectedComponents.split(',')).forEach((cid) => {
|
||||
cid = cid.trim();
|
||||
if (cid == '') { return; }
|
||||
componentIds.push(parseInt(cid));
|
||||
});
|
||||
|
||||
var isComponent = runjsFn.getName().startsWith('component-');
|
||||
var selectedComponentNames = [];
|
||||
var buf = [];
|
||||
if (!isComponent) { buf.push(RUNJS_HEAD_LINE); }
|
||||
buf.push('');
|
||||
componentIds.forEach((cid) => {
|
||||
var c = components[cid];
|
||||
if (c == null) {
|
||||
xprintln('[WARN] Component not found: ' + cid);
|
||||
} else {
|
||||
selectedComponentNames.push(c);
|
||||
var n = c.substring('component-'.length).replace('-ex', '').replace('.js', '') + 'js';
|
||||
buf.push('var ' + n + ' = require(\'' + c + '\');');
|
||||
}
|
||||
});
|
||||
buf.push('');
|
||||
if (isComponent) {
|
||||
buf.push('var sample = () => {');
|
||||
buf.push(' // TODO');
|
||||
buf.push('}');
|
||||
buf.push('');
|
||||
buf.push('if (typeof exports == \'object\') {');
|
||||
buf.push(' exports.sample = sample;');
|
||||
buf.push('}');
|
||||
buf.push('');
|
||||
} else {
|
||||
buf.push('var main = () => {');
|
||||
if (selectedComponentNames.contains('component-args.js')) {
|
||||
buf.push(' var args = argsjs.parseDefARGs([]);');
|
||||
}
|
||||
buf.push(' xprintln(\'[OK] Hello World!\'); // TODO');
|
||||
buf.push('}');
|
||||
buf.push('');
|
||||
buf.push('main();');
|
||||
buf.push('');
|
||||
}
|
||||
$$.rFile(runjsFn).write(buf.join('\n'));
|
||||
if (!isComponent) { filesystemjs.chmodAddExec(runjsFn); }
|
||||
|
||||
xprintln('[SUCCESS] Create file success: ' + runjsFn);
|
||||
};
|
||||
|
||||
main();
|
||||
|
||||
Reference in New Issue
Block a user