Socket
Socket
Sign inDemoInstall

tsc-alias

Package Overview
Dependencies
Maintainers
1
Versions
72
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

tsc-alias - npm Package Compare versions

Comparing version 1.3.10 to 1.4.0

10

dist/bin/index.js
#! /usr/bin/env node
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const program = require("commander");
const commander_1 = require("commander");
const __1 = require("..");
const { version } = require('../../package.json');
const program = new commander_1.Command('tsc-alias');
program
.name('tsc-alias')
.version(version)

@@ -16,9 +16,9 @@ .option('-p, --project <file>', 'path to tsconfig.json')

.parseAsync(process.argv);
__1.replaceTscAliasPaths({
(0, __1.replaceTscAliasPaths)({
configFile: program.project,
watch: !!program.watch,
outDir: program.directory,
silent: program.silent,
resolveFullPaths: program.resolveFullPaths
silent: !!program.silent,
resolveFullPaths: !!program.resolveFullPaths
});
//# sourceMappingURL=index.js.map
export interface IRawTSConfig {
extends?: string;
compilerOptions?: {
baseUrl?: string;
outDir?: string;
paths?: {
[key: string]: string[];
};
};
compilerOptions?: ITSConfig;
}

@@ -11,0 +5,0 @@ export interface ITSConfig {

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getAbsoluteAliasPath = exports.existsResolvedAlias = exports.getProjectDirPathInOutDir = exports.resolveTsConfigExtendsPath = exports.loadConfig = exports.mapPaths = void 0;
const FileUtils = require("@jfonx/file-utils");
const mylas_1 = require("mylas");
const findNodeModulesPath = require("find-node-modules");

@@ -18,2 +18,6 @@ const fs = require("fs");

const loadConfig = (file) => {
if (!fs.existsSync(file)) {
console.log(`\x1b[41m Error: \x1b[0m \x1b[31mFile ${file} not found\x1b[0m`);
process.exit();
}
const { extends: ext, compilerOptions: { baseUrl, outDir, paths } = {

@@ -23,22 +27,14 @@ baseUrl: undefined,

paths: undefined
} } = FileUtils.toObject(file);
} } = mylas_1.Json.loadS(file, true);
const config = {};
if (baseUrl) {
if (baseUrl)
config.baseUrl = baseUrl;
}
if (outDir) {
if (outDir)
config.outDir = outDir;
}
if (paths) {
if (paths)
config.paths = paths;
}
if (ext) {
let parentConfig;
if (ext.startsWith('.')) {
parentConfig = exports.loadConfig(path_1.join(path_1.dirname(file), ext));
}
else {
parentConfig = exports.loadConfig(resolveTsConfigExtendsPath(ext, file));
}
return Object.assign(Object.assign({}, parentConfig), config);
return Object.assign(Object.assign({}, (ext.startsWith('.')
? (0, exports.loadConfig)((0, path_1.join)((0, path_1.dirname)(file), ext.endsWith('.json') ? ext : `${ext}.json`))
: (0, exports.loadConfig)(resolveTsConfigExtendsPath(ext, file)))), config);
}

@@ -49,5 +45,5 @@ return config;

function resolveTsConfigExtendsPath(ext, file) {
const tsConfigDir = path_1.dirname(file);
const tsConfigDir = (0, path_1.dirname)(file);
const node_modules = findNodeModulesPath({ cwd: tsConfigDir });
const targetPaths = node_modules.map((v) => path_1.join(tsConfigDir, v, ext));
const targetPaths = node_modules.map((v) => (0, path_1.join)(tsConfigDir, v, ext));
for (const targetPath of targetPaths) {

@@ -68,3 +64,3 @@ if (ext.endsWith('.json')) {

if (isDirectory) {
return path_1.join(targetPath, 'tsconfig.json');
return (0, path_1.join)(targetPath, 'tsconfig.json');
}

@@ -80,3 +76,3 @@ else {

function getProjectDirPathInOutDir(outDir, projectDir) {
const dirs = globby_1.sync([
const dirs = (0, globby_1.sync)([
`${outDir}/**/${projectDir}`,

@@ -89,6 +85,3 @@ `!${outDir}/**/${projectDir}/**/${projectDir}`,

});
dirs.sort((dirA, dirB) => {
return dirB.split('/').length - dirA.split('/').length;
});
return dirs[0];
return dirs.reduce((prev, curr) => prev.split('/').length > curr.split('/').length ? prev : curr, dirs[0]);
}

@@ -99,10 +92,7 @@ exports.getProjectDirPathInOutDir = getProjectDirPathInOutDir;

return true;
const globPattern = [`${path}.{js,jsx}`];
const files = globby_1.sync(globPattern, {
const files = (0, globby_1.sync)([`${path}.{mjs,cjs,js,jsx}`], {
dot: true,
onlyFiles: true
});
if (files.length)
return true;
return false;
return !!files.length;
}

@@ -116,9 +106,9 @@ exports.existsResolvedAlias = existsResolvedAlias;

let pathExists;
while (!(pathExists = fs.existsSync(path_1.join(basePath, aliasPathPart))) &&
while (!(pathExists = fs.existsSync((0, path_1.join)(basePath, aliasPathPart))) &&
aliasPathParts.length) {
aliasPathPart = aliasPathParts.shift();
}
return path_1.join(basePath, pathExists ? aliasPathPart : '', aliasPathParts.join('/'));
return (0, path_1.join)(basePath, pathExists ? aliasPathPart : '', aliasPathParts.join('/'));
}
exports.getAbsoluteAliasPath = getAbsoluteAliasPath;
//# sourceMappingURL=index.js.map

@@ -27,26 +27,17 @@ "use strict";

output.info('=== tsc-alias starting ===');
if (!options.configFile) {
options.configFile = path_1.resolve(process.cwd(), 'tsconfig.json');
}
else {
if (!path_1.isAbsolute(options.configFile)) {
options.configFile = path_1.resolve(process.cwd(), options.configFile);
}
}
const configFile = options.configFile;
const configFile = !options.configFile
? (0, path_1.resolve)(process.cwd(), 'tsconfig.json')
: !(0, path_1.isAbsolute)(options.configFile)
? (0, path_1.resolve)(process.cwd(), options.configFile)
: options.configFile;
const assert = (claim, message) => claim || output.error(message, true);
assert(fs_1.existsSync(configFile), `Invalid file path => ${configFile}`);
let { baseUrl, outDir, paths } = helpers_1.loadConfig(configFile);
if (options.outDir) {
assert((0, fs_1.existsSync)(configFile), `Invalid file path => ${configFile}`);
let { baseUrl = './', outDir, paths } = (0, helpers_1.loadConfig)(configFile);
if (options.outDir)
outDir = options.outDir;
}
if (!baseUrl) {
baseUrl = './';
}
assert(baseUrl, 'compilerOptions.baseUrl is not set');
assert(paths, 'compilerOptions.paths is not set');
assert(outDir, 'compilerOptions.outDir is not set');
const configDir = normalizePath(path_1.dirname(configFile));
const outPath = normalizePath(path_1.normalize(configDir + '/' + outDir));
const confDirParentFolderName = path_1.basename(configDir);
const configDir = normalizePath((0, path_1.dirname)(configFile));
const outPath = normalizePath((0, path_1.normalize)(configDir + '/' + outDir));
const confDirParentFolderName = (0, path_1.basename)(configDir);
let hasExtraModule = false;

@@ -58,5 +49,5 @@ let configDirInOutPath = null;

const _paths = paths[alias].map((path) => {
path = path.replace(/\*$/, '').replace(/\.ts(x)?$/, '.js$1');
if (path_1.isAbsolute(path)) {
path = path_1.relative(configDir, path);
path = path.replace(/\*$/, '').replace(/\.([mc])?ts(x)?$/, '.$1js$2');
if ((0, path_1.isAbsolute)(path)) {
path = (0, path_1.relative)(configDir, path);
}

@@ -68,10 +59,8 @@ return path;

const basePath = null;
if (path_1.normalize(path).includes('..')) {
if ((0, path_1.normalize)(path).includes('..')) {
if (!configDirInOutPath) {
configDirInOutPath = helpers_1.getProjectDirPathInOutDir(outPath, confDirParentFolderName);
configDirInOutPath = (0, helpers_1.getProjectDirPathInOutDir)(outPath, confDirParentFolderName);
if (configDirInOutPath) {
hasExtraModule = true;
}
if (configDirInOutPath) {
const stepsbackPath = path_1.relative(configDirInOutPath, outPath);
const stepsbackPath = (0, path_1.relative)(configDirInOutPath, outPath);
const splitStepBackPath = normalizePath(stepsbackPath).split('/');

@@ -103,8 +92,8 @@ const nbOfStepBack = splitStepBackPath.length;

aliases.forEach((alias) => {
if (path_1.normalize(alias.path).includes('..')) {
const tempBasePath = normalizePath(path_1.normalize(`${configDir}/${outDir}/${hasExtraModule && relConfDirPathInOutPath
if ((0, path_1.normalize)(alias.path).includes('..')) {
const tempBasePath = normalizePath((0, path_1.normalize)(`${configDir}/${outDir}/${hasExtraModule && relConfDirPathInOutPath
? relConfDirPathInOutPath
: ''}/${baseUrl}`));
const absoluteBasePath = normalizePath(path_1.normalize(`${tempBasePath}/${alias.path}`));
if (helpers_1.existsResolvedAlias(absoluteBasePath)) {
const absoluteBasePath = normalizePath((0, path_1.normalize)(`${tempBasePath}/${alias.path}`));
if ((0, helpers_1.existsResolvedAlias)(absoluteBasePath)) {
alias.isExtra = false;

@@ -120,6 +109,6 @@ alias.basePath = tempBasePath;

alias.isExtra = false;
alias.basePath = normalizePath(path_1.normalize(`${configDir}/${outDir}/${relConfDirPathInOutPath}/${baseUrl}`));
alias.basePath = normalizePath((0, path_1.normalize)(`${configDir}/${outDir}/${relConfDirPathInOutPath}/${baseUrl}`));
}
else {
alias.basePath = normalizePath(path_1.normalize(`${configDir}/${outDir}`));
alias.basePath = normalizePath((0, path_1.normalize)(`${configDir}/${outDir}`));
alias.isExtra = false;

@@ -130,3 +119,3 @@ }

var _a, _b;
const requiredModule = (_b = (_a = orig.match(utils_1.newStringRegex())) === null || _a === void 0 ? void 0 : _a.groups) === null || _b === void 0 ? void 0 : _b.path;
const requiredModule = (_b = (_a = orig.match((0, utils_1.newStringRegex)())) === null || _a === void 0 ? void 0 : _a.groups) === null || _b === void 0 ? void 0 : _b.path;
assert(typeof requiredModule == 'string', `Unexpected import statement pattern ${orig}`);

@@ -141,4 +130,4 @@ const isAlias = alias.shouldPrefixMatchWildly

if (isAlias) {
let absoluteAliasPath = helpers_1.getAbsoluteAliasPath(alias.basePath, alias.path);
let relativeAliasPath = normalizePath(path_1.relative(path_1.dirname(file), absoluteAliasPath));
let absoluteAliasPath = (0, helpers_1.getAbsoluteAliasPath)(alias.basePath, alias.path);
let relativeAliasPath = normalizePath((0, path_1.relative)((0, path_1.dirname)(file), absoluteAliasPath));
if (!relativeAliasPath.startsWith('.')) {

@@ -152,3 +141,3 @@ relativeAliasPath = './' + relativeAliasPath;

orig.substring(index + alias.prefix.length);
const modulePath = newImportScript.match(utils_1.newStringRegex()).groups.path;
const modulePath = newImportScript.match((0, utils_1.newStringRegex)()).groups.path;
return newImportScript.replace(modulePath, normalizePath(modulePath));

@@ -166,6 +155,6 @@ }

};
tempCode = utils_1.replaceSourceImportPaths(tempCode, file, (orig) => replaceImportStatement(Object.assign({ orig }, replacementParams)));
tempCode = (0, utils_1.replaceSourceImportPaths)(tempCode, file, (orig) => replaceImportStatement(Object.assign({ orig }, replacementParams)));
}
if (resolveFullPath) {
tempCode = utils_1.resolveFullImportPaths(tempCode, file);
tempCode = (0, utils_1.resolveFullImportPaths)(tempCode, file);
}

@@ -179,6 +168,6 @@ if (code !== tempCode) {

const globPattern = [
`${outPath}/**/*.{js,jsx,d.ts,d.tsx}`,
`${outPath}/**/*.{mjs,cjs,js,jsx,d.{mts,cts,ts,tsx}}`,
`!${outPath}/**/node_modules`
];
const files = globby_1.sync(globPattern, {
const files = (0, globby_1.sync)(globPattern, {
dot: true,

@@ -192,10 +181,7 @@ onlyFiles: true

output.info('[Watching for file changes...]');
const filesWatcher = chokidar_1.watch(globPattern);
const tsconfigWatcher = chokidar_1.watch(configFile);
filesWatcher.on('add', (file) => __awaiter(this, void 0, void 0, function* () {
yield replaceAlias(file, options === null || options === void 0 ? void 0 : options.resolveFullPaths);
}));
filesWatcher.on('change', (file) => __awaiter(this, void 0, void 0, function* () {
yield replaceAlias(file, options === null || options === void 0 ? void 0 : options.resolveFullPaths);
}));
const filesWatcher = (0, chokidar_1.watch)(globPattern);
const tsconfigWatcher = (0, chokidar_1.watch)(configFile);
const onFileChange = (file) => __awaiter(this, void 0, void 0, function* () { return replaceAlias(file, options === null || options === void 0 ? void 0 : options.resolveFullPaths); });
filesWatcher.on('add', onFileChange);
filesWatcher.on('change', onFileChange);
tsconfigWatcher.on('change', (_) => {

@@ -202,0 +188,0 @@ output.clear();

@@ -20,3 +20,3 @@ "use strict";

get sourceDir() {
return path_1.dirname(this.sourcePath);
return (0, path_1.dirname)(this.sourcePath);
}

@@ -29,3 +29,3 @@ replaceSourceImportPaths(replacer) {

this.replaceSourceImportPaths((importStatement) => {
const importPathMatch = importStatement.match(ImportPathResolver.newStringRegex());
const importPathMatch = importStatement.match((0, exports.newStringRegex)());
if (!importPathMatch) {

@@ -46,11 +46,10 @@ return importStatement;

const asFilePath = `${importPath}.js`;
if (fs_1.existsSync(path_1.resolve(this.sourceDir, asFilePath))) {
if ((0, fs_1.existsSync)((0, path_1.resolve)(this.sourceDir, asFilePath))) {
return asFilePath;
}
}
const asFilePath = path_1.join(importPath, 'index.js');
if (fs_1.existsSync(path_1.resolve(this.sourceDir, asFilePath))) {
return asFilePath;
}
return importPath;
const asFilePath = (0, path_1.join)(importPath, 'index.js');
return (0, fs_1.existsSync)((0, path_1.resolve)(this.sourceDir, asFilePath))
? asFilePath
: importPath;
}

@@ -57,0 +56,0 @@ static newStringRegex() {

export declare class Output {
private silent;
constructor(silent?: boolean);
private static exitProcessWithError;
info(message: string): void;

@@ -6,0 +5,0 @@ error(message: string, exitProcess?: boolean): void;

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Output = void 0;
const console_utils_1 = require("@jfonx/console-utils");
class Output {

@@ -9,20 +8,15 @@ constructor(silent = false) {

}
static exitProcessWithError() {
process.exit(1);
}
info(message) {
if (this.silent)
return;
console_utils_1.Output.info(message);
console.log(`Info: ${message}`);
}
error(message, exitProcess = false) {
if (!this.silent) {
console_utils_1.Output.error(message);
}
if (exitProcess) {
Output.exitProcessWithError();
}
if (!this.silent)
console.log(`\x1b[41m Error: \x1b[0m \x1b[31m${message}\x1b[0m\n`);
if (exitProcess)
process.exit(1);
}
clear() {
console_utils_1.Output.clear();
console.clear();
}

@@ -29,0 +23,0 @@ }

{
"name": "tsc-alias",
"version": "1.3.10",
"version": "1.4.0",
"description": "Replace alias paths with relative paths after typescript compilation.",

@@ -41,23 +41,22 @@ "main": "dist/index.js",

"dependencies": {
"@jfonx/console-utils": "^1.0.3",
"@jfonx/file-utils": "^3.0.1",
"chokidar": "^3.5.0",
"commander": "^6.2.1",
"find-node-modules": "^2.1.0",
"globby": "^11.0.2",
"chokidar": "^3.5.2",
"commander": "^8.2.0",
"find-node-modules": "^2.1.2",
"globby": "^11.0.4",
"mylas": "^2.1.4",
"normalize-path": "^3.0.0"
},
"devDependencies": {
"@types/jest": "^26.0.20",
"@types/node": "^11.12.0",
"@types/rimraf": "^3.0.0",
"@types/shelljs": "^0.8.8",
"husky": "^5.1.3",
"jest": "^26.6.3",
"prettier": "^2.2.1",
"@types/jest": "^27.0.2",
"@types/node": "^16.10.5",
"@types/rimraf": "^3.0.2",
"@types/shelljs": "^0.8.9",
"husky": "^7.0.2",
"jest": "^27.2.5",
"prettier": "^2.4.1",
"rimraf": "^3.0.2",
"shelljs": "^0.8.4",
"ts-jest": "^26.4.4",
"typescript": "^4.1.2"
"ts-jest": "^27.0.5",
"typescript": "^4.4.4"
}
}

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

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc