jialh
2025-01-07 bb59b053247ef82969b64979260e2478bd732e1f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.FileWatcher = void 0;
const fs_extra_1 = __importDefault(require("fs-extra"));
const path_1 = __importDefault(require("path"));
const chokidar_1 = require("chokidar");
const shared_1 = require("@vue/shared");
const utils_1 = require("./utils");
class FileWatcher {
    constructor({ src, dest, transform, verbose }) {
        this.src = !(0, shared_1.isArray)(src) ? [src] : src;
        this.dest = dest;
        this.transform = transform;
        this.verbose = verbose;
    }
    watch(watchOptions, onReady, onChange) {
        if (!this.watcher) {
            const copy = this.copy.bind(this);
            const remove = this.remove.bind(this);
            // escape chokidar cwd
            const src = this.src.map((src) => (0, utils_1.pathToGlob)(path_1.default.resolve(watchOptions.cwd), src));
            this.watcher = (0, chokidar_1.watch)(src, watchOptions)
                .on('add', copy)
                .on('addDir', copy)
                .on('change', copy)
                .on('unlink', remove)
                .on('unlinkDir', remove)
                .on('ready', () => {
                onReady && onReady(this.watcher);
                setTimeout(() => {
                    this.onChange = onChange;
                }, 1000);
            })
                .on('error', (e) => console.error('watch', e));
        }
        return this.watcher;
    }
    add(paths) {
        this.info('add', paths);
        return this.watcher.add(paths);
    }
    unwatch(paths) {
        this.info('unwatch', paths);
        return this.watcher.unwatch(paths);
    }
    close() {
        this.info('close');
        return this.watcher.close();
    }
    copy(from) {
        const to = this.to(from);
        this.info('copy', from + '=>' + to);
        let content;
        if (this.transform) {
            const filename = this.from(from);
            content = this.transform(fs_extra_1.default.readFileSync(filename), filename);
        }
        if (content) {
            return fs_extra_1.default
                .outputFile(to, content)
                .catch(() => {
                // this.info('copy', e)
            })
                .then(() => this.onChange && this.onChange());
        }
        return fs_extra_1.default
            .copy(this.from(from), to, { overwrite: true })
            .catch(() => {
            // this.info('copy', e)
        })
            .then(() => this.onChange && this.onChange());
    }
    remove(from) {
        const to = this.to(from);
        this.info('remove', from + '=>' + to);
        return fs_extra_1.default
            .remove(to)
            .catch(() => {
            // this.info('remove', e)
        })
            .then(() => this.onChange && this.onChange());
    }
    info(type, msg) {
        this.verbose && console.log(type, msg);
    }
    from(from) {
        return path_1.default.join(this.watcher.options.cwd, from);
    }
    to(from) {
        return path_1.default.join(this.dest, from);
    }
}
exports.FileWatcher = FileWatcher;