From b969539c838ae3012d7a7040c5e310bb9c834e95 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Fri, 24 Dec 2021 13:16:55 +0100 Subject: [PATCH 1/1] Fix types dist paths --- .github/workflows/test.yml | 2 +- package.json | 2 ++ scripts/ci.sh | 5 ++++- scripts/release.sh | 2 +- scripts/test.sh | 1 + support/doc/plugins/guide.md | 24 +++++++++++------------- tsconfig.base.json | 2 +- types/generate-package.ts | 5 +---- types/src/client/tsconfig.json | 3 +++ types/src/index.ts | 2 ++ types/tests/test.ts | 32 ++++++++++++++++++++++++++++++++ types/tsconfig.dist.json | 2 +- types/tsconfig.json | 6 +++--- yarn.lock | 16 +++++++++++++++- 14 files changed, 78 insertions(+), 26 deletions(-) create mode 100644 types/tests/test.ts diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 030ec3790..6bd759c06 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -39,7 +39,7 @@ jobs: strategy: fail-fast: false matrix: - test_suite: [ client, api-1, api-2, api-3, api-4, cli-plugin, lint, external-plugins ] + test_suite: [ types, client, api-1, api-2, api-3, api-4, cli-plugin, lint, external-plugins ] env: PGUSER: peertube diff --git a/package.json b/package.json index c9717b5e9..5c133e186 100644 --- a/package.json +++ b/package.json @@ -63,6 +63,7 @@ "nodemon": "nodemon", "ts-node": "ts-node", "eslint": "eslint", + "resolve-tspaths": "resolve-tspaths", "concurrently": "concurrently", "mocha": "mocha", "ci": "bash ./scripts/ci.sh", @@ -203,6 +204,7 @@ "mocha": "^9.0.0", "nodemon": "^2.0.1", "proxy": "^1.0.2", + "resolve-tspaths": "^0.1.2", "socket.io-client": "^4.0.1", "source-map-support": "^0.5.0", "supertest": "^6.0.1", diff --git a/scripts/ci.sh b/scripts/ci.sh index a628cbced..070a104cc 100755 --- a/scripts/ci.sh +++ b/scripts/ci.sh @@ -40,7 +40,10 @@ findTestFiles () { find $1 -type f -name "*.js" $exception | xargs echo } -if [ "$1" = "client" ]; then +if [ "$1" = "types" ]; then + npm run generate-types-package + npm run tsc -- --noEmit --esModuleInterop types/tests/test.ts +elif [ "$1" = "client" ]; then npm run build feedsFiles=$(findTestFiles ./dist/server/tests/feeds) diff --git a/scripts/release.sh b/scripts/release.sh index 7681de90d..409a52977 100755 --- a/scripts/release.sh +++ b/scripts/release.sh @@ -121,5 +121,5 @@ rm -f "./client/dist/embed-stats.json" # Release types package npm run generate-types-package -cd types +cd types/dist npm publish --access public diff --git a/scripts/test.sh b/scripts/test.sh index 2dc79c6ce..4d1d8720a 100755 --- a/scripts/test.sh +++ b/scripts/test.sh @@ -2,6 +2,7 @@ set -eu +npm run ci -- types npm run ci -- client npm run ci -- cli-plugin npm run ci -- api-1 diff --git a/support/doc/plugins/guide.md b/support/doc/plugins/guide.md index 0a91460f5..26fcb8987 100644 --- a/support/doc/plugins/guide.md +++ b/support/doc/plugins/guide.md @@ -37,7 +37,6 @@ - [Update README](#update-readme) - [Update package.json](#update-packagejson) - [Write code](#write-code) - - [Typescript](#typescript) - [Add translations](#add-translations) - [Build your plugin](#build-your-plugin) - [Test your plugin/theme](#test-your-plugintheme) @@ -880,22 +879,23 @@ And if you don't need CSS or client script files, use an empty `array`: ### Write code Now you can register hooks or settings, write CSS and add static directories to your plugin or your theme :) +It's up to you to check the code you write will be compatible with the PeerTube NodeJS version, and will be supported by web browsers. + +**JavaScript** -**Caution:** It's up to you to check the code you write will be compatible with the PeerTube NodeJS version, -and will be supported by web browsers. If you want to write modern JavaScript, please use a transpiler like [Babel](https://babeljs.io/). -If you want to use __Typescript__ see section below. -### Typescript +**Typescript** + +If you want to use __Typescript__, you can add __PeerTube__ types as dev dependencies: -You can add __PeerTube__ types as dev dependencies: ``` npm install --save-dev @peertube/peertube-types ``` This package exposes *server* definition files by default: ```ts -import { RegisterServerOptions } from '@peertube/peertube-types/server/types' +import { RegisterServerOptions } from '@peertube/peertube-types' export async function register ({ registerHook }: RegisterServerOptions) { registerHook({ @@ -907,8 +907,8 @@ export async function register ({ registerHook }: RegisterServerOptions) { But it also exposes client types and various models used in __PeerTube__: ```ts -import { RegisterClientOptions } from '@larriereguichet/peertube-types/client/types'; -import { Video } from '@larriereguichet/peertube-types/shared'; +import { Video } from '@peertube/peertube-types'; +import { RegisterClientOptions } from '@peertube/peertube-types/client'; function register({ registerHook, peertubeHelpers }: RegisterClientOptions) { registerHook({ @@ -926,16 +926,14 @@ function register({ registerHook, peertubeHelpers }: RegisterClientOptions) { fetch(`${peertubeHelpers.getBaseRouterRoute()}/videos/${video.uuid}/captions`, { method: 'PUT', headers: peertubeHelpers.getAuthHeader(), - }) - .then((res) => res.json()) - .then((data) => console.log('Hi %s.', data)); + }).then((res) => res.json()) + .then((data) => console.log('Hi %s.', data)); }, }); } export { register }; ``` -> Other types are accessible from the shared path `@peertube/peertube-types/shared`. ### Add translations diff --git a/tsconfig.base.json b/tsconfig.base.json index a323b0d05..85e3ac213 100644 --- a/tsconfig.base.json +++ b/tsconfig.base.json @@ -27,7 +27,7 @@ "paths": { "@server/*": [ "server/*" ], "@shared/*": [ "shared/*" ], - "@client/*": [ "client/src/*" ], + "@client/*": [ "client/src/*" ] }, "resolveJsonModule": true, "strict": false, diff --git a/types/generate-package.ts b/types/generate-package.ts index a4f049a31..ae061f9b0 100644 --- a/types/generate-package.ts +++ b/types/generate-package.ts @@ -23,6 +23,7 @@ async function run () { 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, @@ -69,9 +70,5 @@ async function run () { console.log(`Writing git ignore to ${typesDistGitIgnorePath}`) await writeFile(typesDistGitIgnorePath, '*.tsbuildinfo') - console.log('Copying tsconfig files') - 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')) } diff --git a/types/src/client/tsconfig.json b/types/src/client/tsconfig.json index 199273538..dea4c131c 100644 --- a/types/src/client/tsconfig.json +++ b/types/src/client/tsconfig.json @@ -1,6 +1,9 @@ { "extends": "../../../tsconfig.base.json", "compilerOptions": { + "stripInternal": true, + "removeComments": false, + "emitDeclarationOnly": true, "outDir": "../../dist/client/", "rootDir": "./", "tsBuildInfoFile": "../../dist/tsconfig.client.types.tsbuildinfo" diff --git a/types/src/index.ts b/types/src/index.ts index f1325777f..a8adca287 100644 --- a/types/src/index.ts +++ b/types/src/index.ts @@ -1 +1,3 @@ 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 new file mode 100644 index 000000000..8c53320a1 --- /dev/null +++ b/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/types/tsconfig.dist.json b/types/tsconfig.dist.json index fc2dbe45b..fbc92712b 100644 --- a/types/tsconfig.dist.json +++ b/types/tsconfig.dist.json @@ -5,7 +5,7 @@ "node_modules/@types", "client/node_modules/@types" ], - "baseUrl": "./", + "baseUrl": "./dist", "paths": { "@server/*": [ "server/*" ], "@shared/*": [ "shared/*" ], diff --git a/types/tsconfig.json b/types/tsconfig.json index 8f09c4a83..514683886 100644 --- a/types/tsconfig.json +++ b/types/tsconfig.json @@ -7,10 +7,11 @@ "outDir": "./dist/", "baseUrl": "./src/", "rootDir": "./src/", + "tsBuildInfoFile": "./dist/tsconfig.server.types.tsbuildinfo", "paths": { "@server/*": [ "../../server/*" ], "@shared/*": [ "../../shared/*" ], - "@client/*": [ "../../client/src/*" ], + "@client/*": [ "../../client/src/*" ] } }, "references": [ @@ -18,6 +19,5 @@ { "path": "../server/tsconfig.types.json" }, { "path": "./src/client/tsconfig.json" } ], - "files": ["./src/index.ts"], + "files": ["./src/index.ts"] } - diff --git a/yarn.lock b/yarn.lock index 8c9527cf9..c0efa2203 100644 --- a/yarn.lock +++ b/yarn.lock @@ -3070,6 +3070,11 @@ combined-stream@^1.0.6, combined-stream@^1.0.8: dependencies: delayed-stream "~1.0.0" +commander@8.2.0: + version "8.2.0" + resolved "https://registry.yarnpkg.com/commander/-/commander-8.2.0.tgz#37fe2bde301d87d47a53adeff8b5915db1381ca8" + integrity sha512-LLKxDvHeL91/8MIyTAD5BFMNtoIwztGPMiM/7Bl8rIPmHCZXRxmSWr91h57dpOpnQ6jIUqEWdXE/uBYMfiVZDA== + commander@^2.19.0, commander@^2.7.1: version "2.20.3" resolved "https://registry.yarnpkg.com/commander/-/commander-2.20.3.tgz#fd485e84c03eb4881c20722ba48035e8531aeb33" @@ -4207,7 +4212,7 @@ fast-fifo@^1.0.0: resolved "https://registry.yarnpkg.com/fast-fifo/-/fast-fifo-1.0.0.tgz#9bc72e6860347bb045a876d1c5c0af11e9b984e7" integrity sha512-4VEXmjxLj7sbs8J//cn2qhRap50dGzF5n8fjay8mau+Jn4hxSeR3xPFwxMaQq/pDaq7+KQk0PAbC2+nWDkJrmQ== -fast-glob@^3.1.1: +fast-glob@3.2.7, fast-glob@^3.1.1: version "3.2.7" resolved "https://registry.yarnpkg.com/fast-glob/-/fast-glob-3.2.7.tgz#fd6cb7a2d7e9aa7a7846111e85a196d6b2f766a1" integrity sha512-rYGMRwip6lUMvYD3BTScMwT1HtAs2d71SMv66Vrxs0IekGZEjhM0pcMfjQPnknBt2zeCwQMEupiN02ZP4DiT1Q== @@ -7472,6 +7477,15 @@ resolve-from@^4.0.0: resolved "https://registry.yarnpkg.com/resolve-from/-/resolve-from-4.0.0.tgz#4abcd852ad32dd7baabfe9b40e00a36db5f392e6" integrity sha512-pb/MYmXstAkysRFx8piNI1tGFNQIFA3vkE3Gq4EuA1dF6gHp/+vgZqsCGJapvy8N3Q+4o7FwvquPJcnZ7RYy4g== +resolve-tspaths@^0.1.2: + version "0.1.2" + resolved "https://registry.yarnpkg.com/resolve-tspaths/-/resolve-tspaths-0.1.2.tgz#621c78f2372dea919f7cecf4f228e7fd6dbd8d45" + integrity sha512-HBU+9gBL/CWzCXudGVJAOGph9xspBShW2Ay8cngXMB8fLBkv8Oz/yoe/LC9cCAzuZouul9YNRyKQKdr/GEFR3w== + dependencies: + ansi-colors "4.1.1" + commander "8.2.0" + fast-glob "3.2.7" + resolve@^1.10.1, resolve@^1.15.1, resolve@^1.18.1, resolve@^1.20.0: version "1.20.0" resolved "https://registry.yarnpkg.com/resolve/-/resolve-1.20.0.tgz#629a013fb3f70755d6f0b7935cc1c2c5378b1975" -- 2.41.0