Skip to content

Commit 7bc08d7

Browse files
committed
feat: add @d-ts/bin package, close #56
1 parent d2184e9 commit 7bc08d7

File tree

13 files changed

+294
-5
lines changed

13 files changed

+294
-5
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
*.log
2+
*.tsbuildinfo
23
.changelog
34
.type-coverage
45
lib

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ install: yarn --frozen-lockfile
1818

1919
script:
2020
- yarn lint
21+
- yarn build
2122
- yarn test
2223
- yarn type-coverage
2324

package.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010
"packages/*"
1111
],
1212
"scripts": {
13+
"build:ts": "tsc -P packages/bin/tsconfig.json",
14+
"build:bundle": "r -i ''",
15+
"build": "run-p build:*",
1316
"lint:es": "eslint . --ext js,md,mdx,ts,tsx -f friendly",
1417
"lint:ts": "tslint -p . -t stylish",
1518
"lint": "run-p lint:*",
@@ -23,6 +26,7 @@
2326
"@1stg/husky-config": "^0.3.0",
2427
"@1stg/lint-staged": "^0.7.4",
2528
"@1stg/prettier-config": "^0.2.0",
29+
"@1stg/rollup-config": "^0.9.4",
2630
"@1stg/tsconfig": "^0.5.1",
2731
"@1stg/tslint-config": "^0.3.2",
2832
"@babel/core": "^7.6.0",
@@ -36,6 +40,7 @@
3640
"lint-staged": "^9.2.5",
3741
"npm-run-all": "^4.1.5",
3842
"prettier": "^1.18.2",
43+
"rollup": "^1.21.4",
3944
"ts-node": "^8.4.1",
4045
"tslib": "^1.10.0",
4146
"tslint": "^5.20.0",
@@ -50,6 +55,7 @@
5055
},
5156
"eslintIgnore": [
5257
"CHANGELOG.md",
58+
"lib",
5359
"!/.*.js"
5460
],
5561
"prettier": "@1stg/prettier-config"

packages/bin/cli.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#!/usr/bin/env node
2+
const path = require('path')
3+
module.exports = require('./lib/cjs')(
4+
path.resolve(process.argv[1], '../../@d-ts'),
5+
)

packages/bin/package.json

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
{
2+
"name": "@d-ts/bin",
3+
"version": "0.0.0",
4+
"description": "Link @d-ts packages to @types automatically",
5+
"repository": "https://github.com/rx-ts/types/blob/master/packages/bin",
6+
"author": "JounQin <admin@1stg.me>",
7+
"license": "MIT",
8+
"engines": {
9+
"node": "*"
10+
},
11+
"bin": {
12+
"t": "cli.js"
13+
},
14+
"main": "lib/cjs",
15+
"module": "lib",
16+
"fesm5": "lib/esm",
17+
"es2015": "lib/es2015",
18+
"types": "lib",
19+
"files": [
20+
"cli.js",
21+
"lib"
22+
],
23+
"scripts": {
24+
"postinstall": "t || exit 0"
25+
},
26+
"publishConfig": {
27+
"access": "public"
28+
}
29+
}

packages/bin/src/index.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import fs from 'fs'
2+
import { resolve } from 'path'
3+
4+
const PKG_PREFIX = '@d-ts/'
5+
const PRIMARY_PKG = PKG_PREFIX + 'bin'
6+
7+
const link = (dtsDir: string, pkgName: string) => {
8+
if (PKG_PREFIX + pkgName === PRIMARY_PKG) {
9+
return
10+
}
11+
12+
const srcPkg = resolve(dtsDir, pkgName)
13+
const paths = [srcPkg, resolve(dtsDir, '../@types', pkgName)] as const
14+
15+
try {
16+
fs.linkSync(...paths)
17+
} catch (e) {
18+
fs.symlinkSync(...(paths.concat('dir') as [string, string, 'dir']))
19+
}
20+
}
21+
22+
export default (dtsDir: string) => {
23+
const pkg = resolve('package.json')
24+
25+
if (!fs.existsSync(pkg)) {
26+
return
27+
}
28+
29+
// eslint-disable-next-line @typescript-eslint/no-require-imports
30+
const pkgName = require(pkg).name
31+
if (!pkgName.startsWith(PKG_PREFIX)) {
32+
return
33+
}
34+
35+
const linkDts = link.bind(null, dtsDir)
36+
if (pkgName === PRIMARY_PKG) {
37+
fs.readdirSync(dtsDir).forEach(linkDts)
38+
} else {
39+
linkDts(pkgName.replace(PKG_PREFIX, ''))
40+
}
41+
}

packages/bin/tsconfig.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"extends": "@1stg/tsconfig/tsconfig",
3+
"compilerOptions": {
4+
"importHelpers": false,
5+
"rootDir": "src",
6+
"outDir": "lib"
7+
}
8+
}

packages/core/package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717
"typings",
1818
"definitions"
1919
],
20+
"scripts": {
21+
"postinstall": "t || exit 0"
22+
},
2023
"publishConfig": {
2124
"access": "public"
2225
}

packages/vue/package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@
2020
"typings",
2121
"definitions"
2222
],
23+
"scripts": {
24+
"postinstall": "t || exit 0"
25+
},
2326
"publishConfig": {
2427
"access": "public"
2528
}

scripts/dtslint.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,14 @@ import { promisify } from 'util'
55
const exec = promisify(_exec)
66
const readdir = promisify(_readdir)
77

8+
const IGNORED_PKGS = ['bin']
9+
810
readdir('packages')
911
.then(pkgs =>
1012
Promise.all(
11-
pkgs.map(pkg => exec(`yarn dtslint --expectOnly packages/${pkg}`)),
13+
pkgs
14+
.filter(pkg => !IGNORED_PKGS.includes(pkg))
15+
.map(pkg => exec(`yarn dtslint --expectOnly packages/${pkg}`)),
1216
),
1317
)
1418
.catch(e => {

0 commit comments

Comments
 (0)