Skip to content

Commit

Permalink
add npm cached version fetch and check
Browse files Browse the repository at this point in the history
update npm packages
  • Loading branch information
TimurRin committed Jul 26, 2024
1 parent ab6c46e commit 7b55ca9
Show file tree
Hide file tree
Showing 6 changed files with 168 additions and 32 deletions.
3 changes: 2 additions & 1 deletion anca.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
"dataVersion": 0,
"version": {
"latest": "0.1.0-dev.1",
"timestamp": 1721650842
"latestNext": "0.1.0-dev.1+next.20240726_213009",
"timestamp": 1722029409
},
"files": [
{
Expand Down
91 changes: 91 additions & 0 deletions package-lock.json

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

9 changes: 6 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,20 +36,23 @@
"cftm": "0.1.0",
"clivo": "0.5.1",
"markdown-it": "14.1.0",
"node-fetch": "3.3.2",
"picocolors": "1.0.1",
"simple-git": "3.25.0"
},
"devDependencies": {
"@cinnabar-forge/eslint-plugin": "0.6.1",
"@cinnabar-forge/meta": "0.2.0",
"@cinnabar-forge/meta": "0.3.1",
"@types/markdown-it": "14.1.1",
"@types/node": "20.14.11",
"@types/node": "20.14.12",
"@types/chai": "4.3.16",
"@types/mocha": "10.0.7",
"chai": "5.1.1",
"esbuild": "0.23.0",
"mocha": "10.7.0",
"pre-commit": "1.2.2",
"tsc-watch": "6.2.0",
"typescript": "5.5.3"
"typescript": "5.5.4"
},
"engines": {
"node": ">=18.0.0"
Expand Down
50 changes: 44 additions & 6 deletions src/actions/nodejs.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* eslint-disable sonarjs/no-duplicate-string */
import { promptText } from "clivo";

import { fetchNpmPackageVersion } from "../api/nodejs-npm.js";
import { fetchNpmPackagesVersion } from "../api/nodejs-npm.js";
import { AncaDevelopment } from "../schema.js";
import { writeFolderJsonFile } from "../utils.js";

Expand Down Expand Up @@ -273,9 +273,7 @@ export async function fixNodejsPackageJson(development: AncaDevelopment) {
}
}

if (contents.dependencies == null) {
contents.dependencies = {};
}
await checkNodejsPackageJsonDependencies(development);

await checkNodejsPackageJsonDevDependencies(development);

Expand All @@ -294,6 +292,40 @@ export async function fixNodejsPackageJson(development: AncaDevelopment) {
}
}

/**
*
* @param development
*/
export async function checkNodejsPackageJsonDependencies(
development: AncaDevelopment,
) {
if (development.state == null) {
return;
}
const contents: NodejsPackageJson | null | undefined =
development.state.jsonFiles["package.json"];

if (contents == null) {
return;
}

if (contents.dependencies == null) {
contents.dependencies = {};
}

const dependencyKeys = Object.keys(contents.dependencies);

try {
const fetchedVersions = await fetchNpmPackagesVersion(dependencyKeys);

for (const pkg of dependencyKeys) {
contents.dependencies[pkg] = fetchedVersions[pkg];
}
} catch (error) {
console.error("Error updating devDependencies:", error);
}
}

/**
*
* @param development
Expand All @@ -320,8 +352,14 @@ export async function checkNodejsPackageJsonDevDependencies(
? DEV_DEPENDENCIES_APP
: DEV_DEPENDENCIES_LIB;

for (const key of devDependencies) {
contents.devDependencies[key] = fetchNpmPackageVersion(key);
try {
const fetchedVersions = await fetchNpmPackagesVersion(devDependencies);

for (const pkg of devDependencies) {
contents.devDependencies[pkg] = fetchedVersions[pkg];
}
} catch (error) {
console.error("Error updating devDependencies:", error);
}
}

Expand Down
43 changes: 23 additions & 20 deletions src/api/nodejs-npm.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,27 @@
const NPM_PACKAGES_PREDEFINED_VERSIONS: Record<string, string> = {
"@cinnabar-forge/eslint-plugin": "0.6.0",
"@cinnabar-forge/meta": "0.1.2",
"@types/chai": "4.3.16",
"@types/mocha": "10.0.7",
"@types/node": "20.14.8",
"@types/sinon": "17.0.3",
chai: "5.1.1",
esbuild: "0.21.5",
mocha: "10.5.1",
"pre-commit": "1.2.2",
sinon: "18.0.0",
"tsc-watch": "6.2.0",
tsup: "8.1.0",
typescript: "5.5.2",
};
import fetch from "node-fetch";

/**
*
* @param packageName
* Predefined versions for npm packages
* @param packagesName
*/
export function fetchNpmPackageVersion(packageName: string): string {
return NPM_PACKAGES_PREDEFINED_VERSIONS[packageName];
export async function fetchNpmPackagesVersion(
packagesName: string[],
): Promise<Record<string, string>> {
const baseUrl = "https://npm-versions.cinnabar.ru/versions";
const query = `?packages=${packagesName.join(",")}`;
const url = `${baseUrl}${query}`;

try {
const response = await fetch(url);
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const data = (await response.json()) as {
response: Record<string, string>;
};
return data.response;
} catch (error) {
console.error("Error fetching package versions:", error);
throw error;
}
}
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 = 1721650842;
export const CINNABAR_PROJECT_VERSION = "0.1.0-dev.1";
export const CINNABAR_PROJECT_TIMESTAMP = 1722029409;
export const CINNABAR_PROJECT_VERSION = "0.1.0-dev.1+next.20240726_213009";

0 comments on commit 7b55ca9

Please sign in to comment.