1 import { execSync } from 'child_process'
2 import depcheck, { PackageDependencies } from 'depcheck'
3 import { copyFile, readJson, remove, writeFile, writeJSON } from 'fs-extra'
4 import { resolve } from 'path'
5 import { cwd } from 'process'
7 if (!process.argv[2]) {
8 console.error('Need version as argument')
12 const version = process.argv[2]
13 console.log('Will generate package version %s.', version)
16 .then(() => process.exit(0))
22 async function run () {
23 const typesPath = resolve(cwd(), './packages/types/')
24 const typesDistPath = resolve(cwd(), typesPath, './dist/')
25 const typesDistPackageJsonPath = resolve(typesDistPath, './package.json')
26 const typesDistGitIgnorePath = resolve(typesDistPath, './.gitignore')
27 const mainPackageJson = await readJson(resolve(cwd(), './package.json'))
28 const distTsConfigPath = resolve(cwd(), typesPath, './tsconfig.dist.json')
29 const distTsConfig = await readJson(distTsConfigPath)
30 const clientPackageJson = await readJson(resolve(cwd(), './client/package.json'))
32 await remove(typesDistPath)
33 execSync('npm run tsc -- -b --verbose packages/types', { stdio: 'inherit' })
34 execSync(`npm run resolve-tspaths -- --project ${distTsConfigPath} --src ${typesDistPath} --out ${typesDistPath}`, { stdio: 'inherit' })
36 const allDependencies = Object.assign(
37 mainPackageJson.dependencies,
38 mainPackageJson.devDepencies,
39 clientPackageJson.dependencies
40 ) as PackageDependencies
42 // https://github.com/depcheck/depcheck#api
43 const depcheckOptions = {
44 parsers: { '**/*.ts': depcheck.parser.typescript },
46 depcheck.detector.requireCallExpression,
47 depcheck.detector.importDeclaration
49 ignoreMatches: Object.keys(distTsConfig?.compilerOptions?.paths || []),
50 package: { dependencies: allDependencies }
53 const { dependencies: unusedDependencies } = await depcheck(resolve(typesPath), depcheckOptions)
54 console.log(`Removing ${Object.keys(unusedDependencies).length} unused dependencies.`)
55 const dependencies = Object
56 .keys(allDependencies)
57 .filter(dependencyName => !unusedDependencies.includes(dependencyName))
58 .reduce((dependencies, dependencyName) => {
59 dependencies[dependencyName] = allDependencies[dependencyName]
63 const { description, licence, engines, author, repository } = mainPackageJson
64 const typesPackageJson = {
65 name: '@peertube/peertube-types',
75 console.log(`Writing package.json to ${typesDistPackageJsonPath}`)
76 await writeJSON(typesDistPackageJsonPath, typesPackageJson, { spaces: 2 })
78 console.log(`Writing git ignore to ${typesDistGitIgnorePath}`)
79 await writeFile(typesDistGitIgnorePath, '*.tsbuildinfo')
81 await copyFile(resolve(typesPath, './README.md'), resolve(typesDistPath, './README.md'))