From 6627dbc957477aa32e21ed1bdc8cd72b928cd616 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Tue, 28 Dec 2021 10:18:15 +0100 Subject: Move types package in packages/ --- .github/workflows/test.yml | 2 +- .gitignore | 4 +- client/tsconfig.types.json | 4 +- package.json | 2 +- packages/types/README.md | 19 +++++++++ packages/types/generate-package.ts | 74 +++++++++++++++++++++++++++++++++ packages/types/src/client/index.ts | 1 + packages/types/src/client/tsconfig.json | 15 +++++++ packages/types/src/index.ts | 3 ++ packages/types/tests/test.ts | 32 ++++++++++++++ packages/types/tsconfig.dist.json | 16 +++++++ packages/types/tsconfig.json | 23 ++++++++++ scripts/ci.sh | 4 +- server/tsconfig.types.json | 2 +- shared/tsconfig.types.json | 2 +- types/README.md | 19 --------- types/generate-package.ts | 74 --------------------------------- types/src/client/index.ts | 1 - types/src/client/tsconfig.json | 15 ------- types/src/index.ts | 3 -- types/tests/test.ts | 32 -------------- types/tsconfig.dist.json | 16 ------- types/tsconfig.json | 23 ---------- 23 files changed, 194 insertions(+), 192 deletions(-) create mode 100644 packages/types/README.md create mode 100644 packages/types/generate-package.ts create mode 100644 packages/types/src/client/index.ts create mode 100644 packages/types/src/client/tsconfig.json create mode 100644 packages/types/src/index.ts create mode 100644 packages/types/tests/test.ts create mode 100644 packages/types/tsconfig.dist.json create mode 100644 packages/types/tsconfig.json delete mode 100644 types/README.md delete mode 100644 types/generate-package.ts delete mode 100644 types/src/client/index.ts delete mode 100644 types/src/client/tsconfig.json delete mode 100644 types/src/index.ts delete mode 100644 types/tests/test.ts delete mode 100644 types/tsconfig.dist.json delete mode 100644 types/tsconfig.json diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 1a59d13d7..8ba2c549d 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -39,7 +39,7 @@ jobs: strategy: fail-fast: false matrix: - test_suite: [ types, client, api-1, api-2, api-3, api-4, cli-plugin, lint, external-plugins ] + test_suite: [ types-package, client, api-1, api-2, api-3, api-4, cli-plugin, lint, external-plugins ] env: PGUSER: peertube diff --git a/.gitignore b/.gitignore index 0ec17217d..5e06248f1 100644 --- a/.gitignore +++ b/.gitignore @@ -53,4 +53,6 @@ yarn-error.log # TypeScript *.tsbuildinfo -/types/dist/ + +# Packages +/packages/types/dist/ diff --git a/client/tsconfig.types.json b/client/tsconfig.types.json index c6ed64100..99d96d413 100644 --- a/client/tsconfig.types.json +++ b/client/tsconfig.types.json @@ -4,11 +4,11 @@ "stripInternal": true, "removeComments": false, "declaration": true, - "outDir": "../types/dist/client/", + "outDir": "../packages/types/dist/client/", "emitDeclarationOnly": true, "composite": true, "rootDir": "src/", - "tsBuildInfoFile": "../types/dist/tsconfig.client.tsbuildinfo" + "tsBuildInfoFile": "../packages/types/dist/tsconfig.client.tsbuildinfo" }, "references": [ { "path": "../shared/tsconfig.types.json" } diff --git a/package.json b/package.json index c7d00ec15..b41422343 100644 --- a/package.json +++ b/package.json @@ -52,7 +52,7 @@ "test": "bash ./scripts/test.sh", "help": "bash ./scripts/help.sh", "generate-cli-doc": "bash ./scripts/generate-cli-doc.sh", - "generate-types-package": "ts-node ./types/generate-package.ts", + "generate-types-package": "ts-node ./packages/types/generate-package.ts", "parse-log": "node ./dist/scripts/parse-log.js", "prune-storage": "node ./dist/scripts/prune-storage.js", "postinstall": "test -n \"$NOCLIENT\" || (cd client && yarn install --pure-lockfile)", diff --git a/packages/types/README.md b/packages/types/README.md new file mode 100644 index 000000000..adeca48e5 --- /dev/null +++ b/packages/types/README.md @@ -0,0 +1,19 @@ +# PeerTube typings + +These **Typescript** *types* are mainly used to write **PeerTube** plugins. + +## Installation + +Npm: +``` +npm install --save-dev @peertube/peertube-types +``` + +Yarn: +``` +yarn add --dev @peertube/peertube-types +``` + +## Usage + +> See [contribute-plugins](https://docs.joinpeertube.org/contribute-plugins?id=typescript) **Typescript** section of the doc. diff --git a/packages/types/generate-package.ts b/packages/types/generate-package.ts new file mode 100644 index 000000000..0c14514e7 --- /dev/null +++ b/packages/types/generate-package.ts @@ -0,0 +1,74 @@ +import { execSync } from 'child_process' +import depcheck, { PackageDependencies } from 'depcheck' +import { copyFile, readJson, remove, writeFile, writeJSON } from 'fs-extra' +import { resolve } from 'path' +import { cwd } from 'process' + +run() + .then(() => process.exit(0)) + .catch(err => { + console.error(err) + process.exit(-1) + }) + +async function run () { + const typesPath = resolve(cwd(), './packages/types/') + const typesDistPath = resolve(cwd(), typesPath, './dist/') + const typesDistPackageJsonPath = resolve(typesDistPath, './package.json') + const typesDistGitIgnorePath = resolve(typesDistPath, './.gitignore') + const mainPackageJson = await readJson(resolve(cwd(), './package.json')) + const distTsConfigPath = resolve(cwd(), typesPath, './tsconfig.dist.json') + const distTsConfig = await readJson(distTsConfigPath) + const clientPackageJson = await readJson(resolve(cwd(), './client/package.json')) + + await remove(typesDistPath) + execSync('npm run tsc -- -b --verbose packages/types', { stdio: 'inherit' }) + execSync(`npm run resolve-tspaths -- --project ${distTsConfigPath} --src ${typesDistPath} --out ${typesDistPath}`, { stdio: 'inherit' }) + + 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(distTsConfig?.compilerOptions?.paths || []), + package: { dependencies: allDependencies } + } + + const { dependencies: unusedDependencies } = await depcheck(resolve(typesPath), 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 ${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')) +} diff --git a/packages/types/src/client/index.ts b/packages/types/src/client/index.ts new file mode 100644 index 000000000..5ee10ecb8 --- /dev/null +++ b/packages/types/src/client/index.ts @@ -0,0 +1 @@ +export * from '@client/types' diff --git a/packages/types/src/client/tsconfig.json b/packages/types/src/client/tsconfig.json new file mode 100644 index 000000000..bb76fbe21 --- /dev/null +++ b/packages/types/src/client/tsconfig.json @@ -0,0 +1,15 @@ +{ + "extends": "../../../../tsconfig.base.json", + "compilerOptions": { + "stripInternal": true, + "removeComments": false, + "emitDeclarationOnly": true, + "outDir": "../../dist/client/", + "rootDir": "./", + "tsBuildInfoFile": "../../dist/tsconfig.client.types.tsbuildinfo" + }, + "references": [ + { "path": "../../../../client/tsconfig.types.json" } + ], + "files": ["index.ts"] +} diff --git a/packages/types/src/index.ts b/packages/types/src/index.ts new file mode 100644 index 000000000..a8adca287 --- /dev/null +++ b/packages/types/src/index.ts @@ -0,0 +1,3 @@ +export * from '@server/types' +export * from '@server/types/models' +export * from '@shared/models' diff --git a/packages/types/tests/test.ts b/packages/types/tests/test.ts new file mode 100644 index 000000000..8c53320a1 --- /dev/null +++ b/packages/types/tests/test.ts @@ -0,0 +1,32 @@ +import { RegisterServerOptions, Video } from '../dist' +import { RegisterClientOptions } from '../dist/client' + +function register1 ({ registerHook }: RegisterServerOptions) { + registerHook({ + target: 'action:application.listening', + handler: () => console.log('hello') + }) +} + +function register2 ({ registerHook, peertubeHelpers }: RegisterClientOptions) { + registerHook({ + target: 'action:admin-plugin-settings.init', + handler: ({ npmName }: { npmName: string }) => { + if ('peertube-plugin-transcription' !== npmName) { + return + } + }, + }) + + registerHook({ + target: 'action:video-watch.video.loaded', + handler: ({ video }: { video: Video }) => { + fetch(`${peertubeHelpers.getBaseRouterRoute()}/videos/${video.uuid}/captions`, { + method: 'PUT', + headers: peertubeHelpers.getAuthHeader(), + }) + .then((res) => res.json()) + .then((data) => console.log('Hi %s.', data)) + }, + }) +} diff --git a/packages/types/tsconfig.dist.json b/packages/types/tsconfig.dist.json new file mode 100644 index 000000000..fbc92712b --- /dev/null +++ b/packages/types/tsconfig.dist.json @@ -0,0 +1,16 @@ +{ + "extends": "./tsconfig.json", + "compilerOptions": { + "typeRoots": [ + "node_modules/@types", + "client/node_modules/@types" + ], + "baseUrl": "./dist", + "paths": { + "@server/*": [ "server/*" ], + "@shared/*": [ "shared/*" ], + "@client/*": [ "client/*" ] + } + } +} + diff --git a/packages/types/tsconfig.json b/packages/types/tsconfig.json new file mode 100644 index 000000000..f8e16f6b4 --- /dev/null +++ b/packages/types/tsconfig.json @@ -0,0 +1,23 @@ +{ + "extends": "../../tsconfig.base.json", + "compilerOptions": { + "stripInternal": true, + "removeComments": false, + "emitDeclarationOnly": true, + "outDir": "./dist/", + "baseUrl": "./src/", + "rootDir": "./src/", + "tsBuildInfoFile": "./dist/tsconfig.server.types.tsbuildinfo", + "paths": { + "@server/*": [ "../../../server/*" ], + "@shared/*": [ "../../../shared/*" ], + "@client/*": [ "../../../client/src/*" ] + } + }, + "references": [ + { "path": "../../shared/tsconfig.types.json" }, + { "path": "../../server/tsconfig.types.json" }, + { "path": "./src/client/tsconfig.json" } + ], + "files": ["./src/index.ts"] +} diff --git a/scripts/ci.sh b/scripts/ci.sh index 070a104cc..5b757d94b 100755 --- a/scripts/ci.sh +++ b/scripts/ci.sh @@ -40,9 +40,9 @@ findTestFiles () { find $1 -type f -name "*.js" $exception | xargs echo } -if [ "$1" = "types" ]; then +if [ "$1" = "types-package" ]; then npm run generate-types-package - npm run tsc -- --noEmit --esModuleInterop types/tests/test.ts + npm run tsc -- --noEmit --esModuleInterop packages/types/tests/test.ts elif [ "$1" = "client" ]; then npm run build diff --git a/server/tsconfig.types.json b/server/tsconfig.types.json index 824834066..da6b572ea 100644 --- a/server/tsconfig.types.json +++ b/server/tsconfig.types.json @@ -1,7 +1,7 @@ { "extends": "./tsconfig.json", "compilerOptions": { - "outDir": "../types/dist/server", + "outDir": "../packages/types/dist/server", "stripInternal": true, "removeComments": false, "emitDeclarationOnly": true diff --git a/shared/tsconfig.types.json b/shared/tsconfig.types.json index 73c1cae6c..6acfc05e1 100644 --- a/shared/tsconfig.types.json +++ b/shared/tsconfig.types.json @@ -1,7 +1,7 @@ { "extends": "./tsconfig.json", "compilerOptions": { - "outDir": "../types/dist/shared", + "outDir": "../packages/types/dist/shared", "stripInternal": true, "removeComments": false, "emitDeclarationOnly": true diff --git a/types/README.md b/types/README.md deleted file mode 100644 index adeca48e5..000000000 --- a/types/README.md +++ /dev/null @@ -1,19 +0,0 @@ -# PeerTube typings - -These **Typescript** *types* are mainly used to write **PeerTube** plugins. - -## Installation - -Npm: -``` -npm install --save-dev @peertube/peertube-types -``` - -Yarn: -``` -yarn add --dev @peertube/peertube-types -``` - -## Usage - -> See [contribute-plugins](https://docs.joinpeertube.org/contribute-plugins?id=typescript) **Typescript** section of the doc. diff --git a/types/generate-package.ts b/types/generate-package.ts deleted file mode 100644 index ae061f9b0..000000000 --- a/types/generate-package.ts +++ /dev/null @@ -1,74 +0,0 @@ -import { execSync } from 'child_process' -import depcheck, { PackageDependencies } from 'depcheck' -import { copyFile, readJson, remove, writeFile, writeJSON } from 'fs-extra' -import { resolve } from 'path' -import { cwd } from 'process' - -run() - .then(() => process.exit(0)) - .catch(err => { - console.error(err) - process.exit(-1) - }) - -async function run () { - const typesPath = resolve(cwd(), './types/') - const typesDistPath = resolve(cwd(), typesPath, './dist/') - const typesDistPackageJsonPath = resolve(typesDistPath, './package.json') - const typesDistGitIgnorePath = resolve(typesDistPath, './.gitignore') - const mainPackageJson = await readJson(resolve(cwd(), './package.json')) - const distTsConfigPath = resolve(cwd(), typesPath, './tsconfig.dist.json') - const distTsConfig = await readJson(distTsConfigPath) - const clientPackageJson = await readJson(resolve(cwd(), './client/package.json')) - - await remove(typesDistPath) - execSync('npm run tsc -- -b --verbose types', { stdio: 'inherit' }) - execSync(`npm run resolve-tspaths -- --project ${distTsConfigPath} --src ${typesDistPath} --out ${typesDistPath}`, { stdio: 'inherit' }) - - 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(distTsConfig?.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 ${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')) -} diff --git a/types/src/client/index.ts b/types/src/client/index.ts deleted file mode 100644 index 5ee10ecb8..000000000 --- a/types/src/client/index.ts +++ /dev/null @@ -1 +0,0 @@ -export * from '@client/types' diff --git a/types/src/client/tsconfig.json b/types/src/client/tsconfig.json deleted file mode 100644 index dea4c131c..000000000 --- a/types/src/client/tsconfig.json +++ /dev/null @@ -1,15 +0,0 @@ -{ - "extends": "../../../tsconfig.base.json", - "compilerOptions": { - "stripInternal": true, - "removeComments": false, - "emitDeclarationOnly": true, - "outDir": "../../dist/client/", - "rootDir": "./", - "tsBuildInfoFile": "../../dist/tsconfig.client.types.tsbuildinfo" - }, - "references": [ - { "path": "../../../client/tsconfig.types.json" } - ], - "files": ["index.ts"] -} diff --git a/types/src/index.ts b/types/src/index.ts deleted file mode 100644 index a8adca287..000000000 --- a/types/src/index.ts +++ /dev/null @@ -1,3 +0,0 @@ -export * from '@server/types' -export * from '@server/types/models' -export * from '@shared/models' diff --git a/types/tests/test.ts b/types/tests/test.ts deleted file mode 100644 index 8c53320a1..000000000 --- a/types/tests/test.ts +++ /dev/null @@ -1,32 +0,0 @@ -import { RegisterServerOptions, Video } from '../dist' -import { RegisterClientOptions } from '../dist/client' - -function register1 ({ registerHook }: RegisterServerOptions) { - registerHook({ - target: 'action:application.listening', - handler: () => console.log('hello') - }) -} - -function register2 ({ registerHook, peertubeHelpers }: RegisterClientOptions) { - registerHook({ - target: 'action:admin-plugin-settings.init', - handler: ({ npmName }: { npmName: string }) => { - if ('peertube-plugin-transcription' !== npmName) { - return - } - }, - }) - - registerHook({ - target: 'action:video-watch.video.loaded', - handler: ({ video }: { video: Video }) => { - fetch(`${peertubeHelpers.getBaseRouterRoute()}/videos/${video.uuid}/captions`, { - method: 'PUT', - headers: peertubeHelpers.getAuthHeader(), - }) - .then((res) => res.json()) - .then((data) => console.log('Hi %s.', data)) - }, - }) -} diff --git a/types/tsconfig.dist.json b/types/tsconfig.dist.json deleted file mode 100644 index fbc92712b..000000000 --- a/types/tsconfig.dist.json +++ /dev/null @@ -1,16 +0,0 @@ -{ - "extends": "./tsconfig.json", - "compilerOptions": { - "typeRoots": [ - "node_modules/@types", - "client/node_modules/@types" - ], - "baseUrl": "./dist", - "paths": { - "@server/*": [ "server/*" ], - "@shared/*": [ "shared/*" ], - "@client/*": [ "client/*" ] - } - } -} - diff --git a/types/tsconfig.json b/types/tsconfig.json deleted file mode 100644 index 514683886..000000000 --- a/types/tsconfig.json +++ /dev/null @@ -1,23 +0,0 @@ -{ - "extends": "../tsconfig.base.json", - "compilerOptions": { - "stripInternal": true, - "removeComments": false, - "emitDeclarationOnly": true, - "outDir": "./dist/", - "baseUrl": "./src/", - "rootDir": "./src/", - "tsBuildInfoFile": "./dist/tsconfig.server.types.tsbuildinfo", - "paths": { - "@server/*": [ "../../server/*" ], - "@shared/*": [ "../../shared/*" ], - "@client/*": [ "../../client/src/*" ] - } - }, - "references": [ - { "path": "../shared/tsconfig.types.json" }, - { "path": "../server/tsconfig.types.json" }, - { "path": "./src/client/tsconfig.json" } - ], - "files": ["./src/index.ts"] -} -- cgit v1.2.3