metamigo-cli: build, fmt, lint
This commit is contained in:
parent
d692b3e7ff
commit
38e68852d9
19 changed files with 4088 additions and 11882 deletions
22
packages/metamigo-cli/src/config.ts
Normal file
22
packages/metamigo-cli/src/config.ts
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
import {
|
||||
generateConfig,
|
||||
printConfigOptions,
|
||||
} from "@digiresilience/metamigo-common";
|
||||
import { loadConfigRaw } from "@digiresilience/metamigo-config";
|
||||
|
||||
export const genConf = async (): Promise<void> => {
|
||||
const c = await loadConfigRaw();
|
||||
// @ts-expect-error TS2589
|
||||
const generated = generateConfig(c);
|
||||
console.log(generated);
|
||||
};
|
||||
|
||||
export const genSchema = async (): Promise<void> => {
|
||||
const c = await loadConfigRaw();
|
||||
console.log(c.getSchemaString());
|
||||
};
|
||||
|
||||
export const listConfig = async (): Promise<void> => {
|
||||
const c = await loadConfigRaw();
|
||||
printConfigOptions(c);
|
||||
};
|
||||
67
packages/metamigo-cli/src/index.ts
Normal file
67
packages/metamigo-cli/src/index.ts
Normal file
|
|
@ -0,0 +1,67 @@
|
|||
#!/usr/bin/env node
|
||||
|
||||
import { Command } from "commander";
|
||||
import { startWithout } from "@digiresilience/montar";
|
||||
import { migrateWrapper } from "@digiresilience/metamigo-db";
|
||||
import { loadConfig } from "@digiresilience/metamigo-config";
|
||||
import { genConf, listConfig } from "./config";
|
||||
import { createTokenForTesting, generateJwks } from "./jwks";
|
||||
import { exportGraphqlSchema } from "./metamigo-postgraphile";
|
||||
import "api/build/main/server";
|
||||
import "api/build/main/logger";
|
||||
import "worker/build/main";
|
||||
|
||||
const program = new Command();
|
||||
|
||||
export async function runServer(): Promise<void> {
|
||||
await startWithout(["worker"]);
|
||||
}
|
||||
|
||||
export async function runWorker(): Promise<void> {
|
||||
await startWithout(["server"]);
|
||||
}
|
||||
|
||||
program
|
||||
.command("config-generate")
|
||||
.description("Generate a sample JSON configuration file (to stdout)")
|
||||
.action(genConf);
|
||||
|
||||
program
|
||||
.command("config-help")
|
||||
.description("Prints the entire convict config ")
|
||||
.action(listConfig);
|
||||
|
||||
program
|
||||
.command("api")
|
||||
.description("Run the application api server")
|
||||
.action(runServer);
|
||||
|
||||
program
|
||||
.command("worker")
|
||||
.description("Run the worker to process jobs")
|
||||
.action(runWorker);
|
||||
|
||||
program
|
||||
.command("db <commands...>")
|
||||
.description("Run graphile-migrate commands with your app's config loaded.")
|
||||
.action(async (args) => {
|
||||
const config = await loadConfig();
|
||||
return migrateWrapper(args, config);
|
||||
});
|
||||
|
||||
program
|
||||
.command("gen-jwks")
|
||||
.description("Generate the JWKS")
|
||||
.action(generateJwks);
|
||||
|
||||
program
|
||||
.command("gen-testing-jwt")
|
||||
.description("Generate a JWT for the test suite")
|
||||
.action(createTokenForTesting);
|
||||
|
||||
program
|
||||
.command("export-graphql-schema")
|
||||
.description("Export the graphql schema")
|
||||
.action(exportGraphqlSchema);
|
||||
|
||||
program.parse(process.argv);
|
||||
67
packages/metamigo-cli/src/jwks.ts
Normal file
67
packages/metamigo-cli/src/jwks.ts
Normal file
|
|
@ -0,0 +1,67 @@
|
|||
import jose from "node-jose";
|
||||
import * as jwt from "jsonwebtoken";
|
||||
|
||||
const generateKeystore = async () => {
|
||||
const keystore = jose.JWK.createKeyStore();
|
||||
await keystore.generate("oct", 256, {
|
||||
alg: "A256GCM",
|
||||
use: "enc",
|
||||
});
|
||||
await keystore.generate("oct", 256, {
|
||||
alg: "HS512",
|
||||
use: "sig",
|
||||
});
|
||||
return keystore;
|
||||
};
|
||||
|
||||
const safeString = (input) =>
|
||||
Buffer.from(JSON.stringify(input)).toString("base64");
|
||||
|
||||
const stringify = (v) => JSON.stringify(v, undefined, 2);
|
||||
|
||||
const _generateJwks = async () => {
|
||||
const keystore = await generateKeystore();
|
||||
const encryption = keystore.all({ use: "enc" })[0].toJSON(true);
|
||||
const signing = keystore.all({ use: "sig" })[0].toJSON(true);
|
||||
|
||||
return {
|
||||
nextAuth: {
|
||||
signingKeyB64: safeString(signing),
|
||||
encryptionKeyB64: safeString(encryption),
|
||||
},
|
||||
};
|
||||
};
|
||||
|
||||
export const generateJwks = async (): Promise<void> => {
|
||||
console.log(stringify(await _generateJwks()));
|
||||
};
|
||||
|
||||
export const createTokenForTesting = async (): Promise<void> => {
|
||||
const keys = await _generateJwks();
|
||||
const signingKey = Buffer.from(
|
||||
JSON.parse(
|
||||
Buffer.from(keys.nextAuth.signingKeyB64, "base64").toString("utf-8")
|
||||
).k,
|
||||
"base64"
|
||||
);
|
||||
|
||||
const token = jwt.sign(
|
||||
{
|
||||
iss: "Test Env",
|
||||
iat: 1606893960,
|
||||
aud: "metamigo",
|
||||
sub: "abel@guardianproject.info",
|
||||
name: "Abel Luck",
|
||||
email: "abel@guardianproject.info",
|
||||
userRole: "admin",
|
||||
},
|
||||
signingKey,
|
||||
{ expiresIn: "100y", algorithm: "HS512" }
|
||||
);
|
||||
console.log("CONFIG");
|
||||
console.log(stringify(keys));
|
||||
console.log();
|
||||
console.log("TOKEN");
|
||||
console.log(token);
|
||||
console.log();
|
||||
};
|
||||
38
packages/metamigo-cli/src/metamigo-postgraphile.ts
Normal file
38
packages/metamigo-cli/src/metamigo-postgraphile.ts
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
import { writeFileSync } from "node:fs";
|
||||
import {
|
||||
getIntrospectionQuery,
|
||||
GraphQLSchema,
|
||||
graphqlSync,
|
||||
lexicographicSortSchema,
|
||||
printSchema,
|
||||
} from "graphql";
|
||||
import { createPostGraphileSchema } from "postgraphile";
|
||||
import { Pool } from "pg";
|
||||
import { loadConfig } from "@digiresilience/metamigo-config";
|
||||
import { getPostGraphileOptions } from "@digiresilience/metamigo-db";
|
||||
|
||||
export const exportGraphqlSchema = async (): Promise<void> => {
|
||||
const config = await loadConfig();
|
||||
|
||||
const rootPgPool = new Pool({
|
||||
connectionString: config.db.connection,
|
||||
});
|
||||
const exportSchema = `../../data/schema.graphql`;
|
||||
const exportJson = `../../frontend/lib/graphql-schema.json`;
|
||||
try {
|
||||
const schema = (await createPostGraphileSchema(
|
||||
config.postgraphile.authConnection,
|
||||
"app_public",
|
||||
getPostGraphileOptions()
|
||||
)) as unknown as GraphQLSchema;
|
||||
const sorted = lexicographicSortSchema(schema);
|
||||
const json = graphqlSync(schema, getIntrospectionQuery());
|
||||
writeFileSync(exportSchema, printSchema(sorted));
|
||||
writeFileSync(exportJson, JSON.stringify(json));
|
||||
|
||||
console.log(`GraphQL schema exported to ${exportSchema}`);
|
||||
console.log(`GraphQL schema json exported to ${exportJson}`);
|
||||
} finally {
|
||||
rootPgPool.end();
|
||||
}
|
||||
};
|
||||
Loading…
Add table
Add a link
Reference in a new issue