Comparing version 2.0.23 to 2.0.24
@@ -8,5 +8,5 @@ import type { Logger } from '@radrat-node/logger'; | ||
/** | ||
* Flags that should be used during compilation. --rootDir / and --outDir are required. | ||
* Typescript tsconfig.json compilerOptions. | ||
*/ | ||
flags?: string[]; | ||
compilerOptions?: CompilerOptions; | ||
/** | ||
@@ -25,3 +25,13 @@ * Logger that will be used by compiler. Requires error, warn, info and debug functions. Default: undefined | ||
fallback: boolean; | ||
flags: string[]; | ||
compilerOptions: { | ||
downlevelIteration: boolean; | ||
emitDecoratorMetadata: boolean; | ||
experimentalDecorators: boolean; | ||
module: string; | ||
outDir: string; | ||
resolveJsonModule: boolean; | ||
rootDir: string; | ||
skipLibCheck: boolean; | ||
target: string; | ||
}; | ||
}; | ||
@@ -36,5 +46,3 @@ options: CompilerOptions & typeof Compiler.defaults; | ||
private buildCache; | ||
private getFlag; | ||
private getFlagValue; | ||
} | ||
export declare const tsImport: Compiler; |
@@ -48,8 +48,2 @@ "use strict"; | ||
this.options = options_defaults_1.defaults(Compiler.defaults, options); | ||
if (!this.getFlag("rootDir")) { | ||
this.options.flags.push("--rootDir /"); | ||
} | ||
if (!this.getFlag("outDir")) { | ||
this.options.flags.push("--outDir '" + path.resolve(__dirname, "../cache") + "'"); | ||
} | ||
} | ||
@@ -82,3 +76,3 @@ /** | ||
return __awaiter(this, void 0, void 0, function () { | ||
var logger, tsPath, tsDir, cwd, tsFileName, jsFileName, outDirValue, cacheDir, jsPath, tsWasModified, buildError; | ||
var logger, tsPath, tsDir, cwd, tsFileName, jsFileName, cacheDir, jsPath, tsWasModified, buildError; | ||
return __generator(this, function (_h) { | ||
@@ -91,6 +85,4 @@ switch (_h.label) { | ||
jsFileName = tsFileName.replace(/\.[^/.]+$/u, ".js"); | ||
outDirValue = this.getFlagValue("outDir").replace(/'/gu, "").replace(/"/gu, ""); | ||
cacheDir = path.resolve(outDirValue, "." + tsDir); | ||
cacheDir = path.resolve(this.options.compilerOptions.outDir, "." + tsDir); | ||
jsPath = path.resolve(cacheDir, jsFileName); | ||
// Check if cached scripts.js exist. | ||
(_a = logger === null || logger === void 0 ? void 0 : logger.verbose) === null || _a === void 0 ? void 0 : _a.call(logger, "Looking for cached file at " + jsPath); | ||
@@ -142,55 +134,66 @@ if (!fs.existsSync(jsPath)) return [3 /*break*/, 3]; | ||
return __awaiter(this, void 0, void 0, function () { | ||
var _a, flags, logger; | ||
return __generator(this, function (_b) { | ||
_a = this.options, flags = _a.flags, logger = _a.logger; | ||
// Compile new scripts.ts to .js. | ||
return [2 /*return*/, new Promise(function (resolve, reject) { | ||
var _a, _b; | ||
var compileCommand = "npx -p typescript tsc '" + absoluteTsPath + "' " + flags.join(" "); | ||
(_a = logger === null || logger === void 0 ? void 0 : logger.info) === null || _a === void 0 ? void 0 : _a.call(logger, "Compiling " + absoluteTsPath); | ||
(_b = logger === null || logger === void 0 ? void 0 : logger.debug) === null || _b === void 0 ? void 0 : _b.call(logger, "Command: " + compileCommand); | ||
childProcess.exec(compileCommand, function (err, stdout, stderr) { | ||
var _a, _b, _c; | ||
if (err) { | ||
(_a = logger === null || logger === void 0 ? void 0 : logger.error) === null || _a === void 0 ? void 0 : _a.call(logger, err); | ||
reject(err); | ||
return; | ||
} | ||
if (stdout.trim()) { | ||
(_b = logger === null || logger === void 0 ? void 0 : logger.debug) === null || _b === void 0 ? void 0 : _b.call(logger, stdout); | ||
} | ||
if (stderr.trim()) { | ||
(_c = logger === null || logger === void 0 ? void 0 : logger.debug) === null || _c === void 0 ? void 0 : _c.call(logger, stderr); | ||
} | ||
resolve(stdout); | ||
}); | ||
})]; | ||
var logger, tmpTsConfigPath; | ||
var _this = this; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
logger = this.options.logger; | ||
tmpTsConfigPath = path.join(this.options.compilerOptions.outDir, path.dirname(absoluteTsPath), "tsconfig-" + path.basename(absoluteTsPath) + ".tmp.json"); | ||
return [4 /*yield*/, fs.promises.writeFile(tmpTsConfigPath, JSON.stringify({ | ||
compilerOptions: this.options.compilerOptions, | ||
include: [absoluteTsPath] | ||
}, undefined, 4))]; | ||
case 1: | ||
_a.sent(); | ||
// Compile new scripts.ts to .js. | ||
return [2 /*return*/, new Promise(function (resolve, reject) { | ||
var _a, _b; | ||
var compileCommand = "npx -p typescript tsc --project '" + tmpTsConfigPath + "'"; | ||
(_a = logger === null || logger === void 0 ? void 0 : logger.info) === null || _a === void 0 ? void 0 : _a.call(logger, "Compiling " + absoluteTsPath); | ||
(_b = logger === null || logger === void 0 ? void 0 : logger.debug) === null || _b === void 0 ? void 0 : _b.call(logger, "Command: " + compileCommand); | ||
childProcess.exec(compileCommand, function (err, stdout, stderr) { return __awaiter(_this, void 0, void 0, function () { | ||
var _a, _b, _c; | ||
return __generator(this, function (_d) { | ||
switch (_d.label) { | ||
case 0: | ||
if (!err) return [3 /*break*/, 2]; | ||
(_a = logger === null || logger === void 0 ? void 0 : logger.error) === null || _a === void 0 ? void 0 : _a.call(logger, err); | ||
return [4 /*yield*/, fs.promises.rm(tmpTsConfigPath)]; | ||
case 1: | ||
_d.sent(); | ||
reject(err); | ||
return [2 /*return*/]; | ||
case 2: | ||
if (stdout.trim()) { | ||
(_b = logger === null || logger === void 0 ? void 0 : logger.debug) === null || _b === void 0 ? void 0 : _b.call(logger, stdout); | ||
} | ||
if (stderr.trim()) { | ||
(_c = logger === null || logger === void 0 ? void 0 : logger.debug) === null || _c === void 0 ? void 0 : _c.call(logger, stderr); | ||
} | ||
return [4 /*yield*/, fs.promises.rm(tmpTsConfigPath)]; | ||
case 3: | ||
_d.sent(); | ||
resolve(stdout); | ||
return [2 /*return*/]; | ||
} | ||
}); | ||
}); }); | ||
})]; | ||
} | ||
}); | ||
}); | ||
}; | ||
Compiler.prototype.getFlag = function (flagName) { | ||
return this.options.flags.find(function (flag) { | ||
return flag.startsWith("--" + flagName); | ||
}); | ||
}; | ||
Compiler.prototype.getFlagValue = function (flagName) { | ||
var selectedFlag = this.getFlag(flagName); | ||
if (!selectedFlag) { | ||
throw new Error("Flag " + flagName + " was not found."); | ||
} | ||
var flagParts = selectedFlag.split(" "); | ||
flagParts.shift(); | ||
return flagParts.join(" "); | ||
}; | ||
Compiler.defaults = { | ||
fallback: false, | ||
flags: [ | ||
"--downlevelIteration", | ||
"--emitDecoratorMetadata", | ||
"--experimentalDecorators", | ||
"--module commonjs", | ||
"--resolveJsonModule", | ||
"--skipLibCheck", | ||
"--target es2015", | ||
] | ||
compilerOptions: { | ||
downlevelIteration: true, | ||
emitDecoratorMetadata: true, | ||
experimentalDecorators: true, | ||
module: "commonjs", | ||
outDir: path.resolve(__dirname, "../cache"), | ||
resolveJsonModule: true, | ||
rootDir: "/", | ||
skipLibCheck: true, | ||
target: "es2015" | ||
} | ||
}; | ||
@@ -197,0 +200,0 @@ return Compiler; |
{ | ||
"name": "ts-import", | ||
"version": "2.0.23", | ||
"version": "2.0.24", | ||
"description": "Import (compile and cache on the fly) TypeScript files dynamically with ease.", | ||
@@ -20,6 +20,6 @@ "keywords": [ | ||
"dependencies": { | ||
"options-defaults": "^2.0.22" | ||
"options-defaults": "^2.0.24" | ||
}, | ||
"devDependencies": { | ||
"@radrat-node/logger": "^2.0.22", | ||
"@radrat-node/logger": "^2.0.24", | ||
"@types/jest": "^26.0.16", | ||
@@ -32,3 +32,3 @@ "@types/node": "^14.11.1", | ||
}, | ||
"gitHead": "1ca2979e97621b965ba32f2d3454721e5f02338e" | ||
"gitHead": "cd7a4ccd15e93a65506fb773b26ecc04d88b4a14" | ||
} |
25734
11
395
Updatedoptions-defaults@^2.0.24