esm.dev 1.7.4 → 1.8.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/dist/lib/watch.js +11 -5
- package/dist/lib/watchIgnoreList.js +50 -0
- package/package.json +5 -2
package/dist/lib/watch.js
CHANGED
|
@@ -5,6 +5,7 @@ import { readFile, writeFile } from 'node:fs/promises';
|
|
|
5
5
|
import { createHash } from 'node:crypto';
|
|
6
6
|
import { glob } from 'glob';
|
|
7
7
|
import { queue, queuedDebounce } from './queue.js';
|
|
8
|
+
import { getWatchIgnorer } from './watchIgnoreList.js';
|
|
8
9
|
export async function watch(packagePath, opts) {
|
|
9
10
|
const { name, packageRoot, private: prvte, } = await getPackageMeta(packagePath);
|
|
10
11
|
if (prvte) {
|
|
@@ -29,7 +30,9 @@ export async function watch(packagePath, opts) {
|
|
|
29
30
|
};
|
|
30
31
|
}
|
|
31
32
|
else {
|
|
32
|
-
const
|
|
33
|
+
const ignorer = await getWatchIgnorer(packagePath);
|
|
34
|
+
const watcher = fsWatch(packageRoot, { recursive: true }, (_, filename) => (filename && ignorer.ignores(filename)) ||
|
|
35
|
+
debounceRepublish(filename ?? ''));
|
|
33
36
|
return () => {
|
|
34
37
|
console.info('stop watching');
|
|
35
38
|
watcher.close();
|
|
@@ -38,7 +41,8 @@ export async function watch(packagePath, opts) {
|
|
|
38
41
|
}
|
|
39
42
|
}
|
|
40
43
|
async function legacyWatch(dirname, cb) {
|
|
41
|
-
const
|
|
44
|
+
const ignorer = await getWatchIgnorer(dirname);
|
|
45
|
+
const filename = await hashDirectory(dirname, ignorer);
|
|
42
46
|
const watcher = watchFile(filename, cb);
|
|
43
47
|
let timer;
|
|
44
48
|
beginTimer();
|
|
@@ -47,12 +51,14 @@ async function legacyWatch(dirname, cb) {
|
|
|
47
51
|
clearTimeout(timer);
|
|
48
52
|
};
|
|
49
53
|
function beginTimer() {
|
|
50
|
-
timer = setTimeout(() => hashDirectory(dirname
|
|
54
|
+
timer = setTimeout(() => hashDirectory(dirname, ignorer)
|
|
55
|
+
.catch(console.error)
|
|
56
|
+
.finally(beginTimer), 1_000);
|
|
51
57
|
}
|
|
52
58
|
}
|
|
53
|
-
async function hashDirectory(dirname) {
|
|
59
|
+
async function hashDirectory(dirname, ignorer) {
|
|
54
60
|
const filename = `/tmp/${dirname.replace(/\//g, '-')}.hash.txt`;
|
|
55
|
-
const files = await glob(`${dirname}/**/*`, { nodir: true });
|
|
61
|
+
const files = ignorer.filter(await glob(`${dirname}/**/*`, { nodir: true }));
|
|
56
62
|
const hashes = await Promise.all(files.map(async (file) => {
|
|
57
63
|
const contents = await readFile(file, 'utf-8');
|
|
58
64
|
return createHash('sha256').update(contents).digest('hex');
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import gitignore from 'ignore';
|
|
2
|
+
import { Minimatch } from 'minimatch';
|
|
3
|
+
import { createReadStream } from 'node:fs';
|
|
4
|
+
import { access, constants } from 'node:fs/promises';
|
|
5
|
+
import * as path from 'node:path';
|
|
6
|
+
import { createInterface as createReadlineInterface } from 'node:readline/promises';
|
|
7
|
+
export async function getWatchIgnorer(dirname) {
|
|
8
|
+
const ignoreList = await getIgnoreList(dirname);
|
|
9
|
+
return ignoreList?.basename === '.gitignore'
|
|
10
|
+
? createGitIgnore(ignoreList.ignoreList)
|
|
11
|
+
: ignoreList?.basename === '.npmignore'
|
|
12
|
+
? createNPMIgnore(ignoreList.ignoreList)
|
|
13
|
+
: createNullIgnore();
|
|
14
|
+
}
|
|
15
|
+
function createNullIgnore() {
|
|
16
|
+
return { ignores: () => false, filter: (filenames) => filenames };
|
|
17
|
+
}
|
|
18
|
+
function createNPMIgnore(list) {
|
|
19
|
+
const mms = list.map((pattern) => new Minimatch(pattern));
|
|
20
|
+
const ignores = (filename) => mms.some((mm) => mm.match(filename));
|
|
21
|
+
return {
|
|
22
|
+
ignores,
|
|
23
|
+
filter: (filenames) => filenames.filter((filename) => !ignores(filename)),
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
function createGitIgnore(list) {
|
|
27
|
+
return gitignore().add(list);
|
|
28
|
+
}
|
|
29
|
+
async function getIgnoreList(dirname) {
|
|
30
|
+
for (const basename of ['.npmignore', '.gitignore']) {
|
|
31
|
+
const filename = path.join(dirname, basename);
|
|
32
|
+
try {
|
|
33
|
+
await access(filename, constants.F_OK);
|
|
34
|
+
}
|
|
35
|
+
catch (error) {
|
|
36
|
+
continue;
|
|
37
|
+
}
|
|
38
|
+
const rl = createReadlineInterface({
|
|
39
|
+
input: createReadStream(filename),
|
|
40
|
+
});
|
|
41
|
+
const ignoreList = [];
|
|
42
|
+
for await (const line of rl)
|
|
43
|
+
if (line)
|
|
44
|
+
ignoreList.push(line.trim());
|
|
45
|
+
return {
|
|
46
|
+
basename,
|
|
47
|
+
ignoreList,
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "esm.dev",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.8.0",
|
|
4
4
|
"description": "TypeScript library template",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"sideEffects": false,
|
|
@@ -16,7 +16,8 @@
|
|
|
16
16
|
"esm.dev": "bun ./src/cli.ts",
|
|
17
17
|
"prepare": "husky",
|
|
18
18
|
"start": "docker compose up --detach --remove-orphans",
|
|
19
|
-
"stop": "docker compose down"
|
|
19
|
+
"stop": "docker compose down",
|
|
20
|
+
"test": "find . -name \"*.test.ts\" -exec sh -c 'bun test \"$1\" || kill \"$PPID\"' sh {} \\;"
|
|
20
21
|
},
|
|
21
22
|
"repository": {
|
|
22
23
|
"type": "git",
|
|
@@ -56,7 +57,9 @@
|
|
|
56
57
|
"escape-string-regexp": "^5.0.0",
|
|
57
58
|
"glob": "^11.0.2",
|
|
58
59
|
"http-proxy": "^1.18.1",
|
|
60
|
+
"ignore": "^7.0.4",
|
|
59
61
|
"lodash.debounce": "^4.0.8",
|
|
62
|
+
"minimatch": "^10.0.1",
|
|
60
63
|
"mustache": "^4.2.0",
|
|
61
64
|
"npm": "^11.4.1",
|
|
62
65
|
"ramda": "^0.30.1",
|