1
0
Fork 0

refactor(types): create dedicated folder for types package src

fix guide examples and add types package readme

refactor(tsconfig): move back base tsconfig to base directory
This commit is contained in:
lutangar 2021-11-09 13:49:08 +01:00 committed by Chocobozzz
parent 8b03e2ce1a
commit b8fa3e8c73
20 changed files with 131 additions and 55 deletions

2
.gitignore vendored
View File

@ -53,4 +53,4 @@ yarn-error.log
# TypeScript # TypeScript
*.tsbuildinfo *.tsbuildinfo
/types /types/dist/

View File

@ -1 +0,0 @@
export * from './types'

View File

@ -4,15 +4,15 @@
"stripInternal": true, "stripInternal": true,
"removeComments": false, "removeComments": false,
"declaration": true, "declaration": true,
"outDir": "../types/client/", "outDir": "../types/dist/client/",
"emitDeclarationOnly": true, "emitDeclarationOnly": true,
"composite": true, "composite": true,
"rootDir": "src/", "rootDir": "src/",
"tsBuildInfoFile": "../types/client.tsbuildinfo" "tsBuildInfoFile": "../types/dist/tsconfig.client.tsbuildinfo"
}, },
"references": [ "references": [
{ "path": "../shared/tsconfig.types.json" } { "path": "../shared/tsconfig.types.json" }
], ],
"files": ["src/index.ts"], "files": ["src/types/index.ts"],
"include": ["src/index.ts", "src/types/**/*"] "include": ["src/**/*", "src/types/**/*"]
} }

View File

@ -1 +0,0 @@
export * from './server/types'

View File

@ -29,7 +29,7 @@
"build:embed": "bash ./scripts/build/embed.sh", "build:embed": "bash ./scripts/build/embed.sh",
"build:server": "bash ./scripts/build/server.sh", "build:server": "bash ./scripts/build/server.sh",
"build:client": "bash ./scripts/build/client.sh", "build:client": "bash ./scripts/build/client.sh",
"build:types": "tsc -b --verbose tsconfig.types.json", "build:types": "tsc -b --verbose types",
"clean:client": "bash ./scripts/clean/client/index.sh", "clean:client": "bash ./scripts/clean/client/index.sh",
"clean:server:test": "bash ./scripts/clean/server/test.sh", "clean:server:test": "bash ./scripts/clean/server/test.sh",
"i18n:update": "bash ./scripts/i18n/update.sh", "i18n:update": "bash ./scripts/i18n/update.sh",
@ -53,7 +53,7 @@
"test": "bash ./scripts/test.sh", "test": "bash ./scripts/test.sh",
"help": "bash ./scripts/help.sh", "help": "bash ./scripts/help.sh",
"generate-cli-doc": "bash ./scripts/generate-cli-doc.sh", "generate-cli-doc": "bash ./scripts/generate-cli-doc.sh",
"generate-types-package": "ts-node ./scripts/generate-types-package.ts", "generate-types-package": "ts-node ./types/generate-package.ts",
"parse-log": "node ./dist/scripts/parse-log.js", "parse-log": "node ./dist/scripts/parse-log.js",
"prune-storage": "node ./dist/scripts/prune-storage.js", "prune-storage": "node ./dist/scripts/prune-storage.js",
"postinstall": "test -n \"$NOCLIENT\" || (cd client && yarn install --pure-lockfile)", "postinstall": "test -n \"$NOCLIENT\" || (cd client && yarn install --pure-lockfile)",

View File

@ -7,5 +7,6 @@
"references": [ "references": [
{ "path": "../" }, { "path": "../" },
], ],
"files": [],
"exclude": [ ] // Overwrite exclude property "exclude": [ ] // Overwrite exclude property
} }

View File

