Skip to content

Commit

Permalink
Initial work at handling compilation of babel/ts
Browse files Browse the repository at this point in the history
  • Loading branch information
orta committed Aug 11, 2017
1 parent fd8cc33 commit a7f7249
Show file tree
Hide file tree
Showing 7 changed files with 89 additions and 122 deletions.
76 changes: 13 additions & 63 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -1,65 +1,15 @@
{
"version": "0.2.0",
"configurations": [{
"name": "Launch Run",
"type": "node",
"request": "launch",
"program": "${workspaceRoot}/distribution/commands/danger-run.js",
"stopOnEntry": false,
"args": [],
"envFile": "${workspaceRoot}/env/development.env",
"cwd": "${workspaceRoot}",
"preLaunchTask": "build",
"runtimeExecutable": null,
"runtimeArgs": [
"--nolazy"
],
"env": {
"NODE_ENV": "development"
},
"console": "internalConsole",
"sourceMaps": true,
"outFiles": [
"${workspaceRoot}/distribution"
]
}, {
"name": "Launch PR",
"type": "node",
"request": "launch",
"program": "${workspaceRoot}/distribution/commands/danger-pr.js",
"stopOnEntry": false,
"args": [
// pass a GitHub PR URL below for testing
],
"envFile": "${workspaceRoot}/env/development.env",
"cwd": "${workspaceRoot}",
"preLaunchTask": "build",
"runtimeExecutable": null,
"runtimeArgs": [
"--nolazy"
],
"env": {
"NODE_ENV": "development"
},
"console": "internalConsole",
"sourceMaps": true,
"outFiles": [
"${workspaceRoot}/distribution/**/*.js"
]
}, {
"name": "Run Tests With Debugger (slower, use npm run watch for normal work)",
"type": "node",
"request": "launch",
"port": 5858,
"address": "localhost",
"stopOnEntry": false,
"runtimeExecutable": null,
"runtimeArgs": [
"--debug-brk",
"./node_modules/.bin/jest",
"-i"
],
"cwd": "${workspaceRoot}",
"sourceMaps": true
}]
"version": "0.2.0",
"configurations": [
{
"name": "Debug Jest",
"type": "node",
"request": "launch",
"runtimeArgs": ["./node_modules/.bin/jest", "-i", "--env", "jest-environment-node-debug"],
"cwd": "${workspaceRoot}",
"protocol": "inspector",
"console": "internalConsole",
"sourceMaps": true
}
]
}
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
"husky": "^0.14.0",
"in-publish": "^2.0.0",
"jest": "^20.0.4",
"jest-environment-node-debug": "^2.0.0",
"lint-staged": "^4.0.0",
"madge": "^2.0.0",
"prettier": "^1.5.3",
Expand Down Expand Up @@ -98,7 +99,7 @@
"parse-diff": "^0.4.0",
"parse-link-header": "^1.0.1",
"rfc6902": "^1.3.0",
"vm2": "^3.4.6",
"vm2": "orta/vm2#compilers",
"voca": "^1.2.0"
},
"optionalDependencies": {}
Expand Down
28 changes: 21 additions & 7 deletions source/ambient.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,26 +16,40 @@ declare module "*/package.json"

declare module "vm2" {
export interface VMRequire {
external?: boolean
builtin?: any[]
rooty?: string
mock?: any
builtin?: string[]
context?: "host" | "sandbox"
external?: boolean
import?: string[]
root?: string
mock?: any
}

/**
* A custom compiler function for all of the JS that comes
* into the VM
*/
type CompilerFunction = (code: string, filename: string) => string

export interface VMOptions {
timeout?: number
compiler?: "javascript" | "coffeescript" | CompilerFunction
sandbox?: any
timeout?: number
}

export interface NodeVMOptions extends VMOptions {
console?: "inherit" | "redirect"
compiler?: "javascript" | "coffeescript"
require?: true | VMRequire
nesting?: boolean
wrapper?: "commonjs" | "none"
}

export class NodeVM {
constructor(options?: NodeVMOptions)
run(js: string, path: string): any
}

export class VM {
constructor(options?: VMOptions)
run(js: string, path: string): NodeVM
run(js: string, path: string): any
}
}
61 changes: 31 additions & 30 deletions source/runner/DangerfileRunnerTwo.ts
Original file line number Diff line number Diff line change
@@ -1,34 +1,49 @@
import * as fs from "fs"
import * as path from "path"

