(commandArgv = argv)
| 47 | } |
| 48 | |
| 49 | async function cli(commandArgv = argv) { |
| 50 | const [, modelName, func, ...args] = commandArgv.args as [string, string, string, ...any[]]; |
| 51 | if (modelName === 'execute') { |
| 52 | try { |
| 53 | // eslint-disable-next-line no-eval |
| 54 | const res = eval(`(async () => { with (require('${require.resolve('../plugin-api')}')) { ${func} } })`); |
| 55 | return console.log(await res()); |
| 56 | } catch (e) { |
| 57 | console.error(`Execution fail: ${e.message}`); |
| 58 | } |
| 59 | } |
| 60 | if (modelName === 'script') { |
| 61 | let arg: any; |
| 62 | console.log(args.join(' ')); |
| 63 | try { |
| 64 | arg = JSON.parse(args.join(' ')); |
| 65 | } catch (e) { |
| 66 | return console.error('Invalid argument'); |
| 67 | } |
| 68 | return await runScript(func, arg); |
| 69 | } |
| 70 | if (!global.Hydro.model[modelName]) { |
| 71 | return console.error(`Model ${modelName} doesn't exist.`); |
| 72 | } |
| 73 | if (!func) { |
| 74 | return console.log(Object.keys(global.Hydro.model[modelName])); |
| 75 | } |
| 76 | if (!global.Hydro.model[modelName][func]) { |
| 77 | return console.error(`Function ${func} doesn't exist in model ${modelName}.`); |
| 78 | } |
| 79 | if (typeof global.Hydro.model[modelName][func] !== 'function') { |
| 80 | return console.error(`${func} in model ${modelName} is not a function.`); |
| 81 | } |
| 82 | const parameterMin = global.Hydro.model[modelName][func].length; |
| 83 | const parameters = parseParameters(global.Hydro.model[modelName][func]); |
| 84 | const parameterMax = parameters.length; |
| 85 | if (args.length > parameterMax) { |
| 86 | console.error(`Too many arguments. Max ${parameterMax}`); |
| 87 | return console.error(parameters.join(', ')); |
| 88 | } |
| 89 | if (args.length < parameterMin) { |
| 90 | console.error(`Too few arguments. Min ${parameterMin}`); |
| 91 | return console.error(parameters.join(', ')); |
| 92 | } |
| 93 | for (let i = 0; i < args.length; i++) { |
| 94 | if ("'\"".includes(args[i][0]) && "'\"".includes(args[i].at(-1))) { |
| 95 | args[i] = args[i].substr(1, args[i].length - 2); |
| 96 | } else if (args[i].length === 24 && ObjectId.isValid(args[i])) { |
| 97 | args[i] = new ObjectId(args[i]); |
| 98 | } else if ((+args[i]).toString() === args[i]) { |
| 99 | args[i] = +args[i]; |
| 100 | } else if (args[i].startsWith('~')) { |
| 101 | args[i] = commandArgv.options[args[i].substr(1)]; |
| 102 | } else if ((args[i].startsWith('[') && args[i].endsWith(']')) || (args[i].startsWith('{') && args[i].endsWith('}'))) { |
| 103 | try { |
| 104 | args[i] = JSON.parse(args[i]); |
| 105 | for (const key in args[i]) { |
| 106 | if (typeof args[i][key] === 'string' && ObjectId.isValid(args[i][key])) { |
no test coverage detected