ntts
Version:
A CLI tool for refactoring an existing NodeJs application to a fully functional TypeScript application.
69 lines (68 loc) • 3.54 kB
JavaScript
;
var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
var fs_1 = require("fs");
var path_1 = require("path");
var file_rename_1 = __importDefault(require("../../../file-rename/file-rename"));
var ImportsReformat = /** @class */ (function () {
function ImportsReformat() {
}
var _a;
_a = ImportsReformat;
ImportsReformat.knownFileEndings = ['json', 'js', 'cjs', 'mjs', 'jsx', 'ts', 'tsx'];
ImportsReformat.refactorModuleSpecifier = function (importStatement, moduleSpecifierRefactor, sourceFile) {
var moduleSpecifier = importStatement.getModuleSpecifierValue();
var isJavaScriptFile = file_rename_1.default.isJavaScriptFile(moduleSpecifier);
if (!importStatement.getModuleSpecifierSourceFile()) {
// if the path has and explicit JavaScript file ending set allowJs to true
if (isJavaScriptFile) {
var renamedSpecifier = file_rename_1.default.renameFileEnding(moduleSpecifier, '');
importStatement.setModuleSpecifier(renamedSpecifier);
return __assign(__assign({}, moduleSpecifierRefactor), { allowJs: true });
}
var absolutePath = (0, path_1.join)(sourceFile.getDirectoryPath(), moduleSpecifier);
var exists = (0, fs_1.existsSync)(absolutePath);
var isRelative = importStatement.isModuleSpecifierRelative();
// if the file exists and is a JSON file the resolveJsonModule option is enabled
if (moduleSpecifier.endsWith('.json') && isRelative && exists) {
return __assign(__assign({}, moduleSpecifierRefactor), { allowJson: true });
}
// if it is an unknown file ending, that file ending is enabled as a module
var unknownEnding = _a.unknownFile(moduleSpecifier);
if (unknownEnding && isRelative && exists) {
return __assign(__assign({}, moduleSpecifierRefactor), { fileEndings: moduleSpecifierRefactor.fileEndings.concat(unknownEnding) });
}
// if no TypeScript file exists for the given path, it is concluded that a javascript file is targeted
var tsPath = (0, path_1.join)(sourceFile.getDirectoryPath(), moduleSpecifier, '.ts');
var tsxPath = (0, path_1.join)(sourceFile.getDirectoryPath(), moduleSpecifier, '.tsx');
if (isRelative && !(0, fs_1.existsSync)(tsPath) && !(0, fs_1.existsSync)(tsxPath)) {
return __assign(__assign({}, moduleSpecifierRefactor), { allowJs: true });
}
}
return moduleSpecifierRefactor;
};
ImportsReformat.unknownFile = function (file) {
var paths = file.split('/');
var split = paths[paths.length - 1].split('.');
var ending = split[split.length - 1];
if (split.length > 1 && !_a.knownFileEndings.includes(ending)) {
return ending;
}
return undefined;
};
return ImportsReformat;
}());
exports.default = ImportsReformat;