Skip to content

Commit

Permalink
On the fly TypeScript transpile using ts-node (cypress-io#26)
Browse files Browse the repository at this point in the history
Using require hook
  • Loading branch information
bahmutov authored May 16, 2017
1 parent 8f21879 commit df3f3fc
Show file tree
Hide file tree
Showing 21 changed files with 79 additions and 49 deletions.
26 changes: 0 additions & 26 deletions packages/launcher/index.d.ts
Original file line number Diff line number Diff line change
@@ -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' {
Expand Down
4 changes: 4 additions & 0 deletions packages/launcher/index.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
// @ts-check

// compile TypeScript files on the fly using
// Node require hook project
require('../ts')
module.exports = require("./lib/launcher")
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {log} from '../log'
import {NotInstalledError} from '../types'
import execa = require('execa')

import fs = require('fs-extra')
Expand Down
Original file line number Diff line number Diff line change
@@ -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')
Expand Down
Original file line number Diff line number Diff line change
@@ -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')

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import cp = require('child_process')
import Promise = require('bluebird')
import {NotInstalledError} from '../types'

const execAsync = Promise.promisify(cp.exec)

Expand Down
File renamed without changes.
25 changes: 25 additions & 0 deletions packages/launcher/lib/types.ts
Original file line number Diff line number Diff line change
@@ -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}
9 changes: 3 additions & 6 deletions packages/launcher/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand All @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion packages/launcher/test/mocha.opts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
test/unit
--compilers coffee:../coffee/register
--compilers coffee:../coffee/register,ts:../ts/register
--recursive
3 changes: 3 additions & 0 deletions packages/ts/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// gives anyone access to ts-node
// https://github.com/TypeStrong/ts-node#programmatic-usage
module.exports = require('ts-node')
16 changes: 16 additions & 0 deletions packages/ts/package.json
Original file line number Diff line number Diff line change
@@ -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 changes: 6 additions & 0 deletions packages/ts/register.js
Original file line number Diff line number Diff line change
@@ -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?
2 changes: 2 additions & 0 deletions packages/ts/test/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
require('../register')
require('./spec')
2 changes: 2 additions & 0 deletions packages/ts/test/spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
const add = (a: number, b: number) => a + b
console.assert(add(10, 2) === 12, '10 + 2 should be 12')
28 changes: 12 additions & 16 deletions packages/launcher/tsconfig.json → packages/ts/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,20 @@
// "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'. */
// "downlevelIteration": true, /* Provide full support for iterables in 'for-of', spread, and destructuring when targeting 'ES5' or 'ES3'. */
// "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. */
Expand All @@ -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 */
Expand All @@ -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"
]
}
}

0 comments on commit df3f3fc

Please sign in to comment.