@vuepress/utils 2.0.0-beta.50-pre → 2.0.0-beta.51
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.d.ts +62 -0
- package/dist/index.js +129 -0
- package/package.json +3 -3
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
export { default as chalk } from 'chalk';
|
|
2
|
+
export { default as debug } from 'debug';
|
|
3
|
+
export { default as fs } from 'fs-extra';
|
|
4
|
+
export { globby } from 'globby';
|
|
5
|
+
export { default as hash } from 'hash-sum';
|
|
6
|
+
import { Ora } from 'ora';
|
|
7
|
+
export { default as ora } from 'ora';
|
|
8
|
+
export { default as path } from 'upath';
|
|
9
|
+
import { HeadConfig, HeadAttrsConfig } from '@vuepress/shared';
|
|
10
|
+
|
|
11
|
+
declare const getDirname: (importMetaUrl: string) => string;
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* A helper for dynamically importing a file path
|
|
15
|
+
*
|
|
16
|
+
* We need to use `pathToFileURL` to transform file path wo compat with windows
|
|
17
|
+
*/
|
|
18
|
+
declare const importFile: <T>(filePath: string) => Promise<T>;
|
|
19
|
+
/**
|
|
20
|
+
* A wrapper of `importFile` and returns the default export
|
|
21
|
+
*/
|
|
22
|
+
declare const importFileDefault: <T>(filePath: string) => Promise<T>;
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Format millisecond
|
|
26
|
+
*/
|
|
27
|
+
declare const formatMs: (ms: number) => string;
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Check if `child` is a sub path of `parent` or not. Return `true` if
|
|
31
|
+
* they are the same path
|
|
32
|
+
*/
|
|
33
|
+
declare const isChildPath: (child: string, parent: string) => boolean;
|
|
34
|
+
|
|
35
|
+
declare const info: (...args: any[]) => void;
|
|
36
|
+
declare const tip: (...args: any[]) => void;
|
|
37
|
+
declare const success: (...args: any[]) => void;
|
|
38
|
+
declare const warn: (...args: any[]) => void;
|
|
39
|
+
declare const error: (...args: any[]) => void;
|
|
40
|
+
declare const createError: (message?: string | undefined) => Error;
|
|
41
|
+
declare const logger: {
|
|
42
|
+
info: (...args: any[]) => void;
|
|
43
|
+
tip: (...args: any[]) => void;
|
|
44
|
+
success: (...args: any[]) => void;
|
|
45
|
+
warn: (...args: any[]) => void;
|
|
46
|
+
error: (...args: any[]) => void;
|
|
47
|
+
createError: (message?: string | undefined) => Error;
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Render head config to string
|
|
52
|
+
*/
|
|
53
|
+
declare const renderHead: ([tag, attrs, innerHTML,]: HeadConfig) => string;
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Render head attrs config to string
|
|
57
|
+
*/
|
|
58
|
+
declare const renderHeadAttrs: (attrs: HeadAttrsConfig) => string;
|
|
59
|
+
|
|
60
|
+
declare const withSpinner: (msg: string) => <T>(target: (spinner?: Ora) => Promise<T>) => Promise<T>;
|
|
61
|
+
|
|
62
|
+
export { createError, error, formatMs, getDirname, importFile, importFileDefault, info, isChildPath, logger, renderHead, renderHeadAttrs, success, tip, warn, withSpinner };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
// src/index.ts
|
|
2
|
+
import chalk2 from "chalk";
|
|
3
|
+
import debug from "debug";
|
|
4
|
+
import fs from "fs-extra";
|
|
5
|
+
import { globby } from "globby";
|
|
6
|
+
import hash from "hash-sum";
|
|
7
|
+
import ora2 from "ora";
|
|
8
|
+
import path3 from "upath";
|
|
9
|
+
|
|
10
|
+
// src/getDirname.ts
|
|
11
|
+
import { fileURLToPath } from "url";
|
|
12
|
+
import path from "upath";
|
|
13
|
+
var getDirname = (importMetaUrl) => path.dirname(fileURLToPath(importMetaUrl));
|
|
14
|
+
|
|
15
|
+
// src/importFile.ts
|
|
16
|
+
import { pathToFileURL } from "url";
|
|
17
|
+
var importFile = (filePath) => import(pathToFileURL(filePath).toString());
|
|
18
|
+
var importFileDefault = (filePath) => importFile(filePath).then((m) => m.default);
|
|
19
|
+
|
|
20
|
+
// src/formatMs.ts
|
|
21
|
+
var formatMs = (ms) => {
|
|
22
|
+
if (ms < 1e3)
|
|
23
|
+
return `${ms}ms`;
|
|
24
|
+
return `${(ms / 1e3).toFixed(2)}s`;
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
// src/isChildPath.ts
|
|
28
|
+
import path2 from "upath";
|
|
29
|
+
var isChildPath = (child, parent) => {
|
|
30
|
+
const childPath = path2.normalize(child);
|
|
31
|
+
const parentPath = path2.normalize(parent);
|
|
32
|
+
if (!path2.win32.isAbsolute(childPath) || !path2.win32.isAbsolute(parentPath)) {
|
|
33
|
+
return false;
|
|
34
|
+
}
|
|
35
|
+
const relativePath = path2.relative(parentPath, childPath);
|
|
36
|
+
return relativePath === "" || !relativePath.startsWith("..");
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
// src/logger.ts
|
|
40
|
+
import chalk from "chalk";
|
|
41
|
+
var info = (...args) => {
|
|
42
|
+
console.log(chalk.cyan("info"), ...args);
|
|
43
|
+
};
|
|
44
|
+
var tip = (...args) => {
|
|
45
|
+
console.log(chalk.blue("tip"), ...args);
|
|
46
|
+
};
|
|
47
|
+
var success = (...args) => {
|
|
48
|
+
console.log(chalk.green("success"), ...args);
|
|
49
|
+
};
|
|
50
|
+
var warn = (...args) => {
|
|
51
|
+
console.warn(chalk.yellow("warning"), ...args);
|
|
52
|
+
};
|
|
53
|
+
var error = (...args) => {
|
|
54
|
+
console.error(chalk.red("error"), ...args);
|
|
55
|
+
};
|
|
56
|
+
var createError = (message) => {
|
|
57
|
+
error(message);
|
|
58
|
+
return new Error(message);
|
|
59
|
+
};
|
|
60
|
+
var logger = {
|
|
61
|
+
info,
|
|
62
|
+
tip,
|
|
63
|
+
success,
|
|
64
|
+
warn,
|
|
65
|
+
error,
|
|
66
|
+
createError
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
// src/renderHeadAttrs.ts
|
|
70
|
+
var renderHeadAttrs = (attrs) => Object.entries(attrs).filter((item) => item[1] !== false).map(
|
|
71
|
+
([key, value]) => value === true ? ` ${key}` : ` ${key}="${attrs[key]}"`
|
|
72
|
+
).join("");
|
|
73
|
+
|
|
74
|
+
// src/renderHead.ts
|
|
75
|
+
var renderHead = ([
|
|
76
|
+
tag,
|
|
77
|
+
attrs,
|
|
78
|
+
innerHTML = ""
|
|
79
|
+
]) => {
|
|
80
|
+
const openTag = `<${tag}${renderHeadAttrs(attrs)}>`;
|
|
81
|
+
if (tag === "link" || tag === "meta" || tag === "base") {
|
|
82
|
+
return openTag;
|
|
83
|
+
}
|
|
84
|
+
return `${openTag}${innerHTML}</${tag}>`;
|
|
85
|
+
};
|
|
86
|
+
|
|
87
|
+
// src/withSpinner.ts
|
|
88
|
+
import process from "process";
|
|
89
|
+
import ora from "ora";
|
|
90
|
+
var withSpinner = (msg) => async (target) => {
|
|
91
|
+
if (process.env.DEBUG) {
|
|
92
|
+
return target();
|
|
93
|
+
}
|
|
94
|
+
const start = Date.now();
|
|
95
|
+
const spinner = ora();
|
|
96
|
+
try {
|
|
97
|
+
spinner.start(msg);
|
|
98
|
+
const result = await target(spinner);
|
|
99
|
+
spinner.succeed(`${msg} - done in ${formatMs(Date.now() - start)}`);
|
|
100
|
+
return result;
|
|
101
|
+
} catch (e) {
|
|
102
|
+
spinner.fail(`${msg} - failed in ${formatMs(Date.now() - start)}`);
|
|
103
|
+
throw e;
|
|
104
|
+
}
|
|
105
|
+
};
|
|
106
|
+
export {
|
|
107
|
+
chalk2 as chalk,
|
|
108
|
+
createError,
|
|
109
|
+
debug,
|
|
110
|
+
error,
|
|
111
|
+
formatMs,
|
|
112
|
+
fs,
|
|
113
|
+
getDirname,
|
|
114
|
+
globby,
|
|
115
|
+
hash,
|
|
116
|
+
importFile,
|
|
117
|
+
importFileDefault,
|
|
118
|
+
info,
|
|
119
|
+
isChildPath,
|
|
120
|
+
logger,
|
|
121
|
+
ora2 as ora,
|
|
122
|
+
path3 as path,
|
|
123
|
+
renderHead,
|
|
124
|
+
renderHeadAttrs,
|
|
125
|
+
success,
|
|
126
|
+
tip,
|
|
127
|
+
warn,
|
|
128
|
+
withSpinner
|
|
129
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@vuepress/utils",
|
|
3
|
-
"version": "2.0.0-beta.
|
|
3
|
+
"version": "2.0.0-beta.51",
|
|
4
4
|
"description": "Utils package of VuePress",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"vuepress",
|
|
@@ -24,13 +24,13 @@
|
|
|
24
24
|
"main": "./dist/index.js",
|
|
25
25
|
"types": "./dist/index.d.ts",
|
|
26
26
|
"files": [
|
|
27
|
-
"
|
|
27
|
+
"dist"
|
|
28
28
|
],
|
|
29
29
|
"dependencies": {
|
|
30
30
|
"@types/debug": "^4.1.7",
|
|
31
31
|
"@types/fs-extra": "^9.0.13",
|
|
32
32
|
"@types/hash-sum": "^1.0.0",
|
|
33
|
-
"@vuepress/shared": "2.0.0-beta.
|
|
33
|
+
"@vuepress/shared": "2.0.0-beta.51",
|
|
34
34
|
"chalk": "^5.0.1",
|
|
35
35
|
"debug": "^4.3.4",
|
|
36
36
|
"fs-extra": "^10.1.0",
|