Skip to content
This repository has been archived by the owner on Aug 22, 2023. It is now read-only.

Commit

Permalink
fix: TS loading improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
jdx committed Jan 28, 2018
1 parent ffdcef8 commit fcae462
Show file tree
Hide file tree
Showing 10 changed files with 93 additions and 5 deletions.
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@
"author": "Jeff Dickey @jdxcode",
"bugs": "https://github.com/dxcli/config/issues",
"dependencies": {
"cli-ux": "^3.3.8",
"debug": "^3.1.0",
"fs-extra": "^5.0.0",
"load-json-file": "^4.0.0",
"lodash": "^4.17.4",
"read-pkg": "^3.0.0"
},
"devDependencies": {
"@dxcli/command": "^0.2.3",
"@dxcli/config": "^0.1.30",
"@dxcli/dev": "^2.0.15",
"@dxcli/engine": "^0.1.15",
Expand Down
8 changes: 5 additions & 3 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,9 @@ export class Config implements IConfig {

private async _tsConfig(): Promise<TSConfig | undefined> {
try {
// ignore if no .git as it's likely not in dev mode
if (!await fs.pathExists(path.join(this.root, '.git'))) return

const tsconfigPath = path.join(this.root, 'tsconfig.json')
const tsconfig = await readJSON(tsconfigPath)
if (!tsconfig || !tsconfig.compilerOptions) return
Expand All @@ -285,9 +288,8 @@ export class Config implements IConfig {
* if there is a tsconfig and the original sources exist, it attempts to require ts-
*/
private async _tsPath(orig: string): Promise<string | undefined> {
if (!orig) return
if (!orig || !this.tsconfig) return
orig = path.join(this.root, orig)
if (!this.tsconfig) return
let {rootDirs, outDir} = this.tsconfig.compilerOptions
if (!rootDirs || !rootDirs.length || !outDir) return
let rootDir = rootDirs[0]
Expand All @@ -302,7 +304,7 @@ export class Config implements IConfig {
// For hooks, it might point to a module, not a file. Something like "./hooks/myhook"
// That file doesn't exist, and the real file is "./hooks/myhook.ts"
// In that case we attempt to resolve to the filename. If it fails it will revert back to the lib path
if (!await fs.pathExists(out)) return require.resolve(out)
if (await fs.pathExists(out) || await fs.pathExists(out + '.ts')) return out
return out
} catch (err) {
debug(err)
Expand Down
5 changes: 5 additions & 0 deletions src/hooks/init.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import cli from 'cli-ux'

export default function () {
cli.log('ran init hook')
}
11 changes: 11 additions & 0 deletions test/fixtures/typescript/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"name": "ts-plugin",
"private": true,
"dxcli": {
"plugins": "./lib/plugins",
"commands": "./lib/commands",
"hooks": {
"init": "./lib/hooks/init"
}
}
}
8 changes: 8 additions & 0 deletions test/fixtures/typescript/src/commands/foo/bar/baz.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import Command from '@dxcli/command'
import cli from 'cli-ux'

export default class extends Command {
async run() {
cli.log('it works!')
}
}
5 changes: 5 additions & 0 deletions test/fixtures/typescript/src/plugins.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import cli from 'cli-ux'

export default function () {
cli.log('loading plugins')
}
11 changes: 11 additions & 0 deletions test/fixtures/typescript/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"compilerOptions": {
"outDir": "./lib",
"rootDirs": [
"./src"
]
},
"include": [
"./src/**/*"
]
}
22 changes: 22 additions & 0 deletions test/typescript.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import {expect, fancy} from 'fancy-test'
import * as path from 'path'

import * as Config from '../src'

const root = path.resolve(__dirname, 'fixtures/typescript')

describe('typescript', () => {
fancy
.it('has props', async () => {
const config = await Config.read({root})
const p = (p: string) => path.join(root, p)
expect(config).to.deep.include({
commandsDir: p('lib/commands'),
commandsDirTS: p('src/commands'),
pluginsModule: p('lib/plugins'),
pluginsModuleTS: p('src/plugins'),
hooks: {init: [p('lib/hooks/init')]},
hooksTS: {init: [p('src/hooks/init')]},
})
})
})
7 changes: 6 additions & 1 deletion tslint.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
{
"extends": "@dxcli/tslint"
"extends": "@dxcli/tslint",
"linterOptions": {
"exclude": [
"test/fixtures/**"
]
}
}
19 changes: 18 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,15 @@
dependencies:
find-up "^2.1.0"

"@dxcli/command@^0.2.3":
version "0.2.3"
resolved "https://registry.yarnpkg.com/@dxcli/command/-/command-0.2.3.tgz#244ffea9939bd3a4b6cd9defe5f8657a5041cb93"
dependencies:
"@dxcli/parser" "^0.0.5"
debug "^3.1.0"
lodash "^4.17.4"
tslib "^1.9.0"

"@dxcli/config@^0.1.30":
version "0.1.30"
resolved "https://registry.yarnpkg.com/@dxcli/config/-/config-0.1.30.tgz#5758c4167c69ad6cfa0cff6cf969452120f0df8c"
Expand Down Expand Up @@ -178,6 +187,14 @@
version "0.0.4"
resolved "https://registry.yarnpkg.com/@dxcli/nyc-config/-/nyc-config-0.0.4.tgz#bd60c089aaa6d5da34e415bab6bc527d8c35a210"

"@dxcli/parser@^0.0.5":
version "0.0.5"
resolved "https://registry.yarnpkg.com/@dxcli/parser/-/parser-0.0.5.tgz#51daf8133e8cec6ff21bfa464c4d58cd46b5ed99"
dependencies:
"@dxcli/screen" "^0.0.1"
chalk "^2.3.0"
lodash "^4.17.4"

"@dxcli/screen@^0.0.1":
version "0.0.1"
resolved "https://registry.yarnpkg.com/@dxcli/screen/-/screen-0.0.1.tgz#9af4e8d0e5a9475e9e4b5f2da775b0447ff72fc2"
Expand Down Expand Up @@ -4437,7 +4454,7 @@ tsconfig@^7.0.0:
strip-bom "^3.0.0"
strip-json-comments "^2.0.0"

tslib@^1.0.0, tslib@^1.7.1, tslib@^1.8.0, tslib@^1.8.1:
tslib@^1.0.0, tslib@^1.7.1, tslib@^1.8.0, tslib@^1.8.1, tslib@^1.9.0:
version "1.9.0"
resolved "https://registry.yarnpkg.com/tslib/-/tslib-1.9.0.tgz#e37a86fda8cbbaf23a057f473c9f4dc64e5fc2e8"

Expand Down

0 comments on commit fcae462

Please sign in to comment.