@shuvi/utils 1.0.0-rc.9 → 1.0.0
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/lib/file.d.ts +1 -0
- package/lib/file.js +5 -1
- package/lib/fileWatcher.d.ts +5 -3
- package/lib/fileWatcher.js +43 -12
- package/lib/logger.d.ts +14 -1
- package/lib/logger.js +10 -7
- package/package.json +1 -1
package/lib/file.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
export declare function withExts(file: string, extensions: string[]): string[];
|
|
2
|
+
export declare function removeExt(file: string): string;
|
|
2
3
|
export declare const findFirstExistedFile: (files: string[]) => string | null;
|
|
3
4
|
export declare const resolveFile: (fileName: string) => string;
|
|
4
5
|
export declare const isDirectory: (dirname: string) => Promise<boolean>;
|
package/lib/file.js
CHANGED
|
@@ -32,13 +32,17 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
32
32
|
});
|
|
33
33
|
};
|
|
34
34
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
35
|
-
exports.normalizePath = exports.isDirectory = exports.resolveFile = exports.findFirstExistedFile = exports.withExts = void 0;
|
|
35
|
+
exports.normalizePath = exports.isDirectory = exports.resolveFile = exports.findFirstExistedFile = exports.removeExt = exports.withExts = void 0;
|
|
36
36
|
const fs = __importStar(require("fs"));
|
|
37
37
|
const path = __importStar(require("path"));
|
|
38
38
|
function withExts(file, extensions) {
|
|
39
39
|
return extensions.map(ext => `${file}${ext}`);
|
|
40
40
|
}
|
|
41
41
|
exports.withExts = withExts;
|
|
42
|
+
function removeExt(file) {
|
|
43
|
+
return file.replace(/\.\w+$/, '');
|
|
44
|
+
}
|
|
45
|
+
exports.removeExt = removeExt;
|
|
42
46
|
const findFirstExistedFile = (files) => {
|
|
43
47
|
for (let index = 0; index < files.length; index++) {
|
|
44
48
|
const file = files[index];
|
package/lib/fileWatcher.d.ts
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
|
-
import { TimeInfo } from 'watchpack';
|
|
2
|
-
export { TimeInfo };
|
|
1
|
+
import Watchpack, { TimeInfo } from 'watchpack';
|
|
2
|
+
export { Watchpack, TimeInfo };
|
|
3
3
|
export interface WatchEvent {
|
|
4
4
|
changes: string[];
|
|
5
5
|
removals: string[];
|
|
6
6
|
getAllFiles: () => string[];
|
|
7
|
+
knownFiles: Map<string, TimeInfo>;
|
|
7
8
|
}
|
|
8
9
|
export interface WatchOptions {
|
|
9
10
|
files?: string[];
|
|
@@ -11,7 +12,8 @@ export interface WatchOptions {
|
|
|
11
12
|
missing?: string[];
|
|
12
13
|
aggregateTimeout?: number;
|
|
13
14
|
startTime?: number;
|
|
15
|
+
ignoreFileContentUpdate?: boolean;
|
|
14
16
|
}
|
|
15
17
|
export declare type WatchCallback = (event: WatchEvent) => void;
|
|
16
18
|
export declare type ChangeCallback = (file: string, time: number) => void;
|
|
17
|
-
export declare function watch({ files, directories, missing, aggregateTimeout, startTime }: WatchOptions, callback: WatchCallback, callbackUndelayed?: ChangeCallback): () => void;
|
|
19
|
+
export declare function watch({ files, directories, missing, aggregateTimeout, startTime, ignoreFileContentUpdate }: WatchOptions, callback: WatchCallback, callbackUndelayed?: ChangeCallback): () => void;
|
package/lib/fileWatcher.js
CHANGED
|
@@ -3,8 +3,13 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
3
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.watch = void 0;
|
|
6
|
+
exports.watch = exports.Watchpack = void 0;
|
|
7
7
|
const watchpack_1 = __importDefault(require("watchpack"));
|
|
8
|
+
exports.Watchpack = watchpack_1.default;
|
|
9
|
+
const watchpackExplanationType = {
|
|
10
|
+
change: 'change',
|
|
11
|
+
rename: 'rename'
|
|
12
|
+
};
|
|
8
13
|
const options = {
|
|
9
14
|
// options:
|
|
10
15
|
aggregateTimeout: 300,
|
|
@@ -16,34 +21,60 @@ const options = {
|
|
|
16
21
|
// ignored: /regexp/ - a regular expression for files or folders that should not be watched
|
|
17
22
|
// All subdirectories are ignored too
|
|
18
23
|
};
|
|
19
|
-
function watch({ files, directories, missing, aggregateTimeout, startTime = Date.now() }, callback, callbackUndelayed) {
|
|
24
|
+
function watch({ files, directories, missing, aggregateTimeout, startTime = Date.now(), ignoreFileContentUpdate }, callback, callbackUndelayed) {
|
|
20
25
|
const watchPackOptions = Object.assign({}, options);
|
|
21
26
|
if (aggregateTimeout !== undefined) {
|
|
22
27
|
watchPackOptions.aggregateTimeout = aggregateTimeout;
|
|
23
28
|
}
|
|
24
29
|
const wp = new watchpack_1.default(watchPackOptions);
|
|
25
|
-
|
|
30
|
+
let aggregatedChanges = new Set();
|
|
31
|
+
let aggregatedRemovals = new Set();
|
|
32
|
+
wp.on('aggregated', () => {
|
|
33
|
+
if (!aggregatedChanges.size && !aggregatedRemovals.size) {
|
|
34
|
+
return;
|
|
35
|
+
}
|
|
26
36
|
const knownFiles = wp.getTimeInfoEntries();
|
|
37
|
+
const changes = Array.from(aggregatedChanges);
|
|
38
|
+
const removals = Array.from(aggregatedRemovals);
|
|
39
|
+
aggregatedChanges = new Set();
|
|
40
|
+
aggregatedRemovals = new Set();
|
|
27
41
|
callback({
|
|
28
|
-
changes
|
|
29
|
-
removals
|
|
42
|
+
changes,
|
|
43
|
+
removals,
|
|
30
44
|
getAllFiles() {
|
|
31
45
|
const res = [];
|
|
32
|
-
for (const [file,
|
|
33
|
-
if (
|
|
46
|
+
for (const [file, timeInfo] of knownFiles.entries()) {
|
|
47
|
+
if (timeInfo && timeInfo.accuracy !== undefined) {
|
|
34
48
|
res.push(file);
|
|
35
49
|
}
|
|
36
50
|
}
|
|
37
51
|
return res;
|
|
38
|
-
}
|
|
52
|
+
},
|
|
53
|
+
knownFiles
|
|
39
54
|
});
|
|
40
55
|
});
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
56
|
+
wp.on('change', (file, time, explanation) => {
|
|
57
|
+
if (ignoreFileContentUpdate &&
|
|
58
|
+
explanation === watchpackExplanationType.change) {
|
|
59
|
+
return;
|
|
60
|
+
}
|
|
61
|
+
aggregatedRemovals.delete(file);
|
|
62
|
+
aggregatedChanges.add(file);
|
|
63
|
+
callbackUndelayed === null || callbackUndelayed === void 0 ? void 0 : callbackUndelayed(file, time);
|
|
64
|
+
});
|
|
65
|
+
wp.on('remove', (file, time, explanation) => {
|
|
66
|
+
if (ignoreFileContentUpdate &&
|
|
67
|
+
explanation === watchpackExplanationType.change) {
|
|
68
|
+
return;
|
|
69
|
+
}
|
|
70
|
+
aggregatedChanges.delete(file);
|
|
71
|
+
aggregatedRemovals.add(file);
|
|
72
|
+
callbackUndelayed === null || callbackUndelayed === void 0 ? void 0 : callbackUndelayed(file, time);
|
|
73
|
+
});
|
|
45
74
|
wp.watch({ files, directories, missing, startTime });
|
|
46
75
|
return () => {
|
|
76
|
+
aggregatedChanges.clear();
|
|
77
|
+
aggregatedRemovals.clear();
|
|
47
78
|
wp.close();
|
|
48
79
|
};
|
|
49
80
|
}
|
package/lib/logger.d.ts
CHANGED
|
@@ -4,4 +4,17 @@ export interface Logger {
|
|
|
4
4
|
warn(...args: any[]): void;
|
|
5
5
|
error(...args: any[]): void;
|
|
6
6
|
}
|
|
7
|
-
export
|
|
7
|
+
export declare const colorize: {
|
|
8
|
+
error: (...message: any[]) => string;
|
|
9
|
+
warn: (...message: any[]) => string;
|
|
10
|
+
};
|
|
11
|
+
declare class LoggerImpl implements Logger {
|
|
12
|
+
private _debug;
|
|
13
|
+
constructor(namespace: string);
|
|
14
|
+
debug(formatter: any, ...args: any[]): void;
|
|
15
|
+
info(...args: any[]): void;
|
|
16
|
+
warn(...args: any[]): void;
|
|
17
|
+
error(...args: any[]): void;
|
|
18
|
+
}
|
|
19
|
+
declare const _default: LoggerImpl;
|
|
20
|
+
export default _default;
|
package/lib/logger.js
CHANGED
|
@@ -3,7 +3,13 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
3
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.colorize = void 0;
|
|
6
7
|
const debug_1 = __importDefault(require("debug"));
|
|
8
|
+
const chalk_1 = __importDefault(require("chalk"));
|
|
9
|
+
exports.colorize = {
|
|
10
|
+
error: (...message) => chalk_1.default.red(...message),
|
|
11
|
+
warn: (...message) => chalk_1.default.yellow(...message)
|
|
12
|
+
};
|
|
7
13
|
class LoggerImpl {
|
|
8
14
|
constructor(namespace) {
|
|
9
15
|
this._debug = (0, debug_1.default)(namespace);
|
|
@@ -12,16 +18,13 @@ class LoggerImpl {
|
|
|
12
18
|
this._debug(formatter, ...args);
|
|
13
19
|
}
|
|
14
20
|
info(...args) {
|
|
15
|
-
console.log(
|
|
21
|
+
console.log(...args);
|
|
16
22
|
}
|
|
17
23
|
warn(...args) {
|
|
18
|
-
console.warn(
|
|
24
|
+
console.warn(exports.colorize.warn(...args));
|
|
19
25
|
}
|
|
20
26
|
error(...args) {
|
|
21
|
-
console.error(
|
|
27
|
+
console.error(exports.colorize.error(...args));
|
|
22
28
|
}
|
|
23
29
|
}
|
|
24
|
-
|
|
25
|
-
return new LoggerImpl(namespace);
|
|
26
|
-
}
|
|
27
|
-
exports.default = logger;
|
|
30
|
+
exports.default = new LoggerImpl('shuvi');
|