From df9736bb3047816efa6544f6b76c069df8be1dbc Mon Sep 17 00:00:00 2001 From: Timur Moziev Date: Wed, 4 Sep 2024 14:06:59 +0000 Subject: [PATCH] add nodejs-src action add openapi stub action --- anca.json | 4 +- src/actions/nodejs-src.ts | 93 +++++++++++++++++++++++++++++++++++++++ src/actions/openapi.ts | 43 ++++++++++++++++++ src/cinnabar.ts | 4 +- src/developments.ts | 53 ++++++++++++++++++++++ src/schema.ts | 3 ++ src/tui.ts | 23 ++++++++++ 7 files changed, 219 insertions(+), 4 deletions(-) create mode 100644 src/actions/nodejs-src.ts create mode 100644 src/actions/openapi.ts diff --git a/anca.json b/anca.json index a134de1..2d4175b 100644 --- a/anca.json +++ b/anca.json @@ -13,8 +13,8 @@ "dataVersion": 0, "version": { "latest": "0.1.0-dev.2", - "latestNext": "0.1.0-dev.2+next.20240904_084209", - "timestamp": 1725439329 + "latestNext": "0.1.0-dev.2+next.20240904_133911", + "timestamp": 1725457151 }, "files": [ { diff --git a/src/actions/nodejs-src.ts b/src/actions/nodejs-src.ts new file mode 100644 index 0000000..e954fde --- /dev/null +++ b/src/actions/nodejs-src.ts @@ -0,0 +1,93 @@ +import fs from "fs"; +import path from "path"; + +import { AncaDevelopment } from "../schema.js"; +import { writeFolderFile } from "../utils.js"; + +const SRC_FILE_PATH = "./src/index.ts"; +const TEST_FILE_PATH = "./test/index.test.ts"; + +/** + * + * @param development + */ +function getSrcContents(development: AncaDevelopment) { + return `console.log("Hello, Anca!"); // ${development.fullPath}\n`; +} + +/** + * + * @param development + */ +function getTestContents(development: AncaDevelopment) { + return `console.log("Hello, Test Anca!"); // ${development.fullPath}\n`; +} + +/** + * + * @param development + */ +export async function checkNodejsSrc(development: AncaDevelopment) { + if (development.state == null) { + return; + } + const contents = development.state.files[SRC_FILE_PATH]; + if (contents == null) { + return false; + } + + return contents === getSrcContents(development); +} + +/** + * + * @param development + */ +export async function checkNodejsTest(development: AncaDevelopment) { + if (development.state == null) { + return; + } + const contents = development.state.files[TEST_FILE_PATH]; + console.log(contents); + if (contents == null) { + return false; + } + + return contents === getTestContents(development); +} + +/** + * + * @param development + */ +export async function fixNodejsSrc(development: AncaDevelopment) { + if (development.state == null) { + return; + } + await fs.promises.mkdir(path.join(development.fullPath, "src"), { + recursive: true, + }); + await writeFolderFile( + development.fullPath, + SRC_FILE_PATH, + getSrcContents(development), + ); +} + +/** + * + * @param development + */ +export async function fixNodejsTest(development: AncaDevelopment) { + if (development.state == null) { + return; + } + await fs.promises.mkdir(path.join(development.fullPath, "test"), { + recursive: true, + }); + await writeFolderFile( + development.fullPath, + TEST_FILE_PATH, + getTestContents(development), + ); +} diff --git a/src/actions/openapi.ts b/src/actions/openapi.ts new file mode 100644 index 0000000..54acbee --- /dev/null +++ b/src/actions/openapi.ts @@ -0,0 +1,43 @@ +import { AncaDevelopment } from "../schema.js"; +import { writeFolderFile } from "../utils.js"; + +const FILE_PATH = "openapi.json"; + +/** + * + * @param development + */ +function getContents(development: AncaDevelopment) { + return "json content of openapi " + development.fullPath; +} + +/** + * + * @param development + */ +export async function checkOpenapiJson(development: AncaDevelopment) { + if (development.state == null) { + return; + } + const contents = development.state.files[FILE_PATH]; + if (contents == null) { + return false; + } + + return contents === getContents(development); +} + +/** + * + * @param development + */ +export async function fixOpenapiJson(development: AncaDevelopment) { + if (development.state == null) { + return; + } + await writeFolderFile( + development.fullPath, + FILE_PATH, + getContents(development), + ); +} diff --git a/src/cinnabar.ts b/src/cinnabar.ts index 15787f7..b5ecbd1 100644 --- a/src/cinnabar.ts +++ b/src/cinnabar.ts @@ -1,4 +1,4 @@ // This file was generated by Cinnabar Meta. Do not edit. -export const CINNABAR_PROJECT_TIMESTAMP = 1725439329; -export const CINNABAR_PROJECT_VERSION = "0.1.0-dev.2+next.20240904_084209"; +export const CINNABAR_PROJECT_TIMESTAMP = 1725457151; +export const CINNABAR_PROJECT_VERSION = "0.1.0-dev.2+next.20240904_133911"; diff --git a/src/developments.ts b/src/developments.ts index a8432bd..41c9358 100644 --- a/src/developments.ts +++ b/src/developments.ts @@ -26,8 +26,10 @@ import { checkNodejsSeaBuildJs, checkNodejsSeaConfigJson, } from "./actions/nodejs-sea.js"; +import { checkNodejsSrc, checkNodejsTest } from "./actions/nodejs-src.js"; import { checkNodejsTsconfigJson } from "./actions/nodejs-tsconfig.js"; import { checkNodejsTsupConfigJs } from "./actions/nodejs-tsup.js"; +import { checkOpenapiJson } from "./actions/openapi.js"; import { checkReadmeMd } from "./actions/readme.js"; import { checkForGit, getGit } from "./git.js"; import { getDevelopmentMeta } from "./meta.js"; @@ -204,6 +206,7 @@ export async function refreshDevelopmentState( await addCommonToDevelopmentPack(development); await addDevcontainersToDevelopmentPack(development); await addGithubActionsToDevelopmentPack(development); + await addOpenapiJsonToDevelopmentPack(development); await addNodeJsToDevelopmentPack(development); state.meta = getDevelopmentMeta(development); @@ -211,6 +214,7 @@ export async function refreshDevelopmentState( await checkCommonToDevelopmentPack(development); await checkDevcontainersToDevelopmentPack(development); await checkGithubActionsToDevelopmentPack(development); + await checkOpenapiJsonDevelopmentPack(development); await checkNodeJsToDevelopmentPack(development); } @@ -393,6 +397,44 @@ async function checkGithubActionsToDevelopmentPack( } } +/** + * + * @param development + */ +async function addOpenapiJsonToDevelopmentPack(development: AncaDevelopment) { + if (development.state == null) { + return; + } + if (development.state.config.monorepo != null) { + return; + } + if (development.state.config.stack !== "nodejs") { + return; + } + + await addJsonFileToPack(development, "openapi.json"); +} + +/** + * + * @param development + */ +async function checkOpenapiJsonDevelopmentPack(development: AncaDevelopment) { + if (development.state == null) { + return; + } + if (development.state.config.monorepo != null) { + return; + } + if (development.state.config.stack !== "nodejs") { + return; + } + + if (!(await checkOpenapiJson(development))) { + development.state.issues.push("openapiJsonSetToDefault"); + } +} + /** * * @param development @@ -408,6 +450,9 @@ async function addNodeJsToDevelopmentPack(development: AncaDevelopment) { return; } + await addFileToPack(development, "./src/index.ts"); + await addFileToPack(development, "./test/index.test.ts"); + await addFileToPack(development, ".prettierignore"); await addFileToPack(development, ".prettierrc"); @@ -444,6 +489,14 @@ async function checkNodeJsToDevelopmentPack(development: AncaDevelopment) { return; } + if (!(await checkNodejsSrc(development))) { + development.state.issues.push("nodejsSrcSetToDefault"); + } + + if (!(await checkNodejsTest(development))) { + development.state.issues.push("nodejsTestSetToDefault"); + } + if (!(await checkNodejsPrettierIgnore(development))) { development.state.issues.push("nodejsPrettierIgnoreSetToDefault"); } diff --git a/src/schema.ts b/src/schema.ts index 807759c..4b3db3b 100644 --- a/src/schema.ts +++ b/src/schema.ts @@ -35,8 +35,11 @@ export type AncaAction = | "nodejsPrettierRcSetToDefault" | "nodejsSeaBuildJsSetToDefault" | "nodejsSeaConfigJsonSetToDefault" + | "nodejsSrcSetToDefault" + | "nodejsTestSetToDefault" | "nodejsTsconfigSetToDefault" | "nodejsTsupConfigJsSetToDefault" + | "openapiJsonSetToDefault" | "readmeSetToDefault"; export interface AncaMeta { diff --git a/src/tui.ts b/src/tui.ts index 0d8abd1..8b9024d 100644 --- a/src/tui.ts +++ b/src/tui.ts @@ -32,8 +32,10 @@ import { fixNodejsSeaBuildJs, fixNodejsSeaConfigJson, } from "./actions/nodejs-sea.js"; +import { fixNodejsSrc, fixNodejsTest } from "./actions/nodejs-src.js"; import { fixNodejsTsconfigJson } from "./actions/nodejs-tsconfig.js"; import { fixNodejsTsupConfigJs } from "./actions/nodejs-tsup.js"; +import { fixOpenapiJson } from "./actions/openapi.js"; import { fixReadmeMd } from "./actions/readme.js"; import { CINNABAR_PROJECT_VERSION } from "./cinnabar.js"; import { getInstance } from "./config.js"; @@ -235,6 +237,20 @@ async function showDevelopmentActions( }, label: "[sea.config.json] Set to default", }, + nodejsSrcSetToDefault: { + action: async () => { + await fixNodejsSrc(development); + await backHere(); + }, + label: "[src/index.ts] Set to default", + }, + nodejsTestSetToDefault: { + action: async () => { + await fixNodejsTest(development); + await backHere(); + }, + label: "[test/index.test.ts] Set to default", + }, nodejsTsconfigSetToDefault: { action: async () => { await fixNodejsTsconfigJson(development); @@ -249,6 +265,13 @@ async function showDevelopmentActions( }, label: "[tsup.config.js] Set to default", }, + openapiJsonSetToDefault: { + action: async () => { + await fixOpenapiJson(development); + await backHere(); + }, + label: "[openapi.json] Set to default", + }, readmeSetToDefault: { action: async () => { await fixReadmeMd(development);