import { DangerResults } from "../dsl/DangerResults"
import { DangerContext } from "../runner/Dangerfile"
import { Path } from "./types"

import { NodeVM, VMOptions } from "vm2"
import { NodeVM, NodeVMOptions } from "vm2"

/**
* Executes a Dangerfile at a specific path, with a context.
* The values inside a Danger context are applied as globals to the Dangerfiles runtime.
*
* @param {DangerContext} dangerfileContext the global danger context
*/
export async function createDangerfileRuntimeEnvironment(dangerfileContext: DangerContext): Promise<VMOptions> {
const context = dangerfileContext

const sandbox = {}
// Adds things like fail, warn ... to global
for (const prop in context) {
if (context.hasOwnProperty(prop)) {
const anyContext: any = context
sandbox[prop] = anyContext[prop]
}
export async function createDangerfileRuntimeEnvironment(dangerfileContext: DangerContext): Promise<NodeVMOptions> {
// if (transpile === "typescript") {
// content = typescriptify(content)
// } else if (transpile === "babel") {
// content = babelify(content, filename)
// }

const sandbox = {
...dangerfileContext,
}

return {
require: {
external: true,
},
sandbox,
compiler: (code, filename) => {
const filetype = path.extname(filename)
// Add a check for TSC/babel etc
if (filetype.startsWith(".ts")) {
console.log("TSX")
return typescriptify(code)
} else if (filetype === ".js") {
// TODO: Check for babelrc
console.log("Babel")
const output = babelify(code, filename)
console.log(output)
return output
}

return code
},
}
}

Expand All @@ -42,21 +57,12 @@ export type TranspileType = null | "babel" | "typescript"
* @param {any} environment the results of createDangerfileRuntimeEnvironment
* @returns {DangerResults} the results of the run
*/
export async function runDangerfileEnvironment(
filename: Path,
environment: VMOptions,
transpile: TranspileType
): Promise<DangerResults> {
export async function runDangerfileEnvironment(filename: Path, environment: NodeVMOptions): Promise<DangerResults> {
const vm = new NodeVM(environment)

// Require our dangerfile
const originalContents = (await readDangerfile(filename)) as string
let content = cleanDangerfile(originalContents)
if (transpile === "typescript") {
content = typescriptify(content)
} else if (transpile === "babel") {
content = await babelify(content, filename)
}

vm.run(content, filename)

Expand Down Expand Up @@ -101,7 +107,7 @@ const typescriptify = (content: string): string => {
return result.outputText
}

const babelify = (content: string, filename: string): Promise<string> => {
const babelify = (content: string, filename: string): string => {
const babel = require("babel-core") // tslint:disable-line
const fileOpts = {
filename,
Expand All @@ -111,13 +117,8 @@ const babelify = (content: string, filename: string): Promise<string> => {
sourceMapTarget: null,
}

return new Promise(res => {
console.log("BABEL")
babel.transformFileSync(content, fileOpts, (result: any) => {
console.log("Done")
res(result.code)
})
})
const result = babel.transform(content, fileOpts)
return result.code
}

const readDangerfile = async (filename: string) =>
Expand Down
14 changes: 5 additions & 9 deletions source/runner/_tests/_danger_runner_two.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ describe("with fixtures", () => {
it("handles relative imports correctly", async () => {
const context = await setupDangerfileContext()
const runtime = await createDangerfileRuntimeEnvironment(context)
await runDangerfileEnvironment(resolve(fixtures, "__DangerfileImportRelative.js"), runtime, "babel")
await runDangerfileEnvironment(resolve(fixtures, "__DangerfileImportRelative.js"), runtime, "typescript")
})

it("handles scheduled (async) code", async () => {
Expand All @@ -92,11 +92,7 @@ describe("with fixtures", () => {
it("handles multiple scheduled statements and all message types", async () => {
const context = await setupDangerfileContext()
const runtime = await createDangerfileRuntimeEnvironment(context)
const results = await runDangerfileEnvironment(
resolve(fixtures, "__DangerfileMultiScheduled.js"),
runtime,
"typescript"
)
const results = await runDangerfileEnvironment(resolve(fixtures, "__DangerfileMultiScheduled.js"), runtime)
expect(results).toEqual({
fails: [{ message: "Asynchronous Failure" }],
messages: [{ message: "Asynchronous Message" }],
Expand All @@ -107,11 +103,11 @@ describe("with fixtures", () => {

// This adds > 6 seconds to the tests! Only orta should be forced into that.
if (process.env["USER"] === "orta") {
it("can execute async/await scheduled functions", async () => {
it.only("can execute async/await scheduled functions", async () => {
// this test takes *forever* because of babel-polyfill being required
const context = await setupDangerfileContext()
const runtime = await createDangerfileRuntimeEnvironment(context)
const results = await runDangerfileEnvironment(resolve(fixtures, "__DangerfileAsync.js"), runtime, "typescript")
const results = await runDangerfileEnvironment(resolve(fixtures, "__DangerfileAsync.js"), runtime)
expect(results.warnings).toEqual([
{
message: "Async Function",
Expand All @@ -123,7 +119,7 @@ describe("with fixtures", () => {
})
}

it("can schedule callback-based promised", async () => {
it("can schedule callback-based promised ", async () => {
const context = await setupDangerfileContext()
const runtime = await createDangerfileRuntimeEnvironment(context)
const results = await runDangerfileEnvironment(resolve(fixtures, "__DangerfileCallback.js"), runtime, "typescript")
Expand Down
21 changes: 11 additions & 10 deletions source/runner/_tests/fixtures/__DangerfileAsync.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
/*eslint-disable */

import 'babel-polyfill';
import "babel-polyfill"

const asyncAction = () => new Promise(res => {
setTimeout(() => {
warn('Async Function');
res();
}, 50);
});
const asyncAction = () =>
new Promise(res => {
setTimeout(() => {
warn("Async Function")
res()
}, 50)
})

schedule(async () => {
await asyncAction();
warn('After Async Function');
});
await asyncAction()
warn("After Async Function")
})
8 changes: 6 additions & 2 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2238,6 +2238,10 @@ jest-environment-jsdom@^20.0.3:
jest-util "^20.0.3"
jsdom "^9.12.0"

jest-environment-node-debug@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/jest-environment-node-debug/-/jest-environment-node-debug-2.0.0.tgz#5ef098942fec1b6af5ee4841f4f8a2ff419562f9"

jest-environment-node@^20.0.0:
version "20.0.0"
resolved "https://registry.yarnpkg.com/jest-environment-node/-/jest-environment-node-20.0.0.tgz#7016d8d1270cbc1ed71a10f242c78324297e1db8"
Expand Down Expand Up @@ -4176,9 +4180,9 @@ verror@1.3.6:
dependencies:
extsprintf "1.0.2"

vm2@^3.4.6:
vm2@orta/vm2#compilers:
version "3.4.6"
resolved "https://registry.yarnpkg.com/vm2/-/vm2-3.4.6.tgz#9f952e4083e3af8398291a40e802854fed6a7673"
resolved "https://codeload.github.com/orta/vm2/tar.gz/69fc00118b0cf4b217002421015409efa8bea317"

voca@^1.2.0:
version "1.3.0"
Expand Down

0 comments on commit a7f7249

Please sign in to comment.