-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathcli.js
38 lines (30 loc) · 1.06 KB
/
cli.js
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
#!/usr/bin/env node
const fs = require('node:fs');
const path = require('node:path');
const command = process.argv[2];
process.env.AC_FAAS_ROOT = process.env.AC_FAAS_ROOT || 'functions';
process.env.AC_PUBLIC_DIR = process.env.AC_PUBLIC_DIR || 'public';
const template = `// @see https://docs.aircode.io/guide/functions/
const aircode = require('aircode');
module.exports = async function (params, context) {
console.log('Received params:', params);
return {
message: 'Hi, AirCode.',
};
};
`;
if(command === '--init') {
if(!fs.existsSync(process.env.AC_FAAS_ROOT)) {
fs.mkdirSync(process.env.AC_FAAS_ROOT);
const helloFile = path.join(process.env.AC_FAAS_ROOT, 'hello.js');
fs.writeFileSync(helloFile, template);
}
if(!fs.existsSync(process.env.AC_PUBLIC_DIR)) {
fs.mkdirSync(process.env.AC_PUBLIC_DIR);
}
}
const {start, file} = require('../src/index.js');
const app = start();
if(fs.existsSync(file('hello.js')) || fs.existsSync(file('hello.cjs')) || fs.existsSync(file('hello.mjs'))) {
require('open')(`http://localhost:${app.PORT}/hello`);
}