From 8b03e2ce1a2098261de2f729f660b1ae2a320b65 Mon Sep 17 00:00:00 2001 From: lutangar Date: Tue, 9 Nov 2021 13:49:08 +0100 Subject: feat(types): create peertube-types package Create dedicated Typescript "types" configuration file for each "projects". Create a types folder which includes every useful ts definition: - client - server - shared Add tooling to create a proper package, extract dependencies, etc... Add CI Github task. refactor(types): publish types package in release script --- scripts/generate-types-package.ts | 78 +++++++++++++++++++++++++++++++++++++++ scripts/release.sh | 5 +++ scripts/tsconfig.json | 2 +- 3 files changed, 84 insertions(+), 1 deletion(-) create mode 100644 scripts/generate-types-package.ts (limited to 'scripts') diff --git a/scripts/generate-types-package.ts b/scripts/generate-types-package.ts new file mode 100644 index 000000000..3543fa472 --- /dev/null +++ b/scripts/generate-types-package.ts @@ -0,0 +1,78 @@ +import { copyFile, readJson, writeFile, writeJSON } from 'fs-extra' +import { resolve } from 'path' +import { cwd } from 'process' +import { execSync } from 'child_process' +import depcheck, { PackageDependencies } from 'depcheck' + +run() + .then(() => process.exit(0)) + .catch(err => { + console.error(err) + process.exit(-1) + }) + +async function run () { + execSync('npm run build:types', { stdio: 'inherit' }) + const typesPath = resolve(cwd(), './types/') + const typesPackageJsonPath = resolve(typesPath, './package.json') + const typesGitIgnorePath = resolve(typesPath, './.gitignore') + const mainPackageJson = await readJson(resolve(cwd(), './package.json')) + const tsConfigPath = resolve(cwd(), './tsconfig.json') + const tsConfig = await readJson(tsConfigPath) + const clientPackageJson = await readJson(resolve(cwd(), './client/package.json')) + + 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 + ], + ignoreMatches: Object.keys(tsConfig?.compilerOptions?.paths || []), + 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 + } + console.log(`Writing package.json to ${typesPackageJsonPath}`) + await writeJSON(typesPackageJsonPath, typesPackageJson, { spaces: 2 }) + + console.log(`Writing git ignore to ${typesGitIgnorePath}`) + await writeFile(typesGitIgnorePath, '*.tsbuildinfo') + + console.log('Copying tsconfig files') + await copyFile(tsConfigPath, resolve(typesPath, './tsconfig.json')) + await copyFile(resolve(cwd(), './tsconfig.base.json'), resolve(typesPath, './tsconfig.base.json')) + tsConfig.references.map(({ path }) => path).forEach((path) => { + const src = resolve(cwd(), path, '/tsconfig.json') + const dest = resolve(typesPath, path, './tsconfig.json') + console.log(`${src} -> ${dest}`) + copyFile(src, dest).catch((e) => console.error(e)) + }) +} diff --git a/scripts/release.sh b/scripts/release.sh index 6423d17ee..7681de90d 100755 --- a/scripts/release.sh +++ b/scripts/release.sh @@ -118,3 +118,8 @@ rm -f "./client/dist/embed-stats.json" git checkout "$branch" fi ) + +# Release types package +npm run generate-types-package +cd types +npm publish --access public diff --git a/scripts/tsconfig.json b/scripts/tsconfig.json index 0d9716f2d..0cfd927a6 100644 --- a/scripts/tsconfig.json +++ b/scripts/tsconfig.json @@ -1,7 +1,7 @@ { "extends": "../tsconfig.base.json", "compilerOptions": { - "outDir": "../dist/scripts", + "outDir": "../dist/scripts" }, "references": [ { "path": "../shared" }, -- cgit v1.2.3