mirror of
https://github.com/FlipsideCrypto/DefinitelyTyped.git
synced 2026-02-06 10:56:53 +00:00
new Travis CI script. Running tests and validate typescript syntax.
This commit is contained in:
parent
dd3fd174b8
commit
f44ddc0fa4
557
_infrastructure/runner.ts
Normal file
557
_infrastructure/runner.ts
Normal file
@ -0,0 +1,557 @@
|
||||
/// <reference path='src/exec.ts' />
|
||||
/// <reference path='src/io.ts' />
|
||||
|
||||
module DefinitelyTyped {
|
||||
|
||||
export module TestManager {
|
||||
|
||||
var path = require('path');
|
||||
|
||||
function endsWith(str, suffix) {
|
||||
return str.indexOf(suffix, str.length - suffix.length) !== -1;
|
||||
}
|
||||
|
||||
class Iterator {
|
||||
index: number = -1;
|
||||
|
||||
constructor(public list: any[]){}
|
||||
|
||||
public next() {
|
||||
this.index++;
|
||||
return this.list[this.index];
|
||||
}
|
||||
|
||||
public hasNext() {
|
||||
return this.list[1 + this.index] != null;
|
||||
}
|
||||
}
|
||||
|
||||
class Tsc {
|
||||
public static run(tsfile: string, callback: Function) {
|
||||
Exec.exec('node ./_infrastructure/typescript/tsc.js ', [tsfile], (ExecResult) => {
|
||||
callback(ExecResult);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
class Test {
|
||||
constructor(public tsfile: string) {}
|
||||
|
||||
public run(callback: Function) {
|
||||
Tsc.run(this.tsfile , callback);
|
||||
}
|
||||
}
|
||||
|
||||
class Typing {
|
||||
public fileHandler: FileHandler;
|
||||
|
||||
constructor(public name: string, baseDir: string) {
|
||||
this.fileHandler = new FileHandler(baseDir + '/' + name + '/', /.\.ts/g);
|
||||
}
|
||||
}
|
||||
|
||||
class FileHandler {
|
||||
public files: string[] = [];
|
||||
public typings: Typing[] = [];
|
||||
|
||||
constructor(public path: string, pattern: any) {
|
||||
this.files = IO.dir(path, pattern, { recursive: true });
|
||||
}
|
||||
|
||||
public allTS(): string[] {
|
||||
return this.files;
|
||||
}
|
||||
|
||||
public allTests(): string[] {
|
||||
var tests = [];
|
||||
|
||||
for(var i = 0; i < this.files.length; i++) {
|
||||
if (endsWith(this.files[i].toUpperCase(), '-TESTS.TS')) {
|
||||
tests.push(this.files[i]);
|
||||
}
|
||||
}
|
||||
|
||||
return tests;
|
||||
}
|
||||
|
||||
public allTypings(): string[] {
|
||||
var typings = {};
|
||||
|
||||
for(var i = 0; i < this.files.length; i++) {
|
||||
var file = this.files[i];
|
||||
var firName = path.dirname(file.substr(this.path.length + 1)).replace('\\', '/');
|
||||
var dir = firName.split('/')[0];
|
||||
|
||||
if(!typings[dir]) typings[dir] = true;
|
||||
}
|
||||
|
||||
var list = [];
|
||||
for(var attr in typings) {
|
||||
list.push(attr);
|
||||
}
|
||||
|
||||
return list;
|
||||
}
|
||||
}
|
||||
|
||||
class Timer {
|
||||
public startTime;
|
||||
public time = 0;
|
||||
public asString: string;
|
||||
|
||||
private static prettyDate(date1, date2): string {
|
||||
var diff = ((date2 - date1) / 1000),
|
||||
day_diff = Math.floor(diff / 86400);
|
||||
|
||||
if ( isNaN(day_diff) || day_diff < 0 || day_diff >= 31 )
|
||||
return;
|
||||
|
||||
return <string><any> (day_diff == 0 && (
|
||||
diff < 60 && (diff + " secconds") ||
|
||||
diff < 120 && "1 minute" ||
|
||||
diff < 3600 && Math.floor( diff / 60 ) + " minutes" ||
|
||||
diff < 7200 && "1 hour" ||
|
||||
diff < 86400 && Math.floor( diff / 3600 ) + " hours") ||
|
||||
day_diff == 1 && "Yesterday" ||
|
||||
day_diff < 7 && day_diff + " days" ||
|
||||
day_diff < 31 && Math.ceil( day_diff / 7 ) + " weeks");
|
||||
}
|
||||
|
||||
public start() {
|
||||
this.time = 0;
|
||||
this.startTime = this.now();
|
||||
}
|
||||
|
||||
private now() {
|
||||
return Date.now();
|
||||
}
|
||||
|
||||
public end() {
|
||||
this.time = (this.now() - this.startTime) / 1000;
|
||||
this.asString = Timer.prettyDate(this.startTime, this.now());
|
||||
}
|
||||
}
|
||||
|
||||
class Print {
|
||||
constructor(public version: string, public typings: number, public tsFiles: number) { }
|
||||
|
||||
public out(s) {
|
||||
process.stdout.write(s);
|
||||
}
|
||||
|
||||
public printHeader() {
|
||||
this.out('=============================================================================\n');
|
||||
this.out(' \33[36m\33[1mDefinitelyTyped test runner 0.2.0\33[0m\n');
|
||||
this.out('=============================================================================\n');
|
||||
this.out(' \33[36m\33[1mTypescript version:\33[0m ' + this.version + '\n');
|
||||
this.out(' \33[36m\33[1mTypings :\33[0m ' + this.typings + '\n');
|
||||
this.out(' \33[36m\33[1mTypeScript files :\33[0m ' + this.tsFiles + '\n');
|
||||
}
|
||||
|
||||
public printSyntaxCheking() {
|
||||
this.out('============================ \33[34m\33[1mSyntax cheking\33[0m =================================\n');
|
||||
}
|
||||
|
||||
public printTypingTests() {
|
||||
this.out('============================= \33[34m\33[1mTyping tests\33[0m ==================================\n');
|
||||
}
|
||||
|
||||
public printSuccess() {
|
||||
this.out('\33[36m\33[1m.\33[0m');
|
||||
}
|
||||
|
||||
public printFailure() {
|
||||
this.out('x');
|
||||
}
|
||||
|
||||
public printDiv() {
|
||||
this.out('-----------------------------------------------------------------------------\n');
|
||||
}
|
||||
|
||||
public printfilesWithSintaxErrorMessage() {
|
||||
this.out(' \33[36m\33[1mFiles with syntax error\33[0m\n');
|
||||
}
|
||||
|
||||
public printFailedTestMessage() {
|
||||
this.out(' \33[36m\33[1mFailed tests\33[0m\n');
|
||||
}
|
||||
|
||||
public printTypingsWithoutTestsMessage() {
|
||||
this.out(' \33[36m\33[1mTyping without tests\33[0m\n');
|
||||
}
|
||||
|
||||
public printTotalMessage() {
|
||||
this.out(' \33[36m\33[1mTotal\33[0m\n');
|
||||
}
|
||||
|
||||
public printErrorFile(file) {
|
||||
this.out(' - ' + file + '\n');
|
||||
}
|
||||
|
||||
public printTypingsWithoutTest(file) {
|
||||
this.out(' - \33[33m\33[1m' + file + '\33[0m\n');
|
||||
}
|
||||
|
||||
public breack() {
|
||||
this.out('\n');
|
||||
}
|
||||
|
||||
public printSuccessCount(current: number, total: number) {
|
||||
this.out(' \33[36m\33[1mSuccessful :\33[0m \33[32m\33[1m' + ((current / total) * 100).toFixed(2) + '% (' + current + '/' + total + ')\33[0m\n');
|
||||
}
|
||||
|
||||
public printFailedCount(current: number, total: number) {
|
||||
this.out(' \33[36m\33[1mFailure :\33[0m \33[31m\33[1m' + ((current / total) * 100).toFixed(2) + '% (' + current + '/' + total + ')\33[0m\n');
|
||||
}
|
||||
|
||||
public printElapsedTime(time, s) {
|
||||
this.out(' \33[36m\33[1mElapsed time :\33[0m ~' + time + ' (' + s + 's)\n');
|
||||
}
|
||||
|
||||
public printSyntaxErrorCount(current: number, total: number) {
|
||||
this.out(' \33[36m\33[1mSyntaxe error :\33[0m \33[31m\33[1m' + ((current / total) * 100).toFixed(2) + '% (' + current + '/' + total + ')\33[0m\n');
|
||||
}
|
||||
|
||||
public printTestErrorCount(current: number, total: number) {
|
||||
this.out(' \33[36m\33[1mFailed tests :\33[0m \33[31m\33[1m' + ((current / total) * 100).toFixed(2) + '% (' + current + '/' + total + ')\33[0m\n');
|
||||
}
|
||||
|
||||
public printWithoutTestCount(current: number, total: number) {
|
||||
this.out(' \33[36m\33[1mWithout tests :\33[0m \33[33m\33[1m' + ((current / total) * 100).toFixed(2) + '% (' + current + '/' + total + ')\33[0m\n');
|
||||
}
|
||||
}
|
||||
|
||||
class File {
|
||||
|
||||
constructor(public name: string, public hasError: boolean) {}
|
||||
|
||||
public formatName(baseDir: string): string {
|
||||
var dirName = path.dirname(this.name.substr(baseDir.length + 1)).replace('\\', '/');
|
||||
var dir = dirName.split('/')[0];
|
||||
var file = path.basename(this.name, '.ts');
|
||||
var ext = path.extname(this.name);
|
||||
|
||||
return dir + ((dirName.split('/').length > 1) ? '/-/' : '/') + '\33[36m\33[1m' + file + '\33[0m' + ext;
|
||||
}
|
||||
}
|
||||
|
||||
class SyntaxCheking {
|
||||
|
||||
private timer: Timer;
|
||||
|
||||
public files: File[] = [];
|
||||
|
||||
private getFailedFiles(): File[] {
|
||||
var list: File[] = [];
|
||||
|
||||
for(var i = 0; i < this.files.length; i++) {
|
||||
if(this.files[i].hasError) {
|
||||
list.push(this.files[i]);
|
||||
}
|
||||
}
|
||||
|
||||
return list;
|
||||
}
|
||||
|
||||
private getSuccessFiles(): File[] {
|
||||
var list: File[] = [];
|
||||
|
||||
for(var i = 0; i < this.files.length; i++) {
|
||||
if(!this.files[i].hasError) {
|
||||
list.push(this.files[i]);
|
||||
}
|
||||
}
|
||||
|
||||
return list;
|
||||
}
|
||||
|
||||
constructor(public fielHandler: FileHandler, public out: Print) {
|
||||
this.timer = new Timer();
|
||||
}
|
||||
|
||||
private printStats() {
|
||||
this.out.printDiv();
|
||||
this.out.printElapsedTime(this.timer.asString, this.timer.time);
|
||||
this.out.printSuccessCount(this.getSuccessFiles().length, this.files.length);
|
||||
this.out.printFailedCount(this.getFailedFiles().length, this.files.length);
|
||||
}
|
||||
|
||||
private printFailedFiles() {
|
||||
if (this.getFailedFiles().length > 0) {
|
||||
this.out.printDiv();
|
||||
|
||||
this.out.printfilesWithSintaxErrorMessage();
|
||||
|
||||
this.out.printDiv();
|
||||
|
||||
for(var i = 0; i < this.getFailedFiles().length; i++) {
|
||||
var errorFile = this.getFailedFiles()[i];
|
||||
this.out.printErrorFile(errorFile.formatName(this.fielHandler.path));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private run(it, file, len, maxLen, callback: Function) {
|
||||
if (!endsWith(file, '-tests.ts')) {
|
||||
new Test(file).run((o) => {
|
||||
var failed = false;
|
||||
|
||||
if(o.exitCode === 1) {
|
||||
this.out.printFailure();
|
||||
failed = true;
|
||||
len++;
|
||||
} else {
|
||||
this.out.printSuccess();
|
||||
len++;
|
||||
}
|
||||
|
||||
this.files.push(new File(file, failed));
|
||||
|
||||
if(len > maxLen) {
|
||||
len = 0;
|
||||
this.out.breack();
|
||||
}
|
||||
|
||||
if (it.hasNext()) {
|
||||
this.run(it, it.next(), len, maxLen, callback);
|
||||
} else {
|
||||
this.out.breack();
|
||||
this.timer.end();
|
||||
this.printFailedFiles();
|
||||
this.printStats();
|
||||
|
||||
callback(this.getFailedFiles().length, this.files.length);
|
||||
}
|
||||
});
|
||||
} else if (it.hasNext()) {
|
||||
this.run(it, it.next(), len, maxLen, callback);
|
||||
} else {
|
||||
this.out.breack();
|
||||
this.timer.end();
|
||||
this.printStats();
|
||||
this.printFailedFiles();
|
||||
|
||||
callback(this.getFailedFiles().length, this.files.length);
|
||||
}
|
||||
}
|
||||
|
||||
public start(callback: Function) {
|
||||
this.timer.start();
|
||||
|
||||
var tsFiles = this.fielHandler.allTS();
|
||||
|
||||
var it = new Iterator(tsFiles);
|
||||
|
||||
var len = 0;
|
||||
var maxLen = 76;
|
||||
|
||||
if (it.hasNext()) {
|
||||
this.run(it, it.next(), len, maxLen, callback);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class TestEval {
|
||||
|
||||
private timer: Timer;
|
||||
|
||||
public files: File[] = [];
|
||||
|
||||
private getFailedFiles(): File[] {
|
||||
var list: File[] = [];
|
||||
|
||||
for(var i = 0; i < this.files.length; i++) {
|
||||
if(this.files[i].hasError) {
|
||||
list.push(this.files[i]);
|
||||
}
|
||||
}
|
||||
|
||||
return list;
|
||||
}
|
||||
|
||||
private getSuccessFiles(): File[] {
|
||||
var list: File[] = [];
|
||||
|
||||
for(var i = 0; i < this.files.length; i++) {
|
||||
if(!this.files[i].hasError) {
|
||||
list.push(this.files[i]);
|
||||
}
|
||||
}
|
||||
|
||||
return list;
|
||||
}
|
||||
|
||||
constructor(public fielHandler: FileHandler, public out: Print) {
|
||||
this.timer = new Timer();
|
||||
}
|
||||
|
||||
private printStats() {
|
||||
this.out.printDiv();
|
||||
this.out.printElapsedTime(this.timer.asString, this.timer.time);
|
||||
this.out.printSuccessCount(this.getSuccessFiles().length, this.files.length);
|
||||
this.out.printFailedCount(this.getFailedFiles().length, this.files.length);
|
||||
}
|
||||
|
||||
private printFailedFiles() {
|
||||
if (this.getFailedFiles().length > 0) {
|
||||
this.out.printDiv();
|
||||
|
||||
this.out.printFailedTestMessage();
|
||||
|
||||
this.out.printDiv();
|
||||
|
||||
for(var i = 0; i < this.getFailedFiles().length; i++) {
|
||||
var errorFile = this.getFailedFiles()[i];
|
||||
this.out.printErrorFile(errorFile.formatName(this.fielHandler.path));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private run(it, file, len, maxLen, callback: Function) {
|
||||
if (endsWith(file, '-tests.ts')) {
|
||||
new Test(file).run((o) => {
|
||||
var failed = false;
|
||||
|
||||
if(o.exitCode === 1) {
|
||||
this.out.printFailure();
|
||||
failed = true;
|
||||
len++;
|
||||
} else {
|
||||
this.out.printSuccess();
|
||||
len++;
|
||||
}
|
||||
|
||||
this.files.push(new File(file, failed));
|
||||
|
||||
if(len > maxLen) {
|
||||
len = 0;
|
||||
this.out.breack();
|
||||
}
|
||||
|
||||
if (it.hasNext()) {
|
||||
this.run(it, it.next(), len, maxLen, callback);
|
||||
} else {
|
||||
this.out.breack();
|
||||
this.timer.end();
|
||||
this.printFailedFiles();
|
||||
this.printStats();
|
||||
|
||||
callback(this.getFailedFiles().length, this.files.length);
|
||||
}
|
||||
});
|
||||
} else if (it.hasNext()) {
|
||||
this.run(it, it.next(), len, maxLen, callback);
|
||||
} else {
|
||||
this.out.breack();
|
||||
this.timer.end();
|
||||
this.printFailedFiles();
|
||||
this.printStats();
|
||||
|
||||
callback(this.getFailedFiles().length, this.files.length);
|
||||
}
|
||||
}
|
||||
|
||||
public start(callback: Function) {
|
||||
this.timer.start();
|
||||
|
||||
var tsFiles = this.fielHandler.allTS();
|
||||
|
||||
var it = new Iterator(tsFiles);
|
||||
|
||||
var len = 0;
|
||||
var maxLen = 76;
|
||||
|
||||
if (it.hasNext()) {
|
||||
this.run(it, it.next(), len, maxLen, callback);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export class TestRunner {
|
||||
private fh: FileHandler;
|
||||
private out: Print;
|
||||
private sc: SyntaxCheking;
|
||||
private te: TestEval;
|
||||
private typings: Typing[] = [];
|
||||
|
||||
private printTypingsWithoutTest() {
|
||||
var count = 0;
|
||||
|
||||
if (this.typings.length > 0) {
|
||||
this.out.printDiv();
|
||||
|
||||
this.out.printTypingsWithoutTestsMessage();
|
||||
|
||||
this.out.printDiv();
|
||||
|
||||
for(var i = 0; i < this.typings.length; i++) {
|
||||
var typing = this.typings[i];
|
||||
if(typing.fileHandler.allTests().length == 0) {
|
||||
if (typing.name != '_infrastructure'
|
||||
&& typing.name != '_ReSharper.DefinitelyTyped'
|
||||
&& typing.name != 'obj'
|
||||
&& typing.name != 'bin'
|
||||
&& typing.name != 'Properties') {
|
||||
this.out.printTypingsWithoutTest(typing.name);
|
||||
count++;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return count;
|
||||
}
|
||||
|
||||
constructor(public dtPath: string) {
|
||||
this.fh = new FileHandler(dtPath, /.\.ts/g);
|
||||
this.out = new Print('0.9.0.0', this.fh.allTypings().length, this.fh.allTS().length);
|
||||
this.sc = new SyntaxCheking(this.fh, this.out);
|
||||
this.te = new TestEval(this.fh, this.out);
|
||||
|
||||
var tpgs = this.fh.allTypings();
|
||||
for(var i = 0; i < tpgs.length; i++) {
|
||||
this.typings.push(new Typing(tpgs[i], this.dtPath));
|
||||
}
|
||||
}
|
||||
|
||||
public run() {
|
||||
var timer = new Timer();
|
||||
timer.start();
|
||||
|
||||
this.out.printHeader();
|
||||
this.out.printSyntaxCheking();
|
||||
|
||||
this.sc.start((syntaxFailedCount, syntaxTotal) => {
|
||||
this.out.printTypingTests();
|
||||
this.te.start((testFailedCount, testTotal) => {
|
||||
var total = this.printTypingsWithoutTest();
|
||||
|
||||
timer.end();
|
||||
|
||||
this.out.printDiv();
|
||||
this.out.printTotalMessage();
|
||||
this.out.printDiv();
|
||||
|
||||
this.out.printElapsedTime(timer.asString, timer.time);
|
||||
this.out.printSyntaxErrorCount(syntaxFailedCount, syntaxTotal);
|
||||
this.out.printTestErrorCount(testFailedCount, testTotal);
|
||||
this.out.printWithoutTestCount(total, this.fh.allTypings().length);
|
||||
|
||||
this.out.printDiv();
|
||||
|
||||
if (syntaxFailedCount > 0 || testFailedCount > 0) {
|
||||
process.exit(1);
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
declare var __dirname: any;
|
||||
|
||||
var dtPath = __dirname + '/..';
|
||||
|
||||
var runner = new DefinitelyTyped.TestManager.TestRunner(dtPath);
|
||||
runner.run();
|
||||
77
_infrastructure/src/exec.ts
Normal file
77
_infrastructure/src/exec.ts
Normal file
@ -0,0 +1,77 @@
|
||||
//
|
||||
// Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
//
|
||||
|
||||
// Allows for executing a program with command-line arguments and reading the result
|
||||
interface IExec {
|
||||
exec: (filename: string, cmdLineArgs: string[], handleResult: (ExecResult) => void) => void;
|
||||
}
|
||||
|
||||
declare var require;
|
||||
|
||||
class ExecResult {
|
||||
public stdout = "";
|
||||
public stderr = "";
|
||||
public exitCode: number;
|
||||
}
|
||||
|
||||
class WindowsScriptHostExec implements IExec {
|
||||
public exec(filename: string, cmdLineArgs: string[], handleResult: (ExecResult) => void) : void {
|
||||
var result = new ExecResult();
|
||||
var shell = new ActiveXObject('WScript.Shell');
|
||||
try {
|
||||
var process = shell.Exec(filename + ' ' + cmdLineArgs.join(' '));
|
||||
} catch(e) {
|
||||
result.stderr = e.message;
|
||||
result.exitCode = 1
|
||||
handleResult(result);
|
||||
return;
|
||||
}
|
||||
// Wait for it to finish running
|
||||
while (process.Status != 0) { /* todo: sleep? */ }
|
||||
|
||||
|
||||
result.exitCode = process.ExitCode;
|
||||
if(!process.StdOut.AtEndOfStream) result.stdout = process.StdOut.ReadAll();
|
||||
if(!process.StdErr.AtEndOfStream) result.stderr = process.StdErr.ReadAll();
|
||||
|
||||
handleResult(result);
|
||||
}
|
||||
}
|
||||
|
||||
class NodeExec implements IExec {
|
||||
public exec(filename: string, cmdLineArgs: string[], handleResult: (ExecResult) => void) : void {
|
||||
var nodeExec = require('child_process').exec;
|
||||
|
||||
var result = new ExecResult();
|
||||
result.exitCode = null;
|
||||
var cmdLine = filename + ' ' + cmdLineArgs.join(' ');
|
||||
|
||||
var process = nodeExec(cmdLine, function (error, stdout, stderr) {
|
||||
result.stdout = stdout;
|
||||
result.stderr = stderr;
|
||||
result.exitCode = error ? error.code : 0;
|
||||
handleResult(result);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
var Exec: IExec = function() : IExec {
|
||||
var global = <any>Function("return this;").call(null);
|
||||
if(typeof global.ActiveXObject !== "undefined") {
|
||||
return new WindowsScriptHostExec();
|
||||
} else {
|
||||
return new NodeExec();
|
||||
}
|
||||
}();
|
||||
525
_infrastructure/src/io.ts
Normal file
525
_infrastructure/src/io.ts
Normal file
@ -0,0 +1,525 @@
|
||||
//
|
||||
// Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
//
|
||||
|
||||
interface IResolvedFile {
|
||||
content: string;
|
||||
path: string;
|
||||
}
|
||||
|
||||
interface IFileWatcher {
|
||||
close(): void;
|
||||
}
|
||||
|
||||
interface IIO {
|
||||
readFile(path: string): string;
|
||||
writeFile(path: string, contents: string): void;
|
||||
createFile(path: string, useUTF8?: bool): ITextWriter;
|
||||
deleteFile(path: string): void;
|
||||
dir(path: string, re?: RegExp, options?: { recursive?: bool; deep?: number; }): string[];
|
||||
fileExists(path: string): bool;
|
||||
directoryExists(path: string): bool;
|
||||
createDirectory(path: string): void;
|
||||
resolvePath(path: string): string;
|
||||
dirName(path: string): string;
|
||||
findFile(rootPath: string, partialFilePath: string): IResolvedFile;
|
||||
print(str: string): void;
|
||||
printLine(str: string): void;
|
||||
arguments: string[];
|
||||
stderr: ITextWriter;
|
||||
stdout: ITextWriter;
|
||||
watchFile(filename: string, callback: (string) => void ): IFileWatcher;
|
||||
run(source: string, filename: string): void;
|
||||
getExecutingFilePath(): string;
|
||||
quit(exitCode?: number);
|
||||
}
|
||||
|
||||
module IOUtils {
|
||||
// Creates the directory including its parent if not already present
|
||||
function createDirectoryStructure(ioHost: IIO, dirName: string) {
|
||||
if (ioHost.directoryExists(dirName)) {
|
||||
return;
|
||||
}
|
||||
|
||||
var parentDirectory = ioHost.dirName(dirName);
|
||||
if (parentDirectory != "") {
|
||||
createDirectoryStructure(ioHost, parentDirectory);
|
||||
}
|
||||
ioHost.createDirectory(dirName);
|
||||
}
|
||||
|
||||
// Creates a file including its directory structure if not already present
|
||||
export function createFileAndFolderStructure(ioHost: IIO, fileName: string, useUTF8?: bool) {
|
||||
var path = ioHost.resolvePath(fileName);
|
||||
var dirName = ioHost.dirName(path);
|
||||
createDirectoryStructure(ioHost, dirName);
|
||||
return ioHost.createFile(path, useUTF8);
|
||||
}
|
||||
|
||||
export function throwIOError(message: string, error: Error) {
|
||||
var errorMessage = message;
|
||||
if (error && error.message) {
|
||||
errorMessage += (" " + error.message);
|
||||
}
|
||||
throw new Error(errorMessage);
|
||||
}
|
||||
}
|
||||
|
||||
// Declare dependencies needed for all supported hosts
|
||||
declare class Enumerator {
|
||||
public atEnd(): bool;
|
||||
public moveNext();
|
||||
public item(): any;
|
||||
constructor (o: any);
|
||||
}
|
||||
declare function setTimeout(callback: () =>void , ms?: number);
|
||||
//declare var require: any;
|
||||
declare module process {
|
||||
export var argv: string[];
|
||||
export var platform: string;
|
||||
export function on(event: string, handler: (any) => void ): void;
|
||||
export module stdout {
|
||||
export function write(str: string);
|
||||
}
|
||||
export module stderr {
|
||||
export function write(str: string);
|
||||
}
|
||||
export module mainModule {
|
||||
export var filename: string;
|
||||
}
|
||||
export function exit(exitCode?: number);
|
||||
}
|
||||
|
||||
var IO = (function() {
|
||||
|
||||
// Create an IO object for use inside WindowsScriptHost hosts
|
||||
// Depends on WSCript and FileSystemObject
|
||||
function getWindowsScriptHostIO(): IIO {
|
||||
var fso = new ActiveXObject("Scripting.FileSystemObject");
|
||||
var streamObjectPool = [];
|
||||
|
||||
function getStreamObject(): any {
|
||||
if (streamObjectPool.length > 0) {
|
||||
return streamObjectPool.pop();
|
||||
} else {
|
||||
return new ActiveXObject("ADODB.Stream");
|
||||
}
|
||||
}
|
||||
|
||||
function releaseStreamObject(obj: any) {
|
||||
streamObjectPool.push(obj);
|
||||
}
|
||||
|
||||
var args = [];
|
||||
for (var i = 0; i < WScript.Arguments.length; i++) {
|
||||
args[i] = WScript.Arguments.Item(i);
|
||||
}
|
||||
|
||||
return {
|
||||
readFile: function(path) {
|
||||
try {
|
||||
var streamObj = getStreamObject();
|
||||
streamObj.Open();
|
||||
streamObj.Type = 2; // Text data
|
||||
streamObj.Charset = 'x-ansi'; // Assume we are reading ansi text
|
||||
streamObj.LoadFromFile(path);
|
||||
var bomChar = streamObj.ReadText(2); // Read the BOM char
|
||||
streamObj.Position = 0; // Position has to be at 0 before changing the encoding
|
||||
if ((bomChar.charCodeAt(0) == 0xFE && bomChar.charCodeAt(1) == 0xFF)
|
||||
|| (bomChar.charCodeAt(0) == 0xFF && bomChar.charCodeAt(1) == 0xFE)) {
|
||||
streamObj.Charset = 'unicode';
|
||||
} else if (bomChar.charCodeAt(0) == 0xEF && bomChar.charCodeAt(1) == 0xBB) {
|
||||
streamObj.Charset = 'utf-8';
|
||||
}
|
||||
|
||||
// Read the whole file
|
||||
var str = streamObj.ReadText(-1 /* read from the current position to EOS */);
|
||||
streamObj.Close();
|
||||
releaseStreamObject(streamObj);
|
||||
return <string>str;
|
||||
}
|
||||
catch (err) {
|
||||
IOUtils.throwIOError("Error reading file \"" + path + "\".", err);
|
||||
}
|
||||
},
|
||||
|
||||
writeFile: function(path, contents) {
|
||||
var file = this.createFile(path);
|
||||
file.Write(contents);
|
||||
file.Close();
|
||||
},
|
||||
|
||||
fileExists: function(path: string): bool {
|
||||
return fso.FileExists(path);
|
||||
},
|
||||
|
||||
resolvePath: function(path: string): string {
|
||||
return fso.GetAbsolutePathName(path);
|
||||
},
|
||||
|
||||
dirName: function(path: string): string {
|
||||
return fso.GetParentFolderName(path);
|
||||
},
|
||||
|
||||
findFile: function(rootPath: string, partialFilePath: string): IResolvedFile {
|
||||
var path = fso.GetAbsolutePathName(rootPath) + "/" + partialFilePath;
|
||||
|
||||
while (true) {
|
||||
if (fso.FileExists(path)) {
|
||||
try {
|
||||
var content = this.readFile(path);
|
||||
return { content: content, path: path };
|
||||
}
|
||||
catch (err) {
|
||||
//Tools.CompilerDiagnostics.debugPrint("Could not find " + path + ", trying parent");
|
||||
}
|
||||
}
|
||||
else {
|
||||
rootPath = fso.GetParentFolderName(fso.GetAbsolutePathName(rootPath));
|
||||
|
||||
if (rootPath == "") {
|
||||
return null;
|
||||
}
|
||||
else {
|
||||
path = fso.BuildPath(rootPath, partialFilePath);
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
deleteFile: function(path: string): void {
|
||||
try {
|
||||
if (fso.FileExists(path)) {
|
||||
fso.DeleteFile(path, true); // true: delete read-only files
|
||||
}
|
||||
} catch (e) {
|
||||
IOUtils.throwIOError("Couldn't delete file '" + path + "'.", e);
|
||||
}
|
||||
},
|
||||
|
||||
createFile: function (path, useUTF8?) {
|
||||
try {
|
||||
var streamObj = getStreamObject();
|
||||
streamObj.Charset = useUTF8 ? 'utf-8' : 'x-ansi';
|
||||
streamObj.Open();
|
||||
return {
|
||||
Write: function (str) { streamObj.WriteText(str, 0); },
|
||||
WriteLine: function (str) { streamObj.WriteText(str, 1); },
|
||||
Close: function() {
|
||||
try {
|
||||
streamObj.SaveToFile(path, 2);
|
||||
} catch (saveError) {
|
||||
IOUtils.throwIOError("Couldn't write to file '" + path + "'.", saveError);
|
||||
}
|
||||
finally {
|
||||
if (streamObj.State != 0 /*adStateClosed*/) {
|
||||
streamObj.Close();
|
||||
}
|
||||
releaseStreamObject(streamObj);
|
||||
}
|
||||
}
|
||||
};
|
||||
} catch (creationError) {
|
||||
IOUtils.throwIOError("Couldn't write to file '" + path + "'.", creationError);
|
||||
}
|
||||
},
|
||||
|
||||
directoryExists: function(path) {
|
||||
return <bool>fso.FolderExists(path);
|
||||
},
|
||||
|
||||
createDirectory: function(path) {
|
||||
try {
|
||||
if (!this.directoryExists(path)) {
|
||||
fso.CreateFolder(path);
|
||||
}
|
||||
} catch (e) {
|
||||
IOUtils.throwIOError("Couldn't create directory '" + path + "'.", e);
|
||||
}
|
||||
},
|
||||
|
||||
dir: function(path, spec?, options?) {
|
||||
options = options || <{ recursive?: bool; deep?: number; }>{};
|
||||
function filesInFolder(folder, root): string[]{
|
||||
var paths = [];
|
||||
var fc: Enumerator;
|
||||
|
||||
if (options.recursive) {
|
||||
fc = new Enumerator(folder.subfolders);
|
||||
|
||||
for (; !fc.atEnd() ; fc.moveNext()) {
|
||||
paths = paths.concat(filesInFolder(fc.item(), root + "/" + fc.item().Name));
|
||||
}
|
||||
}
|
||||
|
||||
fc = new Enumerator(folder.files);
|
||||
|
||||
for (; !fc.atEnd() ; fc.moveNext()) {
|
||||
if (!spec || fc.item().Name.match(spec)) {
|
||||
paths.push(root + "/" + fc.item().Name);
|
||||
}
|
||||
}
|
||||
|
||||
return paths;
|
||||
}
|
||||
|
||||
var folder = fso.GetFolder(path);
|
||||
var paths = [];
|
||||
|
||||
return filesInFolder(folder, path);
|
||||
},
|
||||
|
||||
print: function(str) {
|
||||
WScript.StdOut.Write(str);
|
||||
},
|
||||
|
||||
printLine: function(str) {
|
||||
WScript.Echo(str);
|
||||
},
|
||||
|
||||
arguments: <string[]>args,
|
||||
stderr: WScript.StdErr,
|
||||
stdout: WScript.StdOut,
|
||||
watchFile: null,
|
||||
run: function(source, filename) {
|
||||
try {
|
||||
eval(source);
|
||||
} catch (e) {
|
||||
IOUtils.throwIOError("Error while executing file '" + filename + "'.", e);
|
||||
}
|
||||
},
|
||||
getExecutingFilePath: function () {
|
||||
return WScript.ScriptFullName;
|
||||
},
|
||||
quit: function (exitCode : number = 0) {
|
||||
try {
|
||||
WScript.Quit(exitCode);
|
||||
} catch (e) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
// Create an IO object for use inside Node.js hosts
|
||||
// Depends on 'fs' and 'path' modules
|
||||
function getNodeIO(): IIO {
|
||||
|
||||
var _fs = require('fs');
|
||||
var _path = require('path');
|
||||
var _module = require('module');
|
||||
|
||||
return {
|
||||
readFile: function(file) {
|
||||
try {
|
||||
var buffer = _fs.readFileSync(file);
|
||||
switch (buffer[0]) {
|
||||
case 0xFE:
|
||||
if (buffer[1] == 0xFF) {
|
||||
// utf16-be. Reading the buffer as big endian is not supported, so convert it to
|
||||
// Little Endian first
|
||||
var i = 0;
|
||||
while ((i + 1) < buffer.length) {
|
||||
var temp = buffer[i]
|
||||
buffer[i] = buffer[i + 1];
|
||||
buffer[i + 1] = temp;
|
||||
i += 2;
|
||||
}
|
||||
return buffer.toString("ucs2", 2);
|
||||
}
|
||||
break;
|
||||
case 0xFF:
|
||||
if (buffer[1] == 0xFE) {
|
||||
// utf16-le
|
||||
return buffer.toString("ucs2", 2);
|
||||
}
|
||||
break;
|
||||
case 0xEF:
|
||||
if (buffer[1] == 0xBB) {
|
||||
// utf-8
|
||||
return buffer.toString("utf8", 3);
|
||||
}
|
||||
}
|
||||
// Default behaviour
|
||||
return buffer.toString();
|
||||
} catch (e) {
|
||||
IOUtils.throwIOError("Error reading file \"" + file + "\".", e);
|
||||
}
|
||||
},
|
||||
writeFile: <(path: string, contents: string) => void >_fs.writeFileSync,
|
||||
deleteFile: function(path) {
|
||||
try {
|
||||
_fs.unlinkSync(path);
|
||||
} catch (e) {
|
||||
IOUtils.throwIOError("Couldn't delete file '" + path + "'.", e);
|
||||
}
|
||||
},
|
||||
fileExists: function(path): bool {
|
||||
return _fs.existsSync(path);
|
||||
},
|
||||
createFile: function(path, useUTF8?) {
|
||||
function mkdirRecursiveSync(path) {
|
||||
var stats = _fs.statSync(path);
|
||||
if (stats.isFile()) {
|
||||
IOUtils.throwIOError("\"" + path + "\" exists but isn't a directory.", null);
|
||||
} else if (stats.isDirectory()) {
|
||||
return;
|
||||
} else {
|
||||
mkdirRecursiveSync(_path.dirname(path));
|
||||
_fs.mkdirSync(path, 0775);
|
||||
}
|
||||
}
|
||||
|
||||
mkdirRecursiveSync(_path.dirname(path));
|
||||
|
||||
try {
|
||||
var fd = _fs.openSync(path, 'w');
|
||||
} catch (e) {
|
||||
IOUtils.throwIOError("Couldn't write to file '" + path + "'.", e);
|
||||
}
|
||||
return {
|
||||
Write: function(str) { _fs.writeSync(fd, str); },
|
||||
WriteLine: function(str) { _fs.writeSync(fd, str + '\r\n'); },
|
||||
Close: function() { _fs.closeSync(fd); fd = null; }
|
||||
};
|
||||
},
|
||||
dir: function dir(path, spec?, options?) {
|
||||
options = options || <{ recursive?: bool; deep?: number; }>{};
|
||||
|
||||
function filesInFolder(folder: string, deep?: number): string[]{
|
||||
var paths = [];
|
||||
|
||||
var files = _fs.readdirSync(folder);
|
||||
for (var i = 0; i < files.length; i++) {
|
||||
var stat = _fs.statSync(folder + "/" + files[i]);
|
||||
if (options.recursive && stat.isDirectory()) {
|
||||
if (deep < (options.deep || 100)) {
|
||||
paths = paths.concat(filesInFolder(folder + "/" + files[i], 1));
|
||||
}
|
||||
} else if (stat.isFile() && (!spec || files[i].match(spec))) {
|
||||
paths.push(folder + "/" + files[i]);
|
||||
}
|
||||
}
|
||||
|
||||
return paths;
|
||||
}
|
||||
|
||||
return filesInFolder(path, 0);
|
||||
},
|
||||
createDirectory: function(path: string): void {
|
||||
try {
|
||||
if (!this.directoryExists(path)) {
|
||||
_fs.mkdirSync(path);
|
||||
}
|
||||
} catch (e) {
|
||||
IOUtils.throwIOError("Couldn't create directory '" + path + "'.", e);
|
||||
}
|
||||
},
|
||||
|
||||
directoryExists: function(path: string): bool {
|
||||
return _fs.existsSync(path) && _fs.lstatSync(path).isDirectory();
|
||||
},
|
||||
resolvePath: function(path: string): string {
|
||||
return _path.resolve(path);
|
||||
},
|
||||
dirName: function(path: string): string {
|
||||
return _path.dirname(path);
|
||||
},
|
||||
findFile: function(rootPath: string, partialFilePath): IResolvedFile {
|
||||
var path = rootPath + "/" + partialFilePath;
|
||||
|
||||
while (true) {
|
||||
if (_fs.existsSync(path)) {
|
||||
try {
|
||||
var content = this.readFile(path);
|
||||
return { content: content, path: path };
|
||||
} catch (err) {
|
||||
//Tools.CompilerDiagnostics.debugPrint(("Could not find " + path) + ", trying parent");
|
||||
}
|
||||
}
|
||||
else {
|
||||
var parentPath = _path.resolve(rootPath, "..");
|
||||
|
||||
// Node will just continue to repeat the root path, rather than return null
|
||||
if (rootPath === parentPath) {
|
||||
return null;
|
||||
}
|
||||
else {
|
||||
rootPath = parentPath;
|
||||
path = _path.resolve(rootPath, partialFilePath);
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
print: function(str) { process.stdout.write(str) },
|
||||
printLine: function(str) { process.stdout.write(str + '\n') },
|
||||
arguments: process.argv.slice(2),
|
||||
stderr: {
|
||||
Write: function(str) { process.stderr.write(str); },
|
||||
WriteLine: function(str) { process.stderr.write(str + '\n'); },
|
||||
Close: function() { }
|
||||
},
|
||||
stdout: {
|
||||
Write: function(str) { process.stdout.write(str); },
|
||||
WriteLine: function(str) { process.stdout.write(str + '\n'); },
|
||||
Close: function() { }
|
||||
},
|
||||
watchFile: function(filename: string, callback: (string) => void ): IFileWatcher {
|
||||
var firstRun = true;
|
||||
var processingChange = false;
|
||||
|
||||
var fileChanged: any = function(curr, prev) {
|
||||
if (!firstRun) {
|
||||
if (curr.mtime < prev.mtime) {
|
||||
return;
|
||||
}
|
||||
|
||||
_fs.unwatchFile(filename, fileChanged);
|
||||
if (!processingChange) {
|
||||
processingChange = true;
|
||||
callback(filename);
|
||||
setTimeout(function() { processingChange = false; }, 100);
|
||||
}
|
||||
}
|
||||
firstRun = false;
|
||||
_fs.watchFile(filename, { persistent: true, interval: 500 }, fileChanged);
|
||||
};
|
||||
|
||||
fileChanged();
|
||||
return {
|
||||
filename: filename,
|
||||
close: function() {
|
||||
_fs.unwatchFile(filename, fileChanged);
|
||||
}
|
||||
};
|
||||
},
|
||||
run: function(source, filename) {
|
||||
require.main.filename = filename;
|
||||
require.main.paths = _module._nodeModulePaths(_path.dirname(_fs.realpathSync(filename)));
|
||||
require.main._compile(source, filename);
|
||||
},
|
||||
getExecutingFilePath: function () {
|
||||
return process.mainModule.filename;
|
||||
},
|
||||
quit: process.exit
|
||||
}
|
||||
};
|
||||
|
||||
if (typeof ActiveXObject === "function")
|
||||
return getWindowsScriptHostIO();
|
||||
else if (typeof require === "function")
|
||||
return getNodeIO();
|
||||
else
|
||||
return null; // Unsupported host
|
||||
})();
|
||||
@ -1,65 +1,65 @@
|
||||
var ExecResult = (function () {
|
||||
function ExecResult() {
|
||||
this.stdout = "";
|
||||
this.stderr = "";
|
||||
}
|
||||
return ExecResult;
|
||||
})();
|
||||
|
||||
var WindowsScriptHostExec = (function () {
|
||||
function WindowsScriptHostExec() {
|
||||
}
|
||||
WindowsScriptHostExec.prototype.exec = function (filename, cmdLineArgs, handleResult) {
|
||||
var result = new ExecResult();
|
||||
var shell = new ActiveXObject('WScript.Shell');
|
||||
try {
|
||||
var process = shell.Exec(filename + ' ' + cmdLineArgs.join(' '));
|
||||
} catch (e) {
|
||||
result.stderr = e.message;
|
||||
result.exitCode = 1;
|
||||
handleResult(result);
|
||||
return;
|
||||
}
|
||||
|
||||
while (process.Status != 0) {
|
||||
}
|
||||
|
||||
result.exitCode = process.ExitCode;
|
||||
if (!process.StdOut.AtEndOfStream)
|
||||
result.stdout = process.StdOut.ReadAll();
|
||||
if (!process.StdErr.AtEndOfStream)
|
||||
result.stderr = process.StdErr.ReadAll();
|
||||
|
||||
handleResult(result);
|
||||
};
|
||||
return WindowsScriptHostExec;
|
||||
})();
|
||||
|
||||
var NodeExec = (function () {
|
||||
function NodeExec() {
|
||||
}
|
||||
NodeExec.prototype.exec = function (filename, cmdLineArgs, handleResult) {
|
||||
var nodeExec = require('child_process').exec;
|
||||
|
||||
var result = new ExecResult();
|
||||
result.exitCode = null;
|
||||
var cmdLine = filename + ' ' + cmdLineArgs.join(' ');
|
||||
|
||||
var process = nodeExec(cmdLine, function (error, stdout, stderr) {
|
||||
result.stdout = stdout;
|
||||
result.stderr = stderr;
|
||||
result.exitCode = error ? error.code : 0;
|
||||
handleResult(result);
|
||||
});
|
||||
};
|
||||
return NodeExec;
|
||||
})();
|
||||
|
||||
var Exec = (function () {
|
||||
var global = Function("return this;").call(null);
|
||||
if (typeof global.ActiveXObject !== "undefined") {
|
||||
return new WindowsScriptHostExec();
|
||||
} else {
|
||||
return new NodeExec();
|
||||
}
|
||||
})();
|
||||
var ExecResult = (function () {
|
||||
function ExecResult() {
|
||||
this.stdout = "";
|
||||
this.stderr = "";
|
||||
}
|
||||
return ExecResult;
|
||||
})();
|
||||
|
||||
var WindowsScriptHostExec = (function () {
|
||||
function WindowsScriptHostExec() {
|
||||
}
|
||||
WindowsScriptHostExec.prototype.exec = function (filename, cmdLineArgs, handleResult) {
|
||||
var result = new ExecResult();
|
||||
var shell = new ActiveXObject('WScript.Shell');
|
||||
try {
|
||||
var process = shell.Exec(filename + ' ' + cmdLineArgs.join(' '));
|
||||
} catch (e) {
|
||||
result.stderr = e.message;
|
||||
result.exitCode = 1;
|
||||
handleResult(result);
|
||||
return;
|
||||
}
|
||||
|
||||
while (process.Status != 0) {
|
||||
}
|
||||
|
||||
result.exitCode = process.ExitCode;
|
||||
if (!process.StdOut.AtEndOfStream)
|
||||
result.stdout = process.StdOut.ReadAll();
|
||||
if (!process.StdErr.AtEndOfStream)
|
||||
result.stderr = process.StdErr.ReadAll();
|
||||
|
||||
handleResult(result);
|
||||
};
|
||||
return WindowsScriptHostExec;
|
||||
})();
|
||||
|
||||
var NodeExec = (function () {
|
||||
function NodeExec() {
|
||||
}
|
||||
NodeExec.prototype.exec = function (filename, cmdLineArgs, handleResult) {
|
||||
var nodeExec = require('child_process').exec;
|
||||
|
||||
var result = new ExecResult();
|
||||
result.exitCode = null;
|
||||
var cmdLine = filename + ' ' + cmdLineArgs.join(' ');
|
||||
|
||||
var process = nodeExec(cmdLine, function (error, stdout, stderr) {
|
||||
result.stdout = stdout;
|
||||
result.stderr = stderr;
|
||||
result.exitCode = error ? error.code : 0;
|
||||
handleResult(result);
|
||||
});
|
||||
};
|
||||
return NodeExec;
|
||||
})();
|
||||
|
||||
var Exec = (function () {
|
||||
var global = Function("return this;").call(null);
|
||||
if (typeof global.ActiveXObject !== "undefined") {
|
||||
return new WindowsScriptHostExec();
|
||||
} else {
|
||||
return new NodeExec();
|
||||
}
|
||||
})();
|
||||
|
||||
@ -1,443 +1,443 @@
|
||||
var IOUtils;
|
||||
(function (IOUtils) {
|
||||
function createDirectoryStructure(ioHost, dirName) {
|
||||
if (ioHost.directoryExists(dirName)) {
|
||||
return;
|
||||
}
|
||||
|
||||
var parentDirectory = ioHost.dirName(dirName);
|
||||
if (parentDirectory != "") {
|
||||
createDirectoryStructure(ioHost, parentDirectory);
|
||||
}
|
||||
ioHost.createDirectory(dirName);
|
||||
}
|
||||
|
||||
function createFileAndFolderStructure(ioHost, fileName, useUTF8) {
|
||||
var path = ioHost.resolvePath(fileName);
|
||||
var dirName = ioHost.dirName(path);
|
||||
createDirectoryStructure(ioHost, dirName);
|
||||
return ioHost.createFile(path, useUTF8);
|
||||
}
|
||||
IOUtils.createFileAndFolderStructure = createFileAndFolderStructure;
|
||||
|
||||
function throwIOError(message, error) {
|
||||
var errorMessage = message;
|
||||
if (error && error.message) {
|
||||
errorMessage += (" " + error.message);
|
||||
}
|
||||
throw new Error(errorMessage);
|
||||
}
|
||||
IOUtils.throwIOError = throwIOError;
|
||||
})(IOUtils || (IOUtils = {}));
|
||||
|
||||
var IO = (function () {
|
||||
function getWindowsScriptHostIO() {
|
||||
var fso = new ActiveXObject("Scripting.FileSystemObject");
|
||||
var streamObjectPool = [];
|
||||
|
||||
function getStreamObject() {
|
||||
if (streamObjectPool.length > 0) {
|
||||
return streamObjectPool.pop();
|
||||
} else {
|
||||
return new ActiveXObject("ADODB.Stream");
|
||||
}
|
||||
}
|
||||
|
||||
function releaseStreamObject(obj) {
|
||||
streamObjectPool.push(obj);
|
||||
}
|
||||
|
||||
var args = [];
|
||||
for (var i = 0; i < WScript.Arguments.length; i++) {
|
||||
args[i] = WScript.Arguments.Item(i);
|
||||
}
|
||||
|
||||
return {
|
||||
readFile: function (path) {
|
||||
try {
|
||||
var streamObj = getStreamObject();
|
||||
streamObj.Open();
|
||||
streamObj.Type = 2;
|
||||
streamObj.Charset = 'x-ansi';
|
||||
streamObj.LoadFromFile(path);
|
||||
var bomChar = streamObj.ReadText(2);
|
||||
streamObj.Position = 0;
|
||||
if ((bomChar.charCodeAt(0) == 0xFE && bomChar.charCodeAt(1) == 0xFF) || (bomChar.charCodeAt(0) == 0xFF && bomChar.charCodeAt(1) == 0xFE)) {
|
||||
streamObj.Charset = 'unicode';
|
||||
} else if (bomChar.charCodeAt(0) == 0xEF && bomChar.charCodeAt(1) == 0xBB) {
|
||||
streamObj.Charset = 'utf-8';
|
||||
}
|
||||
|
||||
var str = streamObj.ReadText(-1);
|
||||
streamObj.Close();
|
||||
releaseStreamObject(streamObj);
|
||||
return str;
|
||||
} catch (err) {
|
||||
IOUtils.throwIOError("Error reading file \"" + path + "\".", err);
|
||||
}
|
||||
},
|
||||
writeFile: function (path, contents) {
|
||||
var file = this.createFile(path);
|
||||
file.Write(contents);
|
||||
file.Close();
|
||||
},
|
||||
fileExists: function (path) {
|
||||
return fso.FileExists(path);
|
||||
},
|
||||
resolvePath: function (path) {
|
||||
return fso.GetAbsolutePathName(path);
|
||||
},
|
||||
dirName: function (path) {
|
||||
return fso.GetParentFolderName(path);
|
||||
},
|
||||
findFile: function (rootPath, partialFilePath) {
|
||||
var path = fso.GetAbsolutePathName(rootPath) + "/" + partialFilePath;
|
||||
|
||||
while (true) {
|
||||
if (fso.FileExists(path)) {
|
||||
try {
|
||||
var content = this.readFile(path);
|
||||
return { content: content, path: path };
|
||||
} catch (err) {
|
||||
}
|
||||
} else {
|
||||
rootPath = fso.GetParentFolderName(fso.GetAbsolutePathName(rootPath));
|
||||
|
||||
if (rootPath == "") {
|
||||
return null;
|
||||
} else {
|
||||
path = fso.BuildPath(rootPath, partialFilePath);
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
deleteFile: function (path) {
|
||||
try {
|
||||
if (fso.FileExists(path)) {
|
||||
fso.DeleteFile(path, true);
|
||||
}
|
||||
} catch (e) {
|
||||
IOUtils.throwIOError("Couldn't delete file '" + path + "'.", e);
|
||||
}
|
||||
},
|
||||
createFile: function (path, useUTF8) {
|
||||
try {
|
||||
var streamObj = getStreamObject();
|
||||
streamObj.Charset = useUTF8 ? 'utf-8' : 'x-ansi';
|
||||
streamObj.Open();
|
||||
return {
|
||||
Write: function (str) {
|
||||
streamObj.WriteText(str, 0);
|
||||
},
|
||||
WriteLine: function (str) {
|
||||
streamObj.WriteText(str, 1);
|
||||
},
|
||||
Close: function () {
|
||||
try {
|
||||
streamObj.SaveToFile(path, 2);
|
||||
} catch (saveError) {
|
||||
IOUtils.throwIOError("Couldn't write to file '" + path + "'.", saveError);
|
||||
} finally {
|
||||
if (streamObj.State != 0) {
|
||||
streamObj.Close();
|
||||
}
|
||||
releaseStreamObject(streamObj);
|
||||
}
|
||||
}
|
||||
};
|
||||
} catch (creationError) {
|
||||
IOUtils.throwIOError("Couldn't write to file '" + path + "'.", creationError);
|
||||
}
|
||||
},
|
||||
directoryExists: function (path) {
|
||||
return fso.FolderExists(path);
|
||||
},
|
||||
createDirectory: function (path) {
|
||||
try {
|
||||
if (!this.directoryExists(path)) {
|
||||
fso.CreateFolder(path);
|
||||
}
|
||||
} catch (e) {
|
||||
IOUtils.throwIOError("Couldn't create directory '" + path + "'.", e);
|
||||
}
|
||||
},
|
||||
dir: function (path, spec, options) {
|
||||
options = options || {};
|
||||
function filesInFolder(folder, root) {
|
||||
var paths = [];
|
||||
var fc;
|
||||
|
||||
if (options.recursive) {
|
||||
fc = new Enumerator(folder.subfolders);
|
||||
|
||||
for (; !fc.atEnd(); fc.moveNext()) {
|
||||
paths = paths.concat(filesInFolder(fc.item(), root + "/" + fc.item().Name));
|
||||
}
|
||||
}
|
||||
|
||||
fc = new Enumerator(folder.files);
|
||||
|
||||
for (; !fc.atEnd(); fc.moveNext()) {
|
||||
if (!spec || fc.item().Name.match(spec)) {
|
||||
paths.push(root + "/" + fc.item().Name);
|
||||
}
|
||||
}
|
||||
|
||||
return paths;
|
||||
}
|
||||
|
||||
var folder = fso.GetFolder(path);
|
||||
var paths = [];
|
||||
|
||||
return filesInFolder(folder, path);
|
||||
},
|
||||
print: function (str) {
|
||||
WScript.StdOut.Write(str);
|
||||
},
|
||||
printLine: function (str) {
|
||||
WScript.Echo(str);
|
||||
},
|
||||
arguments: args,
|
||||
stderr: WScript.StdErr,
|
||||
stdout: WScript.StdOut,
|
||||
watchFile: null,
|
||||
run: function (source, filename) {
|
||||
try {
|
||||
eval(source);
|
||||
} catch (e) {
|
||||
IOUtils.throwIOError("Error while executing file '" + filename + "'.", e);
|
||||
}
|
||||
},
|
||||
getExecutingFilePath: function () {
|
||||
return WScript.ScriptFullName;
|
||||
},
|
||||
quit: function (exitCode) {
|
||||
if (typeof exitCode === "undefined") { exitCode = 0; }
|
||||
try {
|
||||
WScript.Quit(exitCode);
|
||||
} catch (e) {
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
;
|
||||
|
||||
function getNodeIO() {
|
||||
var _fs = require('fs');
|
||||
var _path = require('path');
|
||||
var _module = require('module');
|
||||
|
||||
return {
|
||||
readFile: function (file) {
|
||||
try {
|
||||
var buffer = _fs.readFileSync(file);
|
||||
switch (buffer[0]) {
|
||||
case 0xFE:
|
||||
if (buffer[1] == 0xFF) {
|
||||
var i = 0;
|
||||
while ((i + 1) < buffer.length) {
|
||||
var temp = buffer[i];
|
||||
buffer[i] = buffer[i + 1];
|
||||
buffer[i + 1] = temp;
|
||||
i += 2;
|
||||
}
|
||||
return buffer.toString("ucs2", 2);
|
||||
}
|
||||
break;
|
||||
case 0xFF:
|
||||
if (buffer[1] == 0xFE) {
|
||||
return buffer.toString("ucs2", 2);
|
||||
}
|
||||
break;
|
||||
case 0xEF:
|
||||
if (buffer[1] == 0xBB) {
|
||||
return buffer.toString("utf8", 3);
|
||||
}
|
||||
}
|
||||
|
||||
return buffer.toString();
|
||||
} catch (e) {
|
||||
IOUtils.throwIOError("Error reading file \"" + file + "\".", e);
|
||||
}
|
||||
},
|
||||
writeFile: _fs.writeFileSync,
|
||||
deleteFile: function (path) {
|
||||
try {
|
||||
_fs.unlinkSync(path);
|
||||
} catch (e) {
|
||||
IOUtils.throwIOError("Couldn't delete file '" + path + "'.", e);
|
||||
}
|
||||
},
|
||||
fileExists: function (path) {
|
||||
return _fs.existsSync(path);
|
||||
},
|
||||
createFile: function (path, useUTF8) {
|
||||
function mkdirRecursiveSync(path) {
|
||||
var stats = _fs.statSync(path);
|
||||
if (stats.isFile()) {
|
||||
IOUtils.throwIOError("\"" + path + "\" exists but isn't a directory.", null);
|
||||
} else if (stats.isDirectory()) {
|
||||
return;
|
||||
} else {
|
||||
mkdirRecursiveSync(_path.dirname(path));
|
||||
_fs.mkdirSync(path, 0775);
|
||||
}
|
||||
}
|
||||
|
||||
mkdirRecursiveSync(_path.dirname(path));
|
||||
|
||||
try {
|
||||
var fd = _fs.openSync(path, 'w');
|
||||
} catch (e) {
|
||||
IOUtils.throwIOError("Couldn't write to file '" + path + "'.", e);
|
||||
}
|
||||
return {
|
||||
Write: function (str) {
|
||||
_fs.writeSync(fd, str);
|
||||
},
|
||||
WriteLine: function (str) {
|
||||
_fs.writeSync(fd, str + '\r\n');
|
||||
},
|
||||
Close: function () {
|
||||
_fs.closeSync(fd);
|
||||
fd = null;
|
||||
}
|
||||
};
|
||||
},
|
||||
dir: function dir(path, spec, options) {
|
||||
options = options || {};
|
||||
|
||||
function filesInFolder(folder) {
|
||||
var paths = [];
|
||||
|
||||
var files = _fs.readdirSync(folder);
|
||||
for (var i = 0; i < files.length; i++) {
|
||||
var stat = _fs.statSync(folder + "/" + files[i]);
|
||||
if (options.recursive && stat.isDirectory()) {
|
||||
paths = paths.concat(filesInFolder(folder + "/" + files[i]));
|
||||
} else if (stat.isFile() && (!spec || files[i].match(spec))) {
|
||||
paths.push(folder + "/" + files[i]);
|
||||
}
|
||||
}
|
||||
|
||||
return paths;
|
||||
}
|
||||
|
||||
return filesInFolder(path);
|
||||
},
|
||||
createDirectory: function (path) {
|
||||
try {
|
||||
if (!this.directoryExists(path)) {
|
||||
_fs.mkdirSync(path);
|
||||
}
|
||||
} catch (e) {
|
||||
IOUtils.throwIOError("Couldn't create directory '" + path + "'.", e);
|
||||
}
|
||||
},
|
||||
directoryExists: function (path) {
|
||||
return _fs.existsSync(path) && _fs.lstatSync(path).isDirectory();
|
||||
},
|
||||
resolvePath: function (path) {
|
||||
return _path.resolve(path);
|
||||
},
|
||||
dirName: function (path) {
|
||||
return _path.dirname(path);
|
||||
},
|
||||
findFile: function (rootPath, partialFilePath) {
|
||||
var path = rootPath + "/" + partialFilePath;
|
||||
|
||||
while (true) {
|
||||
if (_fs.existsSync(path)) {
|
||||
try {
|
||||
var content = this.readFile(path);
|
||||
return { content: content, path: path };
|
||||
} catch (err) {
|
||||
}
|
||||
} else {
|
||||
var parentPath = _path.resolve(rootPath, "..");
|
||||
|
||||
if (rootPath === parentPath) {
|
||||
return null;
|
||||
} else {
|
||||
rootPath = parentPath;
|
||||
path = _path.resolve(rootPath, partialFilePath);
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
print: function (str) {
|
||||
process.stdout.write(str);
|
||||
},
|
||||
printLine: function (str) {
|
||||
process.stdout.write(str + '\n');
|
||||
},
|
||||
arguments: process.argv.slice(2),
|
||||
stderr: {
|
||||
Write: function (str) {
|
||||
process.stderr.write(str);
|
||||
},
|
||||
WriteLine: function (str) {
|
||||
process.stderr.write(str + '\n');
|
||||
},
|
||||
Close: function () {
|
||||
}
|
||||
},
|
||||
stdout: {
|
||||
Write: function (str) {
|
||||
process.stdout.write(str);
|
||||
},
|
||||
WriteLine: function (str) {
|
||||
process.stdout.write(str + '\n');
|
||||
},
|
||||
Close: function () {
|
||||
}
|
||||
},
|
||||
watchFile: function (filename, callback) {
|
||||
var firstRun = true;
|
||||
var processingChange = false;
|
||||
|
||||
var fileChanged = function (curr, prev) {
|
||||
if (!firstRun) {
|
||||
if (curr.mtime < prev.mtime) {
|
||||
return;
|
||||
}
|
||||
|
||||
_fs.unwatchFile(filename, fileChanged);
|
||||
if (!processingChange) {
|
||||
processingChange = true;
|
||||
callback(filename);
|
||||
setTimeout(function () {
|
||||
processingChange = false;
|
||||
}, 100);
|
||||
}
|
||||
}
|
||||
firstRun = false;
|
||||
_fs.watchFile(filename, { persistent: true, interval: 500 }, fileChanged);
|
||||
};
|
||||
|
||||
fileChanged();
|
||||
return {
|
||||
filename: filename,
|
||||
close: function () {
|
||||
_fs.unwatchFile(filename, fileChanged);
|
||||
}
|
||||
};
|
||||
},
|
||||
run: function (source, filename) {
|
||||
require.main.filename = filename;
|
||||
require.main.paths = _module._nodeModulePaths(_path.dirname(_fs.realpathSync(filename)));
|
||||
require.main._compile(source, filename);
|
||||
},
|
||||
getExecutingFilePath: function () {
|
||||
return process.mainModule.filename;
|
||||
},
|
||||
quit: process.exit
|
||||
};
|
||||
}
|
||||
;
|
||||
|
||||
if (typeof ActiveXObject === "function")
|
||||
return getWindowsScriptHostIO(); else if (typeof require === "function")
|
||||
return getNodeIO(); else
|
||||
return null;
|
||||
})();
|
||||
var IOUtils;
|
||||
(function (IOUtils) {
|
||||
function createDirectoryStructure(ioHost, dirName) {
|
||||
if (ioHost.directoryExists(dirName)) {
|
||||
return;
|
||||
}
|
||||
|
||||
var parentDirectory = ioHost.dirName(dirName);
|
||||
if (parentDirectory != "") {
|
||||
createDirectoryStructure(ioHost, parentDirectory);
|
||||
}
|
||||
ioHost.createDirectory(dirName);
|
||||
}
|
||||
|
||||
function createFileAndFolderStructure(ioHost, fileName, useUTF8) {
|
||||
var path = ioHost.resolvePath(fileName);
|
||||
var dirName = ioHost.dirName(path);
|
||||
createDirectoryStructure(ioHost, dirName);
|
||||
return ioHost.createFile(path, useUTF8);
|
||||
}
|
||||
IOUtils.createFileAndFolderStructure = createFileAndFolderStructure;
|
||||
|
||||
function throwIOError(message, error) {
|
||||
var errorMessage = message;
|
||||
if (error && error.message) {
|
||||
errorMessage += (" " + error.message);
|
||||
}
|
||||
throw new Error(errorMessage);
|
||||
}
|
||||
IOUtils.throwIOError = throwIOError;
|
||||
})(IOUtils || (IOUtils = {}));
|
||||
|
||||
var IO = (function () {
|
||||
function getWindowsScriptHostIO() {
|
||||
var fso = new ActiveXObject("Scripting.FileSystemObject");
|
||||
var streamObjectPool = [];
|
||||
|
||||
function getStreamObject() {
|
||||
if (streamObjectPool.length > 0) {
|
||||
return streamObjectPool.pop();
|
||||
} else {
|
||||
return new ActiveXObject("ADODB.Stream");
|
||||
}
|
||||
}
|
||||
|
||||
function releaseStreamObject(obj) {
|
||||
streamObjectPool.push(obj);
|
||||
}
|
||||
|
||||
var args = [];
|
||||
for (var i = 0; i < WScript.Arguments.length; i++) {
|
||||
args[i] = WScript.Arguments.Item(i);
|
||||
}
|
||||
|
||||
return {
|
||||
readFile: function (path) {
|
||||
try {
|
||||
var streamObj = getStreamObject();
|
||||
streamObj.Open();
|
||||
streamObj.Type = 2;
|
||||
streamObj.Charset = 'x-ansi';
|
||||
streamObj.LoadFromFile(path);
|
||||
var bomChar = streamObj.ReadText(2);
|
||||
streamObj.Position = 0;
|
||||
if ((bomChar.charCodeAt(0) == 0xFE && bomChar.charCodeAt(1) == 0xFF) || (bomChar.charCodeAt(0) == 0xFF && bomChar.charCodeAt(1) == 0xFE)) {
|
||||
streamObj.Charset = 'unicode';
|
||||
} else if (bomChar.charCodeAt(0) == 0xEF && bomChar.charCodeAt(1) == 0xBB) {
|
||||
streamObj.Charset = 'utf-8';
|
||||
}
|
||||
|
||||
var str = streamObj.ReadText(-1);
|
||||
streamObj.Close();
|
||||
releaseStreamObject(streamObj);
|
||||
return str;
|
||||
} catch (err) {
|
||||
IOUtils.throwIOError("Error reading file \"" + path + "\".", err);
|
||||
}
|
||||
},
|
||||
writeFile: function (path, contents) {
|
||||
var file = this.createFile(path);
|
||||
file.Write(contents);
|
||||
file.Close();
|
||||
},
|
||||
fileExists: function (path) {
|
||||
return fso.FileExists(path);
|
||||
},
|
||||
resolvePath: function (path) {
|
||||
return fso.GetAbsolutePathName(path);
|
||||
},
|
||||
dirName: function (path) {
|
||||
return fso.GetParentFolderName(path);
|
||||
},
|
||||
findFile: function (rootPath, partialFilePath) {
|
||||
var path = fso.GetAbsolutePathName(rootPath) + "/" + partialFilePath;
|
||||
|
||||
while (true) {
|
||||
if (fso.FileExists(path)) {
|
||||
try {
|
||||
var content = this.readFile(path);
|
||||
return { content: content, path: path };
|
||||
} catch (err) {
|
||||
}
|
||||
} else {
|
||||
rootPath = fso.GetParentFolderName(fso.GetAbsolutePathName(rootPath));
|
||||
|
||||
if (rootPath == "") {
|
||||
return null;
|
||||
} else {
|
||||
path = fso.BuildPath(rootPath, partialFilePath);
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
deleteFile: function (path) {
|
||||
try {
|
||||
if (fso.FileExists(path)) {
|
||||
fso.DeleteFile(path, true);
|
||||
}
|
||||
} catch (e) {
|
||||
IOUtils.throwIOError("Couldn't delete file '" + path + "'.", e);
|
||||
}
|
||||
},
|
||||
createFile: function (path, useUTF8) {
|
||||
try {
|
||||
var streamObj = getStreamObject();
|
||||
streamObj.Charset = useUTF8 ? 'utf-8' : 'x-ansi';
|
||||
streamObj.Open();
|
||||
return {
|
||||
Write: function (str) {
|
||||
streamObj.WriteText(str, 0);
|
||||
},
|
||||
WriteLine: function (str) {
|
||||
streamObj.WriteText(str, 1);
|
||||
},
|
||||
Close: function () {
|
||||
try {
|
||||
streamObj.SaveToFile(path, 2);
|
||||
} catch (saveError) {
|
||||
IOUtils.throwIOError("Couldn't write to file '" + path + "'.", saveError);
|
||||
} finally {
|
||||
if (streamObj.State != 0) {
|
||||
streamObj.Close();
|
||||
}
|
||||
releaseStreamObject(streamObj);
|
||||
}
|
||||
}
|
||||
};
|
||||
} catch (creationError) {
|
||||
IOUtils.throwIOError("Couldn't write to file '" + path + "'.", creationError);
|
||||
}
|
||||
},
|
||||
directoryExists: function (path) {
|
||||
return fso.FolderExists(path);
|
||||
},
|
||||
createDirectory: function (path) {
|
||||
try {
|
||||
if (!this.directoryExists(path)) {
|
||||
fso.CreateFolder(path);
|
||||
}
|
||||
} catch (e) {
|
||||
IOUtils.throwIOError("Couldn't create directory '" + path + "'.", e);
|
||||
}
|
||||
},
|
||||
dir: function (path, spec, options) {
|
||||
options = options || {};
|
||||
function filesInFolder(folder, root) {
|
||||
var paths = [];
|
||||
var fc;
|
||||
|
||||
if (options.recursive) {
|
||||
fc = new Enumerator(folder.subfolders);
|
||||
|
||||
for (; !fc.atEnd(); fc.moveNext()) {
|
||||
paths = paths.concat(filesInFolder(fc.item(), root + "/" + fc.item().Name));
|
||||
}
|
||||
}
|
||||
|
||||
fc = new Enumerator(folder.files);
|
||||
|
||||
for (; !fc.atEnd(); fc.moveNext()) {
|
||||
if (!spec || fc.item().Name.match(spec)) {
|
||||
paths.push(root + "/" + fc.item().Name);
|
||||
}
|
||||
}
|
||||
|
||||
return paths;
|
||||
}
|
||||
|
||||
var folder = fso.GetFolder(path);
|
||||
var paths = [];
|
||||
|
||||
return filesInFolder(folder, path);
|
||||
},
|
||||
print: function (str) {
|
||||
WScript.StdOut.Write(str);
|
||||
},
|
||||
printLine: function (str) {
|
||||
WScript.Echo(str);
|
||||
},
|
||||
arguments: args,
|
||||
stderr: WScript.StdErr,
|
||||
stdout: WScript.StdOut,
|
||||
watchFile: null,
|
||||
run: function (source, filename) {
|
||||
try {
|
||||
eval(source);
|
||||
} catch (e) {
|
||||
IOUtils.throwIOError("Error while executing file '" + filename + "'.", e);
|
||||
}
|
||||
},
|
||||
getExecutingFilePath: function () {
|
||||
return WScript.ScriptFullName;
|
||||
},
|
||||
quit: function (exitCode) {
|
||||
if (typeof exitCode === "undefined") { exitCode = 0; }
|
||||
try {
|
||||
WScript.Quit(exitCode);
|
||||
} catch (e) {
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
;
|
||||
|
||||
function getNodeIO() {
|
||||
var _fs = require('fs');
|
||||
var _path = require('path');
|
||||
var _module = require('module');
|
||||
|
||||
return {
|
||||
readFile: function (file) {
|
||||
try {
|
||||
var buffer = _fs.readFileSync(file);
|
||||
switch (buffer[0]) {
|
||||
case 0xFE:
|
||||
if (buffer[1] == 0xFF) {
|
||||
var i = 0;
|
||||
while ((i + 1) < buffer.length) {
|
||||
var temp = buffer[i];
|
||||
buffer[i] = buffer[i + 1];
|
||||
buffer[i + 1] = temp;
|
||||
i += 2;
|
||||
}
|
||||
return buffer.toString("ucs2", 2);
|
||||
}
|
||||
break;
|
||||
case 0xFF:
|
||||
if (buffer[1] == 0xFE) {
|
||||
return buffer.toString("ucs2", 2);
|
||||
}
|
||||
break;
|
||||
case 0xEF:
|
||||
if (buffer[1] == 0xBB) {
|
||||
return buffer.toString("utf8", 3);
|
||||
}
|
||||
}
|
||||
|
||||
return buffer.toString();
|
||||
} catch (e) {
|
||||
IOUtils.throwIOError("Error reading file \"" + file + "\".", e);
|
||||
}
|
||||
},
|
||||
writeFile: _fs.writeFileSync,
|
||||
deleteFile: function (path) {
|
||||
try {
|
||||
_fs.unlinkSync(path);
|
||||
} catch (e) {
|
||||
IOUtils.throwIOError("Couldn't delete file '" + path + "'.", e);
|
||||
}
|
||||
},
|
||||
fileExists: function (path) {
|
||||
return _fs.existsSync(path);
|
||||
},
|
||||
createFile: function (path, useUTF8) {
|
||||
function mkdirRecursiveSync(path) {
|
||||
var stats = _fs.statSync(path);
|
||||
if (stats.isFile()) {
|
||||
IOUtils.throwIOError("\"" + path + "\" exists but isn't a directory.", null);
|
||||
} else if (stats.isDirectory()) {
|
||||
return;
|
||||
} else {
|
||||
mkdirRecursiveSync(_path.dirname(path));
|
||||
_fs.mkdirSync(path, 0775);
|
||||
}
|
||||
}
|
||||
|
||||
mkdirRecursiveSync(_path.dirname(path));
|
||||
|
||||
try {
|
||||
var fd = _fs.openSync(path, 'w');
|
||||
} catch (e) {
|
||||
IOUtils.throwIOError("Couldn't write to file '" + path + "'.", e);
|
||||
}
|
||||
return {
|
||||
Write: function (str) {
|
||||
_fs.writeSync(fd, str);
|
||||
},
|
||||
WriteLine: function (str) {
|
||||
_fs.writeSync(fd, str + '\r\n');
|
||||
},
|
||||
Close: function () {
|
||||
_fs.closeSync(fd);
|
||||
fd = null;
|
||||
}
|
||||
};
|
||||
},
|
||||
dir: function dir(path, spec, options) {
|
||||
options = options || {};
|
||||
|
||||
function filesInFolder(folder) {
|
||||
var paths = [];
|
||||
|
||||
var files = _fs.readdirSync(folder);
|
||||
for (var i = 0; i < files.length; i++) {
|
||||
var stat = _fs.statSync(folder + "/" + files[i]);
|
||||
if (options.recursive && stat.isDirectory()) {
|
||||
paths = paths.concat(filesInFolder(folder + "/" + files[i]));
|
||||
} else if (stat.isFile() && (!spec || files[i].match(spec))) {
|
||||
paths.push(folder + "/" + files[i]);
|
||||
}
|
||||
}
|
||||
|
||||
return paths;
|
||||
}
|
||||
|
||||
return filesInFolder(path);
|
||||
},
|
||||
createDirectory: function (path) {
|
||||
try {
|
||||
if (!this.directoryExists(path)) {
|
||||
_fs.mkdirSync(path);
|
||||
}
|
||||
} catch (e) {
|
||||
IOUtils.throwIOError("Couldn't create directory '" + path + "'.", e);
|
||||
}
|
||||
},
|
||||
directoryExists: function (path) {
|
||||
return _fs.existsSync(path) && _fs.lstatSync(path).isDirectory();
|
||||
},
|
||||
resolvePath: function (path) {
|
||||
return _path.resolve(path);
|
||||
},
|
||||
dirName: function (path) {
|
||||
return _path.dirname(path);
|
||||
},
|
||||
findFile: function (rootPath, partialFilePath) {
|
||||
var path = rootPath + "/" + partialFilePath;
|
||||
|
||||
while (true) {
|
||||
if (_fs.existsSync(path)) {
|
||||
try {
|
||||
var content = this.readFile(path);
|
||||
return { content: content, path: path };
|
||||
} catch (err) {
|
||||
}
|
||||
} else {
|
||||
var parentPath = _path.resolve(rootPath, "..");
|
||||
|
||||
if (rootPath === parentPath) {
|
||||
return null;
|
||||
} else {
|
||||
rootPath = parentPath;
|
||||
path = _path.resolve(rootPath, partialFilePath);
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
print: function (str) {
|
||||
process.stdout.write(str);
|
||||
},
|
||||
printLine: function (str) {
|
||||
process.stdout.write(str + '\n');
|
||||
},
|
||||
arguments: process.argv.slice(2),
|
||||
stderr: {
|
||||
Write: function (str) {
|
||||
process.stderr.write(str);
|
||||
},
|
||||
WriteLine: function (str) {
|
||||
process.stderr.write(str + '\n');
|
||||
},
|
||||
Close: function () {
|
||||
}
|
||||
},
|
||||
stdout: {
|
||||
Write: function (str) {
|
||||
process.stdout.write(str);
|
||||
},
|
||||
WriteLine: function (str) {
|
||||
process.stdout.write(str + '\n');
|
||||
},
|
||||
Close: function () {
|
||||
}
|
||||
},
|
||||
watchFile: function (filename, callback) {
|
||||
var firstRun = true;
|
||||
var processingChange = false;
|
||||
|
||||
var fileChanged = function (curr, prev) {
|
||||
if (!firstRun) {
|
||||
if (curr.mtime < prev.mtime) {
|
||||
return;
|
||||
}
|
||||
|
||||
_fs.unwatchFile(filename, fileChanged);
|
||||
if (!processingChange) {
|
||||
processingChange = true;
|
||||
callback(filename);
|
||||
setTimeout(function () {
|
||||
processingChange = false;
|
||||
}, 100);
|
||||
}
|
||||
}
|
||||
firstRun = false;
|
||||
_fs.watchFile(filename, { persistent: true, interval: 500 }, fileChanged);
|
||||
};
|
||||
|
||||
fileChanged();
|
||||
return {
|
||||
filename: filename,
|
||||
close: function () {
|
||||
_fs.unwatchFile(filename, fileChanged);
|
||||
}
|
||||
};
|
||||
},
|
||||
run: function (source, filename) {
|
||||
require.main.filename = filename;
|
||||
require.main.paths = _module._nodeModulePaths(_path.dirname(_fs.realpathSync(filename)));
|
||||
require.main._compile(source, filename);
|
||||
},
|
||||
getExecutingFilePath: function () {
|
||||
return process.mainModule.filename;
|
||||
},
|
||||
quit: process.exit
|
||||
};
|
||||
}
|
||||
;
|
||||
|
||||
if (typeof ActiveXObject === "function")
|
||||
return getWindowsScriptHostIO(); else if (typeof require === "function")
|
||||
return getNodeIO(); else
|
||||
return null;
|
||||
})();
|
||||
|
||||
@ -1,619 +1,152 @@
|
||||
var ExecResult = (function () {
|
||||
function ExecResult() {
|
||||
this.stdout = "";
|
||||
this.stderr = "";
|
||||
}
|
||||
return ExecResult;
|
||||
})();
|
||||
var WindowsScriptHostExec = (function () {
|
||||
function WindowsScriptHostExec() { }
|
||||
WindowsScriptHostExec.prototype.exec = function (filename, cmdLineArgs, handleResult) {
|
||||
var result = new ExecResult();
|
||||
var shell = new ActiveXObject('WScript.Shell');
|
||||
try {
|
||||
var process = shell.Exec(filename + ' ' + cmdLineArgs.join(' '));
|
||||
} catch (e) {
|
||||
result.stderr = e.message;
|
||||
result.exitCode = 1;
|
||||
handleResult(result);
|
||||
return;
|
||||
}
|
||||
while(process.Status != 0) {
|
||||
}
|
||||
result.exitCode = process.ExitCode;
|
||||
if(!process.StdOut.AtEndOfStream) {
|
||||
result.stdout = process.StdOut.ReadAll();
|
||||
}
|
||||
if(!process.StdErr.AtEndOfStream) {
|
||||
result.stderr = process.StdErr.ReadAll();
|
||||
}
|
||||
handleResult(result);
|
||||
};
|
||||
return WindowsScriptHostExec;
|
||||
})();
|
||||
var NodeExec = (function () {
|
||||
function NodeExec() { }
|
||||
NodeExec.prototype.exec = function (filename, cmdLineArgs, handleResult) {
|
||||
var nodeExec = require('child_process').exec;
|
||||
var result = new ExecResult();
|
||||
result.exitCode = null;
|
||||
var cmdLine = filename + ' ' + cmdLineArgs.join(' ');
|
||||
var process = nodeExec(cmdLine, function (error, stdout, stderr) {
|
||||
result.stdout = stdout;
|
||||
result.stderr = stderr;
|
||||
result.exitCode = error ? error.code : 0;
|
||||
handleResult(result);
|
||||
});
|
||||
};
|
||||
return NodeExec;
|
||||
})();
|
||||
var Exec = (function () {
|
||||
var global = Function("return this;").call(null);
|
||||
if(typeof global.ActiveXObject !== "undefined") {
|
||||
return new WindowsScriptHostExec();
|
||||
} else {
|
||||
return new NodeExec();
|
||||
}
|
||||
})();
|
||||
var IOUtils;
|
||||
(function (IOUtils) {
|
||||
function createDirectoryStructure(ioHost, dirName) {
|
||||
if(ioHost.directoryExists(dirName)) {
|
||||
return;
|
||||
}
|
||||
var parentDirectory = ioHost.dirName(dirName);
|
||||
if(parentDirectory != "") {
|
||||
createDirectoryStructure(ioHost, parentDirectory);
|
||||
}
|
||||
ioHost.createDirectory(dirName);
|
||||
}
|
||||
function createFileAndFolderStructure(ioHost, fileName, useUTF8) {
|
||||
var path = ioHost.resolvePath(fileName);
|
||||
var dirName = ioHost.dirName(path);
|
||||
createDirectoryStructure(ioHost, dirName);
|
||||
return ioHost.createFile(path, useUTF8);
|
||||
}
|
||||
IOUtils.createFileAndFolderStructure = createFileAndFolderStructure;
|
||||
function throwIOError(message, error) {
|
||||
var errorMessage = message;
|
||||
if(error && error.message) {
|
||||
errorMessage += (" " + error.message);
|
||||
}
|
||||
throw new Error(errorMessage);
|
||||
}
|
||||
IOUtils.throwIOError = throwIOError;
|
||||
})(IOUtils || (IOUtils = {}));
|
||||
|
||||
var IO = (function () {
|
||||
function getWindowsScriptHostIO() {
|
||||
var fso = new ActiveXObject("Scripting.FileSystemObject");
|
||||
var streamObjectPool = [];
|
||||
function getStreamObject() {
|
||||
if(streamObjectPool.length > 0) {
|
||||
return streamObjectPool.pop();
|
||||
} else {
|
||||
return new ActiveXObject("ADODB.Stream");
|
||||
}
|
||||
}
|
||||
function releaseStreamObject(obj) {
|
||||
streamObjectPool.push(obj);
|
||||
}
|
||||
var args = [];
|
||||
for(var i = 0; i < WScript.Arguments.length; i++) {
|
||||
args[i] = WScript.Arguments.Item(i);
|
||||
}
|
||||
return {
|
||||
readFile: function (path) {
|
||||
try {
|
||||
var streamObj = getStreamObject();
|
||||
streamObj.Open();
|
||||
streamObj.Type = 2;
|
||||
streamObj.Charset = 'x-ansi';
|
||||
streamObj.LoadFromFile(path);
|
||||
var bomChar = streamObj.ReadText(2);
|
||||
streamObj.Position = 0;
|
||||
if((bomChar.charCodeAt(0) == 254 && bomChar.charCodeAt(1) == 255) || (bomChar.charCodeAt(0) == 255 && bomChar.charCodeAt(1) == 254)) {
|
||||
streamObj.Charset = 'unicode';
|
||||
} else if(bomChar.charCodeAt(0) == 239 && bomChar.charCodeAt(1) == 187) {
|
||||
streamObj.Charset = 'utf-8';
|
||||
}
|
||||
var str = streamObj.ReadText(-1);
|
||||
streamObj.Close();
|
||||
releaseStreamObject(streamObj);
|
||||
return str;
|
||||
} catch (err) {
|
||||
IOUtils.throwIOError("Error reading file \"" + path + "\".", err);
|
||||
}
|
||||
},
|
||||
writeFile: function (path, contents) {
|
||||
var file = this.createFile(path);
|
||||
file.Write(contents);
|
||||
file.Close();
|
||||
},
|
||||
fileExists: function (path) {
|
||||
return fso.FileExists(path);
|
||||
},
|
||||
resolvePath: function (path) {
|
||||
return fso.GetAbsolutePathName(path);
|
||||
},
|
||||
dirName: function (path) {
|
||||
return fso.GetParentFolderName(path);
|
||||
},
|
||||
findFile: function (rootPath, partialFilePath) {
|
||||
var path = fso.GetAbsolutePathName(rootPath) + "/" + partialFilePath;
|
||||
while(true) {
|
||||
if(fso.FileExists(path)) {
|
||||
try {
|
||||
var content = this.readFile(path);
|
||||
return {
|
||||
content: content,
|
||||
path: path
|
||||
};
|
||||
} catch (err) {
|
||||
}
|
||||
} else {
|
||||
rootPath = fso.GetParentFolderName(fso.GetAbsolutePathName(rootPath));
|
||||
if(rootPath == "") {
|
||||
return null;
|
||||
} else {
|
||||
path = fso.BuildPath(rootPath, partialFilePath);
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
deleteFile: function (path) {
|
||||
try {
|
||||
if(fso.FileExists(path)) {
|
||||
fso.DeleteFile(path, true);
|
||||
}
|
||||
} catch (e) {
|
||||
IOUtils.throwIOError("Couldn't delete file '" + path + "'.", e);
|
||||
}
|
||||
},
|
||||
createFile: function (path, useUTF8) {
|
||||
try {
|
||||
var streamObj = getStreamObject();
|
||||
streamObj.Charset = useUTF8 ? 'utf-8' : 'x-ansi';
|
||||
streamObj.Open();
|
||||
return {
|
||||
Write: function (str) {
|
||||
streamObj.WriteText(str, 0);
|
||||
},
|
||||
WriteLine: function (str) {
|
||||
streamObj.WriteText(str, 1);
|
||||
},
|
||||
Close: function () {
|
||||
try {
|
||||
streamObj.SaveToFile(path, 2);
|
||||
} catch (saveError) {
|
||||
IOUtils.throwIOError("Couldn't write to file '" + path + "'.", saveError);
|
||||
}finally {
|
||||
if(streamObj.State != 0) {
|
||||
streamObj.Close();
|
||||
}
|
||||
releaseStreamObject(streamObj);
|
||||
}
|
||||
}
|
||||
};
|
||||
} catch (creationError) {
|
||||
IOUtils.throwIOError("Couldn't write to file '" + path + "'.", creationError);
|
||||
}
|
||||
},
|
||||
directoryExists: function (path) {
|
||||
return fso.FolderExists(path);
|
||||
},
|
||||
createDirectory: function (path) {
|
||||
try {
|
||||
if(!this.directoryExists(path)) {
|
||||
fso.CreateFolder(path);
|
||||
}
|
||||
} catch (e) {
|
||||
IOUtils.throwIOError("Couldn't create directory '" + path + "'.", e);
|
||||
}
|
||||
},
|
||||
dir: function (path, spec, options) {
|
||||
options = options || {
|
||||
};
|
||||
function filesInFolder(folder, root) {
|
||||
var paths = [];
|
||||
var fc;
|
||||
if(options.recursive) {
|
||||
fc = new Enumerator(folder.subfolders);
|
||||
for(; !fc.atEnd(); fc.moveNext()) {
|
||||
paths = paths.concat(filesInFolder(fc.item(), root + "/" + fc.item().Name));
|
||||
}
|
||||
}
|
||||
fc = new Enumerator(folder.files);
|
||||
for(; !fc.atEnd(); fc.moveNext()) {
|
||||
if(!spec || fc.item().Name.match(spec)) {
|
||||
paths.push(root + "/" + fc.item().Name);
|
||||
}
|
||||
}
|
||||
return paths;
|
||||
}
|
||||
var folder = fso.GetFolder(path);
|
||||
var paths = [];
|
||||
return filesInFolder(folder, path);
|
||||
},
|
||||
print: function (str) {
|
||||
WScript.StdOut.Write(str);
|
||||
},
|
||||
printLine: function (str) {
|
||||
WScript.Echo(str);
|
||||
},
|
||||
arguments: args,
|
||||
stderr: WScript.StdErr,
|
||||
stdout: WScript.StdOut,
|
||||
watchFile: null,
|
||||
run: function (source, filename) {
|
||||
try {
|
||||
eval(source);
|
||||
} catch (e) {
|
||||
IOUtils.throwIOError("Error while executing file '" + filename + "'.", e);
|
||||
}
|
||||
},
|
||||
getExecutingFilePath: function () {
|
||||
return WScript.ScriptFullName;
|
||||
},
|
||||
quit: function (exitCode) {
|
||||
if (typeof exitCode === "undefined") { exitCode = 0; }
|
||||
try {
|
||||
WScript.Quit(exitCode);
|
||||
} catch (e) {
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
;
|
||||
function getNodeIO() {
|
||||
var _fs = require('fs');
|
||||
var _path = require('path');
|
||||
var _module = require('module');
|
||||
return {
|
||||
readFile: function (file) {
|
||||
try {
|
||||
var buffer = _fs.readFileSync(file);
|
||||
switch(buffer[0]) {
|
||||
case 254:
|
||||
if(buffer[1] == 255) {
|
||||
var i = 0;
|
||||
while((i + 1) < buffer.length) {
|
||||
var temp = buffer[i];
|
||||
buffer[i] = buffer[i + 1];
|
||||
buffer[i + 1] = temp;
|
||||
i += 2;
|
||||
}
|
||||
return buffer.toString("ucs2", 2);
|
||||
}
|
||||
break;
|
||||
case 255:
|
||||
if(buffer[1] == 254) {
|
||||
return buffer.toString("ucs2", 2);
|
||||
}
|
||||
break;
|
||||
case 239:
|
||||
if(buffer[1] == 187) {
|
||||
return buffer.toString("utf8", 3);
|
||||
}
|
||||
}
|
||||
return buffer.toString();
|
||||
} catch (e) {
|
||||
IOUtils.throwIOError("Error reading file \"" + file + "\".", e);
|
||||
}
|
||||
},
|
||||
writeFile: _fs.writeFileSync,
|
||||
deleteFile: function (path) {
|
||||
try {
|
||||
_fs.unlinkSync(path);
|
||||
} catch (e) {
|
||||
IOUtils.throwIOError("Couldn't delete file '" + path + "'.", e);
|
||||
}
|
||||
},
|
||||
fileExists: function (path) {
|
||||
return _fs.existsSync(path);
|
||||
},
|
||||
createFile: function (path, useUTF8) {
|
||||
function mkdirRecursiveSync(path) {
|
||||
var stats = _fs.statSync(path);
|
||||
if(stats.isFile()) {
|
||||
IOUtils.throwIOError("\"" + path + "\" exists but isn't a directory.", null);
|
||||
} else if(stats.isDirectory()) {
|
||||
return;
|
||||
} else {
|
||||
mkdirRecursiveSync(_path.dirname(path));
|
||||
_fs.mkdirSync(path, 775);
|
||||
}
|
||||
}
|
||||
mkdirRecursiveSync(_path.dirname(path));
|
||||
try {
|
||||
var fd = _fs.openSync(path, 'w');
|
||||
} catch (e) {
|
||||
IOUtils.throwIOError("Couldn't write to file '" + path + "'.", e);
|
||||
}
|
||||
return {
|
||||
Write: function (str) {
|
||||
_fs.writeSync(fd, str);
|
||||
},
|
||||
WriteLine: function (str) {
|
||||
_fs.writeSync(fd, str + '\r\n');
|
||||
},
|
||||
Close: function () {
|
||||
_fs.closeSync(fd);
|
||||
fd = null;
|
||||
}
|
||||
};
|
||||
},
|
||||
dir: function dir(path, spec, options) {
|
||||
options = options || {
|
||||
};
|
||||
function filesInFolder(folder, deep) {
|
||||
var paths = [];
|
||||
var files = _fs.readdirSync(folder);
|
||||
for(var i = 0; i < files.length; i++) {
|
||||
var stat = _fs.statSync(folder + "/" + files[i]);
|
||||
if(options.recursive && stat.isDirectory()) {
|
||||
if(deep < (options.deep || 100)) {
|
||||
paths = paths.concat(filesInFolder(folder + "/" + files[i], 1));
|
||||
}
|
||||
} else if(stat.isFile() && (!spec || files[i].match(spec))) {
|
||||
paths.push(folder + "/" + files[i]);
|
||||
}
|
||||
}
|
||||
return paths;
|
||||
}
|
||||
return filesInFolder(path, 0);
|
||||
},
|
||||
createDirectory: function (path) {
|
||||
try {
|
||||
if(!this.directoryExists(path)) {
|
||||
_fs.mkdirSync(path);
|
||||
}
|
||||
} catch (e) {
|
||||
IOUtils.throwIOError("Couldn't create directory '" + path + "'.", e);
|
||||
}
|
||||
},
|
||||
directoryExists: function (path) {
|
||||
return _fs.existsSync(path) && _fs.lstatSync(path).isDirectory();
|
||||
},
|
||||
resolvePath: function (path) {
|
||||
return _path.resolve(path);
|
||||
},
|
||||
dirName: function (path) {
|
||||
return _path.dirname(path);
|
||||
},
|
||||
findFile: function (rootPath, partialFilePath) {
|
||||
var path = rootPath + "/" + partialFilePath;
|
||||
while(true) {
|
||||
if(_fs.existsSync(path)) {
|
||||
try {
|
||||
var content = this.readFile(path);
|
||||
return {
|
||||
content: content,
|
||||
path: path
|
||||
};
|
||||
} catch (err) {
|
||||
}
|
||||
} else {
|
||||
var parentPath = _path.resolve(rootPath, "..");
|
||||
if(rootPath === parentPath) {
|
||||
return null;
|
||||
} else {
|
||||
rootPath = parentPath;
|
||||
path = _path.resolve(rootPath, partialFilePath);
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
print: function (str) {
|
||||
process.stdout.write(str);
|
||||
},
|
||||
printLine: function (str) {
|
||||
process.stdout.write(str + '\n');
|
||||
},
|
||||
arguments: process.argv.slice(2),
|
||||
stderr: {
|
||||
Write: function (str) {
|
||||
process.stderr.write(str);
|
||||
},
|
||||
WriteLine: function (str) {
|
||||
process.stderr.write(str + '\n');
|
||||
},
|
||||
Close: function () {
|
||||
}
|
||||
},
|
||||
stdout: {
|
||||
Write: function (str) {
|
||||
process.stdout.write(str);
|
||||
},
|
||||
WriteLine: function (str) {
|
||||
process.stdout.write(str + '\n');
|
||||
},
|
||||
Close: function () {
|
||||
}
|
||||
},
|
||||
watchFile: function (filename, callback) {
|
||||
var firstRun = true;
|
||||
var processingChange = false;
|
||||
var fileChanged = function (curr, prev) {
|
||||
if(!firstRun) {
|
||||
if(curr.mtime < prev.mtime) {
|
||||
return;
|
||||
}
|
||||
_fs.unwatchFile(filename, fileChanged);
|
||||
if(!processingChange) {
|
||||
processingChange = true;
|
||||
callback(filename);
|
||||
setTimeout(function () {
|
||||
processingChange = false;
|
||||
}, 100);
|
||||
}
|
||||
}
|
||||
firstRun = false;
|
||||
_fs.watchFile(filename, {
|
||||
persistent: true,
|
||||
interval: 500
|
||||
}, fileChanged);
|
||||
};
|
||||
fileChanged();
|
||||
return {
|
||||
filename: filename,
|
||||
close: function () {
|
||||
_fs.unwatchFile(filename, fileChanged);
|
||||
}
|
||||
};
|
||||
},
|
||||
run: function (source, filename) {
|
||||
require.main.filename = filename;
|
||||
require.main.paths = _module._nodeModulePaths(_path.dirname(_fs.realpathSync(filename)));
|
||||
require.main._compile(source, filename);
|
||||
},
|
||||
getExecutingFilePath: function () {
|
||||
return process.mainModule.filename;
|
||||
},
|
||||
quit: process.exit
|
||||
};
|
||||
}
|
||||
;
|
||||
if(typeof ActiveXObject === "function") {
|
||||
return getWindowsScriptHostIO();
|
||||
} else if(typeof require === "function") {
|
||||
return getNodeIO();
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
})();
|
||||
var cfg = {
|
||||
root: '.',
|
||||
pattern: /.\-tests\.ts/g,
|
||||
tsc: 'node ./_infrastructure/tests/typescript/tsc.js ',
|
||||
exclude: {
|
||||
'.git': true,
|
||||
'.gitignore': true,
|
||||
'package.json': true,
|
||||
'_infrastructure': true,
|
||||
'.travis.yml': true,
|
||||
'LICENSE': true,
|
||||
'README.md': true,
|
||||
'_ReSharper.DefinitelyTyped': true,
|
||||
'obj': true,
|
||||
'bin': true,
|
||||
'Properties': true,
|
||||
'DefinitelyTyped.csproj': true,
|
||||
'DefinitelyTyped.csproj.user': true,
|
||||
'DefinitelyTyped.sln': true,
|
||||
'DefinitelyTyped.v11.suo': true
|
||||
}
|
||||
};
|
||||
if(process.argv.length > 2) {
|
||||
cfg.root = process.argv[2];
|
||||
}
|
||||
var TestFile = (function () {
|
||||
function TestFile() {
|
||||
this.errors = [];
|
||||
}
|
||||
return TestFile;
|
||||
})();
|
||||
var Test = (function () {
|
||||
function Test(lib) {
|
||||
this.lib = lib;
|
||||
this.files = [];
|
||||
}
|
||||
return Test;
|
||||
})();
|
||||
var Tests = (function () {
|
||||
function Tests() {
|
||||
this.tests = [];
|
||||
}
|
||||
return Tests;
|
||||
})();
|
||||
function getLibDirectory(file) {
|
||||
return file.substr(cfg.root.length).split('/')[1];
|
||||
}
|
||||
function getErrorList(out) {
|
||||
var splitContentByNewlines = function (content) {
|
||||
var lines = content.split('\r\n');
|
||||
if(lines.length === 1) {
|
||||
lines = content.split('\n');
|
||||
}
|
||||
return lines;
|
||||
};
|
||||
var result = [];
|
||||
var lines = splitContentByNewlines(out);
|
||||
for(var i = 0; i < lines.length; i++) {
|
||||
if(lines[i]) {
|
||||
result.push(lines[i]);
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
function runTests(testFiles) {
|
||||
var tests = new Tests();
|
||||
Exec.exec(cfg.tsc, [
|
||||
testFiles[testIndex]
|
||||
], function (ExecResult) {
|
||||
var lib = getLibDirectory(testFiles[testIndex]);
|
||||
cache_visited_libs[lib] = true;
|
||||
var testFile = new TestFile();
|
||||
testFile.name = testFiles[testIndex];
|
||||
testFile.errors = getErrorList(ExecResult.stderr);
|
||||
if(testFile.errors.length == 0) {
|
||||
total_success++;
|
||||
} else {
|
||||
total_failure++;
|
||||
}
|
||||
console.log(' [\033[36m' + lib + '\033[0m] ' + testFiles[testIndex].substr(cfg.root.length) + ' - ' + (testFile.errors.length == 0 ? '\033[32msuccess\033[0m' : '\033[31mfailure\033[0m'));
|
||||
var test = new Test(lib);
|
||||
test.files.push(testFile);
|
||||
tests.tests.push(test);
|
||||
testIndex++;
|
||||
if(testIndex < totalTest) {
|
||||
Exec.exec(cfg.tsc, [
|
||||
testFiles[testIndex]
|
||||
], arguments.callee);
|
||||
} else {
|
||||
var withoutTests = {
|
||||
};
|
||||
for(var k = 0; k < allFiles.length; k++) {
|
||||
var rootFolder = allFiles[k].substr(cfg.root.length).split('/')[1];
|
||||
if(!(rootFolder in cfg.exclude)) {
|
||||
if(!(rootFolder in cache_visited_libs)) {
|
||||
withoutTests[rootFolder] = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
var withoutTestsCount = 0;
|
||||
for(var attr in withoutTests) {
|
||||
var test = new Test(attr);
|
||||
tests.tests.push(test);
|
||||
console.log(' [\033[36m' + attr + '\033[0m] without tests');
|
||||
withoutTestsCount++;
|
||||
}
|
||||
console.log('\n> ' + (total_failure + total_success + withoutTestsCount) + ' tests. ' + '\033[32m' + total_success + ' tests success\033[0m, ' + '\033[31m' + total_failure + ' tests failed\033[0m and ' + withoutTestsCount + ' definitions without tests.\n');
|
||||
if(total_failure > 0) {
|
||||
process.exit(1);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
var testFiles = IO.dir(cfg.root, cfg.pattern, {
|
||||
recursive: true,
|
||||
deep: 1
|
||||
});
|
||||
var allFiles = IO.dir(cfg.root, null, {
|
||||
recursive: true
|
||||
});
|
||||
var totalTest = testFiles.length;
|
||||
var testIndex = 0;
|
||||
var cache_visited_libs = {
|
||||
};
|
||||
var total_failure = 0;
|
||||
var total_success = 0;
|
||||
var tscVersion = '?.?.?';
|
||||
Exec.exec(cfg.tsc, [
|
||||
'-version'
|
||||
], function (ExecResult) {
|
||||
tscVersion = ExecResult.stdout;
|
||||
console.log('$ tsc -version');
|
||||
console.log(tscVersion);
|
||||
runTests(testFiles);
|
||||
});
|
||||
var cfg = {
|
||||
root: '.',
|
||||
pattern: /.\-tests\.ts/g,
|
||||
tsc: 'node ./_infrastructure/tests/typescript/tsc.js ',
|
||||
exclude: {
|
||||
'.git': true,
|
||||
'.gitignore': true,
|
||||
'package.json': true,
|
||||
'_infrastructure': true,
|
||||
'.travis.yml': true,
|
||||
'LICENSE': true,
|
||||
'README.md': true,
|
||||
'_ReSharper.DefinitelyTyped': true,
|
||||
'obj': true,
|
||||
'bin': true,
|
||||
'Properties': true,
|
||||
'DefinitelyTyped.csproj': true,
|
||||
'DefinitelyTyped.csproj.user': true,
|
||||
'DefinitelyTyped.sln': true,
|
||||
'DefinitelyTyped.v11.suo': true
|
||||
}
|
||||
};
|
||||
|
||||
if (process.argv.length > 2) {
|
||||
cfg.root = process.argv[2];
|
||||
}
|
||||
|
||||
var TestFile = (function () {
|
||||
function TestFile() {
|
||||
this.errors = [];
|
||||
}
|
||||
return TestFile;
|
||||
})();
|
||||
|
||||
var Test = (function () {
|
||||
function Test(lib) {
|
||||
this.lib = lib;
|
||||
this.files = [];
|
||||
}
|
||||
return Test;
|
||||
})();
|
||||
|
||||
var Tests = (function () {
|
||||
function Tests() {
|
||||
this.tests = [];
|
||||
}
|
||||
return Tests;
|
||||
})();
|
||||
|
||||
function getLibDirectory(file) {
|
||||
return file.substr(cfg.root.length).split('/')[1];
|
||||
}
|
||||
|
||||
function getErrorList(out) {
|
||||
var splitContentByNewlines = function (content) {
|
||||
var lines = content.split('\r\n');
|
||||
if (lines.length === 1) {
|
||||
lines = content.split('\n');
|
||||
}
|
||||
return lines;
|
||||
};
|
||||
|
||||
var result = [];
|
||||
|
||||
var lines = splitContentByNewlines(out);
|
||||
|
||||
for (var i = 0; i < lines.length; i++) {
|
||||
if (lines[i]) {
|
||||
result.push(lines[i]);
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
function runTests(testFiles) {
|
||||
var tests = new Tests();
|
||||
|
||||
Exec.exec(cfg.tsc, [testFiles[testIndex]], function (ExecResult) {
|
||||
var lib = getLibDirectory(testFiles[testIndex]);
|
||||
|
||||
cache_visited_libs[lib] = true;
|
||||
|
||||
var testFile = new TestFile();
|
||||
testFile.name = testFiles[testIndex];
|
||||
testFile.errors = getErrorList(ExecResult.stderr);
|
||||
|
||||
if (testFile.errors.length == 0) {
|
||||
total_success++;
|
||||
} else {
|
||||
total_failure++;
|
||||
}
|
||||
|
||||
console.log(' [\033[36m' + lib + '\033[0m] ' + testFiles[testIndex].substr(cfg.root.length) + ' - ' + (testFile.errors.length == 0 ? '\033[32msuccess\033[0m' : '\033[31mfailure\033[0m'));
|
||||
|
||||
var test = new Test(lib);
|
||||
test.files.push(testFile);
|
||||
tests.tests.push(test);
|
||||
|
||||
testIndex++;
|
||||
if (testIndex < totalTest) {
|
||||
Exec.exec(cfg.tsc, [testFiles[testIndex]], arguments.callee);
|
||||
} else {
|
||||
var withoutTests = {};
|
||||
for (var k = 0; k < allFiles.length; k++) {
|
||||
var rootFolder = allFiles[k].substr(cfg.root.length).split('/')[1];
|
||||
if (!(rootFolder in cfg.exclude)) {
|
||||
if (!(rootFolder in cache_visited_libs)) {
|
||||
withoutTests[rootFolder] = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var withoutTestsCount = 0;
|
||||
for (var attr in withoutTests) {
|
||||
var test = new Test(attr);
|
||||
tests.tests.push(test);
|
||||
|
||||
console.log(' [\033[36m' + attr + '\033[0m] without tests');
|
||||
withoutTestsCount++;
|
||||
}
|
||||
|
||||
console.log('\n> ' + (total_failure + total_success + withoutTestsCount) + ' tests. ' + '\033[32m' + total_success + ' tests success\033[0m, ' + '\033[31m' + total_failure + ' tests failed\033[0m and ' + withoutTestsCount + ' definitions without tests.\n');
|
||||
|
||||
if (total_failure > 0) {
|
||||
process.exit(1);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
var testFiles = IO.dir(cfg.root, cfg.pattern, { recursive: true, deep: 1 });
|
||||
|
||||
var allFiles = IO.dir(cfg.root, null, { recursive: true });
|
||||
|
||||
var totalTest = testFiles.length;
|
||||
var testIndex = 0;
|
||||
var cache_visited_libs = {};
|
||||
|
||||
var total_failure = 0;
|
||||
var total_success = 0;
|
||||
|
||||
var tscVersion = '?.?.?';
|
||||
|
||||
Exec.exec(cfg.tsc, ['-version'], function (ExecResult) {
|
||||
tscVersion = ExecResult.stdout;
|
||||
|
||||
console.log('$ tsc -version');
|
||||
console.log(tscVersion);
|
||||
|
||||
runTests(testFiles);
|
||||
});
|
||||
|
||||
9074
_infrastructure/typescript/lib.d.ts
vendored
Normal file
9074
_infrastructure/typescript/lib.d.ts
vendored
Normal file
File diff suppressed because it is too large
Load Diff
2
_infrastructure/typescript/tsc
Normal file
2
_infrastructure/typescript/tsc
Normal file
@ -0,0 +1,2 @@
|
||||
#!/usr/bin/env node
|
||||
require('./tsc.js')
|
||||
@ -2,6 +2,6 @@
|
||||
"name": "DefinitelyTyped",
|
||||
"version": "0.0.0",
|
||||
"scripts": {
|
||||
"test": "node ./_infrastructure/tests/testRunner.js"
|
||||
"test": "node ./_infrastructure/runner.js"
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user