aboutsummaryrefslogtreecommitdiffhomepage
path: root/packages
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2021-12-28 10:18:15 +0100
committerChocobozzz <me@florianbigard.com>2021-12-28 10:18:15 +0100
commit6627dbc957477aa32e21ed1bdc8cd72b928cd616 (patch)
treee6dbe45b7259cc01ff0255887d45d331bc81026f /packages
parent52b356cfe257e8e0d357a498e87d9299c95cdbe5 (diff)
downloadPeerTube-6627dbc957477aa32e21ed1bdc8cd72b928cd616.tar.gz
PeerTube-6627dbc957477aa32e21ed1bdc8cd72b928cd616.tar.zst
PeerTube-6627dbc957477aa32e21ed1bdc8cd72b928cd616.zip
Move types package in packages/
Diffstat (limited to 'packages')
-rw-r--r--packages/types/README.md19
-rw-r--r--packages/types/generate-package.ts74
-rw-r--r--packages/types/src/client/index.ts1
-rw-r--r--packages/types/src/client/tsconfig.json15
-rw-r--r--packages/types/src/index.ts3
-rw-r--r--packages/types/tests/test.ts32
-rw-r--r--packages/types/tsconfig.dist.json16
-rw-r--r--packages/types/tsconfig.json23
8 files changed, 183 insertions, 0 deletions
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 @@
1# PeerTube typings
2
3These **Typescript** *types* are mainly used to write **PeerTube** plugins.
4
5## Installation
6
7Npm:
8```
9npm install --save-dev @peertube/peertube-types
10```
11
12Yarn:
13```
14yarn add --dev @peertube/peertube-types
15```
16
17## Usage
18
19> 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 @@
1import { execSync } from 'child_process'
2import depcheck, { PackageDependencies } from 'depcheck'
3import { copyFile, readJson, remove, writeFile, writeJSON } from 'fs-extra'
4import { resolve } from 'path'
5import { cwd } from 'process'
6
7run()
8 .then(() => process.exit(0))
9 .catch(err => {
10 console.error(err)
11 process.exit(-1)
12 })
13
14async function run () {
15 const typesPath = resolve(cwd(), './packages/types/')
16 const typesDistPath = resolve(cwd(), typesPath, './dist/')
17 const typesDistPackageJsonPath = resolve(typesDistPath, './package.json')
18 const typesDistGitIgnorePath = resolve(typesDistPath, './.gitignore')
19 const mainPackageJson = await readJson(resolve(cwd(), './package.json'))
20 const distTsConfigPath = resolve(cwd(), typesPath, './tsconfig.dist.json')
21 const distTsConfig = await readJson(distTsConfigPath)
22 const clientPackageJson = await readJson(resolve(cwd(), './client/package.json'))
23
24 await remove(typesDistPath)
25 execSync('npm run tsc -- -b --verbose packages/types', { stdio: 'inherit' })
26 execSync(`npm run resolve-tspaths -- --project ${distTsConfigPath} --src ${typesDistPath} --out ${typesDistPath}`, { stdio: 'inherit' })
27
28 const allDependencies = Object.assign(
29 mainPackageJson.dependencies,
30 mainPackageJson.devDepencies,
31 clientPackageJson.dependencies
32 ) as PackageDependencies
33
34 // https://github.com/depcheck/depcheck#api
35 const depcheckOptions = {
36 parsers: { '**/*.ts': depcheck.parser.typescript },
37 detectors: [
38 depcheck.detector.requireCallExpression,
39 depcheck.detector.importDeclaration
40 ],
41 ignoreMatches: Object.keys(distTsConfig?.compilerOptions?.paths || []),
42 package: { dependencies: allDependencies }
43 }
44
45 const { dependencies: unusedDependencies } = await depcheck(resolve(typesPath), depcheckOptions)
46 console.log(`Removing ${Object.keys(unusedDependencies).length} unused dependencies.`)
47 const dependencies = Object
48 .keys(allDependencies)
49 .filter(dependencyName => !unusedDependencies.includes(dependencyName))
50 .reduce((dependencies, dependencyName) => {
51 dependencies[dependencyName] = allDependencies[dependencyName]
52 return dependencies
53 }, {})
54
55 const { description, version, licence, engines, author, repository } = mainPackageJson
56 const typesPackageJson = {
57 name: '@peertube/peertube-types',
58 description,
59 version,
60 private: false,
61 license: licence,
62 engines,
63 author,
64 repository,
65 dependencies
66 }
67 console.log(`Writing package.json to ${typesDistPackageJsonPath}`)
68 await writeJSON(typesDistPackageJsonPath, typesPackageJson, { spaces: 2 })
69
70 console.log(`Writing git ignore to ${typesDistGitIgnorePath}`)
71 await writeFile(typesDistGitIgnorePath, '*.tsbuildinfo')
72
73 await copyFile(resolve(typesPath, './README.md'), resolve(typesDistPath, './README.md'))
74}
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 @@
1{
2 "extends": "../../../../tsconfig.base.json",
3 "compilerOptions": {
4 "stripInternal": true,
5 "removeComments": false,
6 "emitDeclarationOnly": true,
7 "outDir": "../../dist/client/",
8 "rootDir": "./",
9 "tsBuildInfoFile": "../../dist/tsconfig.client.types.tsbuildinfo"
10 },
11 "references": [
12 { "path": "../../../../client/tsconfig.types.json" }
13 ],
14 "files": ["index.ts"]
15}
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 @@
1export * from '@server/types'
2export * from '@server/types/models'
3export * 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 @@
1import { RegisterServerOptions, Video } from '../dist'
2import { RegisterClientOptions } from '../dist/client'
3
4function register1 ({ registerHook }: RegisterServerOptions) {
5 registerHook({
6 target: 'action:application.listening',
7 handler: () => console.log('hello')
8 })
9}
10
11function register2 ({ registerHook, peertubeHelpers }: RegisterClientOptions) {
12 registerHook({
13 target: 'action:admin-plugin-settings.init',
14 handler: ({ npmName }: { npmName: string }) => {
15 if ('peertube-plugin-transcription' !== npmName) {
16 return
17 }
18 },
19 })
20
21 registerHook({
22 target: 'action:video-watch.video.loaded',
23 handler: ({ video }: { video: Video }) => {
24 fetch(`${peertubeHelpers.getBaseRouterRoute()}/videos/${video.uuid}/captions`, {
25 method: 'PUT',
26 headers: peertubeHelpers.getAuthHeader(),
27 })
28 .then((res) => res.json())
29 .then((data) => console.log('Hi %s.', data))
30 },
31 })
32}
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 @@
1{
2 "extends": "./tsconfig.json",
3 "compilerOptions": {
4 "typeRoots": [
5 "node_modules/@types",
6 "client/node_modules/@types"
7 ],
8 "baseUrl": "./dist",
9 "paths": {
10 "@server/*": [ "server/*" ],
11 "@shared/*": [ "shared/*" ],
12 "@client/*": [ "client/*" ]
13 }
14 }
15}
16
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 @@
1{
2 "extends": "../../tsconfig.base.json",
3 "compilerOptions": {
4 "stripInternal": true,
5 "removeComments": false,
6 "emitDeclarationOnly": true,
7 "outDir": "./dist/",
8 "baseUrl": "./src/",
9 "rootDir": "./src/",
10 "tsBuildInfoFile": "./dist/tsconfig.server.types.tsbuildinfo",
11 "paths": {
12 "@server/*": [ "../../../server/*" ],
13 "@shared/*": [ "../../../shared/*" ],
14 "@client/*": [ "../../../client/src/*" ]
15 }
16 },
17 "references": [
18 { "path": "../../shared/tsconfig.types.json" },
19 { "path": "../../server/tsconfig.types.json" },
20 { "path": "./src/client/tsconfig.json" }
21 ],
22 "files": ["./src/index.ts"]
23}