@withstudiocms/internal_helpers 0.1.0-beta.1 → 0.1.0-beta.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
# @withstudiocms/internal_helpers
|
|
2
2
|
|
|
3
|
+
[](https://codecov.io/github/withstudiocms/studiocms)
|
|
4
|
+
|
|
3
5
|
Internal helpers and utilities for the @withstudiocms eco-system.
|
|
4
6
|
|
|
5
7
|
## License
|
|
@@ -1,12 +1,21 @@
|
|
|
1
|
+
import fs from 'node:fs';
|
|
1
2
|
import type { AstroIntegrationLogger } from 'astro';
|
|
3
|
+
interface FileIO {
|
|
4
|
+
readFileSync: typeof fs.readFileSync;
|
|
5
|
+
writeFileSync: typeof fs.writeFileSync;
|
|
6
|
+
}
|
|
2
7
|
/**
|
|
3
8
|
* Fetches the latest version of a given npm package from the npm registry.
|
|
4
9
|
*
|
|
5
10
|
* @param packageName - The name of the npm package to fetch the latest version for.
|
|
6
11
|
* @param logger - An instance of `AstroIntegrationLogger` used to log errors if the fetch fails.
|
|
12
|
+
* @param cacheJsonFile - File URL for dev-mode cache (optional).
|
|
13
|
+
* @param isDevMode - When true, uses/updates a 1h TTL cache in `cacheJsonFile`.
|
|
14
|
+
* @param io - Optional file IO overrides for testing.
|
|
7
15
|
* @returns A promise that resolves to the latest version of the package as a string,
|
|
8
16
|
* or `null` if an error occurs during the fetch process.
|
|
9
17
|
*
|
|
10
18
|
* @throws Will throw an error if the HTTP response from the npm registry is not successful.
|
|
11
19
|
*/
|
|
12
|
-
export declare function getLatestVersion(packageName: string, logger: AstroIntegrationLogger, cacheJsonFile: URL | undefined, isDevMode: boolean): Promise<string | null>;
|
|
20
|
+
export declare function getLatestVersion(packageName: string, logger: AstroIntegrationLogger, cacheJsonFile: URL | undefined, isDevMode: boolean, io?: FileIO): Promise<string | null>;
|
|
21
|
+
export {};
|
|
@@ -1,29 +1,41 @@
|
|
|
1
1
|
import fs from "node:fs";
|
|
2
2
|
import { jsonParse } from "../utils/jsonUtils.js";
|
|
3
|
-
async function getLatestVersion(packageName, logger, cacheJsonFile, isDevMode) {
|
|
3
|
+
async function getLatestVersion(packageName, logger, cacheJsonFile, isDevMode, io = fs) {
|
|
4
4
|
let cacheData = {};
|
|
5
5
|
if (isDevMode && cacheJsonFile) {
|
|
6
|
-
|
|
7
|
-
|
|
6
|
+
try {
|
|
7
|
+
const file = io.readFileSync(cacheJsonFile, { encoding: "utf-8" });
|
|
8
|
+
cacheData = jsonParse(file) ?? {};
|
|
9
|
+
} catch (err) {
|
|
10
|
+
if (!err?.code?.includes("ENOENT")) {
|
|
11
|
+
logger?.warn?.(`Ignoring cache read error for ${cacheJsonFile}: ${err.message}`);
|
|
12
|
+
}
|
|
13
|
+
}
|
|
8
14
|
if (cacheData.latestVersionCheck?.lastChecked && new Date(cacheData.latestVersionCheck.lastChecked).getTime() > new Date(Date.now() - 60 * 60 * 1e3).getTime()) {
|
|
9
15
|
return cacheData.latestVersionCheck.version;
|
|
10
16
|
}
|
|
11
17
|
}
|
|
12
18
|
try {
|
|
13
|
-
const
|
|
19
|
+
const ac = new AbortController();
|
|
20
|
+
const t = setTimeout(() => ac.abort(), 5e3);
|
|
21
|
+
const response = await fetch(`https://registry.npmjs.org/${packageName}/latest`, {
|
|
22
|
+
signal: ac.signal
|
|
23
|
+
});
|
|
24
|
+
clearTimeout(t);
|
|
14
25
|
if (!response.ok) {
|
|
15
|
-
|
|
26
|
+
logger.warn(`Failed to fetch package info from registry.npmjs.org: ${response.statusText}`);
|
|
27
|
+
return null;
|
|
16
28
|
}
|
|
17
29
|
const data = await response.json();
|
|
18
30
|
if (isDevMode && cacheJsonFile) {
|
|
19
31
|
const updatedCacheData = {
|
|
20
32
|
...cacheData,
|
|
21
33
|
latestVersionCheck: {
|
|
22
|
-
lastChecked: /* @__PURE__ */ new Date(),
|
|
34
|
+
lastChecked: (/* @__PURE__ */ new Date()).toISOString(),
|
|
23
35
|
version: data.version
|
|
24
36
|
}
|
|
25
37
|
};
|
|
26
|
-
|
|
38
|
+
io.writeFileSync(cacheJsonFile, JSON.stringify(updatedCacheData, null, 2), "utf-8");
|
|
27
39
|
}
|
|
28
40
|
return data.version;
|
|
29
41
|
} catch (error) {
|
|
@@ -10,4 +10,4 @@ export declare function jsonParse<T extends object>(text: string): T;
|
|
|
10
10
|
/**
|
|
11
11
|
* Reads a JSON file and parses it into an object of type T.
|
|
12
12
|
*/
|
|
13
|
-
export declare function readJson<T extends object>(path: string | URL): T;
|
|
13
|
+
export declare function readJson<T extends object>(path: string | URL, readFileSync?: (path: string | URL, encoding: BufferEncoding) => string): T;
|
package/dist/utils/jsonUtils.js
CHANGED
|
@@ -2,8 +2,9 @@ import fs from "node:fs";
|
|
|
2
2
|
function jsonParse(text) {
|
|
3
3
|
return JSON.parse(text);
|
|
4
4
|
}
|
|
5
|
-
function readJson(path) {
|
|
6
|
-
|
|
5
|
+
function readJson(path, readFileSync = fs.readFileSync) {
|
|
6
|
+
const content = readFileSync(path, "utf-8");
|
|
7
|
+
return jsonParse(content);
|
|
7
8
|
}
|
|
8
9
|
export {
|
|
9
10
|
jsonParse,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@withstudiocms/internal_helpers",
|
|
3
|
-
"version": "0.1.0-beta.
|
|
3
|
+
"version": "0.1.0-beta.2",
|
|
4
4
|
"description": "Internal helper utilities for StudioCMS",
|
|
5
5
|
"author": {
|
|
6
6
|
"name": "withstudiocms",
|
|
@@ -75,7 +75,7 @@
|
|
|
75
75
|
"scripts": {
|
|
76
76
|
"build": "buildkit build 'src/**/*.{ts,astro,css,json,png}'",
|
|
77
77
|
"dev": "buildkit dev 'src/**/*.{ts,astro,css,json,png}'",
|
|
78
|
-
"test": "
|
|
78
|
+
"test": "vitest",
|
|
79
79
|
"typecheck": "tspc -p tsconfig.tspc.json"
|
|
80
80
|
}
|
|
81
81
|
}
|