@serwist/utils 10.0.0-preview.1 → 10.0.0-preview.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/dist/index.js +12 -2
- package/package.json +19 -4
- package/src/index.node.ts +3 -0
- package/src/index.ts +2 -3
- package/src/node/log.ts +71 -0
- package/src/node/paths.ts +34 -0
- package/src/paths.ts +16 -0
- package/src/toUnix.ts +0 -3
package/dist/index.js
CHANGED
|
@@ -26,8 +26,18 @@ const parallel = async (limit, array, func)=>{
|
|
|
26
26
|
return results;
|
|
27
27
|
};
|
|
28
28
|
|
|
29
|
+
const slash = (str)=>{
|
|
30
|
+
return str.replace(/\\/g, "/");
|
|
31
|
+
};
|
|
29
32
|
const toUnix = (p)=>{
|
|
30
|
-
return p
|
|
33
|
+
return slash(p).replace(/(?<!^)\/+/g, "/");
|
|
34
|
+
};
|
|
35
|
+
const resolveBasePath = (base)=>{
|
|
36
|
+
if (isAbsolute(base)) return base;
|
|
37
|
+
return !base.startsWith("/") && !base.startsWith("./") ? `/${base}` : base;
|
|
38
|
+
};
|
|
39
|
+
const isAbsolute = (url)=>{
|
|
40
|
+
return url.match(/^(?:[a-z]+:)?\/\//i);
|
|
31
41
|
};
|
|
32
42
|
|
|
33
|
-
export { nonNullable, parallel, toUnix };
|
|
43
|
+
export { isAbsolute, nonNullable, parallel, resolveBasePath, slash, toUnix };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@serwist/utils",
|
|
3
|
-
"version": "10.0.0-preview.
|
|
3
|
+
"version": "10.0.0-preview.2",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "This module contains internal utilities used by Serwist packages.",
|
|
6
6
|
"files": [
|
|
@@ -19,22 +19,37 @@
|
|
|
19
19
|
"homepage": "https://serwist.pages.dev",
|
|
20
20
|
"main": "./dist/index.js",
|
|
21
21
|
"types": "./dist/index.d.ts",
|
|
22
|
+
"typesVersions": {
|
|
23
|
+
"node": {
|
|
24
|
+
"*": [
|
|
25
|
+
"./dist/index.node.d.ts"
|
|
26
|
+
]
|
|
27
|
+
}
|
|
28
|
+
},
|
|
22
29
|
"exports": {
|
|
23
30
|
".": {
|
|
24
31
|
"types": "./dist/index.d.ts",
|
|
25
32
|
"default": "./dist/index.js"
|
|
26
33
|
},
|
|
34
|
+
"./node": {
|
|
35
|
+
"types": "./dist/index.node.d.ts",
|
|
36
|
+
"default": "./dist/index.node.js"
|
|
37
|
+
},
|
|
27
38
|
"./package.json": "./package.json"
|
|
28
39
|
},
|
|
40
|
+
"dependencies": {
|
|
41
|
+
"kolorist": "1.8.0"
|
|
42
|
+
},
|
|
29
43
|
"devDependencies": {
|
|
30
|
-
"rollup": "4.
|
|
31
|
-
"typescript": "5.
|
|
32
|
-
"@serwist/configs": "10.0.0-preview.
|
|
44
|
+
"rollup": "4.40.0",
|
|
45
|
+
"typescript": "5.8.3",
|
|
46
|
+
"@serwist/configs": "10.0.0-preview.2"
|
|
33
47
|
},
|
|
34
48
|
"scripts": {
|
|
35
49
|
"build": "rimraf dist && NODE_ENV=production rollup --config rollup.config.js",
|
|
36
50
|
"dev": "rollup --config rollup.config.js --watch",
|
|
37
51
|
"lint": "biome lint ./src",
|
|
52
|
+
"qcheck": "biome check ./src",
|
|
38
53
|
"typecheck": "tsc"
|
|
39
54
|
}
|
|
40
55
|
}
|
package/src/index.ts
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import { nonNullable } from "./nonNullable.js";
|
|
2
2
|
import { parallel } from "./parallel.js";
|
|
3
|
-
import { toUnix } from "./toUnix.js";
|
|
4
|
-
|
|
5
|
-
export { nonNullable, parallel, toUnix };
|
|
6
3
|
|
|
4
|
+
export { nonNullable, parallel };
|
|
5
|
+
export { slash, toUnix, resolveBasePath, isAbsolute } from "./paths.js";
|
|
7
6
|
export type * from "./types.js";
|
package/src/node/log.ts
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import { readFileSync } from "node:fs";
|
|
2
|
+
import { createRequire } from "node:module";
|
|
3
|
+
import { cyan, yellow, red } from "kolorist";
|
|
4
|
+
|
|
5
|
+
export type LogType = "error" | "warn" | "info";
|
|
6
|
+
export type LogLevel = LogType | "silent";
|
|
7
|
+
export interface LogOptions {
|
|
8
|
+
clear?: boolean;
|
|
9
|
+
skipLine?: boolean;
|
|
10
|
+
}
|
|
11
|
+
export const LogLevels: Record<LogLevel, number> = {
|
|
12
|
+
silent: 0,
|
|
13
|
+
error: 1,
|
|
14
|
+
warn: 2,
|
|
15
|
+
info: 3,
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
export interface LoggerOptions {
|
|
19
|
+
prefix?: string;
|
|
20
|
+
showVersion?: boolean;
|
|
21
|
+
allowClearScreen?: boolean;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export interface Logger {
|
|
25
|
+
info(msg: string, options?: LogOptions): void;
|
|
26
|
+
warn(msg: string, options?: LogOptions): void;
|
|
27
|
+
warnOnce(msg: string, options?: LogOptions): void;
|
|
28
|
+
error(msg: string, options?: LogOptions): void;
|
|
29
|
+
clearScreen(type: LogType): void;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
const require = createRequire(import.meta.url);
|
|
33
|
+
|
|
34
|
+
export const createLogger = (level: LogLevel = "info", options: LoggerOptions = {}): Logger => {
|
|
35
|
+
const { prefix: _prefix = "serwist", showVersion = true, allowClearScreen = true } = options;
|
|
36
|
+
const packageJson = showVersion ? JSON.parse(readFileSync(require.resolve("@serwist/utils/package.json"), "utf-8")) : null;
|
|
37
|
+
const prefix = `${_prefix}${showVersion ? ` v${packageJson.version}` : ""}`;
|
|
38
|
+
const thresh = LogLevels[level];
|
|
39
|
+
const canClearScreen = allowClearScreen && process.stdout.isTTY && !process.env.CI;
|
|
40
|
+
const clear = canClearScreen ? console.clear : () => {};
|
|
41
|
+
const output = (type: LogType, msg: string, options: LogOptions = {}) => {
|
|
42
|
+
if (thresh >= LogLevels[type]) {
|
|
43
|
+
const method = type === "info" ? "log" : type;
|
|
44
|
+
if (options.clear) clear();
|
|
45
|
+
let tag = type === "error" ? red(prefix) : type === "warn" ? yellow(prefix) : cyan(prefix);
|
|
46
|
+
console[method](`${options.skipLine ? "\n" : ""}${tag} ${msg}`);
|
|
47
|
+
}
|
|
48
|
+
};
|
|
49
|
+
const warnedMessages = new Set<string>();
|
|
50
|
+
return {
|
|
51
|
+
info(msg, opts) {
|
|
52
|
+
output("info", msg, opts);
|
|
53
|
+
},
|
|
54
|
+
warn(msg, opts) {
|
|
55
|
+
output("warn", msg, opts);
|
|
56
|
+
},
|
|
57
|
+
warnOnce(msg, opts) {
|
|
58
|
+
if (warnedMessages.has(msg)) return;
|
|
59
|
+
output("warn", msg, opts);
|
|
60
|
+
warnedMessages.add(msg);
|
|
61
|
+
},
|
|
62
|
+
error(msg, opts) {
|
|
63
|
+
output("error", msg, opts);
|
|
64
|
+
},
|
|
65
|
+
clearScreen(type) {
|
|
66
|
+
if (thresh >= LogLevels[type]) {
|
|
67
|
+
clear();
|
|
68
|
+
}
|
|
69
|
+
},
|
|
70
|
+
} satisfies Logger;
|
|
71
|
+
};
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import fs from "node:fs";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
|
|
4
|
+
// Source: https://github.com/sveltejs/kit/blob/6419d3eaa7bf1b0a756b28f06a73f71fe042de0a/packages/kit/src/utils/filesystem.js
|
|
5
|
+
// License: MIT
|
|
6
|
+
/**
|
|
7
|
+
* Resolves a file path without extension. Also handles `/index` if the path
|
|
8
|
+
* actually points to a directory.
|
|
9
|
+
* @param ctx
|
|
10
|
+
* @param api
|
|
11
|
+
* @returns
|
|
12
|
+
*/
|
|
13
|
+
export const resolveEntry = (entry: string): string | null => {
|
|
14
|
+
if (fs.existsSync(entry)) {
|
|
15
|
+
const stats = fs.statSync(entry);
|
|
16
|
+
if (stats.isDirectory()) {
|
|
17
|
+
return resolveEntry(path.join(entry, "index"));
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
return entry;
|
|
21
|
+
}
|
|
22
|
+
const dir = path.dirname(entry);
|
|
23
|
+
|
|
24
|
+
if (fs.existsSync(dir)) {
|
|
25
|
+
const base = path.basename(entry);
|
|
26
|
+
const files = fs.readdirSync(dir);
|
|
27
|
+
|
|
28
|
+
const found = files.find((file) => file.replace(/\.[^.]+$/, "") === base);
|
|
29
|
+
|
|
30
|
+
if (found) return path.join(dir, found);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
return null;
|
|
34
|
+
};
|
package/src/paths.ts
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
export const slash = (str: string) => {
|
|
2
|
+
return str.replace(/\\/g, "/");
|
|
3
|
+
};
|
|
4
|
+
|
|
5
|
+
export const toUnix = (p: string) => {
|
|
6
|
+
return slash(p).replace(/(?<!^)\/+/g, "/");
|
|
7
|
+
};
|
|
8
|
+
|
|
9
|
+
export const resolveBasePath = (base: string) => {
|
|
10
|
+
if (isAbsolute(base)) return base;
|
|
11
|
+
return !base.startsWith("/") && !base.startsWith("./") ? `/${base}` : base;
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
export const isAbsolute = (url: string) => {
|
|
15
|
+
return url.match(/^(?:[a-z]+:)?\/\//i);
|
|
16
|
+
};
|
package/src/toUnix.ts
DELETED