Skip to content

Commit

Permalink
add ability to launch current working folder if anca.json is presented
Browse files Browse the repository at this point in the history
add development display on startup if its only development
  • Loading branch information
TimurRin committed Sep 25, 2024
1 parent 0b054ab commit 359c873
Show file tree
Hide file tree
Showing 10 changed files with 71 additions and 28 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.20240922_155921",
"timestamp": 1727020761
"latestNext": "0.1.0-dev.3+next.20240925_144217",
"timestamp": 1727275337
},
"files": [
{
Expand Down
2 changes: 1 addition & 1 deletion src/api/nodejs-npm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,6 @@ export async function fetchNpmPackagesVersion(
return data.response;
} catch (error) {
console.error("Error fetching package versions:", error);
throw error;
}
return {};
}
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 = 1727020761;
export const CINNABAR_PROJECT_VERSION = "0.1.0-dev.3+next.20240922_155921";
export const CINNABAR_PROJECT_TIMESTAMP = 1727275337;
export const CINNABAR_PROJECT_VERSION = "0.1.0-dev.3+next.20240925_144217";
22 changes: 22 additions & 0 deletions src/cli.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { parseCli } from "clivo";
import fs from "fs";

/**
* Parses cli arguments
Expand All @@ -8,6 +9,11 @@ export function setupCli() {
const cli = parseCli({
args: process.argv,
options: [
{
label: "Specify the actions to perform on all projects",
letter: "a",
name: "action",
},
{
label: "Specify the path to the workfolder files",
letter: "c",
Expand All @@ -31,6 +37,12 @@ export function setupCli() {
],
});

const throwProjects = () => {
throw new Error(
"Please specify the projects to work on (--config, --github, --project)",
);
};

const throwWorkfolder = () => {
throw new Error(
"Please specify the path to the main workfolder (--workfolder)",
Expand All @@ -44,5 +56,15 @@ export function setupCli() {
}
}

if (cli.config == null && cli.github == null && cli.project == null) {
const currentDir = process.cwd();

if (fs.existsSync(`${currentDir}/anca.json`)) {
cli.project = [currentDir];
} else {
throwProjects();
}
}

return cli;
}
22 changes: 16 additions & 6 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ export function loadAndValidateConfig(
data: deployment,
fullPath,
};
instance.deployments.push(deploymentInstance);
instance.deployments?.push(deploymentInstance);
}

for (const development of configContents.developments) {
Expand All @@ -120,7 +120,7 @@ export function loadAndValidateConfig(
folderPath,
fullPath,
};
instance.developments.push(developmentInstance);
instance.developments?.push(developmentInstance);
const monorepo = getMonorepo(fullPath);
if (monorepo) {
for (const part of monorepo) {
Expand All @@ -131,10 +131,12 @@ export function loadAndValidateConfig(
monorepoFullPath: fullPath,
monorepoPart: part.data,
};
instance.developments.push(developmentInstancePart);
instance.developments?.push(developmentInstancePart);
}
}
}

return instance;
}

/**
Expand Down Expand Up @@ -198,7 +200,7 @@ export async function loadProjects(projectPaths: string[]) {
const developmentInstance: AncaDevelopment = {
fullPath,
};
instance.developments.push(developmentInstance);
instance.developments?.push(developmentInstance);
const monorepo = getMonorepo(fullPath);
if (monorepo) {
for (const part of monorepo) {
Expand All @@ -207,10 +209,12 @@ export async function loadProjects(projectPaths: string[]) {
monorepoFullPath: fullPath,
monorepoPart: part.data,
};
instance.developments.push(developmentInstancePart);
instance.developments?.push(developmentInstancePart);
}
}
}

return instance;
}

/**
Expand All @@ -223,7 +227,13 @@ export async function createFolders(workfolderPath: string) {
await fs.promises.mkdir(deploymentsPath, { recursive: true });
}

for (const development of getInstance().developments) {
const developments = getInstance().developments;

if (!developments) {
return;
}

for (const development of developments) {
if (
development.folderPath &&
!(await checkExistence(development.folderPath))
Expand Down
4 changes: 4 additions & 0 deletions src/developments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,11 +126,15 @@ export async function syncDevelopment(development: AncaDevelopment) {
const folderExists = await checkExistence(development.fullPath);
const mailFullPath = getDevelopmentMainFullPath(development);
const gitExists = await checkForGit(mailFullPath);

if (folderExists && gitExists) {
console.log("Fetching to", mailFullPath);
await getGit().cwd(mailFullPath).fetch();
} else if (folderExists) {
console.log("Init in", mailFullPath);
await getGit().cwd(mailFullPath).init();
} else {
console.log("Cloning to", mailFullPath);
await getGit().clone(development.data.gitOrigin, mailFullPath);
}
}
Expand Down
23 changes: 16 additions & 7 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,20 @@ import {
loadProjects,
readConfigFile,
} from "./config.js";
import { showMainMenu } from "./tui.js";
import { Anca } from "./schema.js";
import { showDevelopmentActions, showMainMenu } from "./tui.js";

/**
*
*/
async function main() {
const options = setupCli();

try {
let projects: Anca | null = null;
const options = setupCli();
if (options.project) {
await loadProjects(options.project);
projects = await loadProjects(options.project);
} else if (options.workfolder) {
loadAndValidateConfig(
projects = loadAndValidateConfig(
options.workfolder[0],
options.github
? getConfigFromGithub(options.github)
Expand All @@ -32,9 +33,17 @@ async function main() {
loadEmpty();
}

showMainMenu();
if (projects) {
if (projects.deployments?.length || projects.developments?.length !== 1) {
showMainMenu();
} else {
showDevelopmentActions(projects.developments[0]);
}
} else {
console.error("No config file provided");
}
} catch (error: any) {
throw new Error(`Failed to start Anca: ${error.message}`);
console.error(`Failed to start Anca: ${error.message}`);
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ export interface AncaDevelopment {
}

export interface Anca {
deployments: AncaDeployment[];
developments: AncaDevelopment[];
deployments?: AncaDeployment[];
developments?: AncaDevelopment[];
}

export interface AncaWorkfolder {
Expand Down
12 changes: 6 additions & 6 deletions src/tui.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,11 @@ const APP_NAME_AND_VERSION = `ANCA v${CINNABAR_PROJECT_VERSION}`;
* @param development
* @param previousMenu
*/
async function showDevelopmentActions(
export async function showDevelopmentActions(
development: AncaDevelopment,
previousMenu: () => Promise<void>,
previousMenu?: () => Promise<void>,
) {
const menu = [{ action: previousMenu, label: "Back" }];
const menu = previousMenu ? [{ action: previousMenu, label: "Back" }] : [];

const backHere = async () => {
development.state = undefined;
Expand Down Expand Up @@ -335,7 +335,7 @@ async function showAllDevelopments() {

const state = getInstance();

for (const development of state.developments) {
for (const development of state.developments || []) {
options.push({
action: async () => {
showDevelopmentActions(development, showAllDevelopments);
Expand All @@ -355,7 +355,7 @@ async function showLocalDevelopments() {

const state = getInstance();

for (const development of state.developments) {
for (const development of state.developments || []) {
if (await checkExistence(development.fullPath)) {
options.push({
action: async () => {
Expand All @@ -377,7 +377,7 @@ async function showProblematicLocalDevelopments() {

const state = getInstance();

for (const development of state.developments) {
for (const development of state.developments || []) {
if (await checkExistence(development.fullPath)) {
await refreshDevelopmentState(development);
if (development.state != null && development.state.issues.length > 0) {
Expand Down
2 changes: 0 additions & 2 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,6 @@ export async function writeFolderFile(
`Failed to write file at ${path.resolve(folder, filePath)}:`,
error,
);
throw error;
}
}

Expand Down Expand Up @@ -176,7 +175,6 @@ export async function writeFolderJsonFile(
`Failed to write file at ${path.resolve(folder, filePath)}:`,
error,
);
throw error;
}
}

Expand Down

0 comments on commit 359c873

Please sign in to comment.