-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.ts
More file actions
97 lines (85 loc) · 2.78 KB
/
main.ts
File metadata and controls
97 lines (85 loc) · 2.78 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
89
90
91
92
93
94
95
96
97
import { Command } from "@cliffy/command";
import { get_schema_command } from "./commands/get_schema.ts";
import { validate_schema_command } from "./commands/validate.ts";
import { diff_schema_command } from "./commands/diff.ts";
import { colors } from "@cliffy/ansi/colors";
import { VERSION } from "./version.ts";
//Airtable constants
export const AIRTABLE_API_URL = "https://api.airtable.com/v0";
export enum AIRTABLE_API_ENDPOINTS {
get_base_schema = "v0/meta/bases/{baseId}/tables",
}
export type AIRTABLE_PATH_PARAMS = {
baseId?: string;
};
export const AIRTABLE_PATH_PARAMS_FORMAT = {
baseId: /^app[a-zA-Z0-9]*$/,
};
// Learn more at https://docs.deno.com/runtime/manual/examples/module_metadata#concepts
if (import.meta.main) {
main();
}
//Define theme colors
export const log = {
error: (message: string) =>
console.error(colors.bold.red("[❌ ERROR] " + message)),
warn: (message: string) =>
console.warn(colors.bold.yellow("[⚠️ WARNING] " + message)),
info: (message: string) => console.log(colors.bold.blue("[INFO] " + message)),
success: (message: string) =>
console.log(colors.bold.green("[✅ SUCCESS] " + message)),
};
//When CLI is run
async function main() {
//Commands definition
const main_command = new Command()
//main command
.name("airtable-devops")
.version(VERSION)
.description(`
🪄 Airtable DevOps Tools
--
A set of DevOps tools for managing Airtable bases using the command line.`)
//custom types available for all subcommands
.globalType("AirtableBaseId", (valueToTest: { value: string }) => {
if (AIRTABLE_PATH_PARAMS_FORMAT.baseId.test(valueToTest.value)) {
return valueToTest.value;
} else {
throw new Error(
"baseId must start with 'app' and contain only alphanumeric characters."
);
}
})
.action((): void => main_command.showHelp({
long:true
}))
//subcommands
.command("get-schema", get_schema_command)
.command("validate", validate_schema_command)
.command("diff", diff_schema_command);
try {
await main_command.parse(Deno.args);
} catch (error: unknown) {
log.error(error instanceof Error ? error.message : "An unknown error occurred");
Deno.exit(1);
}
}
export const bindEndpointsParams = (
endpoint: string,
params: AIRTABLE_PATH_PARAMS
): string => {
const url = new URL(AIRTABLE_API_URL); // Base URL is required for URL class
// Replace path parameters
let path = endpoint;
for (const [key, value] of Object.entries(params)) {
path = path.replace(`{${key}}`, value);
}
url.pathname = path;
// Replace query parameters
for (const [key, value] of Object.entries(params)) {
if (url.searchParams.has(key)) {
url.searchParams.set(key, value);
}
}
return url.href;
};