Comparing version 0.0.9-2 to 0.0.9-3
@@ -77,25 +77,28 @@ "use strict"; | ||
return __awaiter(this, void 0, void 0, function () { | ||
var cacheDir, absoluteTsPath, tsFileName, jsFileName, absoluteTsDir, cachedDir, cachedFile, tsWasModified; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
var _a, cacheDir, logger, absoluteTsPath, absoluteTsDir, tsFileName, jsFileName, cachedDir, cachedFile, tsWasModified; | ||
return __generator(this, function (_b) { | ||
switch (_b.label) { | ||
case 0: | ||
cacheDir = this.options.cacheDir; | ||
_a = this.options, cacheDir = _a.cacheDir, logger = _a.logger; | ||
absoluteTsPath = path.resolve(process.cwd(), tsPath); | ||
absoluteTsDir = path.dirname(absoluteTsPath); | ||
tsFileName = path.basename(absoluteTsPath); | ||
jsFileName = tsFileName.replace(/\.[^/.]+$/, ".js"); | ||
absoluteTsDir = path.dirname(absoluteTsPath); | ||
cachedDir = path.resolve(cacheDir, "." + absoluteTsDir); | ||
cachedFile = path.resolve(cachedDir, jsFileName); | ||
// Check if cached scripts.js exist. | ||
logger.debug("Looking for cached file at " + cachedFile); | ||
if (!fs.existsSync(cachedFile)) return [3 /*break*/, 3]; | ||
return [4 /*yield*/, this.wasModified(absoluteTsPath, cachedFile)]; | ||
case 1: | ||
tsWasModified = _a.sent(); | ||
tsWasModified = _b.sent(); | ||
if (!tsWasModified) { | ||
logger.debug("File was not modified, importing."); | ||
return [2 /*return*/, Promise.resolve().then(function () { return require(cachedFile); })]; | ||
} | ||
// Cache is incorrect, rebuild. | ||
logger.debug("File was modified."); | ||
return [4 /*yield*/, this.buildCachedScripts(absoluteTsPath)]; | ||
case 2: | ||
// Cache is incorrect, rebuild. | ||
_a.sent(); | ||
_b.sent(); | ||
return [2 /*return*/, Promise.resolve().then(function () { return require(cachedFile); })]; | ||
@@ -105,2 +108,3 @@ case 3: | ||
if (!fs.existsSync(cachedDir)) { | ||
logger.debug("Creating cache directory."); | ||
fs.mkdirSync(cachedDir, { | ||
@@ -111,6 +115,6 @@ recursive: true | ||
// Build cache. | ||
logger.debug("File was not cached, caching..."); | ||
return [4 /*yield*/, this.buildCachedScripts(absoluteTsPath)]; | ||
case 4: | ||
// Build cache. | ||
_a.sent(); | ||
_b.sent(); | ||
return [2 /*return*/, Promise.resolve().then(function () { return require(cachedFile); })]; | ||
@@ -122,4 +126,4 @@ } | ||
Compiler.prototype.buildCachedScripts = function (absoluteTsPath) { | ||
var _a = this.options, flags = _a.flags, cacheDir = _a.cacheDir; | ||
this.options.logger.info("Compiling " + absoluteTsPath + " to " + cacheDir + "..."); | ||
var _a = this.options, flags = _a.flags, cacheDir = _a.cacheDir, logger = _a.logger; | ||
logger.info("Compiling " + absoluteTsPath + " to " + cacheDir + "..."); | ||
return Promise.all([ | ||
@@ -126,0 +130,0 @@ // Compile new scripts.ts to .js. |
{ | ||
"name": "ts-import", | ||
"version": "0.0.9-2", | ||
"version": "0.0.9-3", | ||
"license": "GPL-3.0", | ||
@@ -5,0 +5,0 @@ "author": "Artur Kurowski <radarsu@gmail.com>", |
@@ -41,9 +41,9 @@ import * as childProcess from 'child_process'; | ||
async compile(tsPath: string = ``): Promise<any> { | ||
const { cacheDir } = this.options; | ||
const { cacheDir, logger } = this.options; | ||
const absoluteTsPath = path.resolve(process.cwd(), tsPath); | ||
const absoluteTsDir = path.dirname(absoluteTsPath); | ||
const tsFileName = path.basename(absoluteTsPath); | ||
const jsFileName = tsFileName.replace(/\.[^/.]+$/, `.js`); | ||
const absoluteTsDir = path.dirname(absoluteTsPath); | ||
@@ -54,2 +54,3 @@ const cachedDir = path.resolve(cacheDir, `.${absoluteTsDir}`); | ||
// Check if cached scripts.js exist. | ||
logger.debug(`Looking for cached file at ${cachedFile}`); | ||
if (fs.existsSync(cachedFile)) { | ||
@@ -59,2 +60,3 @@ // Cache is correct, do nothing. | ||
if (!tsWasModified) { | ||
logger.debug(`File was not modified, importing.`); | ||
return import(cachedFile); | ||
@@ -64,2 +66,3 @@ } | ||
// Cache is incorrect, rebuild. | ||
logger.debug(`File was modified.`); | ||
await this.buildCachedScripts(absoluteTsPath); | ||
@@ -71,2 +74,3 @@ return import(cachedFile); | ||
if (!fs.existsSync(cachedDir)) { | ||
logger.debug(`Creating cache directory.`); | ||
fs.mkdirSync(cachedDir, { | ||
@@ -78,2 +82,3 @@ recursive: true, | ||
// Build cache. | ||
logger.debug(`File was not cached, caching...`); | ||
await this.buildCachedScripts(absoluteTsPath); | ||
@@ -84,5 +89,5 @@ return import(cachedFile); | ||
buildCachedScripts(absoluteTsPath: string) { | ||
const { flags, cacheDir } = this.options; | ||
const { flags, cacheDir, logger } = this.options; | ||
this.options.logger.info(`Compiling ${absoluteTsPath} to ${cacheDir}...`); | ||
logger.info(`Compiling ${absoluteTsPath} to ${cacheDir}...`); | ||
@@ -89,0 +94,0 @@ return Promise.all([ |
16330
325