generated from rhdeck/ts-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
62 lines (62 loc) · 1.87 KB
/
index.ts
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
import { join } from "path";
import { existsSync, mkdirSync, writeFileSync, readFileSync } from "fs";
import { getResources } from "@raydeck/serverless-resources";
import { findRegion, findStage } from "@raydeck/serverless-stage";
import { getServerlessConfig } from "@raydeck/serverless-base";
import type { PromiseValue } from "type-fest";
export async function getConfig({
region,
stage,
path = process.cwd(),
cognitoPath,
clientPath,
clientName,
}: {
region?: string;
stage: string;
path?: string;
cognitoPath?: string;
clientPath?: string;
clientName?: string;
}) {
const { cognito, client, appsync } = getServerlessConfig();
if (!stage) stage = findStage() ?? "dev";
if (!cognitoPath) {
if (cognito) cognitoPath = cognito;
}
if (!region) region = findRegion();
const r = await getResources({ region, stage, path: cognitoPath });
const candidates = Object.entries(r).find(([key, _]) =>
key.startsWith("CognitoUserPool")
);
const userPoolId = (candidates && candidates[1]) || "";
if (!clientPath) {
clientPath = client || appsync;
}
const r2 = await getResources({ region, stage, path: clientPath });
const userPoolWebClientId = clientName
? r2[clientName]
: r2.CognitoClient
? r2.CognitoClient
: (Object.entries(r2).find(
([key, value]) => key.endsWith("Client") && !key.startsWith("GraphQ")
// &&
// /^[0-9a-z]*$/.test(key)
) ?? [])[1];
const region2 = userPoolId.split("_").shift();
return {
region: region2,
userPoolId,
userPoolWebClientId,
mandatorySignIn: false,
identityPoolId: "",
};
}
export function writeConfig(
config: PromiseValue<ReturnType<typeof getConfig>>,
path = join(process.cwd(), "config")
) {
if (!existsSync(path)) mkdirSync(path);
const fullPath = join(path, "cognito.json");
writeFileSync(fullPath, JSON.stringify(config, null, 2));
}