From fcae462f59a1da436e1dd1dd8ff06acb43d43425 Mon Sep 17 00:00:00 2001 From: Jeff Dickey <216188+jdxcode@users.noreply.github.com> Date: Sun, 28 Jan 2018 14:05:43 -0800 Subject: [PATCH] fix: TS loading improvements --- package.json | 2 ++ src/config.ts | 8 ++++--- src/hooks/init.ts | 5 +++++ test/fixtures/typescript/package.json | 11 ++++++++++ .../typescript/src/commands/foo/bar/baz.ts | 8 +++++++ test/fixtures/typescript/src/plugins.ts | 5 +++++ test/fixtures/typescript/tsconfig.json | 11 ++++++++++ test/typescript.test.ts | 22 +++++++++++++++++++ tslint.json | 7 +++++- yarn.lock | 19 +++++++++++++++- 10 files changed, 93 insertions(+), 5 deletions(-) create mode 100644 src/hooks/init.ts create mode 100644 test/fixtures/typescript/package.json create mode 100644 test/fixtures/typescript/src/commands/foo/bar/baz.ts create mode 100644 test/fixtures/typescript/src/plugins.ts create mode 100644 test/fixtures/typescript/tsconfig.json create mode 100644 test/typescript.test.ts diff --git a/package.json b/package.json index fe53f37b..456e3d13 100644 --- a/package.json +++ b/package.json @@ -5,6 +5,7 @@ "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", @@ -12,6 +13,7 @@ "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", diff --git a/src/config.ts b/src/config.ts index f897e821..b018d1af 100644 --- a/src/config.ts +++ b/src/config.ts @@ -270,6 +270,9 @@ export class Config implements IConfig { private async _tsConfig(): Promise { 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 @@ -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 { - 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] @@ -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) diff --git a/src/hooks/init.ts b/src/hooks/init.ts new file mode 100644 index 00000000..c11bc2b2 --- /dev/null +++ b/src/hooks/init.ts @@ -0,0 +1,5 @@ +import cli from 'cli-ux' + +export default function () { + cli.log('ran init hook') +} diff --git a/test/fixtures/typescript/package.json b/test/fixtures/typescript/package.json new file mode 100644 index 00000000..91be0b37 --- /dev/null +++ b/test/fixtures/typescript/package.json @@ -0,0 +1,11 @@ +{ + "name": "ts-plugin", + "private": true, + "dxcli": { + "plugins": "./lib/plugins", + "commands": "./lib/commands", + "hooks": { + "init": "./lib/hooks/init" + } + } +} diff --git a/test/fixtures/typescript/src/commands/foo/bar/baz.ts b/test/fixtures/typescript/src/commands/foo/bar/baz.ts new file mode 100644 index 00000000..bfc9a1c8 --- /dev/null +++ b/test/fixtures/typescript/src/commands/foo/bar/baz.ts @@ -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!') + } +} diff --git a/test/fixtures/typescript/src/plugins.ts b/test/fixtures/typescript/src/plugins.ts new file mode 100644 index 00000000..ed5b29a5 --- /dev/null +++ b/test/fixtures/typescript/src/plugins.ts @@ -0,0 +1,5 @@ +import cli from 'cli-ux' + +export default function () { + cli.log('loading plugins') +} diff --git a/test/fixtures/typescript/tsconfig.json b/test/fixtures/typescript/tsconfig.json new file mode 100644 index 00000000..75c61de8 --- /dev/null +++ b/test/fixtures/typescript/tsconfig.json @@ -0,0 +1,11 @@ +{ + "compilerOptions": { + "outDir": "./lib", + "rootDirs": [ + "./src" + ] + }, + "include": [ + "./src/**/*" + ] +} diff --git a/test/typescript.test.ts b/test/typescript.test.ts new file mode 100644 index 00000000..9b872f29 --- /dev/null +++ b/test/typescript.test.ts @@ -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')]}, + }) + }) +}) diff --git a/tslint.json b/tslint.json index 3a427ce1..b8c18543 100644 --- a/tslint.json +++ b/tslint.json @@ -1,3 +1,8 @@ { - "extends": "@dxcli/tslint" + "extends": "@dxcli/tslint", + "linterOptions": { + "exclude": [ + "test/fixtures/**" + ] + } } diff --git a/yarn.lock b/yarn.lock index 99d23d62..5dfe6e7b 100644 --- a/yarn.lock +++ b/yarn.lock @@ -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" @@ -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" @@ -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"