File size: 1,389 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { existsSync, renameSync, writeFileSync } from "fs"
import { resolve } from "path/posix"

const backupFilename = "package.backup.json"

async function main() {
  const cwd = process.cwd()

  const sourcePath = resolve(cwd, "package.json")
  const backupPath = resolve(cwd, backupFilename)

  if (process.argv.includes("--restore")) {
    return restore({
      sourcePath,
      backupPath,
    })
  }

  await clean({
    sourcePath,
    backupPath,
  })
}

async function clean(options: { sourcePath: string; backupPath: string }) {
  const packageJson = (await import(options.sourcePath)).default

  let changed = false

  for (const key in packageJson.exports) {
    if (packageJson.exports[key]["dev"]) {
      delete packageJson.exports[key]["dev"]
      changed = true
    }
  }

  if (changed) {
    await backup(options)
    writeFileSync(
      options.sourcePath,
      `${JSON.stringify(packageJson, null, 2)}\n`,
    )
  }
}

async function backup({
  sourcePath,
  backupPath,
}: {
  sourcePath: string
  backupPath: string
}) {
  renameSync(sourcePath, backupPath)
}

async function restore({
  sourcePath,
  backupPath,
}: {
  sourcePath: string
  backupPath: string
}) {
  const exists = existsSync(backupPath)
  console.log({ exists })
  if (exists) {
    renameSync(backupPath, sourcePath)
  }
}

main().catch((err) => {
  console.error(err)
  process.exit(1)
})