From df3f3fcd4277bd84a5a492193bc35fdaf4ed4ee7 Mon Sep 17 00:00:00 2001 From: Gleb Bahmutov Date: Tue, 16 May 2017 17:53:17 -0400 Subject: [PATCH] On the fly TypeScript transpile using ts-node (#26) Using require hook --- packages/launcher/index.d.ts | 26 ----------------- packages/launcher/index.js | 4 +++ packages/launcher/{src => lib}/browsers.ts | 1 + .../launcher/{src => lib}/darwin/canary.ts | 0 .../launcher/{src => lib}/darwin/chrome.ts | 0 .../launcher/{src => lib}/darwin/chromium.ts | 0 .../launcher/{src => lib}/darwin/index.ts | 0 packages/launcher/{src => lib}/darwin/util.ts | 1 + packages/launcher/{src => lib}/detect.ts | 1 + packages/launcher/{src => lib}/launcher.ts | 1 + packages/launcher/{src => lib}/linux/index.ts | 1 + packages/launcher/{src => lib}/log.ts | 0 packages/launcher/lib/types.ts | 25 +++++++++++++++++ packages/launcher/package.json | 9 ++---- packages/launcher/test/mocha.opts | 2 +- packages/ts/index.js | 3 ++ packages/ts/package.json | 16 +++++++++++ packages/ts/register.js | 6 ++++ packages/ts/test/index.js | 2 ++ packages/ts/test/spec.ts | 2 ++ packages/{launcher => ts}/tsconfig.json | 28 ++++++++----------- 21 files changed, 79 insertions(+), 49 deletions(-) rename packages/launcher/{src => lib}/browsers.ts (94%) rename packages/launcher/{src => lib}/darwin/canary.ts (100%) rename packages/launcher/{src => lib}/darwin/chrome.ts (100%) rename packages/launcher/{src => lib}/darwin/chromium.ts (100%) rename packages/launcher/{src => lib}/darwin/index.ts (100%) rename packages/launcher/{src => lib}/darwin/util.ts (96%) rename packages/launcher/{src => lib}/detect.ts (97%) rename packages/launcher/{src => lib}/launcher.ts (95%) rename packages/launcher/{src => lib}/linux/index.ts (94%) rename packages/launcher/{src => lib}/log.ts (100%) create mode 100644 packages/launcher/lib/types.ts create mode 100644 packages/ts/index.js create mode 100644 packages/ts/package.json create mode 100644 packages/ts/register.js create mode 100644 packages/ts/test/index.js create mode 100644 packages/ts/test/spec.ts rename packages/{launcher => ts}/tsconfig.json (73%) diff --git a/packages/launcher/index.d.ts b/packages/launcher/index.d.ts index 2bc264aed1..e04fa4d267 100644 --- a/packages/launcher/index.d.ts +++ b/packages/launcher/index.d.ts @@ -1,29 +1,3 @@ -// all common type definition for this module - -type NotInstalledError = Error & {notInstalled: boolean} - -type BrowserNotFoundError = Error & {specificBrowserNotFound: boolean} - -interface ExtraLauncherMethods { - update: Function, - detect: Function -} - -type LauncherLaunch = (browsers?: any[]) => Promise - -type LauncherApi = LauncherLaunch & ExtraLauncherMethods - -type Browser = { - name: string, - re: RegExp, - profile: boolean, - binary: string, - executable: string, - version?: string, - majorVersion?: string, - page?: string -} - // missing type definitions for 3rd party libraries // https://glebbahmutov.com/blog/trying-typescript/#manual-types-for-3rd-party-libraries declare module 'execa' { diff --git a/packages/launcher/index.js b/packages/launcher/index.js index 378652de30..a947de0d7f 100644 --- a/packages/launcher/index.js +++ b/packages/launcher/index.js @@ -1,2 +1,6 @@ // @ts-check + +// compile TypeScript files on the fly using +// Node require hook project +require('../ts') module.exports = require("./lib/launcher") diff --git a/packages/launcher/src/browsers.ts b/packages/launcher/lib/browsers.ts similarity index 94% rename from packages/launcher/src/browsers.ts rename to packages/launcher/lib/browsers.ts index b9e5e7f9db..e687056e4e 100644 --- a/packages/launcher/src/browsers.ts +++ b/packages/launcher/lib/browsers.ts @@ -1,6 +1,7 @@ import {log} from './log' import {find, map} from 'lodash' import cp = require('child_process') +import {Browser, BrowserNotFoundError} from './types' type FoundBrowser = { name: string, diff --git a/packages/launcher/src/darwin/canary.ts b/packages/launcher/lib/darwin/canary.ts similarity index 100% rename from packages/launcher/src/darwin/canary.ts rename to packages/launcher/lib/darwin/canary.ts diff --git a/packages/launcher/src/darwin/chrome.ts b/packages/launcher/lib/darwin/chrome.ts similarity index 100% rename from packages/launcher/src/darwin/chrome.ts rename to packages/launcher/lib/darwin/chrome.ts diff --git a/packages/launcher/src/darwin/chromium.ts b/packages/launcher/lib/darwin/chromium.ts similarity index 100% rename from packages/launcher/src/darwin/chromium.ts rename to packages/launcher/lib/darwin/chromium.ts diff --git a/packages/launcher/src/darwin/index.ts b/packages/launcher/lib/darwin/index.ts similarity index 100% rename from packages/launcher/src/darwin/index.ts rename to packages/launcher/lib/darwin/index.ts diff --git a/packages/launcher/src/darwin/util.ts b/packages/launcher/lib/darwin/util.ts similarity index 96% rename from packages/launcher/src/darwin/util.ts rename to packages/launcher/lib/darwin/util.ts index 4265381d8e..498769caee 100644 --- a/packages/launcher/src/darwin/util.ts +++ b/packages/launcher/lib/darwin/util.ts @@ -1,4 +1,5 @@ import {log} from '../log' +import {NotInstalledError} from '../types' import execa = require('execa') import fs = require('fs-extra') diff --git a/packages/launcher/src/detect.ts b/packages/launcher/lib/detect.ts similarity index 97% rename from packages/launcher/src/detect.ts rename to packages/launcher/lib/detect.ts index 1ac7c35f37..70d6e4f0ff 100644 --- a/packages/launcher/src/detect.ts +++ b/packages/launcher/lib/detect.ts @@ -1,6 +1,7 @@ import {linuxBrowser} from './linux' import darwin from './darwin' import {log} from './log' +import {Browser, NotInstalledError} from './types' import _ = require('lodash') import os = require('os') diff --git a/packages/launcher/src/launcher.ts b/packages/launcher/lib/launcher.ts similarity index 95% rename from packages/launcher/src/launcher.ts rename to packages/launcher/lib/launcher.ts index 2e00b343ad..498e54b9b1 100644 --- a/packages/launcher/src/launcher.ts +++ b/packages/launcher/lib/launcher.ts @@ -1,6 +1,7 @@ import {writeJson} from 'fs-extra' import {launch} from './browsers' import detect from './detect' +import {Browser, LauncherApi} from './types' const Promise = require('bluebird') diff --git a/packages/launcher/src/linux/index.ts b/packages/launcher/lib/linux/index.ts similarity index 94% rename from packages/launcher/src/linux/index.ts rename to packages/launcher/lib/linux/index.ts index 1451f980d6..a435b920ea 100644 --- a/packages/launcher/src/linux/index.ts +++ b/packages/launcher/lib/linux/index.ts @@ -1,5 +1,6 @@ import cp = require('child_process') import Promise = require('bluebird') +import {NotInstalledError} from '../types' const execAsync = Promise.promisify(cp.exec) diff --git a/packages/launcher/src/log.ts b/packages/launcher/lib/log.ts similarity index 100% rename from packages/launcher/src/log.ts rename to packages/launcher/lib/log.ts diff --git a/packages/launcher/lib/types.ts b/packages/launcher/lib/types.ts new file mode 100644 index 0000000000..09968fc510 --- /dev/null +++ b/packages/launcher/lib/types.ts @@ -0,0 +1,25 @@ +export type Browser = { + name: string, + re: RegExp, + profile: boolean, + binary: string, + executable: string, + version?: string, + majorVersion?: string, + page?: string +} + +interface ExtraLauncherMethods { + update: Function, + detect: Function +} + +type LauncherLaunch = (browsers?: any[]) => Promise + +export type LauncherApi = LauncherLaunch & ExtraLauncherMethods + +// all common type definition for this module + +export type NotInstalledError = Error & {notInstalled: boolean} + +export type BrowserNotFoundError = Error & {specificBrowserNotFound: boolean} diff --git a/packages/launcher/package.json b/packages/launcher/package.json index bd3009f289..6cb2396e4d 100644 --- a/packages/launcher/package.json +++ b/packages/launcher/package.json @@ -7,10 +7,8 @@ "scripts": { "clean-deps": "rm -rf node_modules", "clean-all": "npm run clean-deps", - "lint": "tslint --type-check --project . --fix --format stylish src/**/*.ts", - "build": "tsc", - "build-dev": "npm run build", - "prebuild": "npm run lint", + "lint": "tslint --type-check --project ../ts --fix --format stylish launcher/lib/*.ts launcher/lib/**/*.ts", + "build-dev": "npm run lint", "test": "mocha", "build-and-test": "npm run build && npm test" }, @@ -36,8 +34,7 @@ "sinon": "^1.17.3", "sinon-chai": "^2.8.0", "tslint": "^5.2.0", - "tslint-config-standard": "^5.0.2", - "typescript": "^2.3.2" + "tslint-config-standard": "^5.0.2" }, "dependencies": { "bluebird": "^3.3.5", diff --git a/packages/launcher/test/mocha.opts b/packages/launcher/test/mocha.opts index e1451697de..d2d238db08 100644 --- a/packages/launcher/test/mocha.opts +++ b/packages/launcher/test/mocha.opts @@ -1,3 +1,3 @@ test/unit ---compilers coffee:../coffee/register +--compilers coffee:../coffee/register,ts:../ts/register --recursive diff --git a/packages/ts/index.js b/packages/ts/index.js new file mode 100644 index 0000000000..84d2d95990 --- /dev/null +++ b/packages/ts/index.js @@ -0,0 +1,3 @@ +// gives anyone access to ts-node +// https://github.com/TypeStrong/ts-node#programmatic-usage +module.exports = require('ts-node') diff --git a/packages/ts/package.json b/packages/ts/package.json new file mode 100644 index 0000000000..9653e306e4 --- /dev/null +++ b/packages/ts/package.json @@ -0,0 +1,16 @@ +{ + "name": "ts", + "version": "1.0.0", + "description": "TypeScript runtime Node hook", + "main": "index.js", + "scripts": { + "test": "node test" + }, + "keywords": [], + "author": "", + "license": "ISC", + "devDependencies": { + "ts-node": "^3.0.4", + "typescript": "^2.3.2" + } +} diff --git a/packages/ts/register.js b/packages/ts/register.js new file mode 100644 index 0000000000..a37da05dd8 --- /dev/null +++ b/packages/ts/register.js @@ -0,0 +1,6 @@ +// register TypeScript Node require hook +// https://github.com/TypeStrong/ts-node#programmatic-usage +require('ts-node/register') + +// do we need to prevent any other TypeScript hooks? +// like ../coffee/register.js does? diff --git a/packages/ts/test/index.js b/packages/ts/test/index.js new file mode 100644 index 0000000000..906c30ead7 --- /dev/null +++ b/packages/ts/test/index.js @@ -0,0 +1,2 @@ +require('../register') +require('./spec') diff --git a/packages/ts/test/spec.ts b/packages/ts/test/spec.ts new file mode 100644 index 0000000000..0df0f9cc07 --- /dev/null +++ b/packages/ts/test/spec.ts @@ -0,0 +1,2 @@ +const add = (a: number, b: number) => a + b +console.assert(add(10, 2) === 12, '10 + 2 should be 12') diff --git a/packages/launcher/tsconfig.json b/packages/ts/tsconfig.json similarity index 73% rename from packages/launcher/tsconfig.json rename to packages/ts/tsconfig.json index a3cecafde8..9673bf82db 100644 --- a/packages/launcher/tsconfig.json +++ b/packages/ts/tsconfig.json @@ -10,8 +10,8 @@ // "declaration": true, /* Generates corresponding '.d.ts' file. */ // "sourceMap": true, /* Generates corresponding '.map' file. */ // "outFile": "./", /* Concatenate and emit output to single file. */ - "outDir": "./lib", /* Redirect output structure to the directory. */ - // "rootDir": "./src", /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */ + // "outDir": "./", /* Redirect output structure to the directory. */ + // "rootDir": "./", /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */ // "removeComments": true, /* Do not emit comments to output. */ // "noEmit": true, /* Do not emit outputs. */ // "importHelpers": true, /* Import emit helpers from 'tslib'. */ @@ -19,11 +19,11 @@ // "isolatedModules": true, /* Transpile each file as a separate module (similar to 'ts.transpileModule'). */ /* Strict Type-Checking Options */ - "strict": true, /* Enable all strict type-checking options. */ - "noImplicitAny": true, /* Raise error on expressions and declarations with an implied 'any' type. */ - "strictNullChecks": true, /* Enable strict null checks. */ - "noImplicitThis": true, /* Raise error on 'this' expressions with an implied 'any' type. */ - "alwaysStrict": true, /* Parse in strict mode and emit "use strict" for each source file. */ + "strict": true, /* Enable all strict type-checking options. */ + // "noImplicitAny": true, /* Raise error on expressions and declarations with an implied 'any' type. */ + // "strictNullChecks": true, /* Enable strict null checks. */ + // "noImplicitThis": true, /* Raise error on 'this' expressions with an implied 'any' type. */ + // "alwaysStrict": true, /* Parse in strict mode and emit "use strict" for each source file. */ /* Additional Checks */ "noUnusedLocals": true, /* Report errors on unused locals. */ @@ -32,12 +32,12 @@ "noFallthroughCasesInSwitch": true, /* Report errors for fallthrough cases in switch statement. */ /* Module Resolution Options */ - "moduleResolution": "node", /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */ - "baseUrl": "./src" /* Base directory to resolve non-absolute module names. */ + "moduleResolution": "node" /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */ + // "baseUrl": "./", /* Base directory to resolve non-absolute module names. */ // "paths": {}, /* A series of entries which re-map imports to lookup locations relative to the 'baseUrl'. */ // "rootDirs": [], /* List of root folders whose combined content represents the structure of the project at runtime. */ - // "typeRoots": [] /* List of folders to include type definitions from. */ - // "types": [] /* Type declaration files to be included in compilation. */ + // "typeRoots": [], /* List of folders to include type definitions from. */ + // "types": [], /* Type declaration files to be included in compilation. */ // "allowSyntheticDefaultImports": true, /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */ /* Source Map Options */ @@ -49,9 +49,5 @@ /* Experimental Options */ // "experimentalDecorators": true, /* Enables experimental support for ES7 decorators. */ // "emitDecoratorMetadata": true, /* Enables experimental support for emitting type metadata for decorators. */ - }, - "include": [ - "./src/*.ts", - "./index.d.ts" - ] + } }