Comparing version 1.1.5 to 1.2.0
@@ -16,4 +16,4 @@ #! /usr/bin/env node | ||
watch: !!program.watch, | ||
outDir: program.directory | ||
outDir: program.directory, | ||
}); | ||
//# sourceMappingURL=index.js.map |
@@ -12,3 +12,3 @@ "use strict"; | ||
function replaceTscAliasPaths(options = { | ||
watch: false | ||
watch: false, | ||
}) { | ||
@@ -48,4 +48,4 @@ console.log('=== tsc-alias starting ==='); | ||
const aliases = Object.keys(paths) | ||
.map(alias => { | ||
const _paths = paths[alias].map(path => { | ||
.map((alias) => { | ||
const _paths = paths[alias].map((path) => { | ||
path = path.replace(/\*$/, '').replace('.ts', ''); | ||
@@ -90,7 +90,7 @@ if (path_1.isAbsolute(path)) { | ||
paths: _paths, | ||
isExtra | ||
isExtra, | ||
}; | ||
}) | ||
.filter(({ prefix }) => prefix); | ||
aliases.forEach(alias => { | ||
aliases.forEach((alias) => { | ||
if (path_1.normalize(alias.path).includes('..')) { | ||
@@ -120,3 +120,3 @@ const tempBasePath = normalizePath(path_1.normalize(`${configDir}/${outDir}/${hasExtraModule && relConfDirPathInOutPath | ||
const importRegex = /(?:import|from) ['"]([^'"]*)['"]/g; | ||
const replaceImportStatement = ({ orig, file, alias }) => { | ||
const replaceImportStatement = ({ orig, file, alias, }) => { | ||
const requiredModule = orig.split(/"|'/)[1]; | ||
@@ -129,3 +129,3 @@ const index = orig.indexOf(alias.prefix); | ||
let relativeAliasPath = normalizePath(path_1.relative(path_1.dirname(file), absoluteAliasPath)); | ||
if (relativeAliasPath[0] !== '.') { | ||
if (!relativeAliasPath.startsWith('.')) { | ||
relativeAliasPath = './' + relativeAliasPath; | ||
@@ -136,3 +136,3 @@ } | ||
orig.substring(index + alias.prefix.length); | ||
return modulePath; | ||
return modulePath.replace(/\/\//g, '/'); | ||
} | ||
@@ -142,15 +142,15 @@ return orig; | ||
const replaceAlias = (file) => { | ||
const text = fs_1.readFileSync(file, 'utf8'); | ||
let newText = text; | ||
const code = fs_1.readFileSync(file, 'utf8'); | ||
let tempCode = code; | ||
for (const alias of aliases) { | ||
const replacementParams = { | ||
file, | ||
alias | ||
alias, | ||
}; | ||
newText = newText | ||
.replace(requireRegex, orig => replaceImportStatement(Object.assign({ orig }, replacementParams))) | ||
.replace(importRegex, orig => replaceImportStatement(Object.assign({ orig }, replacementParams))); | ||
tempCode = tempCode | ||
.replace(requireRegex, (orig) => replaceImportStatement(Object.assign({ orig }, replacementParams))) | ||
.replace(importRegex, (orig) => replaceImportStatement(Object.assign({ orig }, replacementParams))); | ||
} | ||
if (text !== newText) { | ||
fs_1.writeFileSync(file, newText, 'utf8'); | ||
if (code !== tempCode) { | ||
fs_1.writeFileSync(file, tempCode, 'utf8'); | ||
return true; | ||
@@ -162,7 +162,7 @@ } | ||
`${outPath}/**/*.{js,jsx,ts,tsx}`, | ||
`!${outPath}/**/node_modules` | ||
`!${outPath}/**/node_modules`, | ||
]; | ||
const files = globby_1.sync(globPattern, { | ||
dot: true, | ||
onlyFiles: true | ||
onlyFiles: true, | ||
}); | ||
@@ -182,6 +182,6 @@ const flen = files.length; | ||
const tsconfigWatcher = chokidar_1.watch(configFile); | ||
filesWatcher.on('change', file => { | ||
filesWatcher.on('change', (file) => { | ||
replaceAlias(file); | ||
}); | ||
tsconfigWatcher.on('change', _ => { | ||
tsconfigWatcher.on('change', (_) => { | ||
console_utils_1.Output.clear(); | ||
@@ -188,0 +188,0 @@ filesWatcher.close(); |
@@ -5,7 +5,8 @@ "use strict"; | ||
const file_utils_1 = require("@jfonx/file-utils"); | ||
const findNodeModulesPath = require("find-node-modules"); | ||
const globby_1 = require("globby"); | ||
const path_1 = require("path"); | ||
exports.mapPaths = (paths, mapper) => { | ||
const mapPaths = (paths, mapper) => { | ||
const dest = {}; | ||
Object.keys(paths).forEach(key => { | ||
Object.keys(paths).forEach((key) => { | ||
dest[key] = paths[key].map(mapper); | ||
@@ -15,8 +16,9 @@ }); | ||
}; | ||
exports.loadConfig = (file) => { | ||
exports.mapPaths = mapPaths; | ||
const loadConfig = (file) => { | ||
const { extends: ext, compilerOptions: { baseUrl, outDir, paths } = { | ||
baseUrl: undefined, | ||
outDir: undefined, | ||
paths: undefined | ||
} } = file_utils_1.FileUtils.toObject(file); | ||
paths: undefined, | ||
}, } = file_utils_1.FileUtils.toObject(file); | ||
const config = {}; | ||
@@ -33,3 +35,12 @@ if (baseUrl) { | ||
if (ext) { | ||
const parentConfig = exports.loadConfig(path_1.resolve(path_1.dirname(file), ext)); | ||
let parentConfig; | ||
if (ext.startsWith('.')) { | ||
parentConfig = exports.loadConfig(path_1.join(path_1.dirname(file), ext)); | ||
} | ||
else { | ||
const tsConfigDir = path_1.dirname(file); | ||
const node_modules = findNodeModulesPath({ cwd: tsConfigDir })[0]; | ||
const nodeModulesTsConfig = !ext.includes('.json') ? `${ext}.json` : ext; | ||
parentConfig = exports.loadConfig(path_1.join(tsConfigDir, node_modules, nodeModulesTsConfig)); | ||
} | ||
return Object.assign(Object.assign({}, parentConfig), config); | ||
@@ -39,2 +50,3 @@ } | ||
}; | ||
exports.loadConfig = loadConfig; | ||
function getProjectDirPathInOutDir(outDir, projectDir) { | ||
@@ -44,6 +56,6 @@ const dirs = globby_1.sync([ | ||
`!${outDir}/**/${projectDir}/**/${projectDir}`, | ||
`!${outDir}/**/node_modules` | ||
`!${outDir}/**/node_modules`, | ||
], { | ||
dot: true, | ||
onlyDirectories: true | ||
onlyDirectories: true, | ||
}); | ||
@@ -50,0 +62,0 @@ dirs.sort((dirA, dirB) => { |
{ | ||
"name": "tsc-alias", | ||
"version": "1.1.5", | ||
"version": "1.2.0", | ||
"description": "Replace absolute paths to relative paths after typescript compilation", | ||
@@ -18,3 +18,3 @@ "main": "dist/index.js", | ||
"postversion": "git push && git push --tags && npm publish", | ||
"format": "prettier --write \"./*.{js,jsx,ts,tsx}\" \"./src/**/*.{js,jsx,ts,tsx}\"" | ||
"format": "prettier --write \"**/*.{js,ts}\"" | ||
}, | ||
@@ -45,2 +45,3 @@ "repository": { | ||
"commander": "^2.19.0", | ||
"find-node-modules": "^2.1.0", | ||
"globby": "^9.2.0", | ||
@@ -51,4 +52,6 @@ "normalize-path": "^3.0.0" | ||
"@types/node": "^11.12.0", | ||
"rimraf": "^3.0.2" | ||
"prettier": "^2.2.1", | ||
"rimraf": "^3.0.2", | ||
"typescript": "^4.1.2" | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
34388
295
7
4
+ Addedfind-node-modules@^2.1.0
+ Addeddetect-file@1.0.0(transitive)
+ Addedexpand-tilde@2.0.2(transitive)
+ Addedfind-node-modules@2.1.3(transitive)
+ Addedfindup-sync@4.0.0(transitive)
+ Addedglobal-modules@1.0.0(transitive)
+ Addedglobal-prefix@1.0.2(transitive)
+ Addedhomedir-polyfill@1.0.3(transitive)
+ Addedini@1.3.8(transitive)
+ Addedisexe@2.0.0(transitive)
+ Addedmerge@2.1.1(transitive)
+ Addedmicromatch@4.0.8(transitive)
+ Addedparse-passwd@1.0.0(transitive)
+ Addedresolve-dir@1.0.1(transitive)
+ Addedwhich@1.3.1(transitive)