Skip to content

Commit

Permalink
add early monorepo support
Browse files Browse the repository at this point in the history
  • Loading branch information
TimurRin committed Sep 2, 2024
1 parent 71b77eb commit 0d2cdfb
Show file tree
Hide file tree
Showing 8 changed files with 113 additions and 18 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.20240812_190908",
"timestamp": 1723489748
"latestNext": "0.1.0-dev.2+next.20240902_221440",
"timestamp": 1725315280
},
"files": [
{
Expand Down
10 changes: 10 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
"clivo": "0.5.1",
"diff": "6.0.0",
"markdown-it": "14.1.0",
"mergician": "2.0.2",
"node-fetch": "3.3.2",
"picocolors": "1.0.1",
"simple-git": "3.26.0"
Expand Down
10 changes: 8 additions & 2 deletions src/actions/anca.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,10 @@ export async function fixAncaConfig(development: AncaDevelopment) {
contents.namings.text = development.data?.name || "Unnamed development";
}

if (contents.type == null || AncaConfigType[contents.type] == null) {
if (
contents.monorepo == null &&
(contents.type == null || AncaConfigType[contents.type] == null)
) {
contents.type = (
await promptOptions("\nChoose project type:", [
{ label: "API", name: "api" },
Expand All @@ -61,7 +64,10 @@ export async function fixAncaConfig(development: AncaDevelopment) {
).name as AncaConfigType;
}

if (contents.stack == null || AncaConfigStack[contents.stack] == null) {
if (
contents.monorepo == null &&
(contents.stack == null || AncaConfigStack[contents.stack] == null)
) {
contents.stack = (
await promptOptions("\nChoose project stack:", [
{ label: "Nodejs", name: "nodejs" },
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 = 1723489748;
export const CINNABAR_PROJECT_VERSION = "0.1.0-dev.2+next.20240812_190908";
export const CINNABAR_PROJECT_TIMESTAMP = 1725315280;
export const CINNABAR_PROJECT_VERSION = "0.1.0-dev.2+next.20240902_221440";
39 changes: 39 additions & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import path from "path";
import {
ANCA_WORKFOLDER_SCHEMA,
Anca,
AncaConfig,
AncaDeployment,
AncaDevelopment,
AncaWorkfolder,
Expand All @@ -12,6 +13,8 @@ import { checkExistence, verifyAjv } from "./utils.js";

let instance: Anca;

const configsCache = new Map<string, AncaConfig>();

/**
* Gets current Anca instance
* @returns Anca instance
Expand All @@ -20,6 +23,18 @@ export function getInstance(): Anca {
return instance;
}

/**
*
* @param fullPath
*/
function getMonorepo(fullPath: string) {
const ancaJson = path.join(fullPath, "anca.json");
if (!configsCache.has(fullPath) && fs.existsSync(ancaJson)) {
configsCache.set(fullPath, JSON.parse(fs.readFileSync(ancaJson, "utf-8")));
}
return configsCache.get(fullPath)?.monorepo;
}

/**
* Loads and validates config
* @param {string} workfolderPath path to work folder
Expand Down Expand Up @@ -66,6 +81,19 @@ export function loadAndValidateConfig(
fullPath,
};
instance.developments.push(developmentInstance);
const monorepo = getMonorepo(fullPath);
if (monorepo) {
for (const part of monorepo) {
const developmentInstancePart: AncaDevelopment = {
data: development,
folderPath: path.resolve(folderPath, part.name),
fullPath: path.resolve(fullPath, part.name),
monorepoFullPath: fullPath,
monorepoPart: part.data,
};
instance.developments.push(developmentInstancePart);
}
}
}
}

Expand All @@ -85,6 +113,17 @@ export async function loadProjects(projectPaths: string[]) {
fullPath,
};
instance.developments.push(developmentInstance);
const monorepo = getMonorepo(fullPath);
if (monorepo) {
for (const part of monorepo) {
const developmentInstancePart: AncaDevelopment = {
fullPath: path.resolve(fullPath, part.name),
monorepoFullPath: fullPath,
monorepoPart: part.data,
};
instance.developments.push(developmentInstancePart);
}
}
}
}

Expand Down
46 changes: 35 additions & 11 deletions src/developments.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { mergician } from "mergician";
import path from "path";
import pc from "picocolors";

Expand Down Expand Up @@ -30,28 +31,39 @@ import { checkNodejsTsupConfigJs } from "./actions/nodejs-tsup.js";
import { checkReadmeMd } from "./actions/readme.js";
import { checkForGit, getGit } from "./git.js";
import { getDevelopmentMeta } from "./meta.js";
import { AncaDevelopment, AncaDevelopmentState } from "./schema.js";
import { AncaConfig, AncaDevelopment, AncaDevelopmentState } from "./schema.js";
import { checkExistence, readFolderFile, readFolderJsonFile } from "./utils.js";

/**
*
* @param development
*/
export function getDevelopmentMainFullPath(development: AncaDevelopment) {
return development.monorepoFullPath || development.fullPath;
}

/**
*
* @param development
*/
export async function getDevelopmentStatus(development: AncaDevelopment) {
const mainFullPath = getDevelopmentMainFullPath(development);

const exists = await checkExistence(development.fullPath);
if (!exists) {
return ["remote"];
}

const hasAncaJson =
(await checkExistence(development.fullPath)) &&
(await checkExistence(path.join(development.fullPath, "anca.json")));
development.monorepoFullPath != null ||
((await checkExistence(development.fullPath)) &&
(await checkExistence(path.join(development.fullPath, "anca.json"))));

const statuses = [];

const gitExists = await checkForGit(development.fullPath);
const gitExists = await checkForGit(mainFullPath);
if (gitExists) {
await getGit().cwd(development.fullPath);
await getGit().cwd(mainFullPath);
const statusSummary = await getGit().status();

const isSyncPending = statusSummary.behind > 0 || statusSummary.ahead > 0;
Expand Down Expand Up @@ -90,7 +102,9 @@ export async function getDevelopmentStatus(development: AncaDevelopment) {
development.state.config != null
) {
statuses.unshift(
`${development.state.config.stack || "unsupported"} ${development.state.config.type || "project"}`,
development.state.config.monorepo != null
? "monorepo"
: `${development.state.config.stack || "unsupported"} ${development.state.config.type || "project"}`,
);
}

Expand All @@ -106,13 +120,14 @@ export async function syncDevelopment(development: AncaDevelopment) {
return;
}
const folderExists = await checkExistence(development.fullPath);
const gitExists = await checkForGit(development.fullPath);
const mailFullPath = getDevelopmentMainFullPath(development);
const gitExists = await checkForGit(mailFullPath);
if (folderExists && gitExists) {
await getGit().cwd(development.fullPath).fetch();
await getGit().cwd(mailFullPath).fetch();
} else if (folderExists) {
await getGit().cwd(development.fullPath).init();
await getGit().cwd(mailFullPath).init();
} else {
await getGit().clone(development.data.gitOrigin, development.fullPath);
await getGit().clone(development.data.gitOrigin, mailFullPath);
}
}

Expand All @@ -138,7 +153,16 @@ export async function refreshDevelopmentState(
}
const exists = await checkExistence(development.fullPath);

const config = await readFolderJsonFile(development.fullPath, "anca.json");
let config = (await readFolderJsonFile(
getDevelopmentMainFullPath(development),
"anca.json",
)) as AncaConfig | null;

if (config != null && development.monorepoPart != null) {
config = mergician(config, development.monorepoPart);
config.monorepo = undefined;
console.log(config);
}

const state: AncaDevelopmentState = {
actions: [],
Expand Down
17 changes: 16 additions & 1 deletion src/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ export interface AncaDevelopment {
data?: AncaDevelopmentWorkfolderData;
folderPath?: string;
fullPath: string;
monorepoFullPath?: string;
monorepoPart?: AncaConfigMonorepoData;
state?: AncaDevelopmentState;
}

Expand Down Expand Up @@ -108,11 +110,24 @@ export interface AncaConfigNamings {
textShort?: string;
}

export interface AncaConfigMonorepoData {
development?: AncaDevelopmentConfig;
namings?: AncaConfigNamings;
stack?: AncaConfigStack;
type?: AncaConfigType;
}

export interface AncaConfigMonorepo {
data: AncaConfigMonorepoData;
name: string;
}

export interface AncaConfig {
authors?: AncaConfigAuthor[];
deployment?: AncaDeploymentConfig;
development?: AncaDevelopmentConfig;
downloadBinariesUrl?: string;
monorepo?: AncaConfigMonorepo[];
namings?: AncaConfigNamings;
organizations?: AncaConfigOrganization[];
stack?: AncaConfigStack;
Expand Down Expand Up @@ -321,7 +336,7 @@ export const ANCA_CONFIG_SCHEMA = {
type: "string",
},
},
required: ["namings", "stack", "type"],
required: ["namings"],
type: "object",
};

Expand Down

0 comments on commit 0d2cdfb

Please sign in to comment.