generated from dan3988/node-ts-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrollup-plugin-copy-libs.js
181 lines (156 loc) · 4.6 KB
/
rollup-plugin-copy-libs.js
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/// <reference path="rollup-plugin-copy-libs.d.ts" />
import * as rl from "rollup";
import * as fs from "node:fs";
import * as path from "node:path";
import { createFilter } from "@rollup/pluginutils";
import chalk from "chalk-template";
import archiver from "archiver";
const r = { recursive: true };
/** @type {{ bigint: true }} */
const bi = { bigint: true };
/** @typedef {(text: TemplateStringsArray, ...placeholders: unknown[]) => void} LogImpl */
/** @typedef {[path: string, mtimeMs: bigint, watch: fs.StatWatcher]} FileInfo */
/**
* @param {LogImpl} log
* @param {keyof import("fs")} key
* @param {number} ix
*/
function safeOp(log, key, ix, ...args) {
function callback(err) {
err && log && log`{red ${key} call failed for \"${args[ix]}\" - ${err}}`;
}
args.push(callback);
fs[key].apply(fs, args);
}
/**
* @param {import("./rollup-plugin-copy-libs").WatchInit[]} inputs
* @param {string} outDir
* @param {boolean} watch
* @param {LogImpl} log
* @returns {import("./rollup-plugin-copy-libs").Copier | undefined}
*/
function copyFiles(inputs, outDir, watch, log) {
/** @type {(() => void)[]} */
const dispose = [];
for (const input of inputs) {
if (input.mode === "dir") {
const { include, exclude, output = path.relative(".", input.path) } = input;
const filter = createFilter(include, exclude, {
resolve: false
});
for (const file of fs.readdirSync(input.path)) {
if (filter(file)) {
const src = path.join(input.path, file);
const dst = path.join(outDir, output, file);
fs.cpSync(src, dst, r);
}
}
if (watch) {
const { recursive } = input;
const watcher = fs.watch(input.path, { recursive }, (event, file) => {
if (filter(file)) {
log && log`{greenBright File ${event}} {green "${input.path}"} {yellow "${file}"}`;
const src = path.join(input.path, file);
const dst = path.join(outDir, output, file);
if (fs.existsSync(src)) {
safeOp(log, "cp", 0, src, dst, r);
} else if (fs.existsSync(dst)) {
safeOp(log, "rm", 0, dst, r);
}
}
});
dispose.push(() => watcher.close());
}
} else {
const isJson = input.mode === "json";
/** @type {Map<string, FileInfo>} */
const copied = new Map();
const update = () => {
let data = fs.readFileSync(input.path, { encoding: "utf8" });
if (isJson)
data = JSON.parse(data);
const parsed = input.parse(data);
const uncopied = new Map(copied);
for (const file of parsed) {
const modif = fs.lstatSync(file, bi);
let fileInfo = copied.get(file);
if (fileInfo == undefined) {
const resolved = path.join(outDir, file);
const watcher = watch && fs.watchFile(file, bi, (stat) => {
log && log`{greenBright File change} {green "${input.path}"} {yellow "${file}"}`;
safeOp(log, "cp", 0, file, fileInfo[0]);
fileInfo[1] = stat.mtimeMs;
});
fileInfo = [resolved, modif.mtimeMs, watcher];
} else if (fileInfo[1] !== modif.mtimeMs) {
fileInfo[1] = modif.mtimeMs;
} else {
uncopied.delete(file);
continue;
}
fs.cpSync(file, fileInfo[0], r);
copied.set(file, fileInfo);
uncopied.delete(file);
}
for (const [key, [file, _modif, watch]] of uncopied) {
copied.delete(key);
watch.unref();
safeOp(log, "rm", 0, file);
}
}
if (watch) {
const w = fs.watchFile(input.path, () => {
log && log`{greenBright Input file change detected} {green "${input.path}"}`;
update();
});
dispose.push(() => {
w.unref();
copied.forEach(v => v[2].unref());
});
}
update();
}
}
if (!watch)
return;
function close() {
dispose.forEach(v => v());
}
return { close };
}
/**
* @type {import("./rollup-plugin-copy-libs")["copyLibs"]}
*/
export function copyLibs(options) {
const { outDir, inputs, log, archive } = options;
const watch = !!process.env.ROLLUP_WATCH;
/** @type {Copier} */
let watcher;
/** @type {LogImpl} */
let logImpl;
if (log)
logImpl = LogImpl.bind(typeof log === "function" ? log : console.log);
return {
name: "rollup-plugin-copy-libs",
closeBundle() {
if (archive) {
let { fileName, format = "zip" } = archive;
if (!fileName.includes("."))
fileName += "." + format;
const output = fs.createWriteStream(fileName);
const zip = archiver(format);
zip.pipe(output);
zip.directory(outDir, false);
return zip.finalize();
}
},
buildEnd() {
watcher ??= copyFiles(inputs, outDir, watch, logImpl);
}
}
}
function LogImpl(text, ...placeholders) {
const msg = chalk(text, ...placeholders);
this(msg);
}
export default copyLibs;