]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - types/generate-package.ts
Fix some build scripts and lint
[github/Chocobozzz/PeerTube.git] / types / generate-package.ts
1 import { copyFile, readJson, writeFile, writeJSON } from 'fs-extra'
2 import { resolve } from 'path'
3 import { cwd } from 'process'
4 import { execSync } from 'child_process'
5 import depcheck, { PackageDependencies } from 'depcheck'
6
7 run()
8 .then(() => process.exit(0))
9 .catch(err => {
10 console.error(err)
11 process.exit(-1)
12 })
13
14 async function run () {
15 execSync('npm run build:types', { stdio: 'inherit' })
16
17 const typesPath = resolve(cwd(), './types/')
18 const typesDistPath = resolve(cwd(), typesPath, './dist/')
19 const typesDistPackageJsonPath = resolve(typesDistPath, './package.json')
20 const typesDistGitIgnorePath = resolve(typesDistPath, './.gitignore')
21 const mainPackageJson = await readJson(resolve(cwd(), './package.json'))
22 const distTsConfigPath = resolve(cwd(), typesPath, './tsconfig.dist.json')
23 const distTsConfig = await readJson(distTsConfigPath)
24 const clientPackageJson = await readJson(resolve(cwd(), './client/package.json'))
25
26 const allDependencies = Object.assign(
27 mainPackageJson.dependencies,
28 mainPackageJson.devDepencies,
29 clientPackageJson.dependencies
30 ) as PackageDependencies
31
32 // https://github.com/depcheck/depcheck#api
33 const depcheckOptions = {
34 parsers: { '**/*.ts': depcheck.parser.typescript },
35 detectors: [
36 depcheck.detector.requireCallExpression,
37 depcheck.detector.importDeclaration
38 ],
39 ignoreMatches: Object.keys(distTsConfig?.compilerOptions?.paths || []),
40 package: { dependencies: allDependencies }
41 }
42
43 const { dependencies: unusedDependencies } = await depcheck(resolve(cwd(), './types/'), depcheckOptions)
44 console.log(`Removing ${Object.keys(unusedDependencies).length} unused dependencies.`)
45 const dependencies = Object
46 .keys(allDependencies)
47 .filter(dependencyName => !unusedDependencies.includes(dependencyName))
48 .reduce((dependencies, dependencyName) => {
49 dependencies[dependencyName] = allDependencies[dependencyName]
50 return dependencies
51 }, {})
52
53 const { description, version, licence, engines, author, repository } = mainPackageJson
54 const typesPackageJson = {
55 name: '@peertube/peertube-types',
56 description,
57 version,
58 private: false,
59 license: licence,
60 engines,
61 author,
62 repository,
63 dependencies
64 }
65 console.log(`Writing package.json to ${typesDistPackageJsonPath}`)
66 await writeJSON(typesDistPackageJsonPath, typesPackageJson, { spaces: 2 })
67
68 console.log(`Writing git ignore to ${typesDistGitIgnorePath}`)
69 await writeFile(typesDistGitIgnorePath, '*.tsbuildinfo')
70
71 console.log('Copying tsconfig files')
72 await copyFile(distTsConfigPath, resolve(typesDistPath, './tsconfig.json'))
73 await copyFile(resolve(cwd(), './tsconfig.base.json'), resolve(typesDistPath, './tsconfig.base.json'))
74
75 await copyFile(resolve(typesPath, './README.md'), resolve(typesDistPath, './README.md'))
76 }