Skip to content

Commit

Permalink
include resource to AncaWorkfolder config
Browse files Browse the repository at this point in the history
  • Loading branch information
TimurRin committed Sep 5, 2024
1 parent 5f8d4ff commit ed0d847
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 22 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.2",
"latestNext": "0.1.0-dev.2+next.20240904_133911",
"timestamp": 1725457151
"latestNext": "0.1.0-dev.2+next.20240905_055554",
"timestamp": 1725515754
},
"files": [
{
Expand Down
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 = 1725457151;
export const CINNABAR_PROJECT_VERSION = "0.1.0-dev.2+next.20240904_133911";
export const CINNABAR_PROJECT_TIMESTAMP = 1725515754;
export const CINNABAR_PROJECT_VERSION = "0.1.0-dev.2+next.20240905_055554";
70 changes: 61 additions & 9 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,10 @@ export function getConfigFromGithub(githubUrls: string[]) {
const repo = splitted[splitted.length - 1];
console.log(githubUrl, splitted, owner, repo);
config.developments.push({
folder: owner,
gitOrigin: `https://github.com/${owner}/${repo}.git`,
gitOrigin: `git@github.com:${owner}/${repo}.git`,
name: repo,
owner,
resource: "github.com",
});
}
return config;
Expand All @@ -69,19 +70,23 @@ export function loadEmpty() {
};
}

/**
*
* @param configPath
*/
export function readConfigFile(configPath: string) {
return JSON.parse(fs.readFileSync(configPath, "utf-8"));
}

/**
* Loads and validates config
* @param {string} workfolderPath path to work folder
* @param {string[]} configsPath paths to config files
* @param {AncaWorkfolder} configContents paths to config files
*/
export function loadAndValidateConfig(
workfolderPath: string,
configsPath: string[],
configContents: AncaWorkfolder,
) {
const configContents: AncaWorkfolder = JSON.parse(
fs.readFileSync(configsPath[0], "utf-8"),
);

verifyAjv(ANCA_WORKFOLDER_SCHEMA, configContents);

instance = {
Expand All @@ -106,7 +111,8 @@ export function loadAndValidateConfig(
const folderPath = path.resolve(
workfolderPath,
"developments",
development.folder,
development.resource,
development.owner,
);
const fullPath = path.resolve(folderPath, development.name);
const developmentInstance: AncaDevelopment = {
Expand All @@ -131,6 +137,52 @@ export function loadAndValidateConfig(
}
}

/**
*
* @param config
* @param currentPath
*/
async function traverseDirectory(config: AncaWorkfolder, currentPath: string) {
const entries = await fs.promises.readdir(currentPath, {
withFileTypes: true,
});

for (const entry of entries) {
if (entry.isDirectory()) {
const entryPath = path.join(currentPath, entry.name);
const parts = entryPath.split(path.sep);

if (parts.length >= 4) {
const resource = parts[parts.length - 3];
const owner = parts[parts.length - 2];
const repo = parts[parts.length - 1];

config.developments.push({
name: repo,
owner,
resource,
});
} else {
await traverseDirectory(config, entryPath);
}
}
}
}

/**
*
* @param workfolderPath
*/
export async function loadWorkfolder(workfolderPath: string) {
const config: AncaWorkfolder = {
ancaDataVersion: 0,
deployments: [],
developments: [],
};

await traverseDirectory(config, workfolderPath);
}

/**
*
* @param projectPaths
Expand Down
2 changes: 1 addition & 1 deletion src/developments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ export async function syncDevelopment(development: AncaDevelopment) {
export function getDevelopmentDisplayName(
development: AncaDevelopment,
): string {
return `${development.state?.config.namings?.text || development.data?.name || development.fullPath.split("/").pop()}${pc.dim(" (" + (development.data?.folder || development.fullPath) + ")")}`;
return `${development.state?.config.namings?.text || development.data?.name || development.fullPath.split("/").pop()}${pc.dim(" (" + (development.data?.owner || development.fullPath) + ")")}`;
}

/**
Expand Down
7 changes: 6 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
loadAndValidateConfig,
loadEmpty,
loadProjects,
readConfigFile,
} from "./config.js";
import { showMainMenu } from "./tui.js";

Expand All @@ -20,7 +21,11 @@ async function main() {
} else if (options.workfolder) {
loadAndValidateConfig(
options.workfolder[0],
options.github ? getConfigFromGithub(options.github) : options.config,
options.github
? getConfigFromGithub(options.github)
: options.config
? readConfigFile(options.config[0])
: {},
);
await createFolders(options.workfolder[0]);
} else {
Expand Down
14 changes: 9 additions & 5 deletions src/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@ export interface AncaDeployment {
}

export interface AncaDevelopmentWorkfolderData {
folder: string;
gitOrigin?: string;
gitProds?: string[];
name: string;
owner: string;
resource: string;
}

export type AncaAction =
Expand Down Expand Up @@ -189,9 +190,6 @@ export const ANCA_WORKFOLDER_SCHEMA = {
description: "List of development projects",
items: {
properties: {
folder: {
type: "string",
},
gitOrigin: {
type: "string",
},
Expand All @@ -207,8 +205,14 @@ export const ANCA_WORKFOLDER_SCHEMA = {
name: {
type: "string",
},
owner: {
type: "string",
},
resource: {
type: "string",
},
},
required: ["folder", "name"],
required: ["resource", "owner", "name"],
type: "object",
},
type: "array",
Expand Down
4 changes: 2 additions & 2 deletions src/tui.ts
Original file line number Diff line number Diff line change
Expand Up @@ -302,8 +302,8 @@ async function showDevelopmentActions(
state.actions.forEach((code) => map(code, false));

await promptMenu(
development.data?.name && development.data?.folder
? `\n[${development.data.name.toUpperCase()} at ${development.data.folder.toUpperCase()}] (${(await getDevelopmentStatus(development)).join(", ")})`
development.data?.name && development.data?.owner
? `\n[${development.data.name.toUpperCase()} at ${development.data.owner.toUpperCase()}] (${(await getDevelopmentStatus(development)).join(", ")})`
: `\n[${development.fullPath}] (${(await getDevelopmentStatus(development)).join(", ")})`,
menu,
);
Expand Down

0 comments on commit ed0d847

Please sign in to comment.