Skip to content

Commit

Permalink
add monorepo logic to developments
Browse files Browse the repository at this point in the history
update dependencies update logic
  • Loading branch information
TimurRin committed Sep 4, 2024
1 parent 0d2cdfb commit 38fdcca
Show file tree
Hide file tree
Showing 6 changed files with 143 additions and 51 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.20240902_221440",
"timestamp": 1725315280
"latestNext": "0.1.0-dev.2+next.20240904_035621",
"timestamp": 1725422181
},
"files": [
{
Expand Down
103 changes: 78 additions & 25 deletions src/actions/nodejs.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/* eslint-disable sonarjs/no-duplicate-string */
import { exec } from "child_process";
import { promptText } from "clivo";
import { isDeepStrictEqual } from "util";

Expand Down Expand Up @@ -83,6 +84,23 @@ const packageNameOrder = [
"pre-commit",
];

// eslint-disable-next-line @typescript-eslint/no-unused-vars
const SCRIPTS_API: Record<string, string> = {
build: "node ../esbuild.js",
dev: 'tsc-watch --onSuccess "node ./build/dev/src/index.js"',
fix: "prettier . --write --ignore-path ../.gitignore --ignore-path ../.prettierignore && eslint --fix .",
format:
"prettier . --write --ignore-path ../.gitignore --ignore-path ../.prettierignore",
lint: "eslint --fix .",
"migration:down": "knex migrate:rollback",
"migration:ls": "npx knex migrate:list",
"migration:restart": "knex migrate:rollback && knex migrate:latest",
"migration:up": "knex migrate:latest",
prepack: "npm run build",
start: "node dist/index.js",
test: "prettier . -c && eslint --max-warnings 0 . && tsc && mocha './build/dev/test'",
};

const SCRIPTS_APP: Record<string, string> = {
build: "node esbuild.js",
"build:bundle": "node esbuild.js full",
Expand All @@ -106,6 +124,12 @@ const SCRIPTS_LIB: Record<string, string> = {
test: "prettier . -c && eslint --max-warnings 0 . && tsc && mocha './build/dev/test'",
};

// eslint-disable-next-line @typescript-eslint/no-unused-vars
const DEPENDENCIES_API: string[] = [];

// eslint-disable-next-line @typescript-eslint/no-unused-vars
const DEV_DEPENDENCIES_API: string[] = [];

const DEV_DEPENDENCIES_APP: string[] = [
"@cinnabar-forge/eslint-plugin",
"@cinnabar-forge/meta",
Expand Down Expand Up @@ -241,7 +265,7 @@ export async function checkNodejsPackageJson(
hasPrivate(contents) &&
hasPublishConfig(contents) &&
hasWorkspaces(contents) &&
hasPreCommit(contents) &&
hasPreCommit(contents, development.monorepoPart ? true : false) &&
checkPackageJsonOrder(contents)
);
}
Expand Down Expand Up @@ -590,9 +614,10 @@ function hasWorkspaces(contents: NodejsPackageJson) {
/**
*
* @param contents
* @param isMonorepoPart
*/
function hasPreCommit(contents: NodejsPackageJson) {
return contents["pre-commit"] != null;
function hasPreCommit(contents: NodejsPackageJson, isMonorepoPart: boolean) {
return isMonorepoPart || contents["pre-commit"] != null;
}

/**
Expand Down Expand Up @@ -662,7 +687,7 @@ export async function fixNodejsPackageJson(
await fixPackageScripts(rebuildFile, contents, config);
await fixPackageConfig(rebuildFile, contents);
await updateNodejsPackageJsonDependencies(rebuildFile, development);
await updateNodejsPackageJsonDevDependencies(rebuildFile, development);
await updateNodejsPackageJsonDevDependencies(rebuildFile, development, true);
await fixPackagePeerDependencies(rebuildFile, contents);
await fixPackagePeerDependenciesMeta(rebuildFile, contents);
await fixPackageBundleDependencies(rebuildFile, contents);
Expand All @@ -685,6 +710,26 @@ export async function fixNodejsPackageJson(
development.state.jsonFiles["package.json"] = rebuildFile;
}

/**
*
* @param development
*/
export async function installNodejsDependencies(development: AncaDevelopment) {
// eslint-disable-next-line security/detect-child-process
exec(`cd ${development.fullPath} && npm i`, (error, stdout, stderr) => {
if (error) {
console.log(
`package.json has been updated, but can't use 'npm i', please use it in ${development.fullPath} to install dependencies manually`,
);
return;
}
console.log(`npm install output: ${stdout}`);
if (stderr) {
console.error(`npm install errors: ${stderr}`);
}
});
}

/**
*
* @param rebuildFile
Expand Down Expand Up @@ -1062,25 +1107,26 @@ export async function updateNodejsPackageJsonDependencies(

rebuildFile.dependencies = {};

const predefinedDependencies: string[] = [];

const allDependencies = new Set([
...predefinedDependencies,
...Object.keys(contents.dependencies),
]);
const allDependencies = Object.keys(contents.dependencies);

const allDependenciesList = Array.from(allDependencies).sort();

try {
const fetchedVersions = await fetchNpmPackagesVersion(allDependenciesList);
const fetchedVersions = await fetchNpmPackagesVersion(
allDependenciesList.filter((dep: string) => !dep.includes("file:")),
);

for (const pkg of allDependenciesList) {
if (fetchedVersions[pkg] !== contents.dependencies[pkg]) {
if (
fetchedVersions[pkg] !== contents.dependencies[pkg] &&
fetchedVersions[pkg]
) {
console.log(
`Updating dep '${pkg}' from ${contents.dependencies[pkg]} to ${fetchedVersions[pkg]}`,
);
}
rebuildFile.dependencies[pkg] = fetchedVersions[pkg];
rebuildFile.dependencies[pkg] =
fetchedVersions[pkg] || contents.dependencies[pkg];
}
} catch (error) {
console.error("Error updating dependencies:", error);
Expand All @@ -1091,10 +1137,12 @@ export async function updateNodejsPackageJsonDependencies(
*
* @param rebuildFile
* @param development
* @param addPredefined
*/
export async function updateNodejsPackageJsonDevDependencies(
rebuildFile: NodejsPackageJson,
development: AncaDevelopment,
addPredefined: boolean,
) {
if (development.state == null) {
return;
Expand All @@ -1112,30 +1160,35 @@ export async function updateNodejsPackageJsonDevDependencies(

rebuildFile.devDependencies = {};

const predefinedDevDependencies =
development.state.config.type === "app"
? DEV_DEPENDENCIES_APP
: DEV_DEPENDENCIES_LIB;

const allDevDependencies = new Set([
...predefinedDevDependencies,
...Object.keys(contents.devDependencies),
]);
const allDevDependencies = addPredefined
? new Set([
...(development.state.config.type === "app"
? DEV_DEPENDENCIES_APP
: development.state.config.type === "library"
? DEV_DEPENDENCIES_LIB
: []),
...Object.keys(contents.devDependencies),
])
: Object.keys(contents.devDependencies);

const allDevDependenciesList = Array.from(allDevDependencies).sort();

try {
const fetchedVersions = await fetchNpmPackagesVersion(
allDevDependenciesList,
allDevDependenciesList.filter((dep: string) => !dep.includes("file:")),
);

for (const pkg of allDevDependenciesList) {
if (fetchedVersions[pkg] !== contents.devDependencies[pkg]) {
if (
fetchedVersions[pkg] !== contents.devDependencies[pkg] &&
fetchedVersions[pkg]
) {
console.log(
`Updating dev-dep '${pkg}' from ${contents.devDependencies[pkg]} to ${fetchedVersions[pkg]}`,
);
}
rebuildFile.devDependencies[pkg] = fetchedVersions[pkg];
rebuildFile.devDependencies[pkg] =
fetchedVersions[pkg] || contents.devDependencies[pkg];
}
} catch (error) {
console.error("Error updating devDependencies:", error);
Expand Down
5 changes: 5 additions & 0 deletions src/api/nodejs-npm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,15 @@ import fetch from "node-fetch";
export async function fetchNpmPackagesVersion(
packagesName: string[],
): Promise<Record<string, string>> {
if (packagesName.length === 0) {
return {};
}
const baseUrl = "https://npm-versions.cinnabar.ru/versions";
const query = `?packages=${packagesName.join(",")}`;
const url = `${baseUrl}${query}`;

console.log("Requesting from Cinnabar Forge NPM Cache:", url);

try {
const response = await fetch(url);
if (!response.ok) {
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 = 1725315280;
export const CINNABAR_PROJECT_VERSION = "0.1.0-dev.2+next.20240902_221440";
export const CINNABAR_PROJECT_TIMESTAMP = 1725422181;
export const CINNABAR_PROJECT_VERSION = "0.1.0-dev.2+next.20240904_035621";
Loading

0 comments on commit 38fdcca

Please sign in to comment.