-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnew.js
More file actions
88 lines (64 loc) · 2.63 KB
/
Copy pathnew.js
File metadata and controls
88 lines (64 loc) · 2.63 KB
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import { API as DenoAPI } from "./api_deno.js";
import { API as StudioAPI } from "./api_studio.js";
import { load } from "https://deno.land/std@0.177.0/dotenv/mod.ts";
import { colors } from "https://deno.land/x/cliffy@v0.25.7/ansi/mod.ts";
import { exists, existsSync } from "https://deno.land/std/fs/mod.ts";
const error = colors.bold.red;
const warn = colors.bold.yellow;
const info = colors.bold.blue;
export default async function _new(args) {
const functionName = args._.shift()
if (!functionName) {
console.log(`Function name required`)
Deno.exit()
}
// const TOKEN = Deno.env.get('TOKEN')
// let PROJECT_REF = args.project || Deno.env.get('PROJECT_REF')
// if (!PROJECT_REF) {
// const studio_api = StudioAPI.fromToken(TOKEN)
// const studio_projects = await studio_api.requestJson(`/projects`)
// if (studio_projects.error) {
// console.log(studio_projects)
// Deno.exit()
// }
// console.table(studio_projects.map(o => ({ name: o.name, ref: o.ref, url: `https://${o.ref}.tictapp.io`, updated: o.updated_at })))
// PROJECT_REF = prompt(`Enter project ref`)
// }
// const studioAPI = StudioAPI.fromToken(TOKEN)
// const project = await studioAPI.requestJson(`/projects/${PROJECT_REF}?_data`)
// const DENO_DEPLOY_PROJECT = `${PROJECT_REF}-${functionName}`
// const DENO_DEPLOY_TOKEN = Deno.env.get("DENO_DEPLOY_TOKEN")
// const DENO_DEPLOY_ORG = Deno.env.get("DENO_DEPLOY_ORG")
// const denoAPI = DenoAPI.fromToken(DENO_DEPLOY_TOKEN);
// const envVars = await load({
// envPath: `functions/${functionName}/.env`,
// defaultsPath: `.env.defaults`,
// export: false
// });
// const res = await denoAPI.requestJson('/projects', {
// method: 'POST',
// body: {
// "name": DENO_DEPLOY_PROJECT,
// "organizationId": DENO_DEPLOY_ORG,
// "envVars": envVars
// }
// })
if (await exists(`functions/${functionName}`)) {
console.error(`Function already exists`, error(`functions/${functionName}`))
Deno.exit()
}
await Deno.mkdir(`functions/${functionName}`, { recursive: true });
const func_code = `
import { serve } from "https://deno.land/std@0.168.0/http/server.ts"
console.log("Welcome Function")
serve((req) => {
const data = {
url: req.url,
message: Deno.env.toObject(),
}
return Response.json(data)
})
`
await Deno.writeTextFile(`functions/${functionName}/index.js`, func_code)
console.log(`Function created:`, info(`functions/${functionName}/index.js`))
}