Comparing version 0.0.41 to 0.0.42
@@ -13,3 +13,5 @@ "use strict"; | ||
const package_1 = require("@radrat-scripts/package"); | ||
const jest = require("jest"); | ||
const scripts = (cli) => __awaiter(void 0, void 0, void 0, function* () { | ||
console.log(jest); | ||
yield cli.command({ | ||
@@ -16,0 +18,0 @@ name: `link`, |
@@ -20,3 +20,4 @@ export interface ICompilerOptions { | ||
*/ | ||
compile(tsPath?: string): Promise<any>; | ||
compile(tsPath?: string, changeDirectory?: boolean): Promise<any>; | ||
import(cachedFilePath: string, changeDirectory?: boolean): Promise<any>; | ||
buildCachedScripts(absoluteTsPath: string): Promise<unknown>; | ||
@@ -23,0 +24,0 @@ wasModified(tsFilePath: string, jsFilePath: string): Promise<boolean>; |
@@ -73,6 +73,7 @@ "use strict"; | ||
*/ | ||
Compiler.prototype.compile = function (tsPath) { | ||
Compiler.prototype.compile = function (tsPath, changeDirectory) { | ||
if (tsPath === void 0) { tsPath = ""; } | ||
if (changeDirectory === void 0) { changeDirectory = false; } | ||
return __awaiter(this, void 0, void 0, function () { | ||
var _a, cacheDir, logger, absoluteTsPath, absoluteTsDir, tsFileName, jsFileName, cachedDir, cachedFile, tsWasModified; | ||
var _a, cacheDir, logger, absoluteTsPath, absoluteTsDir, tsFileName, jsFileName, cachedDir, cachedFilePath, tsWasModified; | ||
return __generator(this, function (_b) { | ||
@@ -87,7 +88,7 @@ switch (_b.label) { | ||
cachedDir = path.resolve(cacheDir, "." + absoluteTsDir); | ||
cachedFile = path.resolve(cachedDir, jsFileName); | ||
cachedFilePath = 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)]; | ||
logger.debug("Looking for cached file at " + cachedFilePath); | ||
if (!fs.existsSync(cachedFilePath)) return [3 /*break*/, 3]; | ||
return [4 /*yield*/, this.wasModified(absoluteTsPath, cachedFilePath)]; | ||
case 1: | ||
@@ -97,3 +98,3 @@ tsWasModified = _b.sent(); | ||
logger.debug("File was not modified, importing."); | ||
return [2 /*return*/, Promise.resolve().then(function () { return require(cachedFile); })]; | ||
return [2 /*return*/, this["import"](cachedFilePath)]; | ||
} | ||
@@ -105,3 +106,3 @@ // Cache is incorrect, rebuild. | ||
_b.sent(); | ||
return [2 /*return*/, Promise.resolve().then(function () { return require(cachedFile); })]; | ||
return [2 /*return*/, this["import"](cachedFilePath)]; | ||
case 3: | ||
@@ -120,3 +121,3 @@ // Create cache directory if it does not exist. | ||
_b.sent(); | ||
return [2 /*return*/, Promise.resolve().then(function () { return require(cachedFile); })]; | ||
return [2 /*return*/, this["import"](cachedFilePath)]; | ||
} | ||
@@ -126,2 +127,25 @@ }); | ||
}; | ||
Compiler.prototype["import"] = function (cachedFilePath, changeDirectory) { | ||
if (changeDirectory === void 0) { changeDirectory = false; } | ||
return __awaiter(this, void 0, void 0, function () { | ||
var cwd, cachedFileDir, imported; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
cwd = process.cwd(); | ||
cachedFileDir = path.dirname(cachedFilePath); | ||
if (changeDirectory) { | ||
process.chdir(cachedFileDir); | ||
} | ||
return [4 /*yield*/, Promise.resolve().then(function () { return require(cachedFilePath); })]; | ||
case 1: | ||
imported = _a.sent(); | ||
if (changeDirectory) { | ||
process.chdir(cwd); | ||
} | ||
return [2 /*return*/, imported]; | ||
} | ||
}); | ||
}); | ||
}; | ||
Compiler.prototype.buildCachedScripts = function (absoluteTsPath) { | ||
@@ -128,0 +152,0 @@ var _a = this.options, flags = _a.flags, cacheDir = _a.cacheDir, logger = _a.logger; |
{ | ||
"name": "ts-import", | ||
"version": "0.0.41", | ||
"version": "0.0.42", | ||
"description": "Import (compile and cache on the fly) TypeScript files dynamically with ease.", | ||
@@ -24,3 +24,3 @@ "keywords": [ | ||
"@radrat-scripts/readme": "0.0.41", | ||
"@radrat/cli": "0.0.41", | ||
"@radrat/cli": "0.0.54", | ||
"@types/node": "^14.6.0", | ||
@@ -27,0 +27,0 @@ "typescript": "^3.9.7" |
@@ -42,3 +42,3 @@ import * as childProcess from 'child_process'; | ||
*/ | ||
async compile(tsPath: string = ``): Promise<any> { | ||
async compile(tsPath: string = ``, changeDirectory = false): Promise<any> { | ||
const { cacheDir, logger } = this.options; | ||
@@ -53,12 +53,12 @@ | ||
const cachedDir = path.resolve(cacheDir, `.${absoluteTsDir}`); | ||
const cachedFile = path.resolve(cachedDir, jsFileName); | ||
const cachedFilePath = path.resolve(cachedDir, jsFileName); | ||
// Check if cached scripts.js exist. | ||
logger.debug(`Looking for cached file at ${cachedFile}`); | ||
if (fs.existsSync(cachedFile)) { | ||
logger.debug(`Looking for cached file at ${cachedFilePath}`); | ||
if (fs.existsSync(cachedFilePath)) { | ||
// Cache is correct, do nothing. | ||
const tsWasModified = await this.wasModified(absoluteTsPath, cachedFile); | ||
const tsWasModified = await this.wasModified(absoluteTsPath, cachedFilePath); | ||
if (!tsWasModified) { | ||
logger.debug(`File was not modified, importing.`); | ||
return import(cachedFile); | ||
return this.import(cachedFilePath); | ||
} | ||
@@ -69,3 +69,3 @@ | ||
await this.buildCachedScripts(absoluteTsPath); | ||
return import(cachedFile); | ||
return this.import(cachedFilePath); | ||
} | ||
@@ -84,5 +84,22 @@ | ||
await this.buildCachedScripts(absoluteTsPath); | ||
return import(cachedFile); | ||
return this.import(cachedFilePath); | ||
} | ||
async import(cachedFilePath: string, changeDirectory = false) { | ||
const cwd = process.cwd(); | ||
const cachedFileDir = path.dirname(cachedFilePath); | ||
if (changeDirectory) { | ||
process.chdir(cachedFileDir); | ||
} | ||
const imported = await import(cachedFilePath); | ||
if (changeDirectory) { | ||
process.chdir(cwd); | ||
} | ||
return imported; | ||
} | ||
buildCachedScripts(absoluteTsPath: string) { | ||
@@ -89,0 +106,0 @@ const { flags, cacheDir, logger } = this.options; |
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
53622
1054
6
1