Skip to content

Commit

Permalink
add bulk action support from cli
Browse files Browse the repository at this point in the history
refactor actions to separate file
  • Loading branch information
TimurRin committed Sep 25, 2024
1 parent 359c873 commit 3c1cd5e
Show file tree
Hide file tree
Showing 7 changed files with 342 additions and 275 deletions.
4 changes: 2 additions & 2 deletions anca.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
"dataVersion": 0,
"version": {
"latest": "0.1.0-dev.3",
"latestNext": "0.1.0-dev.3+next.20240925_144217",
"timestamp": 1727275337
"latestNext": "0.1.0-dev.3+next.20240925_172241",
"timestamp": 1727284961
},
"files": [
{
Expand Down
243 changes: 243 additions & 0 deletions src/action.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,243 @@
import { fixAncaConfig } from "./actions/anca.js";
import { fixContributingMd } from "./actions/contributing.js";
import {
fixDevcontainerDockerfile,
fixDevcontainerJson,
} from "./actions/devcontainers.js";
import { fixGitIgnore } from "./actions/git.js";
import {
fixGithubActionsOtherFiles,
fixGithubActionsRelease,
fixGithubActionsTest,
} from "./actions/github-actions.js";
import { fixLicenseMd } from "./actions/license.js";
import {
fixNodejsPackageJson,
getUpdatedPackagesCommitMessage,
installNodejsDependencies,
NodejsPackageJson,
NpmUpdate,
updateNodejsPackageJsonDependencies,
updateNodejsPackageJsonDevDependencies,
writeNodejsPackageJson,
} from "./actions/nodejs.js";
import { fixNodejsEsbuildJs } from "./actions/nodejs-esbuild.js";
import { fixNodejsEslintConfigJs } from "./actions/nodejs-eslint.js";
import { generateNodejsOpenapiFiles } from "./actions/nodejs-openapi.js";
import {
fixNodejsPrettierIgnore,
fixNodejsPrettierRc,
} from "./actions/nodejs-prettier.js";
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 { syncDevelopment } from "./developments.js";
import { AncaAction, AncaDevelopment } from "./schema.js";

const actionMappings: Record<
AncaAction,
{ action: (development: AncaDevelopment) => Promise<void>; label: string }
> = {
ancaJsonFix: {
action: async (development: AncaDevelopment) => {
await fixAncaConfig(development);
},
label: "[anca.json] Fix",
},
contributingSetToDefault: {
action: async (development: AncaDevelopment) => {
await fixContributingMd(development);
},
label: "[CONTRIBUTING.md] Set to default",
},
devcontainerDockerfileSetToDefault: {
action: async (development: AncaDevelopment) => {
await fixDevcontainerDockerfile(development);
},
label: "[.devcontainer/Dockerfile] Set to default",
},
devcontainerJsonSetToDefault: {
action: async (development: AncaDevelopment) => {
await fixDevcontainerJson(development);
},
label: "[.devcontainer/devcontainer.json] Set to default",
},
gitClone: {
action: async (development: AncaDevelopment) => {
await syncDevelopment(development);
},
label: "[git] Clone",
},
githubActionsOtherFilesRemove: {
action: async (development: AncaDevelopment) => {
await fixGithubActionsOtherFiles(development);
},
label: "[.github/workflows] Remove other files",
},
githubActionsReleaseSetToDefault: {
action: async (development: AncaDevelopment) => {
await fixGithubActionsRelease(development);
},
label: "[.github/workflows/release.yml] Set to default",
},
githubActionsTestSetToDefault: {
action: async (development: AncaDevelopment) => {
await fixGithubActionsTest(development);
},
label: "[.github/workflows/test.yml] Set to default",
},
gitIgnoreSetToDefault: {
action: async (development: AncaDevelopment) => {
await fixGitIgnore(development);
},
label: "[.gitignore] Set to default",
},
licenseSetToDefault: {
action: async (development: AncaDevelopment) => {
await fixLicenseMd(development);
},
label: "[LICENSE] Set to default",
},
nodejsEsbuildSetToDefault: {
action: async (development: AncaDevelopment) => {
await fixNodejsEsbuildJs(development);
},
label: "[esbuild.js] Set to default",
},
nodejsEslintSetToDefault: {
action: async (development: AncaDevelopment) => {
await fixNodejsEslintConfigJs(development);
},
label: "[eslint.config.js] Set to default",
},
nodejsOpenapiSetToDefault: {
action: async (development: AncaDevelopment) => {
await generateNodejsOpenapiFiles(development);
},
label: "[openapi] Set to default",
},
nodejsPackageJsonCheckUpdates: {
action: async (development: AncaDevelopment) => {
const fileContents = development.state?.jsonFiles[
"package.json"
] as NodejsPackageJson;
if (fileContents != null) {
const rebuildFile: NodejsPackageJson = {};
const npmUpdate: NpmUpdate = await updateNodejsPackageJsonDependencies(
rebuildFile,
development,
false,
true,
);
npmUpdate.push(
...(await updateNodejsPackageJsonDevDependencies(
rebuildFile,
development,
false,
true,
)),
);
if (npmUpdate.length > 0) {
console.log("\nAdd to commit message: \n");
console.log(getUpdatedPackagesCommitMessage(npmUpdate));
console.log();
}
fileContents.dependencies = rebuildFile.dependencies;
fileContents.devDependencies = rebuildFile.devDependencies;
await writeNodejsPackageJson(development);
await installNodejsDependencies(development);
}
},
label: "[package.json] Check dependencies updates",
},
nodejsPackageJsonFix: {
action: async (development: AncaDevelopment) => {
await fixNodejsPackageJson(development, false);
await writeNodejsPackageJson(development);
await installNodejsDependencies(development);
},
label: "[package.json] Fix",
},
nodejsPackageJsonFixFull: {
action: async (development: AncaDevelopment) => {
await fixNodejsPackageJson(development, true);
await writeNodejsPackageJson(development);
await installNodejsDependencies(development);
},
label: "[package.json] Fix & add optional fields",
},
nodejsPrettierIgnoreSetToDefault: {
action: async (development: AncaDevelopment) => {
await fixNodejsPrettierIgnore(development);
},
label: "[.prettierignore] Set to default",
},
nodejsPrettierRcSetToDefault: {
action: async (development: AncaDevelopment) => {
await fixNodejsPrettierRc(development);
},
label: "[.prettierrc] Set to default",
},
nodejsSeaBuildJsSetToDefault: {
action: async (development: AncaDevelopment) => {
await fixNodejsSeaBuildJs(development);
},
label: "[sea.build.js] Set to default",
},
nodejsSeaConfigJsonSetToDefault: {
action: async (development: AncaDevelopment) => {
await fixNodejsSeaConfigJson(development);
},
label: "[sea.config.json] Set to default",
},
nodejsSrcSetToDefault: {
action: async (development: AncaDevelopment) => {
await fixNodejsSrc(development);
},
label: "[src/index.ts] Set to default",
},
nodejsTestSetToDefault: {
action: async (development: AncaDevelopment) => {
await fixNodejsTest(development);
},
label: "[test/index.test.ts] Set to default",
},
nodejsTsconfigSetToDefault: {
action: async (development: AncaDevelopment) => {
await fixNodejsTsconfigJson(development);
},
label: "[tsconfig.json] Set to default",
},
nodejsTsupConfigJsSetToDefault: {
action: async (development: AncaDevelopment) => {
await fixNodejsTsupConfigJs(development);
},
label: "[tsup.config.js] Set to default",
},
openapiJsonSetToDefault: {
action: async (development: AncaDevelopment) => {
await fixOpenapiJson(development);
},
label: "[openapi.json] Set to default",
},
readmeSetToDefault: {
action: async (development: AncaDevelopment) => {
await fixReadmeMd(development);
},
label: "[README.md] Set to default",
},
};

/**
*
* @param action
*/
export function getAction(action: AncaAction) {
return actionMappings[action];
}
4 changes: 2 additions & 2 deletions src/cinnabar.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// This file was generated by Cinnabar Meta. Do not edit.

export const CINNABAR_PROJECT_TIMESTAMP = 1727275337;
export const CINNABAR_PROJECT_VERSION = "0.1.0-dev.3+next.20240925_144217";
export const CINNABAR_PROJECT_TIMESTAMP = 1727284961;
export const CINNABAR_PROJECT_VERSION = "0.1.0-dev.3+next.20240925_172241";
41 changes: 40 additions & 1 deletion src/developments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { mergician } from "mergician";
import path from "path";
import pc from "picocolors";

import { getAction } from "./action.js";
import { checkAnca } from "./actions/anca.js";
import { checkContributingMd } from "./actions/contributing.js";
import {
Expand Down Expand Up @@ -33,7 +34,13 @@ import { checkOpenapiJson } from "./actions/openapi.js";
import { checkReadmeMd } from "./actions/readme.js";
import { checkForGit, getGit } from "./git.js";
import { getDevelopmentMeta } from "./meta.js";
import { AncaConfig, AncaDevelopment, AncaDevelopmentState } from "./schema.js";
import {
ANCA_ACTIONS,
AncaAction,
AncaConfig,
AncaDevelopment,
AncaDevelopmentState,
} from "./schema.js";
import { checkExistence, readFolderFile, readFolderJsonFile } from "./utils.js";

/**
Expand Down Expand Up @@ -557,3 +564,35 @@ async function checkNodeJsToDevelopmentPack(development: AncaDevelopment) {
development.state.issues.push("nodejsTsupConfigJsSetToDefault");
}
}

/**
*
* @param actions
* @param developments
*/
export async function doActionsOnDevelopments(
actions: AncaAction[],
developments: AncaDevelopment[],
) {
console.log(
`\nPerforming ${actions.length} action(s) on ${developments.length} development(s)...\n`,
);
for (const actionName of actions) {
const action = getAction(actionName);

if (action != null) {
console.log(`Performing action '${actionName}'...`);
for (const development of developments) {
await getDevelopmentStatus(development);
if (development.state == null) {
continue;
}
console.log(`...on ${getDevelopmentDisplayName(development)}...`);
action.action(development);
}
} else {
console.log(`Action '${actionName}' doesn't exist.`);
console.log(`Try: ${ANCA_ACTIONS.join(", ")}`);
}
}
}
23 changes: 19 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import {
loadProjects,
readConfigFile,
} from "./config.js";
import { Anca } from "./schema.js";
import { doActionsOnDevelopments } from "./developments.js";
import { Anca, AncaAction } from "./schema.js";
import { showDevelopmentActions, showMainMenu } from "./tui.js";

/**
Expand All @@ -34,10 +35,24 @@ async function main() {
}

if (projects) {
if (projects.deployments?.length || projects.developments?.length !== 1) {
showMainMenu();
if (options.action) {
if (projects.developments?.length) {
await doActionsOnDevelopments(
options.action as AncaAction[],
projects.developments,
);
} else {
console.error("No developments available");
}
} else {
showDevelopmentActions(projects.developments[0]);
if (
projects.deployments?.length ||
projects.developments?.length !== 1
) {
await showMainMenu();
} else {
await showDevelopmentActions(projects.developments[0]);
}
}
} else {
console.error("No config file provided");
Expand Down
29 changes: 29 additions & 0 deletions src/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,35 @@ export type AncaAction =
| "openapiJsonSetToDefault"
| "readmeSetToDefault";

export const ANCA_ACTIONS: AncaAction[] = [
"ancaJsonFix",
"contributingSetToDefault",
"devcontainerDockerfileSetToDefault",
"devcontainerJsonSetToDefault",
"gitClone",
"githubActionsOtherFilesRemove",
"githubActionsReleaseSetToDefault",
"githubActionsTestSetToDefault",
"gitIgnoreSetToDefault",
"licenseSetToDefault",
"nodejsEsbuildSetToDefault",
"nodejsEslintSetToDefault",
"nodejsOpenapiSetToDefault",
"nodejsPackageJsonCheckUpdates",
"nodejsPackageJsonFix",
"nodejsPackageJsonFixFull",
"nodejsPrettierIgnoreSetToDefault",
"nodejsPrettierRcSetToDefault",
"nodejsSeaBuildJsSetToDefault",
"nodejsSeaConfigJsonSetToDefault",
"nodejsSrcSetToDefault",
"nodejsTestSetToDefault",
"nodejsTsconfigSetToDefault",
"nodejsTsupConfigJsSetToDefault",
"openapiJsonSetToDefault",
"readmeSetToDefault",
];

export interface AncaMeta {
description?: string;
name?: string;
Expand Down
Loading

0 comments on commit 3c1cd5e

Please sign in to comment.