Skip to content

Commit 82235f6

Browse files
committed
Improve list-exports
1 parent a586e48 commit 82235f6

File tree

1 file changed

+41
-20
lines changed

1 file changed

+41
-20
lines changed

src/scripts/list-exports.ts

Lines changed: 41 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,37 +2,58 @@ import * as fs from "fs";
22
import { join as pathJoin } from "path";
33
import { getProjectRoot } from "../bin/tools/getProjectRoot";
44
import { exclude } from "tsafe/exclude";
5+
import { execSync } from "child_process";
6+
import { same } from "evt/tools/inDepth/same";
57

68
const packageJsonFilePath = pathJoin(getProjectRoot(), "package.json");
79

810
const packageJsonParsed = JSON.parse(fs.readFileSync(packageJsonFilePath).toString("utf8"));
911

12+
const srcDirPath = pathJoin(getProjectRoot(), "src");
13+
14+
const newExports = {
15+
".": `./${packageJsonParsed["module"]}`,
16+
...Object.fromEntries(
17+
fs
18+
.readdirSync(srcDirPath)
19+
.filter(basename => {
20+
try {
21+
execSync(`git ls-files --error-unmatch ${pathJoin(srcDirPath, basename)}`, {
22+
"stdio": []
23+
});
24+
} catch {
25+
return false;
26+
}
27+
28+
return true;
29+
})
30+
.map(basename => {
31+
const match = basename.match(/^([^.]+)\.tsx?$/);
32+
33+
if (match === null) {
34+
return undefined;
35+
}
36+
37+
return match[1];
38+
})
39+
.filter(exclude(undefined))
40+
.sort()
41+
.reverse()
42+
.map(name => [`./${name}`, `./dist/${name}.js`])
43+
)
44+
};
45+
46+
if (same(packageJsonParsed["exports"], newExports)) {
47+
process.exit(0);
48+
}
49+
1050
fs.writeFileSync(
1151
packageJsonFilePath,
1252
Buffer.from(
1353
JSON.stringify(
1454
{
1555
...packageJsonParsed,
16-
"exports": {
17-
".": `./${packageJsonParsed["module"]}`,
18-
...Object.fromEntries(
19-
fs
20-
.readdirSync(pathJoin(getProjectRoot(), "src"))
21-
.map(basename => {
22-
const match = basename.match(/^([^.]+)\.tsx?$/);
23-
24-
if (match === null) {
25-
return undefined;
26-
}
27-
28-
return match[1];
29-
})
30-
.filter(exclude(undefined))
31-
.sort()
32-
.reverse()
33-
.map(name => [`./${name}`, `./dist/${name}.js`])
34-
)
35-
}
56+
"exports": newExports
3657
},
3758
null,
3859
2

0 commit comments

Comments
 (0)