diff options
Diffstat (limited to 'types')
-rw-r--r-- | types/README.md | 19 | ||||
-rw-r--r-- | types/generate-package.ts | 75 | ||||
-rw-r--r-- | types/src/client/index.ts | 1 | ||||
-rw-r--r-- | types/src/client/tsconfig.json | 12 | ||||
-rw-r--r-- | types/src/index.ts | 1 | ||||
-rw-r--r-- | types/tsconfig.dist.json | 16 | ||||
-rw-r--r-- | types/tsconfig.json | 23 |
7 files changed, 147 insertions, 0 deletions
diff --git a/types/README.md b/types/README.md new file mode 100644 index 000000000..adeca48e5 --- /dev/null +++ b/types/README.md | |||
@@ -0,0 +1,19 @@ | |||
1 | # PeerTube typings | ||
2 | |||
3 | These **Typescript** *types* are mainly used to write **PeerTube** plugins. | ||
4 | |||
5 | ## Installation | ||
6 | |||
7 | Npm: | ||
8 | ``` | ||
9 | npm install --save-dev @peertube/peertube-types | ||
10 | ``` | ||
11 | |||
12 | Yarn: | ||
13 | ``` | ||
14 | yarn 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/types/generate-package.ts b/types/generate-package.ts new file mode 100644 index 000000000..e0bdd721a --- /dev/null +++ b/types/generate-package.ts | |||
@@ -0,0 +1,75 @@ | |||
1 | import { copyFile, readJson, writeFile, writeJSON } from 'fs-extra' | ||
2 | import { resolve } from 'path' | ||
3 | import { cwd } from 'process' | ||
4 | import { execSync } from 'child_process' | ||
5 | import depcheck, { PackageDependencies } from 'depcheck' | ||
6 | |||
7 | run() | ||
8 | .then(() => process.exit(0)) | ||
9 | .catch(err => { | ||
10 | console.error(err) | ||
11 | process.exit(-1) | ||
12 | }) | ||
13 | |||
14 | async function run () { | ||
15 | execSync('npm run build:types', { stdio: 'inherit' }) | ||
16 | const typesPath = resolve(cwd(), './types/') | ||
17 | const typesDistPath = resolve(cwd(), typesPath, './dist/') | ||
18 | const typesDistPackageJsonPath = resolve(typesDistPath, './package.json') | ||
19 | const typesDistGitIgnorePath = resolve(typesDistPath, './.gitignore') | ||
20 | const mainPackageJson = await readJson(resolve(cwd(), './package.json')) | ||
21 | const distTsConfigPath = resolve(cwd(), typesPath, './tsconfig.dist.json') | ||
22 | const distTsConfig = await readJson(distTsConfigPath) | ||
23 | const clientPackageJson = await readJson(resolve(cwd(), './client/package.json')) | ||
24 | |||
25 | const allDependencies = Object.assign( | ||
26 | mainPackageJson.dependencies, | ||
27 | mainPackageJson.devDepencies, | ||
28 | clientPackageJson.dependencies | ||
29 | ) as PackageDependencies | ||
30 | |||
31 | // https://github.com/depcheck/depcheck#api | ||
32 | const depcheckOptions = { | ||
33 | parsers: { '**/*.ts': depcheck.parser.typescript }, | ||
34 | detectors: [ | ||
35 | depcheck.detector.requireCallExpression, | ||
36 | depcheck.detector.importDeclaration | ||
37 | ], | ||
38 | ignoreMatches: Object.keys(distTsConfig?.compilerOptions?.paths || []), | ||
39 | package: { dependencies: allDependencies } | ||
40 | } | ||
41 | |||
42 | const { dependencies: unusedDependencies } = await depcheck(resolve(cwd(), './types/'), depcheckOptions) | ||
43 | console.log(`Removing ${Object.keys(unusedDependencies).length} unused dependencies.`) | ||
44 | const dependencies = Object | ||
45 | .keys(allDependencies) | ||
46 | .filter(dependencyName => !unusedDependencies.includes(dependencyName)) | ||
47 | .reduce((dependencies, dependencyName) => { | ||
48 | dependencies[dependencyName] = allDependencies[dependencyName] | ||
49 | return dependencies | ||
50 | }, {}) | ||
51 | |||
52 | const { description, version, licence, engines, author, repository } = mainPackageJson | ||
53 | const typesPackageJson = { | ||
54 | name: '@peertube/peertube-types', | ||
55 | description, | ||
56 | version, | ||
57 | private: false, | ||
58 | license: licence, | ||
59 | engines, | ||
60 | author, | ||
61 | repository, | ||
62 | dependencies | ||
63 | } | ||
64 | console.log(`Writing package.json to ${typesDistPackageJsonPath}`) | ||
65 | await writeJSON(typesDistPackageJsonPath, typesPackageJson, { spaces: 2 }) | ||
66 | |||
67 | console.log(`Writing git ignore to ${typesDistGitIgnorePath}`) | ||
68 | await writeFile(typesDistGitIgnorePath, '*.tsbuildinfo') | ||
69 | |||
70 | console.log('Copying tsconfig files') | ||
71 | await copyFile(distTsConfigPath, resolve(typesDistPath, './tsconfig.json')) | ||
72 | await copyFile(resolve(cwd(), './tsconfig.base.json'), resolve(typesDistPath, './tsconfig.base.json')) | ||
73 | |||
74 | await copyFile(resolve(typesPath, './README.md'), resolve(typesDistPath, './README.md')) | ||
75 | } | ||
diff --git a/types/src/client/index.ts b/types/src/client/index.ts new file mode 100644 index 000000000..5ee10ecb8 --- /dev/null +++ b/types/src/client/index.ts | |||
@@ -0,0 +1 @@ | |||
export * from '@client/types' | |||
diff --git a/types/src/client/tsconfig.json b/types/src/client/tsconfig.json new file mode 100644 index 000000000..199273538 --- /dev/null +++ b/types/src/client/tsconfig.json | |||
@@ -0,0 +1,12 @@ | |||
1 | { | ||
2 | "extends": "../../../tsconfig.base.json", | ||
3 | "compilerOptions": { | ||
4 | "outDir": "../../dist/client/", | ||
5 | "rootDir": "./", | ||
6 | "tsBuildInfoFile": "../../dist/tsconfig.client.types.tsbuildinfo" | ||
7 | }, | ||
8 | "references": [ | ||
9 | { "path": "../../../client/tsconfig.types.json" } | ||
10 | ], | ||
11 | "files": ["index.ts"] | ||
12 | } | ||
diff --git a/types/src/index.ts b/types/src/index.ts new file mode 100644 index 000000000..f1325777f --- /dev/null +++ b/types/src/index.ts | |||
@@ -0,0 +1 @@ | |||
export * from '@server/types' | |||
diff --git a/types/tsconfig.dist.json b/types/tsconfig.dist.json new file mode 100644 index 000000000..d9c3fdfc3 --- /dev/null +++ b/types/tsconfig.dist.json | |||
@@ -0,0 +1,16 @@ | |||
1 | { | ||
2 | "extends": "./tsconfig.base.json", | ||
3 | "compilerOptions": { | ||
4 | "typeRoots": [ | ||
5 | "node_modules/@types", | ||
6 | "client/node_modules/@types" | ||
7 | ], | ||
8 | "baseUrl": "./", | ||
9 | "paths": { | ||
10 | "@server/*": [ "server/*" ], | ||
11 | "@shared/*": [ "shared/*" ], | ||
12 | "@client/*": [ "client/*" ] | ||
13 | } | ||
14 | } | ||
15 | } | ||
16 | |||
diff --git a/types/tsconfig.json b/types/tsconfig.json new file mode 100644 index 000000000..8f09c4a83 --- /dev/null +++ b/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 | "paths": { | ||
11 | "@server/*": [ "../../server/*" ], | ||
12 | "@shared/*": [ "../../shared/*" ], | ||
13 | "@client/*": [ "../../client/src/*" ], | ||
14 | } | ||
15 | }, | ||
16 | "references": [ | ||
17 | { "path": "../shared/tsconfig.types.json" }, | ||
18 | { "path": "../server/tsconfig.types.json" }, | ||
19 | { "path": "./src/client/tsconfig.json" } | ||
20 | ], | ||
21 | "files": ["./src/index.ts"], | ||
22 | } | ||
23 | |||