MCPcopy Create free account
hub / github.com/acode/cli / run

Method run

cli/commands/endpoints/create.js:66–146  ·  view source on GitHub ↗
(params, callback)

Source from the content-addressed store, hash-verified

64 }
65
66 async run(params, callback) {
67
68 let functionName = params.args[0] || '';
69 let functionDescription = params.args[1] || '';
70 let functionParams = params.args.slice(2) || [];
71 let newDir = !!(params.flags.n || params.vflags['new'] || false);
72
73 if (!fs.existsSync('package.json') && !fs.existsSync('stdlib.json')) {
74 return callback(new Error('Not in valid Autocode directory'));
75 }
76
77 let questions = [];
78
79 functionName || questions.push({
80 name: 'functionName',
81 type: 'input',
82 default: '',
83 message: 'Endpoint name'
84 });
85
86 let promptResult = await inquirer.prompt(questions);
87
88 functionName = functionName || promptResult.functionName;
89
90 if (!functionName.split('/').pop().match(/^[A-Z]/i)) {
91 return callback(new Error(`Invalid function name: ${functionName}`));
92 }
93
94 let fPath = path.join(process.cwd(), 'functions');
95 let functionPath = fPath;
96
97 !fs.existsSync(fPath) && fs.mkdirSync(fPath);
98
99 let directories = functionName.split('/');
100
101 for (let i = 0; i < directories.length - 1; i++) {
102 let relpath = path.join.apply(path, [fPath].concat(directories.slice(0, i + 1)));
103 !fs.existsSync(relpath) && fs.mkdirSync(relpath);
104 functionPath = relpath;
105 }
106
107 let name = directories[directories.length - 1];
108 let checkPaths = [
109 path.join(functionPath, `${name}.js`),
110 path.join(functionPath, `${name}`, `__main__.js`)
111 ];
112
113 for (let i = 0; i < checkPaths.length; i++) {
114 let pathname = checkPaths[i];
115 if (fs.existsSync(pathname)) {
116 console.log();
117 console.log(chalk.bold.red('Oops!'));
118 console.log();
119 console.log(`The endpoint you're trying to create already seems to exist:`);
120 console.log(` ${chalk.bold(pathname)}`);
121 console.log();
122 console.log(`Try removing the existing file first.`);
123 console.log();

Callers

nothing calls this directly

Calls 1

generateFunctionFunction · 0.85

Tested by

no test coverage detected