Init commit
This commit is contained in:
14
packages/types-generator/README.md
Normal file
14
packages/types-generator/README.md
Normal file
@@ -0,0 +1,14 @@
|
||||
# PeerTube typings
|
||||
|
||||
These **Typescript** *types* are mainly used to write **PeerTube** plugins.
|
||||
|
||||
## Installation
|
||||
|
||||
Npm:
|
||||
```
|
||||
npm install --save-dev @peertube/peertube-types
|
||||
```
|
||||
|
||||
## Usage
|
||||
|
||||
> See [contribute-plugins](https://docs.joinpeertube.org/contribute/plugins#typescript) **Typescript** section of the doc.
|
||||
117
packages/types-generator/generate-package.ts
Normal file
117
packages/types-generator/generate-package.ts
Normal file
@@ -0,0 +1,117 @@
|
||||
import { currentDir, root } from '@peertube/peertube-node-utils'
|
||||
import { execSync } from 'child_process'
|
||||
import depcheck, { PackageDependencies } from 'depcheck'
|
||||
import { readJson, remove, writeJSON } from 'fs-extra/esm'
|
||||
import { copyFile, writeFile } from 'fs/promises'
|
||||
import { join, resolve } from 'path'
|
||||
|
||||
if (!process.argv[2]) {
|
||||
console.error('Need version as argument')
|
||||
process.exit(-1)
|
||||
}
|
||||
|
||||
const version = process.argv[2]
|
||||
console.log('Will generate package version %s.', version)
|
||||
|
||||
run()
|
||||
.then(() => process.exit(0))
|
||||
.catch(err => {
|
||||
console.error(err)
|
||||
process.exit(-1)
|
||||
})
|
||||
|
||||
async function run () {
|
||||
const typesPath = currentDir(import.meta.url)
|
||||
const typesDistTMPPath = join(typesPath, 'dist-tmp')
|
||||
|
||||
await remove(typesDistTMPPath)
|
||||
|
||||
const mainPackageJson = await readJson(join(root(), 'package.json'))
|
||||
|
||||
const typesTsConfigPath = join(typesPath, 'tsconfig.types.json')
|
||||
|
||||
const distTmpTsConfigPath = join(typesPath, 'tsconfig.dist-tmp.json')
|
||||
const distTmpTsConfig = await readJson(distTmpTsConfigPath)
|
||||
|
||||
const clientPackageJson = await readJson(join(root(), 'client', 'package.json'))
|
||||
|
||||
const typesDistPath = join(typesPath, 'dist')
|
||||
const rollupConfig = join(typesPath, 'rollup.config.js')
|
||||
|
||||
await remove(typesDistTMPPath)
|
||||
|
||||
execSync(`npm run tsc -- -b ${typesTsConfigPath} --verbose`, { stdio: 'inherit' })
|
||||
// eslint-disable-next-line max-len
|
||||
execSync(`npm run resolve-tspaths -- --project ${distTmpTsConfigPath} --src ${typesDistTMPPath} --out ${typesDistTMPPath}`, { stdio: 'inherit' })
|
||||
|
||||
execSync(`./node_modules/.bin/rollup -c ${rollupConfig}`, { stdio: 'inherit' })
|
||||
await remove(typesDistTMPPath)
|
||||
|
||||
const allDependencies = Object.assign(
|
||||
mainPackageJson.dependencies,
|
||||
mainPackageJson.devDependencies,
|
||||
clientPackageJson.dependencies,
|
||||
clientPackageJson.devDependencies
|
||||
) as PackageDependencies
|
||||
|
||||
const toIgnore = Object.keys(distTmpTsConfig?.compilerOptions?.paths || [])
|
||||
|
||||
// https://github.com/depcheck/depcheck#api
|
||||
const depcheckOptions = {
|
||||
parsers: { '**/*.ts': depcheck.parser.typescript },
|
||||
detectors: [
|
||||
depcheck.detector.requireCallExpression,
|
||||
depcheck.detector.importDeclaration
|
||||
],
|
||||
ignoreMatches: toIgnore,
|
||||
package: { dependencies: allDependencies }
|
||||
}
|
||||
|
||||
const result = await depcheck(typesDistPath, depcheckOptions)
|
||||
|
||||
if (Object.keys(result.invalidDirs).length !== 0) {
|
||||
console.error('Invalid directories detected.', { invalidDirs: result.invalidDirs })
|
||||
process.exit(-1)
|
||||
}
|
||||
|
||||
if (Object.keys(result.invalidFiles).length !== 0) {
|
||||
console.error('Invalid files detected.', { invalidFiles: result.invalidFiles })
|
||||
process.exit(-1)
|
||||
}
|
||||
|
||||
const unusedDependencies = result.dependencies
|
||||
|
||||
console.log(`Removing ${Object.keys(unusedDependencies).length} unused dependencies.`)
|
||||
const dependencies = Object
|
||||
.keys(allDependencies)
|
||||
.filter(dependencyName => !unusedDependencies.includes(dependencyName) && !toIgnore.includes(dependencyName))
|
||||
.reduce((dependencies, dependencyName) => {
|
||||
dependencies[dependencyName] = allDependencies[dependencyName]
|
||||
return dependencies
|
||||
}, {})
|
||||
|
||||
const { description, licence, engines, author, repository } = mainPackageJson
|
||||
const typesPackageJson = {
|
||||
name: '@peertube/peertube-types',
|
||||
description,
|
||||
version,
|
||||
private: false,
|
||||
main: '',
|
||||
license: licence,
|
||||
engines,
|
||||
author,
|
||||
repository,
|
||||
dependencies
|
||||
}
|
||||
|
||||
const typesDistPackageJsonPath = join(typesDistPath, 'package.json')
|
||||
const typesDistGitIgnorePath = join(typesDistPath, '.gitignore')
|
||||
|
||||
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'))
|
||||
}
|
||||
9
packages/types-generator/package.json
Normal file
9
packages/types-generator/package.json
Normal file
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"name": "@peertube/peertube-types-generator",
|
||||
"private": true,
|
||||
"version": "0.0.0",
|
||||
"type": "module",
|
||||
"dependencies": {
|
||||
"@peertube/peertube-core-utils": "workspace:*"
|
||||
}
|
||||
}
|
||||
16
packages/types-generator/rollup.config.js
Normal file
16
packages/types-generator/rollup.config.js
Normal file
@@ -0,0 +1,16 @@
|
||||
import { dts } from 'rollup-plugin-dts'
|
||||
|
||||
const config = [
|
||||
{
|
||||
input: './packages/types-generator/dist-tmp/index.d.ts',
|
||||
output: [ { file: './packages/types-generator/dist/index.d.ts', format: 'es' } ],
|
||||
plugins: [ dts({ tsconfig: './packages/types-generator/tsconfig.dist-tmp.json' }) ],
|
||||
},
|
||||
{
|
||||
input: './packages/types-generator/dist-tmp/client/index.d.ts',
|
||||
output: [ { file: './packages/types-generator/dist/client/index.d.ts', format: 'es' } ],
|
||||
plugins: [ dts({ tsconfig: './packages/types-generator/tsconfig.dist-tmp.json' }) ],
|
||||
}
|
||||
]
|
||||
|
||||
export default config
|
||||
1
packages/types-generator/src/client/index.ts
Normal file
1
packages/types-generator/src/client/index.ts
Normal file
@@ -0,0 +1 @@
|
||||
export * from '@client/types/index.js'
|
||||
19
packages/types-generator/src/client/tsconfig.types.json
Normal file
19
packages/types-generator/src/client/tsconfig.types.json
Normal file
@@ -0,0 +1,19 @@
|
||||
{
|
||||
"extends": "../../../../tsconfig.base.json",
|
||||
"compilerOptions": {
|
||||
"stripInternal": true,
|
||||
"removeComments": false,
|
||||
"emitDeclarationOnly": true,
|
||||
"outDir": "../../dist-tmp/client/",
|
||||
"rootDir": "./",
|
||||
"baseUrl": "./",
|
||||
"tsBuildInfoFile": "../../dist-tmp/tsconfig.client.types.tsbuildinfo",
|
||||
"paths": {
|
||||
"@client/*": [ "../../../../client/src/*" ]
|
||||
}
|
||||
},
|
||||
"references": [
|
||||
{ "path": "../../../../client/tsconfig.types.json" }
|
||||
],
|
||||
"files": [ "./index.ts" ]
|
||||
}
|
||||
11
packages/types-generator/src/index.ts
Normal file
11
packages/types-generator/src/index.ts
Normal file
@@ -0,0 +1,11 @@
|
||||
export * from '@server/types/index.js'
|
||||
export * from '@server/types/models/index.js'
|
||||
export * from '@peertube/peertube-models'
|
||||
|
||||
declare global {
|
||||
namespace Express {
|
||||
interface Request {
|
||||
rawBody: Buffer
|
||||
}
|
||||
}
|
||||
}
|
||||
41
packages/types-generator/tests/test.ts
Normal file
41
packages/types-generator/tests/test.ts
Normal file
@@ -0,0 +1,41 @@
|
||||
import { RegisterServerOptions, Video, MVideo } from '../dist/index.js'
|
||||
import { RegisterClientOptions } from '../dist/client/index.js'
|
||||
|
||||
function register1 ({ registerHook, getRouter }: RegisterServerOptions) {
|
||||
registerHook({
|
||||
target: 'action:application.listening',
|
||||
handler: () => console.log('hello')
|
||||
})
|
||||
|
||||
const router = getRouter()
|
||||
|
||||
router.get('/ping', (req, res) => {
|
||||
console.log(req.rawBody)
|
||||
res.status(200).json({ message: 'pong' })
|
||||
})
|
||||
}
|
||||
|
||||
function register2 ({ registerHook, peertubeHelpers }: RegisterClientOptions) {
|
||||
registerHook({
|
||||
target: 'action:admin-plugin-settings.init',
|
||||
handler: ({ npmName }: { npmName: string }) => {
|
||||
let video: MVideo
|
||||
|
||||
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))
|
||||
}
|
||||
})
|
||||
}
|
||||
18
packages/types-generator/tsconfig.dist-tmp.json
Normal file
18
packages/types-generator/tsconfig.dist-tmp.json
Normal file
@@ -0,0 +1,18 @@
|
||||
{
|
||||
"extends": "./tsconfig.json",
|
||||
"compilerOptions": {
|
||||
"typeRoots": [
|
||||
"node_modules/@types",
|
||||
"client/node_modules/@types"
|
||||
],
|
||||
"baseUrl": "./dist-tmp",
|
||||
"paths": {
|
||||
"@server/*": [ "server/core/*" ],
|
||||
"@client/*": [ "client/*" ],
|
||||
"@peertube/peertube-models": [ "peertube-models" ],
|
||||
"@peertube/peertube-typescript-utils": [ "peertube-typescript-utils" ],
|
||||
"@peertube/peertube-transcription": [ "peertube-transcription" ]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
11
packages/types-generator/tsconfig.json
Normal file
11
packages/types-generator/tsconfig.json
Normal file
@@ -0,0 +1,11 @@
|
||||
{
|
||||
"extends": "../../tsconfig.base.json",
|
||||
"compilerOptions": {
|
||||
"rootDir": ".",
|
||||
"noEmit": true
|
||||
},
|
||||
"files": [ "./generate-package.ts" ],
|
||||
"references": [
|
||||
{ "path": "../node-utils" }
|
||||
]
|
||||
}
|
||||
24
packages/types-generator/tsconfig.types.json
Normal file
24
packages/types-generator/tsconfig.types.json
Normal file
@@ -0,0 +1,24 @@
|
||||
{
|
||||
"extends": "../../tsconfig.base.json",
|
||||
"compilerOptions": {
|
||||
"stripInternal": true,
|
||||
"removeComments": false,
|
||||
"emitDeclarationOnly": true,
|
||||
"sourceMap": false,
|
||||
"outDir": "./dist-tmp/",
|
||||
"baseUrl": "./",
|
||||
"rootDir": "./src",
|
||||
"tsBuildInfoFile": "./dist-tmp/tsconfig.server.types.tsbuildinfo",
|
||||
"paths": {
|
||||
"@server/*": [ "../../server/core/*" ]
|
||||
}
|
||||
},
|
||||
"references": [
|
||||
{ "path": "../models/tsconfig.types.json" },
|
||||
{ "path": "../typescript-utils/tsconfig.types.json" },
|
||||
{ "path": "../transcription/tsconfig.types.json" },
|
||||
{ "path": "../../server/tsconfig.types.json" },
|
||||
{ "path": "./src/client/tsconfig.types.json" }
|
||||
],
|
||||
"files": ["./src/index.ts"]
|
||||
}
|
||||
Reference in New Issue
Block a user