ts-runtime
Advanced tools
Comparing version 0.1.16 to 0.1.17
@@ -110,5 +110,5 @@ #!/usr/bin/env node | ||
} | ||
function setKeepTemp() { | ||
options.keepTemp = true; | ||
} | ||
// function setKeepTemp() { | ||
// options.keepTemp = true; | ||
// } | ||
function setLibIdentifier(identifier) { | ||
@@ -123,2 +123,5 @@ options.libIdentifier = identifier; | ||
} | ||
function setModuleAlias() { | ||
options.moduleAlias = true; | ||
} | ||
function setDeclarationPrefix(prefix) { | ||
@@ -130,8 +133,5 @@ options.declarationPrefix = prefix; | ||
} | ||
function setTempFolderName(name) { | ||
options.tempFolderName = name; | ||
} | ||
function setModuleAlias() { | ||
options.moduleAlias = true; | ||
} | ||
// function setTempFolderName(name: string) { | ||
// options.tempFolderName = name; | ||
// } | ||
commander | ||
@@ -148,7 +148,6 @@ .version(pkg.version, '-v, --version') | ||
.option('-d, --declarationFileName <fileName>', 'set file name for global declarations. defaults to "tsr-declarations"', setDeclarationFileName) | ||
.option('-e, --excludeDeclarationFile', 'don not import the ambient declarationsfile. defaults to false', setExcludeDeclarationFile) | ||
.option('-e, --excludeDeclarationFile', 'do not import the ambient declarations file. defaults to false', setExcludeDeclarationFile) | ||
.option('-E, --excludeLib', 'do not automatically import the runtime library. defaults to false', setExcludeLib) | ||
.option('-f, --force', 'try to finish on TypeScript compiler error. defaults to false', setForce) | ||
.option('-F, --fast', 'no status for the command line, but faster processing. defaults to false', setFast) | ||
.option('-k, --keepTemp', 'keep temporary files. defaults to false', setKeepTemp) | ||
.option('-l, --libIdentifier <name>', 'lib import name. defaults to "t"', setLibIdentifier) | ||
@@ -160,3 +159,2 @@ .option('-L, --libDeclarations', 'reflect declarations from global libs (e.g. DOM). defaults to false', setLibDeclarations) | ||
.option('-s, --stackTraceOutput <limit>', 'output a specified number of lines of the stack trace. defaults to 3', setStackTraceOutput) | ||
.option('-t, --tempFolderName <name>', 'set folder name for temporary files. defaults to ".tsr"', setTempFolderName) | ||
.on('-h, --help', () => { | ||
@@ -163,0 +161,0 @@ console.log(' Examples:'); |
@@ -10,2 +10,1 @@ import * as bus from './bus'; | ||
export * from './transform'; | ||
export * from './transformModules'; |
@@ -16,2 +16,1 @@ "use strict"; | ||
__export(require("./transform")); | ||
__export(require("./transformModules")); |
{ | ||
"name": "ts-runtime", | ||
"version": "0.1.16", | ||
"version": "0.1.17", | ||
"description": "Runtime type checks for TypeScript", | ||
@@ -17,5 +17,3 @@ "main": "index.js", | ||
"build": "./node_modules/.bin/tsc --rootDir src --outDir dist --module CommonJS --target es6 --skipLibCheck && cp package.json dist/package.json && cp README.md dist/README.md && cp LICENSE dist/LICENSE", | ||
"build:docs": "yarn build:docs:lib && yarn build:docs:playground", | ||
"build:docs:playground": "node_modules/.bin/rimraf docs/*worker.js* && node_modules/.bin/webpack --config webpack.docs.config.js", | ||
"build:docs:lib": "node_modules/.bin/webpack --config webpack.lib.config.js", | ||
"build:docs": "node_modules/.bin/rimraf docs/*worker.js* && node_modules/.bin/webpack", | ||
"build:tsr": "ts-node ./src/bin/index src/index src/lib/index src/bin/index src/bin/process --moduleAlias --compilerOptions '{\"outDir\": \"dist-tsr\", \"strictNullChecks\": false, \"moduleResolution\": \"Node\", \"module\": \"commonjs\", \"target\": \"ES2015\", \"lib\": [\"ESNext\"]}'", | ||
@@ -27,2 +25,7 @@ "build:tsr:fast": "ts-node ./src/bin/index src/index src/lib/index src/bin/index src/bin/process --fast --moduleAlias --compilerOptions '{\"outDir\": \"dist-tsr\", \"strictNullChecks\": false, \"moduleResolution\": \"Node\", \"module\": \"commonjs\", \"target\": \"ES2015\", \"lib\": [\"ESNext\"]}'" | ||
}, | ||
"tsr": { | ||
"docs": { | ||
"version": "0.1.16-4" | ||
} | ||
}, | ||
"dependencies": { | ||
@@ -53,2 +56,4 @@ "chalk": "^2.0.1", | ||
"monaco-editor": "^0.9.0", | ||
"monaco-typescript": "git+https://git@github.com/Microsoft/monaco-typescript", | ||
"null-loader": "^0.1.1", | ||
"raw-loader": "^0.5.1", | ||
@@ -55,0 +60,0 @@ "source-map-loader": "^0.2.1", |
@@ -0,3 +1,5 @@ | ||
import { FileReflection } from './host'; | ||
import { Options } from './options'; | ||
export declare function transform(entryFiles: string[], options?: Options): void; | ||
export declare function transformReflection(rootNames: string | string[], reflections: FileReflection[], options?: Options): FileReflection[]; | ||
export declare function getOptions(options?: Options): Options; |
232
transform.js
@@ -12,2 +12,3 @@ "use strict"; | ||
const mutators_1 = require("./mutators"); | ||
const host_1 = require("./host"); | ||
const options_1 = require("./options"); | ||
@@ -20,2 +21,9 @@ const scanner_1 = require("./scanner"); | ||
exports.transform = transform; | ||
function transformReflection(rootNames, reflections, options) { | ||
const host = transformProgram(rootNames, options, reflections); | ||
if (!host) | ||
return []; | ||
return host.getResult(); | ||
} | ||
exports.transformReflection = transformReflection; | ||
function getOptions(options = {}) { | ||
@@ -27,14 +35,34 @@ const opts = Object.assign({}, options_1.defaultOptions, options); | ||
exports.getOptions = getOptions; | ||
function transformProgram(entryFiles, options) { | ||
start = elapsed = process.hrtime(); | ||
function transformProgram(rootNames, options, reflections) { | ||
if (!reflections) | ||
start = elapsed = process.hrtime(); | ||
const entryFiles = util | ||
.asArray(rootNames) | ||
.map(file => path.normalize(file)) | ||
.map(file => !path.extname(file) ? file + '.ts' : file); | ||
options = getOptions(options); | ||
emit(bus.events.START); | ||
entryFiles = entryFiles | ||
.map(file => path.normalize(file)) | ||
.map(file => !path.extname(file) ? file + '.ts' : file); | ||
const commonDir = commondir(entryFiles.map(f => path.dirname(path.resolve(f)))); | ||
setCompilerOptions(); | ||
let tempEntryFiles = entryFiles.map(f => path.resolve(f)); | ||
let tempEntryFiles; | ||
let commonDir; | ||
let host; | ||
if (!reflections) { | ||
tempEntryFiles = entryFiles.map(f => path.resolve(f)); | ||
commonDir = commondir(tempEntryFiles.map(f => path.dirname(f))); | ||
setCompilerOptions(); | ||
host = ts.createCompilerHost(options.compilerOptions, true); | ||
} | ||
else { | ||
tempEntryFiles = entryFiles.map(f => path.join(`${path.resolve(path.dirname(f))}`, path.basename(f))); | ||
commonDir = commondir(tempEntryFiles.map(f => path.dirname(f))); | ||
reflections = reflections.map(f => { | ||
return { | ||
name: path.join(`${path.resolve(path.dirname(f.name))}`, path.basename(f.name)), | ||
text: f.text | ||
}; | ||
}); | ||
setCompilerOptions(); | ||
setCompilerOptionsForReflection(); | ||
host = new host_1.Host(reflections, options.compilerOptions, true); | ||
} | ||
let tempBasePath = options.compilerOptions.rootDir; | ||
let host; | ||
let program; | ||
@@ -46,21 +74,8 @@ let scanner; | ||
function startTransformation() { | ||
let sourceFiles; | ||
deleteTempFiles(); | ||
host = ts.createCompilerHost(options.compilerOptions, true); | ||
program = ts.createProgram(tempEntryFiles, options.compilerOptions, host); | ||
const diagnostics = []; | ||
diagnostics.push(...program.getOptionsDiagnostics()); | ||
diagnostics.push(...program.getGlobalDiagnostics()); | ||
for (let sourceFile of program.getSourceFiles().filter(sf => !/\.d\.ts$/.test(sf.fileName))) { | ||
diagnostics.push(...program.getSyntacticDiagnostics(sourceFile)); | ||
diagnostics.push(...program.getSemanticDiagnostics(sourceFile)); | ||
} | ||
// Check original file (pre-diagnostics) | ||
if (!check(diagnostics, options.log) && !options.force) { | ||
if (!options.keepTemp) | ||
deleteTempFiles(); | ||
if (!check(getDiagnostics(), options.log) && !options.force) { | ||
emit(bus.events.STOP); | ||
return; | ||
} | ||
sourceFiles = program.getSourceFiles().filter(sf => !sf.isDeclarationFile); | ||
const sourceFiles = program.getSourceFiles().filter(sf => !sf.isDeclarationFile); | ||
emit(bus.events.SCAN, getElapsedTime()); | ||
@@ -71,66 +86,20 @@ scanner = new scanner_1.Scanner(program, options); | ||
emit(bus.events.EMIT, getElapsedTime()); | ||
writeTempFiles(result); | ||
// do not check post-diagnostics of temp file | ||
// check(result.diagnostics, options.log) | ||
emitDeclarations(); | ||
if (!emitTransformed() && !options.force) { | ||
if (!options.keepTemp) | ||
deleteTempFiles(); | ||
if (!emitTransformed(result) && !options.force) { | ||
emit(bus.events.STOP); | ||
return; | ||
} | ||
emit(bus.events.CLEAN, getElapsedTime()); | ||
if (!options.keepTemp) { | ||
deleteTempFiles(); | ||
} | ||
emitDeclarations(); | ||
result.dispose(); | ||
emit(bus.events.END, getElapsedTime(), getElapsedTime(true)); | ||
return host; | ||
} | ||
; | ||
function getOutDir() { | ||
if (options.compilerOptions.outFile) { | ||
return path.dirname(options.compilerOptions.outFile); | ||
} | ||
if (options.compilerOptions.outDir) { | ||
return options.compilerOptions.outDir; | ||
} | ||
return commonDir; | ||
} | ||
function deleteTempFiles() { | ||
const tempPath = path.join(commonDir, options.tempFolderName); | ||
rimraf.sync(tempPath); | ||
} | ||
function createProgramFromTempFiles() { | ||
tempEntryFiles = tempEntryFiles.map(f => toTempPath(f)); | ||
tempBasePath = path.join(tempBasePath, options.tempFolderName); | ||
options.compilerOptions.rootDir = tempBasePath; | ||
host = ts.createCompilerHost(options.compilerOptions); | ||
program = ts.createProgram(tempEntryFiles, options.compilerOptions, host, undefined); | ||
} | ||
function writeTempFiles(result) { | ||
const printHandlers = {}; | ||
const printer = ts.createPrinter(undefined, printHandlers); | ||
for (let transformed of result.transformed) { | ||
const filePath = toTempPath(transformed.fileName); | ||
const source = printer.printFile(transformed); | ||
ts.sys.writeFile(filePath, source); | ||
} | ||
} | ||
function emitTransformed() { | ||
createProgramFromTempFiles(); | ||
function emitTransformed(result) { | ||
if (!options.compilerOptions.outFile && !options.compilerOptions.outDir) { | ||
options.compilerOptions.outDir = commonDir; | ||
} | ||
const diagnostics = []; | ||
diagnostics.push(...program.getOptionsDiagnostics()); | ||
diagnostics.push(...program.getGlobalDiagnostics()); | ||
for (let sourceFile of program.getSourceFiles().filter(sf => !/\.d\.ts$/.test(sf.fileName))) { | ||
diagnostics.push(...program.getSyntacticDiagnostics(sourceFile)); | ||
diagnostics.push(...program.getSemanticDiagnostics(sourceFile)); | ||
} | ||
// do not check pre-diagnostics of temp file | ||
// check(diagnostics, options.log); | ||
const emitResult = program.emit(); | ||
// check final result (post-diagnostics) | ||
return check(emitResult.diagnostics, options.log); | ||
host = getHostFromTransformationResult(result); | ||
program = ts.createProgram(tempEntryFiles, options.compilerOptions, host, undefined); | ||
const { diagnostics } = program.emit(); | ||
return check(diagnostics, options.log); | ||
} | ||
@@ -141,3 +110,4 @@ function emitDeclarations() { | ||
const location = path.join(outDir, filename); | ||
rimraf.sync(location); | ||
if (!reflections) | ||
rimraf.sync(location); | ||
const printerOptions = { | ||
@@ -149,3 +119,3 @@ removeComments: false | ||
node.parent = undefined; | ||
node.flags = ts.NodeFlags.Synthesized; | ||
node.flags |= ts.NodeFlags.Synthesized; | ||
return node; | ||
@@ -155,3 +125,3 @@ } | ||
const printer = ts.createPrinter(printerOptions, printHandlers); | ||
let sf = ts.createSourceFile(filename, '', options.compilerOptions.target, true, ts.ScriptKind.TS); | ||
let sourceFile = ts.createSourceFile(filename, '', options.compilerOptions.target, true, ts.ScriptKind.TS); | ||
const expressions = []; | ||
@@ -180,3 +150,3 @@ let names = []; | ||
} | ||
sf = ts.updateSourceFileNode(sf, [ | ||
sourceFile = ts.updateSourceFileNode(sourceFile, [ | ||
context.factory.importLibStatement(), | ||
@@ -187,5 +157,6 @@ ...expressions.map(exp => { | ||
]); | ||
const printed = printer.printFile(sf); | ||
const printed = printer.printFile(sourceFile); | ||
const transpiled = ts.transpile(printed, options.compilerOptions); | ||
ts.sys.writeFile(location, transpiled); | ||
const writeHost = !reflections ? ts.sys : host; | ||
writeHost.writeFile(location, transpiled); | ||
} | ||
@@ -256,38 +227,69 @@ function createMutationContext(node, transformationContext) { | ||
} | ||
function toTempPath(fileName) { | ||
const tempPath = path.dirname(fileName).replace(tempBasePath, ''); | ||
const location = path.join(path.join(tempBasePath, options.tempFolderName, tempPath), path.basename(fileName)); | ||
return location; | ||
function setCompilerOptionsForReflection() { | ||
} | ||
} | ||
function check(diagnostics, log) { | ||
if (diagnostics && diagnostics.length > 0) { | ||
emit(bus.events.DIAGNOSTICS, diagnostics, diagnostics.length); | ||
if (log) { | ||
for (let diag of diagnostics) { | ||
console.error(ts.formatDiagnostics([diag], { | ||
getCurrentDirectory: () => '', | ||
getNewLine: () => '\n', | ||
getCanonicalFileName: (f) => f | ||
})); | ||
function getHostFromTransformationResult(result) { | ||
const previousHost = host; | ||
const printer = ts.createPrinter(); | ||
const files = []; | ||
for (let transformed of result.transformed) { | ||
const name = transformed.fileName; | ||
const text = printer.printFile(transformed); | ||
files.push({ name, text }); | ||
} | ||
const newHost = new host_1.Host(files, options.compilerOptions); | ||
newHost.setNewLine(previousHost.getNewLine()); | ||
newHost.setCurrentDirectory(previousHost.getCurrentDirectory()); | ||
newHost.setDefaultLibFileName(previousHost.getDefaultLibFileName(options.compilerOptions)); | ||
newHost.setDefaultLibLocation(previousHost.getDefaultLibLocation()); | ||
newHost.setUseCaseSensitiveFileNames(previousHost.useCaseSensitiveFileNames()); | ||
newHost.writeFile = previousHost.writeFile; | ||
return newHost; | ||
} | ||
function getDiagnostics() { | ||
const diagnostics = []; | ||
diagnostics.push(...program.getOptionsDiagnostics()); | ||
diagnostics.push(...program.getGlobalDiagnostics()); | ||
for (let sourceFile of program.getSourceFiles().filter(sf => !/\.d\.ts$/.test(sf.fileName))) { | ||
diagnostics.push(...program.getSyntacticDiagnostics(sourceFile)); | ||
diagnostics.push(...program.getSemanticDiagnostics(sourceFile)); | ||
} | ||
return diagnostics; | ||
} | ||
function getOutDir() { | ||
if (options.compilerOptions.outFile) { | ||
return path.dirname(options.compilerOptions.outFile); | ||
} | ||
if (options.compilerOptions.outDir) { | ||
return options.compilerOptions.outDir; | ||
} | ||
return commonDir; | ||
} | ||
function check(diagnostics, log) { | ||
if (diagnostics && diagnostics.length > 0) { | ||
emit(bus.events.DIAGNOSTICS, diagnostics, diagnostics.length); | ||
if (log) { | ||
for (let diag of diagnostics) { | ||
console.error(ts.formatDiagnostics([diag], { | ||
getCurrentDirectory: () => '', | ||
getNewLine: () => '\n', | ||
getCanonicalFileName: (f) => f | ||
}).trim()); | ||
} | ||
} | ||
return false; | ||
} | ||
return false; | ||
return true; | ||
} | ||
return true; | ||
} | ||
function emit(event, ...args) { | ||
return bus.emit(event, args); | ||
} | ||
function getRootNames(rootNames) { | ||
if (Array.isArray(rootNames)) { | ||
return rootNames; | ||
function emit(event, ...args) { | ||
return bus.emit(event, args); | ||
} | ||
return [rootNames]; | ||
function getElapsedTime(fromBeginning = false) { | ||
if (reflections) { | ||
return ''; | ||
} | ||
const time = process.hrtime(fromBeginning ? start : elapsed); | ||
if (!fromBeginning) | ||
elapsed = process.hrtime(); | ||
return format(time, fromBeginning ? 'ms' : void 0); | ||
} | ||
} | ||
function getElapsedTime(fromBeginning = false) { | ||
const time = process.hrtime(fromBeginning ? start : elapsed); | ||
if (!fromBeginning) | ||
elapsed = process.hrtime(); | ||
return format(time, fromBeginning ? 'ms' : void 0); | ||
} |
@@ -7,3 +7,3 @@ import { Options } from './options'; | ||
}; | ||
export declare function transformModules(sources: FileReflection[], options?: Options): FileReflection[]; | ||
export declare function transformModules1(sources: FileReflection[], options?: Options): FileReflection[]; | ||
export declare function transformModule(source: string, options?: Options, name?: string): FileReflection[]; |
@@ -9,6 +9,6 @@ "use strict"; | ||
const scanner_1 = require("./scanner"); | ||
function transformModules(sources, options) { | ||
function transformModules1(sources, options) { | ||
return transformProgram(sources, options); | ||
} | ||
exports.transformModules = transformModules; | ||
exports.transformModules1 = transformModules1; | ||
function transformModule(source, options, name) { | ||
@@ -49,3 +49,4 @@ return transformProgram([ | ||
}, | ||
getDefaultLibFileName: () => "lib.d.ts", | ||
getDefaultLibFileName: (options) => "lib.es6.d.ts", | ||
getDefaultLibLocation: () => "", | ||
getCurrentDirectory: () => "", | ||
@@ -52,0 +53,0 @@ getDirectories: () => [], |
400536
121
9303
22