@ -1,7 +1,7 @@
{ {
"extends": "./tsconfig.json", "extends": "./tsconfig.json",
"compilerOptions": { "compilerOptions": {
"outDir": "../types/server", "outDir": "../types/dist/server",
"stripInternal": true, "stripInternal": true,
"removeComments": false, "removeComments": false,
"emitDeclarationOnly": true "emitDeclarationOnly": true

View File

@ -1,6 +1,7 @@
{ {
"extends": "../tsconfig.base.json", "extends": "../tsconfig.base.json",
"compilerOptions": { "compilerOptions": {
"outDir": "../dist/shared" "outDir": "../dist/shared",
} }
} }

View File

@ -1,7 +1,7 @@
{ {
"extends": "./tsconfig.json", "extends": "./tsconfig.json",
"compilerOptions": { "compilerOptions": {
"outDir": "../types/shared", "outDir": "../types/dist/shared",
"stripInternal": true, "stripInternal": true,
"removeComments": false, "removeComments": false,
"emitDeclarationOnly": true "emitDeclarationOnly": true

View File

@ -889,12 +889,12 @@ If you want to use __Typescript__ see section below.
You can add __PeerTube__ types as dev dependencies: You can add __PeerTube__ types as dev dependencies:
``` ```
npm install --dev @peertube/peertube-types npm install --save-dev @peertube/peertube-types
``` ```
This package exposes *server* definition files by default: This package exposes *server* definition files by default:
```ts ```ts
import { RegisterServerOptions } from '@peertube/peertube-types' import { RegisterServerOptions } from '@peertube/peertube-types/server/types'
export async function register ({ registerHook }: RegisterServerOptions) { export async function register ({ registerHook }: RegisterServerOptions) {
registerHook({ registerHook({
@ -906,14 +906,33 @@ export async function register ({ registerHook }: RegisterServerOptions) {
But it also exposes client types and various models used in __PeerTube__: But it also exposes client types and various models used in __PeerTube__:
```ts ```ts
import { RegisterClientOptions } from '@peertube/peertube-types/client' import { RegisterClientOptions } from '@larriereguichet/peertube-types/client/types';
import { Video } from '@larriereguichet/peertube-types/shared';
export function register ({ registerHook, peertubeHelpers }: RegisterClientOptions) { function register({ registerHook, peertubeHelpers }: RegisterClientOptions) {
registerHook({ registerHook({
target: 'action:application.init', target: 'action:admin-plugin-settings.init',
handler: () => onApplicationInit(peertubeHelpers) 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));
},
});
} }
export { register };
``` ```
> Other types are accessible from the shared path `@peertube/peertube-types/shared`. > Other types are accessible from the shared path `@peertube/peertube-types/shared`.

View File

@ -24,10 +24,10 @@
"client/node_modules/@types" "client/node_modules/@types"
], ],
"baseUrl": "./", "baseUrl": "./",
"outDir": "./dist/",
"paths": { "paths": {
"@server/*": [ "server/*" ], "@server/*": [ "server/*" ],
"@shared/*": [ "shared/*" ] "@shared/*": [ "shared/*" ],
"@client/*": [ "client/src/*" ],
}, },
"resolveJsonModule": true, "resolveJsonModule": true,
"strict": false, "strict": false,

View File

@ -6,7 +6,11 @@
"paths": { "paths": {
"@server/*": [ "server/*" ], "@server/*": [ "server/*" ],
"@shared/*": [ "shared/*" ] "@shared/*": [ "shared/*" ]
} },
"typeRoots": [
"server/typings",
"node_modules/@types"
]
}, },
"references": [ "references": [
{ "path": "./shared" }, { "path": "./shared" },

View File

@ -1,16 +0,0 @@
{
"extends": "./tsconfig.base.json",
"compilerOptions": {
"stripInternal": true,
"removeComments": false,
"emitDeclarationOnly": true,
"outDir": "./types/"
},
"references": [
{ "path": "./shared/tsconfig.types.json" },
{ "path": "./server/tsconfig.types.json" },
{ "path": "./client/tsconfig.types.json" }
],
"files": ["./index.ts"],
}

19
types/README.md Normal file
View File

@ -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.

View File

@ -14,11 +14,12 @@ run()
async function run () { async function run () {
execSync('npm run build:types', { stdio: 'inherit' }) execSync('npm run build:types', { stdio: 'inherit' })
const typesPath = resolve(cwd(), './types/') const typesPath = resolve(cwd(), './types/')
const typesPackageJsonPath = resolve(typesPath, './package.json') const typesDistPath = resolve(cwd(), typesPath, './dist/')
const typesGitIgnorePath = resolve(typesPath, './.gitignore') const typesDistPackageJsonPath = resolve(typesDistPath, './package.json')
const typesDistGitIgnorePath = resolve(typesDistPath, './.gitignore')
const mainPackageJson = await readJson(resolve(cwd(), './package.json')) const mainPackageJson = await readJson(resolve(cwd(), './package.json'))
const tsConfigPath = resolve(cwd(), './tsconfig.json') const distTsConfigPath = resolve(cwd(), typesPath, './tsconfig.dist.json')
const tsConfig = await readJson(tsConfigPath) const distTsConfig = await readJson(distTsConfigPath)
const clientPackageJson = await readJson(resolve(cwd(), './client/package.json')) const clientPackageJson = await readJson(resolve(cwd(), './client/package.json'))
const allDependencies = Object.assign( const allDependencies = Object.assign(
@ -34,7 +35,7 @@ async function run () {
depcheck.detector.requireCallExpression, depcheck.detector.requireCallExpression,
depcheck.detector.importDeclaration depcheck.detector.importDeclaration
], ],
ignoreMatches: Object.keys(tsConfig?.compilerOptions?.paths || []), ignoreMatches: Object.keys(distTsConfig?.compilerOptions?.paths || []),
package: { dependencies: allDependencies } package: { dependencies: allDependencies }
} }
@ -60,19 +61,15 @@ async function run () {
repository, repository,
dependencies dependencies
} }
console.log(`Writing package.json to ${typesPackageJsonPath}`) console.log(`Writing package.json to ${typesDistPackageJsonPath}`)
await writeJSON(typesPackageJsonPath, typesPackageJson, { spaces: 2 }) await writeJSON(typesDistPackageJsonPath, typesPackageJson, { spaces: 2 })
console.log(`Writing git ignore to ${typesGitIgnorePath}`) console.log(`Writing git ignore to ${typesDistGitIgnorePath}`)
await writeFile(typesGitIgnorePath, '*.tsbuildinfo') await writeFile(typesDistGitIgnorePath, '*.tsbuildinfo')
console.log('Copying tsconfig files') console.log('Copying tsconfig files')
await copyFile(tsConfigPath, resolve(typesPath, './tsconfig.json')) await copyFile(distTsConfigPath, resolve(typesDistPath, './tsconfig.json'))
await copyFile(resolve(cwd(), './tsconfig.base.json'), resolve(typesPath, './tsconfig.base.json')) await copyFile(resolve(cwd(), './tsconfig.base.json'), resolve(typesDistPath, './tsconfig.base.json'))
tsConfig.references.map(({ path }) => path).forEach((path) => {
const src = resolve(cwd(), path, '/tsconfig.json') await copyFile(resolve(typesPath, './README.md'), resolve(typesDistPath, './README.md'))
const dest = resolve(typesPath, path, './tsconfig.json')
console.log(`${src} -> ${dest}`)
copyFile(src, dest).catch((e) => console.error(e))
})
} }

View File

@ -0,0 +1 @@
export * from '@client/types'

View File

@ -0,0 +1,12 @@
{
"extends": "../../../tsconfig.base.json",
"compilerOptions": {
"outDir": "../../dist/client/",
"rootDir": "./",
"tsBuildInfoFile": "../../dist/tsconfig.client.types.tsbuildinfo"
},
"references": [
{ "path": "../../../client/tsconfig.types.json" }
],
"files": ["index.ts"]
}

1
types/src/index.ts Normal file
View File

@ -0,0 +1 @@
export * from '@server/types'

16
types/tsconfig.dist.json Normal file
View File

@ -0,0 +1,16 @@
{
"extends": "./tsconfig.base.json",
"compilerOptions": {
"typeRoots": [
"node_modules/@types",
"client/node_modules/@types"
],
"baseUrl": "./",
"paths": {
"@server/*": [ "server/*" ],
"@shared/*": [ "shared/*" ],
"@client/*": [ "client/*" ]
}
}
}

23
types/tsconfig.json Normal file
View File

@ -0,0 +1,23 @@
{
"extends": "../tsconfig.base.json",
"compilerOptions": {
"stripInternal": true,
"removeComments": false,
"emitDeclarationOnly": true,
"outDir": "./dist/",
"baseUrl": "./src/",
"rootDir": "./src/",
"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"],
}