Skip to content

Commit

Permalink
feat: add @d-ts/bin package, close #56
Browse files Browse the repository at this point in the history
  • Loading branch information
JounQin committed Sep 21, 2019
1 parent d2184e9 commit 7bc08d7
Show file tree
Hide file tree
Showing 13 changed files with 294 additions and 5 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
*.log
*.tsbuildinfo
.changelog
.type-coverage
lib
Expand Down
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ install: yarn --frozen-lockfile

script:
- yarn lint
- yarn build
- yarn test
- yarn type-coverage

Expand Down
6 changes: 6 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
"packages/*"
],
"scripts": {
"build:ts": "tsc -P packages/bin/tsconfig.json",
"build:bundle": "r -i ''",
"build": "run-p build:*",
"lint:es": "eslint . --ext js,md,mdx,ts,tsx -f friendly",
"lint:ts": "tslint -p . -t stylish",
"lint": "run-p lint:*",
Expand All @@ -23,6 +26,7 @@
"@1stg/husky-config": "^0.3.0",
"@1stg/lint-staged": "^0.7.4",
"@1stg/prettier-config": "^0.2.0",
"@1stg/rollup-config": "^0.9.4",
"@1stg/tsconfig": "^0.5.1",
"@1stg/tslint-config": "^0.3.2",
"@babel/core": "^7.6.0",
Expand All @@ -36,6 +40,7 @@
"lint-staged": "^9.2.5",
"npm-run-all": "^4.1.5",
"prettier": "^1.18.2",
"rollup": "^1.21.4",
"ts-node": "^8.4.1",
"tslib": "^1.10.0",
"tslint": "^5.20.0",
Expand All @@ -50,6 +55,7 @@
},
"eslintIgnore": [
"CHANGELOG.md",
"lib",
"!/.*.js"
],
"prettier": "@1stg/prettier-config"
Expand Down
5 changes: 5 additions & 0 deletions packages/bin/cli.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/usr/bin/env node
const path = require('path')
module.exports = require('./lib/cjs')(
path.resolve(process.argv[1], '../../@d-ts'),
)
29 changes: 29 additions & 0 deletions packages/bin/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"name": "@d-ts/bin",
"version": "0.0.0",
"description": "Link @d-ts packages to @types automatically",
"repository": "https://github.com/rx-ts/types/blob/master/packages/bin",
"author": "JounQin <admin@1stg.me>",
"license": "MIT",
"engines": {
"node": "*"
},
"bin": {
"t": "cli.js"
},
"main": "lib/cjs",
"module": "lib",
"fesm5": "lib/esm",
"es2015": "lib/es2015",
"types": "lib",
"files": [
"cli.js",
"lib"
],
"scripts": {
"postinstall": "t || exit 0"
},
"publishConfig": {
"access": "public"
}
}
41 changes: 41 additions & 0 deletions packages/bin/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import fs from 'fs'
import { resolve } from 'path'

const PKG_PREFIX = '@d-ts/'
const PRIMARY_PKG = PKG_PREFIX + 'bin'

const link = (dtsDir: string, pkgName: string) => {
if (PKG_PREFIX + pkgName === PRIMARY_PKG) {
return
}

const srcPkg = resolve(dtsDir, pkgName)
const paths = [srcPkg, resolve(dtsDir, '../@types', pkgName)] as const

try {
fs.linkSync(...paths)
} catch (e) {
fs.symlinkSync(...(paths.concat('dir') as [string, string, 'dir']))
}
}

export default (dtsDir: string) => {
const pkg = resolve('package.json')

if (!fs.existsSync(pkg)) {
return
}

// eslint-disable-next-line @typescript-eslint/no-require-imports
const pkgName = require(pkg).name
if (!pkgName.startsWith(PKG_PREFIX)) {
return
}

const linkDts = link.bind(null, dtsDir)
if (pkgName === PRIMARY_PKG) {
fs.readdirSync(dtsDir).forEach(linkDts)
} else {
linkDts(pkgName.replace(PKG_PREFIX, ''))
}
}
8 changes: 8 additions & 0 deletions packages/bin/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"extends": "@1stg/tsconfig/tsconfig",
"compilerOptions": {
"importHelpers": false,
"rootDir": "src",
"outDir": "lib"
}
}
3 changes: 3 additions & 0 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
"typings",
"definitions"
],
"scripts": {
"postinstall": "t || exit 0"
},
"publishConfig": {
"access": "public"
}
Expand Down
3 changes: 3 additions & 0 deletions packages/vue/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
"typings",
"definitions"
],
"scripts": {
"postinstall": "t || exit 0"
},
"publishConfig": {
"access": "public"
}
Expand Down
6 changes: 5 additions & 1 deletion scripts/dtslint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,14 @@ import { promisify } from 'util'
const exec = promisify(_exec)
const readdir = promisify(_readdir)

const IGNORED_PKGS = ['bin']

readdir('packages')
.then(pkgs =>
Promise.all(
pkgs.map(pkg => exec(`yarn dtslint --expectOnly packages/${pkg}`)),
pkgs
.filter(pkg => !IGNORED_PKGS.includes(pkg))
.map(pkg => exec(`yarn dtslint --expectOnly packages/${pkg}`)),
),
)
.catch(e => {
Expand Down
4 changes: 3 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@
"extends": "@1stg/tsconfig/tsconfig",
"compilerOptions": {
"baseUrl": ".",
"module": "commonjs",
"incremental": false,
"module": "commonjs",
"outDir": "lib",
"skipLibCheck": true,
"typeRoots": ["node_modules/@types", "packages"]
}
}
3 changes: 3 additions & 0 deletions tslint.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,8 @@
"rules": {
"no-reference": false,
"no-single-declare-module": false
},
"linterOptions": {
"exclude": ["node_modules/**", "**/lib/**"]
}
}
Loading

0 comments on commit 7bc08d7

Please sign in to comment.