2021-11-09 06:49:08 -06:00
|
|
|
import { execSync } from 'child_process'
|
|
|
|
import depcheck, { PackageDependencies } from 'depcheck'
|
2021-12-16 11:03:52 -06:00
|
|
|
import { copyFile, readJson, remove, writeFile, writeJSON } from 'fs-extra'
|
|
|
|
import { resolve } from 'path'
|
|
|
|
import { cwd } from 'process'
|
2021-11-09 06:49:08 -06:00
|
|
|
|
|
|
|
run()
|
|
|
|
.then(() => process.exit(0))
|
|
|
|
.catch(err => {
|
|
|
|
console.error(err)
|
|
|
|
process.exit(-1)
|
|
|
|
})
|
|
|
|
|
|
|
|
async function run () {
|
|
|
|
const typesPath = resolve(cwd(), './types/')
|
2021-11-09 06:49:08 -06:00
|
|
|
const typesDistPath = resolve(cwd(), typesPath, './dist/')
|
|
|
|
const typesDistPackageJsonPath = resolve(typesDistPath, './package.json')
|
|
|
|
const typesDistGitIgnorePath = resolve(typesDistPath, './.gitignore')
|
2021-11-09 06:49:08 -06:00
|
|
|
const mainPackageJson = await readJson(resolve(cwd(), './package.json'))
|
2021-11-09 06:49:08 -06:00
|
|
|
const distTsConfigPath = resolve(cwd(), typesPath, './tsconfig.dist.json')
|
|
|
|
const distTsConfig = await readJson(distTsConfigPath)
|
2021-11-09 06:49:08 -06:00
|
|
|
const clientPackageJson = await readJson(resolve(cwd(), './client/package.json'))
|
|
|
|
|
2021-12-16 11:03:52 -06:00
|
|
|
await remove(typesDistPath)
|
|
|
|
execSync('npm run tsc -- -b --verbose types', { stdio: 'inherit' })
|
|
|
|
|
2021-11-09 06:49:08 -06:00
|
|
|
const allDependencies = Object.assign(
|
|
|
|
mainPackageJson.dependencies,
|
|
|
|
mainPackageJson.devDepencies,
|
|
|
|
clientPackageJson.dependencies
|
|
|
|
) as PackageDependencies
|
|
|
|
|
|
|
|
// https://github.com/depcheck/depcheck#api
|
|
|
|
const depcheckOptions = {
|
|
|
|
parsers: { '**/*.ts': depcheck.parser.typescript },
|
|
|
|
detectors: [
|
|
|
|
depcheck.detector.requireCallExpression,
|
|
|
|
depcheck.detector.importDeclaration
|
|
|
|
],
|
2021-11-09 06:49:08 -06:00
|
|
|
ignoreMatches: Object.keys(distTsConfig?.compilerOptions?.paths || []),
|
2021-11-09 06:49:08 -06:00
|
|
|
package: { dependencies: allDependencies }
|
|
|
|
}
|
|
|
|
|
|
|
|
const { dependencies: unusedDependencies } = await depcheck(resolve(cwd(), './types/'), depcheckOptions)
|
|
|
|
console.log(`Removing ${Object.keys(unusedDependencies).length} unused dependencies.`)
|
|
|
|
const dependencies = Object
|
|
|
|
.keys(allDependencies)
|
|
|
|
.filter(dependencyName => !unusedDependencies.includes(dependencyName))
|
|
|
|
.reduce((dependencies, dependencyName) => {
|
|
|
|
dependencies[dependencyName] = allDependencies[dependencyName]
|
|
|
|
return dependencies
|
|
|
|
}, {})
|
|
|
|
|
|
|
|
const { description, version, licence, engines, author, repository } = mainPackageJson
|
|
|
|
const typesPackageJson = {
|
|
|
|
name: '@peertube/peertube-types',
|
|
|
|
description,
|
|
|
|
version,
|
|
|
|
private: false,
|
|
|
|
license: licence,
|
|
|
|
engines,
|
|
|
|
author,
|
|
|
|
repository,
|
|
|
|
dependencies
|
|
|
|
}
|
2021-11-09 06:49:08 -06:00
|
|
|
console.log(`Writing package.json to ${typesDistPackageJsonPath}`)
|
|
|
|
await writeJSON(typesDistPackageJsonPath, typesPackageJson, { spaces: 2 })
|
2021-11-09 06:49:08 -06:00
|
|
|
|
2021-11-09 06:49:08 -06:00
|
|
|
console.log(`Writing git ignore to ${typesDistGitIgnorePath}`)
|
|
|
|
await writeFile(typesDistGitIgnorePath, '*.tsbuildinfo')
|
2021-11-09 06:49:08 -06:00
|
|
|
|
|
|
|
console.log('Copying tsconfig files')
|
2021-11-09 06:49:08 -06:00
|
|
|
await copyFile(distTsConfigPath, resolve(typesDistPath, './tsconfig.json'))
|
|
|
|
await copyFile(resolve(cwd(), './tsconfig.base.json'), resolve(typesDistPath, './tsconfig.base.json'))
|
|
|
|
|
|
|
|
await copyFile(resolve(typesPath, './README.md'), resolve(typesDistPath, './README.md'))
|
2021-11-09 06:49:08 -06:00
|
|
|
}
|