Skip to content

Commit 1defdc3

Browse files
committed
Gulfile runs tests and compiles like a champ
1 parent e6838b6 commit 1defdc3

4 files changed

Lines changed: 176 additions & 190 deletions

File tree

Gulpfile.ts

Lines changed: 68 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import originalGulp = require("gulp");
77
import helpMaker = require("gulp-help");
88
import runSequence = require("run-sequence");
99
import concat = require("gulp-concat");
10+
import clone = require("gulp-clone");
1011
import tsc = require("gulp-typescript");
1112
declare module "gulp-typescript" {
1213
interface Settings {
@@ -23,10 +24,10 @@ declare global {
2324
}
2425
import del = require("del");
2526
import mkdirP = require("mkdirP");
26-
import merge = require("merge-stream");
2727
import minimist = require("minimist");
2828
import browserify = require("browserify");
29-
import transform = require("vinyl-transform");
29+
import through2 = require("through2");
30+
import intoStream = require("into-stream");
3031
import * as os from "os";
3132
import Linter = require("tslint");
3233
const gulp = helpMaker(originalGulp);
@@ -60,22 +61,6 @@ const cmdLineOptions = minimist(process.argv.slice(2), {
6061
}
6162
});
6263

63-
function disableAtTypes() {
64-
if (fs.existsSync("node_modules/@types")) {
65-
fs.renameSync("node_modules/@types", "node_modules/@types_off");
66-
}
67-
}
68-
69-
function enableAtTypes() {
70-
if (fs.existsSync("node_modules/@types_off")) {
71-
fs.renameSync("node_modules/@types_off", "node_modules/@types");
72-
}
73-
}
74-
75-
disableAtTypes();
76-
process.on("exit", enableAtTypes);
77-
process.on("uncaughtException", enableAtTypes);
78-
7964
function exec(cmd: string, args: string[], complete: () => void = (() => {}), error: (e: any, status: number) => void = (() => {})) {
8065
console.log(`${cmd} ${args.join(" ")}`);
8166
const ex = cp.spawn(cmd, args);
@@ -114,25 +99,7 @@ const nodeModulesPathPrefix = path.resolve("./node_modules/.bin/");
11499
const isWin = /^win/.test(process.platform);
115100
const mocha = path.join(nodeModulesPathPrefix, "mocha") + (isWin ? ".cmd" : "");
116101

117-
const compilerSources = [
118-
"core.ts",
119-
"sys.ts",
120-
"types.ts",
121-
"scanner.ts",
122-
"parser.ts",
123-
"utilities.ts",
124-
"binder.ts",
125-
"checker.ts",
126-
"sourcemap.ts",
127-
"declarationEmitter.ts",
128-
"emitter.ts",
129-
"program.ts",
130-
"commandLineParser.ts",
131-
"tsc.ts",
132-
"diagnosticInformationMap.generated.ts"
133-
].map(function (f) {
134-
return path.join(compilerDirectory, f);
135-
});
102+
const compilerSources = require("./src/compiler/tsconfig.json").files.map((file) => path.join(compilerDirectory, file));
136103

137104
const servicesSources = [
138105
"core.ts",
@@ -320,21 +287,6 @@ const configureNightlyTs = path.join(scriptsDirectory, "configureNightly.ts");
320287
const packageJson = "package.json";
321288
const programTs = path.join(compilerDirectory, "program.ts");
322289

323-
324-
// Prepends the contents of prefixFile to destinationFile
325-
function prependFile(prefixFile, destinationFile) {
326-
if (!fs.existsSync(prefixFile)) {
327-
throw new Error(prefixFile + " does not exist!");
328-
}
329-
if (!fs.existsSync(destinationFile)) {
330-
throw new Error(destinationFile + " failed to be created!");
331-
}
332-
const temp = "temptemp";
333-
fs.writeFileSync(temp, fs.readFileSync(prefixFile));
334-
fs.appendFileSync(temp, fs.readFileSync(destinationFile));
335-
fs.renameSync(temp, destinationFile);
336-
}
337-
338290
function needsUpdate(source: string | string[], dest: string | string[]): boolean {
339291
if (typeof source === "string" && typeof dest === "string") {
340292
if (fs.existsSync(dest)) {
@@ -399,7 +351,10 @@ function needsUpdate(source: string | string[], dest: string | string[]): boolea
399351
}
400352

401353
function getCompilerSettings(base: tsc.Settings, useBuiltCompiler: boolean): tsc.Settings {
402-
const copy: tsc.Settings = Object.create(base);
354+
const copy: tsc.Settings = {};
355+
for (const key in base) {
356+
copy[key] = base[key];
357+
}
403358
if (!useDebugMode) {
404359
if (copy.removeComments === undefined) copy.removeComments = true;
405360
copy.newLine = 1;
@@ -430,7 +385,7 @@ gulp.task(configureNightlyJs, false, [], () => {
430385
.pipe(sourcemaps.init())
431386
.pipe(tsc(settings))
432387
.pipe(sourcemaps.write(path.dirname(configureNightlyJs)))
433-
.pipe(gulp.dest(path.dirname(configureNightlyJs)))
388+
.pipe(gulp.dest(path.dirname(configureNightlyJs)));
434389
});
435390

436391

@@ -466,7 +421,7 @@ gulp.task(importDefinitelyTypedTestsJs, false, [], () => {
466421
.pipe(sourcemaps.init())
467422
.pipe(tsc(settings))
468423
.pipe(sourcemaps.write("."))
469-
.pipe(gulp.dest("."))
424+
.pipe(gulp.dest("."));
470425
});
471426

472427
gulp.task("importDefinitelyTypedTests", "Runs scripts/importDefinitelyTypedTests/importDefinitelyTypedTests.ts to copy DT's tests to the TS-internal RWC tests", [importDefinitelyTypedTestsJs], (done) => {
@@ -497,7 +452,7 @@ gulp.task(processDiagnosticMessagesJs, false, [], () => {
497452
.pipe(sourcemaps.init())
498453
.pipe(tsc(settings))
499454
.pipe(sourcemaps.write("."))
500-
.pipe(gulp.dest("."))
455+
.pipe(gulp.dest("."));
501456
});
502457

503458
// The generated diagnostics map; built for the compiler and for the "generate-diagnostics" task
@@ -519,19 +474,16 @@ gulp.task(builtGeneratedDiagnosticMessagesJSON, [diagnosticInfoMapTs], (done) =>
519474

520475
gulp.task("generate-diagnostics", "Generates a diagnostic file in TypeScript based on an input JSON file", [diagnosticInfoMapTs]);
521476

477+
const localCompilerProject = tsc.createProject("src/compiler/tsconfig.json", {typescript: require("./lib/typescript.js")});
522478
gulp.task(builtLocalCompiler, false, ["lib", "generate-diagnostics"], () => {
523-
const settings: tsc.Settings = getCompilerSettings({
524-
declaration: true,
525-
outFile: builtLocalCompiler
526-
}, /*useBuiltCompiler*/ false);
527-
let result: NodeJS.ReadWriteStream = gulp.src(compilerSources)
479+
let result: NodeJS.ReadWriteStream = localCompilerProject.src()
528480
.pipe(sourcemaps.init())
529-
.pipe(tsc(settings));
481+
.pipe(tsc(localCompilerProject));
530482
if (!useDebugMode) {
531483
result = result.pipe(insert.prepend(fs.readFileSync(copyright)));
532484
}
533485
return result.pipe(sourcemaps.write("."))
534-
.pipe(gulp.dest("."));
486+
.pipe(gulp.dest(builtLocalDirectory));
535487
});
536488

537489
const servicesFile = path.join(builtLocalDirectory, "typescriptServices.js");
@@ -540,50 +492,68 @@ const nodePackageFile = path.join(builtLocalDirectory, "typescript.js");
540492
const nodeDefinitionsFile = path.join(builtLocalDirectory, "typescript.d.ts");
541493
const nodeStandaloneDefinitionsFile = path.join(builtLocalDirectory, "typescript_standalone.d.ts");
542494

543-
gulp.task(servicesFile, false, [builtLocalCompiler], (done) => {
495+
gulp.task(servicesFile, false, ["lib", "generate-diagnostics"], (done) => {
544496
const settings: tsc.Settings = getCompilerSettings({
545497
declaration: true,
546498
preserveConstEnums: true,
547499
removeComments: false,
548500
noResolve: false,
549501
stripInternal: true,
550502
outFile: servicesFile
551-
}, /*useBuiltCompiler*/ true);
552-
let result: NodeJS.ReadWriteStream = gulp.src(servicesSources)
503+
}, /*useBuiltCompiler*/ false);
504+
const {js, dts} = gulp.src(servicesSources)
553505
.pipe(sourcemaps.init())
554506
.pipe(tsc(settings));
507+
let result: NodeJS.ReadableStream = js;
555508
if (!useDebugMode) {
556509
result = result.pipe(insert.prepend(fs.readFileSync(copyright)));
557510
}
558511
result.pipe(sourcemaps.write("."))
559512
.pipe(gulp.dest("."))
560513
.on("end", () => {
561-
gulp.src(servicesFile).pipe(gulp.dest(nodePackageFile)).on("end", () => {
514+
gulp.src(servicesFile).pipe(insert.transform((content, file) => (file.path = nodePackageFile, content))).pipe(gulp.dest(builtLocalDirectory)).on("end", () => {
562515
// Stanalone/web definition file using global 'ts' namespace
563-
const defs = gulp.src(standaloneDefinitionsFile)
564-
.pipe(insert.transform((contents, file) => {
516+
const defs = dts.pipe(insert.prepend(fs.readFileSync(copyright))).pipe(insert.transform((contents, file) => {
517+
file.path = standaloneDefinitionsFile;
565518
return contents.replace(/^(\s*)(export )?const enum (\S+) {(\s*)$/gm, "$1$2enum $3 {$4");
566-
}));
519+
})).pipe(gulp.dest("."));
520+
defs.on("error", (err) => console.error(err));
567521

568522
// Official node package definition file, pointed to by 'typings' in package.json
569523
// Created by appending 'export = ts;' at the end of the standalone file to turn it into an external module
570-
const nodeDefs = defs.pipe(insert.transform((content, file) => {
571-
return content + "\r\nexport = ts;"
572-
})).pipe(gulp.dest(nodeDefinitionsFile));
524+
const nodeDefs = defs.pipe(clone()).pipe(insert.transform((content, file) => {
525+
file.path = nodeDefinitionsFile;
526+
return content + "\r\nexport = ts;";
527+
})).pipe(gulp.dest("."));
528+
nodeDefs.on("error", (err) => console.error(err));
573529

574530
// Node package definition file to be distributed without the package. Created by replacing
575531
// 'ts' namespace with '"typescript"' as a module.
576-
const nodeStandaloneDefs = defs.pipe(insert.transform((content, file) => {
532+
const nodeStandaloneDefs = defs.pipe(clone()).pipe(insert.transform((content, file) => {
533+
file.path = nodeStandaloneDefinitionsFile;
577534
return content.replace(/declare (namespace|module) ts/g, 'declare module "typescript"');
578-
})).pipe(gulp.dest(nodeStandaloneDefinitionsFile));
579-
merge(defs.pipe(gulp.dest(standaloneDefinitionsFile)), nodeDefs, nodeStandaloneDefs).on("end", done);
580-
});
581-
});
535+
})).pipe(gulp.dest("."));
536+
nodeStandaloneDefs.on("error", (err) => console.error(err));
537+
538+
defs.on("end", () => complete());
539+
nodeDefs.on("end", () => complete());
540+
nodeStandaloneDefs.on("end", () => complete());
541+
let count = 0;
542+
function complete() {
543+
count++;
544+
if (count >= 3) {
545+
done();
546+
}
547+
}
548+
})
549+
.on("error", (err) => console.error(err));
550+
})
551+
.on("error", (err) => console.error(err));
582552
});
583553

584554
const serverFile = path.join(builtLocalDirectory, "tsserver.js");
585555

586-
gulp.task(serverFile, false, [builtLocalCompiler], () => {
556+
gulp.task(serverFile, false, [servicesFile], () => {
587557
const settings: tsc.Settings = getCompilerSettings({
588558
outFile: serverFile
589559
}, /*useBuiltCompiler*/ true);
@@ -600,7 +570,7 @@ gulp.task(serverFile, false, [builtLocalCompiler], () => {
600570
const tsserverLibraryFile = path.join(builtLocalDirectory, "tsserverlibrary.js");
601571
const tsserverLibraryDefinitionFile = path.join(builtLocalDirectory, "tsserverlibrary.d.ts");
602572

603-
gulp.task(tsserverLibraryFile, false, [builtLocalCompiler], () => {
573+
gulp.task(tsserverLibraryFile, false, [servicesFile], () => {
604574
const settings: tsc.Settings = getCompilerSettings({
605575
declaration: true,
606576
outFile: tsserverLibraryFile
@@ -802,7 +772,6 @@ function runConsoleTests(defaultReporter: string, runInParallel: boolean, done:
802772
finish(err);
803773
}
804774
});
805-
806775
});
807776
}
808777
});
@@ -849,26 +818,29 @@ gulp.task("runtests",
849818

850819
const nodeServerOutFile = "tests/webTestServer.js";
851820
const nodeServerInFile = "tests/webTestServer.ts";
852-
gulp.task(nodeServerOutFile, false, [builtLocalCompiler], () => {
821+
gulp.task(nodeServerOutFile, false, [servicesFile], () => {
853822
const settings: tsc.Settings = getCompilerSettings({}, /*useBuiltCompiler*/ true);
854823
return gulp.src(nodeServerInFile)
855824
.pipe(sourcemaps.init())
856825
.pipe(tsc(settings))
857-
.pipe(sourcemaps.write('.'))
826+
.pipe(sourcemaps.write("."))
858827
.pipe(gulp.dest(path.dirname(nodeServerOutFile)));
859828
});
860829

861-
gulp.task("browserify", "Runs browserify on run.js to produce a file suitable for running tests in the browser", [builtLocalCompiler, nodeServerOutFile], (done) => {
862-
// TODO (weswig): Use browserify JS api with gulp streams and correctly manage sourcemaps
863-
//exec(browserify, [run, "-d", "-o", "built/local/bundle.js"], done, done);
830+
gulp.task("browserify", "Runs browserify on run.js to produce a file suitable for running tests in the browser", [servicesFile], (done) => {
864831
const settings: tsc.Settings = getCompilerSettings({
865832
outFile: "built/local/bundle.js"
866833
}, /*useBuiltCompiler*/ true);
867834
return gulp.src(harnessSources)
868835
.pipe(sourcemaps.init())
869836
.pipe(tsc(settings))
870-
.pipe(transform((filename) => {
871-
return browserify(filename).bundle();
837+
.pipe(through2.obj((file, enc, next) => {
838+
browserify(intoStream(file.contents))
839+
.bundle((err, res) => {
840+
// assumes file.contents is a Buffer
841+
file.contents = res;
842+
next(undefined, file);
843+
});
872844
}))
873845
.pipe(sourcemaps.write("."))
874846
.pipe(gulp.dest("."));
@@ -899,11 +871,11 @@ function writeTestConfigFile(tests: string, light: boolean, taskConfigsFolder?:
899871
}
900872

901873

902-
gulp.task("runtests-browser", "Runs the tests using the built run.js file like 'gulp runtests'. Syntax is gulp runtests-browser. Additional optional parameters --tests=[regex], --port=, --browser=[chrome|IE]", ["browserify"], (done) => {
874+
gulp.task("runtests-browser", "Runs the tests using the built run.js file like 'gulp runtests'. Syntax is gulp runtests-browser. Additional optional parameters --tests=[regex], --port=, --browser=[chrome|IE]", ["browserify", nodeServerOutFile], (done) => {
903875
cleanTestDirs((err) => {
904876
if (err) { console.error(err); done(err); process.exit(1); }
905877
host = "node";
906-
let tests = cmdLineOptions["tests"];
878+
const tests = cmdLineOptions["tests"];
907879
const light = cmdLineOptions["light"];
908880
const testConfigFile = "test.config";
909881
if (fs.existsSync(testConfigFile)) {
@@ -980,7 +952,7 @@ gulp.task("baseline-accept-test262", "Makes the most recent test262 test results
980952
// Webhost
981953
const webhostPath = "tests/webhost/webtsc.ts";
982954
const webhostJsPath = "tests/webhost/webtsc.js";
983-
gulp.task(webhostJsPath, false, [builtLocalCompiler], () => {
955+
gulp.task(webhostJsPath, false, [servicesFile], () => {
984956
const settings: tsc.Settings = getCompilerSettings({
985957
outFile: webhostJsPath
986958
}, /*useBuiltCompiler*/ true);
@@ -999,7 +971,7 @@ gulp.task("webhost", "Builds the tsc web host", [webhostJsPath], () => {
999971
// Perf compiler
1000972
const perftscPath = "tests/perftsc.ts";
1001973
const perftscJsPath = "built/local/perftsc.js";
1002-
gulp.task(perftscJsPath, false, [builtLocalCompiler], () => {
974+
gulp.task(perftscJsPath, false, [servicesFile], () => {
1003975
const settings: tsc.Settings = getCompilerSettings({
1004976
outFile: perftscJsPath
1005977
}, /*useBuiltCompiler*/ true);
@@ -1029,7 +1001,7 @@ gulp.task(loggedIOJsPath, false, [], (done) => {
10291001

10301002
const instrumenterPath = path.join(harnessDirectory, "instrumenter.ts");
10311003
const instrumenterJsPath = path.join(builtLocalDirectory, "instrumenter.js");
1032-
gulp.task(instrumenterJsPath, false, [builtLocalCompiler], () => {
1004+
gulp.task(instrumenterJsPath, false, [servicesFile], () => {
10331005
const settings: tsc.Settings = getCompilerSettings({
10341006
outFile: instrumenterJsPath
10351007
}, /*useBuiltCompiler*/ true);
@@ -1040,7 +1012,7 @@ gulp.task(instrumenterJsPath, false, [builtLocalCompiler], () => {
10401012
.pipe(gulp.dest("."));
10411013
});
10421014

1043-
gulp.task("tsc-instrumented", "Builds an instrumented tsc.js", [loggedIOJsPath, instrumenterJsPath, builtLocalCompiler], (done) => {
1015+
gulp.task("tsc-instrumented", "Builds an instrumented tsc.js", [loggedIOJsPath, instrumenterJsPath, servicesFile], (done) => {
10441016
exec(host, [instrumenterJsPath, "record", "iocapture", builtLocalDirectory, compilerFilename], done, done);
10451017
});
10461018

@@ -1064,12 +1036,12 @@ const tslintRulesFiles = tslintRules.map(function(p) {
10641036
const tslintRulesOutFiles = tslintRules.map(function(p, i) {
10651037
const pathname = path.join(builtLocalDirectory, "tslint", p + ".js");
10661038
gulp.task(pathname, false, [], () => {
1067-
const settings: tsc.Settings = getCompilerSettings({outDir: path.join(builtLocalDirectory, "tslint")}, /*useBuiltCompiler*/ false);
1039+
const settings: tsc.Settings = getCompilerSettings({}, /*useBuiltCompiler*/ false);
10681040
return gulp.src(tslintRulesFiles[i])
10691041
.pipe(sourcemaps.init())
10701042
.pipe(tsc(settings))
10711043
.pipe(sourcemaps.write("."))
1072-
.pipe(gulp.dest("."));
1044+
.pipe(gulp.dest(path.join(builtLocalDirectory, "tslint")));
10731045
});
10741046
return pathname;
10751047
});

0 commit comments

Comments
 (0)