On the fly TypeScript transpile using ts-node (#26)

Using require hook
This commit is contained in:
Gleb Bahmutov
2017-05-16 17:53:17 -04:00
committed by GitHub
parent 8f21879cc4
commit df3f3fcd42
21 changed files with 79 additions and 49 deletions

View File

@@ -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<any>
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' {

View File

@@ -1,2 +1,6 @@
// @ts-check
// compile TypeScript files on the fly using
// Node require hook project
require('../ts')
module.exports = require("./lib/launcher")

View File

@@ -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,

View File

@@ -1,4 +1,5 @@
import {log} from '../log'
import {NotInstalledError} from '../types'
import execa = require('execa')
import fs = require('fs-extra')

View File

@@ -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')

View File

@@ -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')

View File

@@ -1,5 +1,6 @@
import cp = require('child_process')
import Promise = require('bluebird')
import {NotInstalledError} from '../types'
const execAsync = Promise.promisify(cp.exec)

View File

@@ -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<any>
export type LauncherApi = LauncherLaunch & ExtraLauncherMethods
// all common type definition for this module
export type NotInstalledError = Error & {notInstalled: boolean}
export type BrowserNotFoundError = Error & {specificBrowserNotFound: boolean}

View File

@@ -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",

View File

@@ -1,3 +1,3 @@
test/unit
--compilers coffee:../coffee/register
--compilers coffee:../coffee/register,ts:../ts/register
--recursive

3
packages/ts/index.js Normal file
View File

@@ -0,0 +1,3 @@
// gives anyone access to ts-node
// https://github.com/TypeStrong/ts-node#programmatic-usage
module.exports = require('ts-node')

16
packages/ts/package.json Normal file
View File

@@ -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"
}
}

6
packages/ts/register.js Normal file
View File

@@ -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?

View File

@@ -0,0 +1,2 @@
require('../register')
require('./spec')

2
packages/ts/test/spec.ts Normal file
View File

@@ -0,0 +1,2 @@
const add = (a: number, b: number) => a + b
console.assert(add(10, 2) === 12, '10 + 2 should be 12')

View File

@@ -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"
]
}
}