Comparing version 5.2.1 to 5.3.0
@@ -56,3 +56,2 @@ "use strict"; | ||
var fs_1 = __importDefault(require("fs")); | ||
var glob_1 = __importDefault(require("glob")); | ||
var path_1 = __importDefault(require("path")); | ||
@@ -62,17 +61,2 @@ var errors_1 = require("../error/errors"); | ||
var ts_helpers_1 = require("@zerollup/ts-helpers"); | ||
function guessProjectFiles(globPattern) { | ||
return __awaiter(this, void 0, void 0, function () { | ||
return __generator(this, function (_a) { | ||
return [2 /*return*/, new Promise(function (resolve, reject) { | ||
glob_1.default(globPattern, function (err, files) { | ||
if (err !== null) { | ||
reject(err); | ||
return; | ||
} | ||
resolve(files); | ||
}); | ||
})]; | ||
}); | ||
}); | ||
} | ||
// TODO write exception code free everywhere | ||
@@ -109,2 +93,12 @@ function guessLocationOfTsconfig() { | ||
} | ||
function getProjectFiles(rootDir, compilerHost, config) { | ||
var _a, _b; | ||
var files = compilerHost.readDirectory | ||
? compilerHost.readDirectory(rootDir, ["ts", "tsx"], (_a = config.exclude) !== null && _a !== void 0 ? _a : [], (_b = config.include) !== null && _b !== void 0 ? _b : []) | ||
: undefined; | ||
if (files === undefined) { | ||
throw new errors_1.TechnicalError("compiler could not resolve project files"); | ||
} | ||
return files; | ||
} | ||
var graphCache = new Map(); | ||
@@ -133,76 +127,70 @@ function extractGraph(configFileName) { | ||
return __awaiter(this, void 0, void 0, function () { | ||
var configFile, config, parsedConfig, rootDir, globpattern, files, host, program, imports, _loop_1, _a, _b, sourceFile; | ||
var configFile, config, parsedConfig, rootDir, compilerHost, files, program, imports, _loop_1, _a, _b, sourceFile; | ||
var e_2, _c; | ||
return __generator(this, function (_d) { | ||
switch (_d.label) { | ||
case 0: | ||
configFile = configFileName; | ||
if (configFile === undefined) { | ||
configFile = guessLocationOfTsconfig(); | ||
} | ||
if (configFile === undefined) { | ||
throw new errors_1.TechnicalError("Could not find configuration path"); | ||
} | ||
config = typescript_1.default.readConfigFile(configFile, function (path) { | ||
return fs_1.default.readFileSync(path).toString(); | ||
}); | ||
if (config.error !== undefined) { | ||
throw new errors_1.TechnicalError("invalid config path"); | ||
} | ||
parsedConfig = config.config; | ||
rootDir = path_1.default.dirname(path_1.default.resolve(configFile)); | ||
globpattern = rootDir + "/**/*.{ts,tsx}"; | ||
return [4 /*yield*/, guessProjectFiles(globpattern)]; | ||
case 1: | ||
files = _d.sent(); | ||
host = typescript_1.default.createCompilerHost(parsedConfig); | ||
program = typescript_1.default.createProgram({ | ||
rootNames: files, | ||
options: parsedConfig, | ||
host: host | ||
}); | ||
imports = []; | ||
_loop_1 = function (sourceFile) { | ||
typescript_1.default.forEachChild(sourceFile, function (x) { | ||
if (typescript_1.default.isImportDeclaration(x)) { | ||
var normalizedSourceFileName = path_1.default.relative(rootDir, sourceFile.fileName); | ||
var specifier = x.moduleSpecifier; | ||
var module_1 = specifier["text"]; | ||
if (module_1 === undefined) { | ||
return; | ||
} | ||
var resolver = new ts_helpers_1.ImportPathsResolver(parsedConfig.compilerOptions); | ||
var suggestion = resolver.getImportSuggestions(module_1, path_1.default.dirname(normalizedSourceFileName)); | ||
var bestGuess = suggestion !== undefined ? suggestion[0] : undefined; | ||
// TODO use module resolution cache | ||
var resolvedModule = typescript_1.default.resolveModuleName(bestGuess !== null && bestGuess !== void 0 ? bestGuess : module_1, sourceFile.fileName, parsedConfig, host).resolvedModule; | ||
if (resolvedModule === undefined) { | ||
return; | ||
} | ||
var resolvedFileName = resolvedModule.resolvedFileName, isExternalLibraryImport = resolvedModule.isExternalLibraryImport; | ||
var normalizedTargetFileName = path_1.default.relative(rootDir, resolvedFileName); | ||
imports.push({ | ||
source: pathUtils_1.normalizeWindowsPaths(normalizedSourceFileName), | ||
target: pathUtils_1.normalizeWindowsPaths(normalizedTargetFileName), | ||
external: isExternalLibraryImport !== null && isExternalLibraryImport !== void 0 ? isExternalLibraryImport : false | ||
}); | ||
} | ||
}); | ||
}; | ||
try { | ||
// TODO currently the graph is made of imports as edges. Files that are not imported are not found in this graph. | ||
for (_a = __values(program.getSourceFiles()), _b = _a.next(); !_b.done; _b = _a.next()) { | ||
sourceFile = _b.value; | ||
_loop_1(sourceFile); | ||
configFile = configFileName; | ||
if (configFile === undefined) { | ||
configFile = guessLocationOfTsconfig(); | ||
} | ||
if (configFile === undefined) { | ||
throw new errors_1.TechnicalError("Could not find configuration path"); | ||
} | ||
config = typescript_1.default.readConfigFile(configFile, function (path) { | ||
return fs_1.default.readFileSync(path).toString(); | ||
}); | ||
if (config.error !== undefined) { | ||
throw new errors_1.TechnicalError("invalid config path"); | ||
} | ||
parsedConfig = config.config; | ||
rootDir = path_1.default.dirname(path_1.default.resolve(configFile)); | ||
compilerHost = typescript_1.default.createCompilerHost(parsedConfig); | ||
files = getProjectFiles(rootDir, compilerHost, config === null || config === void 0 ? void 0 : config.config); | ||
program = typescript_1.default.createProgram({ | ||
rootNames: files !== null && files !== void 0 ? files : [], | ||
options: parsedConfig, | ||
host: compilerHost | ||
}); | ||
imports = []; | ||
_loop_1 = function (sourceFile) { | ||
typescript_1.default.forEachChild(sourceFile, function (x) { | ||
if (typescript_1.default.isImportDeclaration(x)) { | ||
var normalizedSourceFileName = path_1.default.relative(rootDir, sourceFile.fileName); | ||
var specifier = x.moduleSpecifier; | ||
var module_1 = specifier["text"]; | ||
if (module_1 === undefined) { | ||
return; | ||
} | ||
} | ||
catch (e_2_1) { e_2 = { error: e_2_1 }; } | ||
finally { | ||
try { | ||
if (_b && !_b.done && (_c = _a.return)) _c.call(_a); | ||
var resolver = new ts_helpers_1.ImportPathsResolver(parsedConfig.compilerOptions); | ||
var suggestion = resolver.getImportSuggestions(module_1, path_1.default.dirname(normalizedSourceFileName)); | ||
var bestGuess = suggestion !== undefined ? suggestion[0] : undefined; | ||
// TODO use module resolution cache | ||
var resolvedModule = typescript_1.default.resolveModuleName(bestGuess !== null && bestGuess !== void 0 ? bestGuess : module_1, sourceFile.fileName, parsedConfig, compilerHost).resolvedModule; | ||
if (resolvedModule === undefined) { | ||
return; | ||
} | ||
finally { if (e_2) throw e_2.error; } | ||
var resolvedFileName = resolvedModule.resolvedFileName, isExternalLibraryImport = resolvedModule.isExternalLibraryImport; | ||
var normalizedTargetFileName = path_1.default.relative(rootDir, resolvedFileName); | ||
imports.push({ | ||
source: pathUtils_1.normalizeWindowsPaths(normalizedSourceFileName), | ||
target: pathUtils_1.normalizeWindowsPaths(normalizedTargetFileName), | ||
external: isExternalLibraryImport !== null && isExternalLibraryImport !== void 0 ? isExternalLibraryImport : false | ||
}); | ||
} | ||
return [2 /*return*/, imports]; | ||
}); | ||
}; | ||
try { | ||
// TODO currently the graph is made of imports as edges. Files that are not imported are not found in this graph. | ||
for (_a = __values(program.getSourceFiles()), _b = _a.next(); !_b.done; _b = _a.next()) { | ||
sourceFile = _b.value; | ||
_loop_1(sourceFile); | ||
} | ||
} | ||
catch (e_2_1) { e_2 = { error: e_2_1 }; } | ||
finally { | ||
try { | ||
if (_b && !_b.done && (_c = _a.return)) _c.call(_a); | ||
} | ||
finally { if (e_2) throw e_2.error; } | ||
} | ||
return [2 /*return*/, imports]; | ||
}); | ||
@@ -209,0 +197,0 @@ }); |
@@ -204,3 +204,20 @@ "use strict"; | ||
}); }); | ||
it("correctly ignores files exlucded by tsconfig", function () { return __awaiter(void 0, void 0, void 0, function () { | ||
var violations; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: return [4 /*yield*/, files_1.filesOfProject(path_1.default.resolve(__dirname, "samples", "ignores", "tsconfig.json")) | ||
.inFolder("ignore") | ||
.shouldNot() | ||
.dependOnFiles() | ||
.inFolder("dontImport") | ||
.check()]; | ||
case 1: | ||
violations = _a.sent(); | ||
expect(violations).toEqual([]); | ||
return [2 /*return*/]; | ||
} | ||
}); | ||
}); }); | ||
}); | ||
//# sourceMappingURL=integration.spec.js.map |
{ | ||
"name": "tsarch", | ||
"version": "5.2.1", | ||
"version": "5.3.0", | ||
"description": "", | ||
@@ -79,3 +79,2 @@ "keywords": [], | ||
"devDependencies": { | ||
"@types/glob": "^7.1.3", | ||
"@types/jest": "^23.0.0", | ||
@@ -87,3 +86,2 @@ "@types/node": "^10.17.28", | ||
"cz-conventional-changelog": "^2.0.0", | ||
"glob": "^7.1.6", | ||
"jest": "^23.0.0", | ||
@@ -97,3 +95,3 @@ "lint-staged": "^7.2.0", | ||
"rimraf": "^2.6.1", | ||
"semantic-release": "^17.1.1", | ||
"semantic-release": "^19.0.3", | ||
"travis-deploy-once": "^5.0.1", | ||
@@ -100,0 +98,0 @@ "ts-jest": "^23.10.5", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
218445
24
171
3184