PeerTube/packages/types/generate-package.ts

97 lines
3.3 KiB
TypeScript
Raw Normal View History

import { execSync } from 'child_process'
import depcheck, { PackageDependencies } from 'depcheck'
2021-12-16 18:03:52 +01:00
import { copyFile, readJson, remove, writeFile, writeJSON } from 'fs-extra'
2021-12-28 15:07:46 +01:00
import { join, resolve } from 'path'
import { root } from '../../shared/core-utils'
2021-12-28 10:25:30 +01:00
if (!process.argv[2]) {
console.error('Need version as argument')
process.exit(-1)
}
const version = process.argv[2]
console.log('Will generate package version %s.', version)
run()
.then(() => process.exit(0))
.catch(err => {
console.error(err)
process.exit(-1)
})
async function run () {
2021-12-28 15:07:46 +01:00
const typesPath = __dirname
const typesDistPath = join(typesPath, 'dist')
const typesDistPackageJsonPath = join(typesDistPath, 'package.json')
const typesDistGitIgnorePath = join(typesDistPath, '.gitignore')
const mainPackageJson = await readJson(join(root(), 'package.json'))
const distTsConfigPath = join(typesPath, 'tsconfig.dist.json')
const distTsConfig = await readJson(distTsConfigPath)
2021-12-28 15:07:46 +01:00
const clientPackageJson = await readJson(join(root(), 'client', 'package.json'))
2021-12-16 18:03:52 +01:00
await remove(typesDistPath)
2021-12-28 10:18:15 +01:00
execSync('npm run tsc -- -b --verbose packages/types', { stdio: 'inherit' })
2021-12-24 13:16:55 +01:00
execSync(`npm run resolve-tspaths -- --project ${distTsConfigPath} --src ${typesDistPath} --out ${typesDistPath}`, { stdio: 'inherit' })
2021-12-16 18:03:52 +01:00
const allDependencies = Object.assign(
mainPackageJson.dependencies,
2021-12-28 15:07:46 +01:00
mainPackageJson.devDependencies,
clientPackageJson.dependencies,
clientPackageJson.devDependencies
) as PackageDependencies
// https://github.com/depcheck/depcheck#api
const depcheckOptions = {
parsers: { '**/*.ts': depcheck.parser.typescript },
detectors: [
depcheck.detector.requireCallExpression,
depcheck.detector.importDeclaration
],
ignoreMatches: Object.keys(distTsConfig?.compilerOptions?.paths || []),
package: { dependencies: allDependencies }
}
2021-12-28 15:07:46 +01:00
const result = await depcheck(typesDistPath, depcheckOptions)
if (Object.keys(result.invalidDirs).length !== 0) {
console.error('Invalid directories detected.', { invalidDirs: result.invalidDirs })
process.exit(-1)
}
if (Object.keys(result.invalidFiles).length !== 0) {
console.error('Invalid files detected.', { invalidFiles: result.invalidFiles })
process.exit(-1)
}
const unusedDependencies = result.dependencies
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
}, {})
2021-12-28 10:25:30 +01:00
const { description, licence, engines, author, repository } = mainPackageJson
const typesPackageJson = {
name: '@peertube/peertube-types',
description,
version,
private: false,
license: licence,
engines,
author,
repository,
dependencies
}
console.log(`Writing package.json to ${typesDistPackageJsonPath}`)
await writeJSON(typesDistPackageJsonPath, typesPackageJson, { spaces: 2 })
console.log(`Writing git ignore to ${typesDistGitIgnorePath}`)
await writeFile(typesDistGitIgnorePath, '*.tsbuildinfo')
await copyFile(resolve(typesPath, './README.md'), resolve(typesDistPath, './README.md'))
}