Open History to see the last commit
1import { unlink, rmdir } from 'fs/promises'
2import fg from 'fast-glob'
3β
4const constants = {
5 IGNORE_FILES_PATTERNS: [
6 'dist/**',
7 'node_modules/**',
8 '.git/**',
9 'scripts/**',
10 ],
11 CLEANUP_FILES_GLOB: ['**/*.d.ts'],
12 IGNORE_REMOVE_DIRECTORIES: [
13 'dist/**',
14 'node_modules/**',
15 '.git/**',
16 'scripts/**',
17 'src/**',
18 ],
19}
20β
21async function postpack() {
22 console.log(
23 'Running postpack script to cleanup type declaration linked files used for publishing',
24 )
25β
26 const typeFiles = await fg(constants.CLEANUP_FILES_GLOB, {
27 ignore: constants.IGNORE_FILES_PATTERNS,
28 })
29β
30 if (typeFiles.length === 0) {
31 return
32 }
33β
34 await Promise.all(typeFiles.map((file) => unlink(file)))
35β
36 const dirs = await fg(['**/'], {
37 onlyDirectories: true,
38 ignore: constants.IGNORE_REMOVE_DIRECTORIES,
39 })
40β
41 // Remove empty directories (deepest first)
42 const sortedDirs = dirs.sort(
43 (a, b) => b.split('/').length - a.split('/').length,
44 )
45 await Promise.all(
46 sortedDirs.map(
47 (dir) => rmdir(dir).catch(() => {}), // Ignore errors (dir not empty)
48 ),
49 )
50}
51β
52postpack().catch((error) => {
53 console.error('Postpack failed:', error)
54})