Skip to content

Commit 26d1679

Browse files
authored
stable for multiple modules from a specific directory from development (#1)
* logger with meaningful status color * common utilities * get tsconfig as js object * new functions for check path Directory and paths join * get-config.ts throw error when TSConfig parsing time occurs issue * created function which resolve the task by looping * new packages added babel-parser and babel-generator * created transformer utility for create code ATS * ccreate new checkModuleType utilities for module type detect from AST * JSDocs type added for JS code * transform and createAST and function new params created and JSDocs * new function for throw error * module-type-detector utility throw error * !*important*! create more function which modify the path alis to relative * invoke getRelativePath function * created terminal-loader for animation in terminal * modified code rewrite * common-utilities.ts pretty indent * path-converter.ts debuging friendly error handled * main.ts invoke function by startExecute as a arg * gitignore & tsconfig.json little bit update * remove all type extension from ts config path alias value * entry point for tsconf-paths-resolver * dev mode starting entry changed * resolving baseUrl based on cli source argument * simple cli created with -s and -ds flags * invoke startExecution with cli srcArg * fixed tsConfigAliasVal extension not removing and isTSPathAlias * prevented unnessary loop and print task status
1 parent 923a84c commit 26d1679

14 files changed

+733
-4
lines changed

.gitignore

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
11
node_modules
2-
dist
3-
sample
2+
dist

package-lock.json

Lines changed: 131 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,12 @@
44
"version": "1.0.0",
55
"main": "dist/index.js",
66
"scripts": {
7-
"dev": "tsnd --respawn --clear ./src/main.ts",
7+
"dev": "tsnd --respawn --clear ./src/index.ts",
88
"build": "tsc src/index.ts"
99
},
10+
"bin": {
11+
"tsr":"./dist/index.js"
12+
},
1013
"repository": {
1114
"type": "git",
1215
"url": "git+https://github.com/Safin-Ali/tsconfig-paths-resolver.git"
@@ -19,10 +22,13 @@
1922
"homepage": "https://github.com/Safin-Ali/tsconfig-paths-resolver#readme",
2023
"description": "",
2124
"dependencies": {
25+
"@babel/generator": "^7.23.0",
26+
"@babel/parser": "^7.23.0",
2227
"json-to-js-obj": "^1.0.5",
2328
"typescript": "^5.2.2"
2429
},
2530
"devDependencies": {
31+
"@types/babel__generator": "^7.6.5",
2632
"@types/node": "^20.8.6"
2733
}
2834
}

src/index.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#!/usr/bin/env node
2+
3+
import run from './main';
4+
5+
/**
6+
* This script starts the directory loop from the specified entry path.
7+
* @function
8+
* @returns {void}
9+
*/
10+
run();

src/main.ts

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import { extname } from 'path';
2+
import { isDir, pathJoin, readDir, readFile, resolveWithRoot, thorwError,} from './utilities/common-utilities';
3+
import transform from './utilities/transformer';
4+
import startExecute from './utilities/terminal-loader';
5+
import argObj from './utilities/command-handler';
6+
import logger from './utilities/color-logger';
7+
8+
9+
/**
10+
* this function wrapped for terminal can show loading animation sync
11+
* start path resolving
12+
*/
13+
14+
const run = () => startExecute(()=>{
15+
16+
let status:boolean = false;
17+
18+
/**
19+
* Recursively loops through a directory and performs an action on each file.
20+
* @param {string} loopPath - The path of the directory to loop through.
21+
* @returns {void}
22+
*/
23+
const loopDir = (loopPath: string): void => {
24+
25+
// store loopPath directory elements
26+
const allDir = readDir(loopPath);
27+
28+
// after checking loopPath has not any js file print warning message
29+
if(!allDir.filter(elm => elm.endsWith('.mjs') || elm.endsWith('.js') || elm.endsWith('.cjs')).length) return;
30+
31+
try{
32+
33+
allDir.forEach((elm) => {
34+
const currElmPath = pathJoin(loopPath, elm);
35+
36+
if (!isDir(currElmPath)) {
37+
/**
38+
* Check if the file extension is .js, .cjs, or .mjs.
39+
* If true, invoke the transformer function.
40+
*/
41+
if (extname(elm) === '.js' || extname(elm) === '.cjs' || extname(elm) === '.mjs') {
42+
transform(readFile(currElmPath),currElmPath)
43+
}
44+
} else {
45+
// If 'currElmPath' is a directory, recursively call 'loopDir' on that directory.
46+
loopDir(pathJoin(loopPath, elm));
47+
}
48+
});
49+
50+
status = true
51+
} catch (err:any) {
52+
thorwError(err.message);
53+
}
54+
};
55+
56+
// Start the directory loop from the specified entry path.
57+
loopDir(resolveWithRoot(argObj.srcArg));
58+
// print status
59+
status ? logger.success(`\nTS alias to relative path convert successfull 😊`) : logger.warn(`\nnot founded any JS file in ${resolveWithRoot(argObj.srcArg)} 🥹 😥`)
60+
61+
});
62+
63+
export default run;

src/types/types.d.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
export type ModuleLiteral = 'ESM' | 'CommonJS' | 'CESM';
2+
3+
export type CMDArgFlag = '-s' | '-ds'
4+
5+
export interface CMDArgShape {
6+
srcArg: string,
7+
destination: string
8+
}
9+
10+
export interface PathExistStatusShape {
11+
executeBool: boolean,
12+
TSPathAls: string
13+
}

src/utilities/color-logger.ts

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/**
2+
* Formats a given message with ANSI escape codes to apply color.
3+
*
4+
* @param {string} message - The message to be colorized.
5+
* @param {number} colorCode - The ANSI color code to apply to the message.
6+
* @returns {string} The colorized message.
7+
*/
8+
const colorize = (message: string, colorCode: number) => `\x1b[${colorCode}m${message}\x1b[0m`;
9+
10+
/**
11+
* A logger utility that provides methods for logging messages with different colors.
12+
*/
13+
const logger = (function () {
14+
return {
15+
/**
16+
* Logs an error message in red.
17+
*
18+
* @param {string} message - The error message to log.
19+
*/
20+
error: (message: string) => console.error(colorize(message, 31)),
21+
22+
/**
23+
* Logs a process message in blue.
24+
*
25+
* @param {string} message - The process message to log.
26+
*/
27+
process: (message: string) => console.log(colorize(message, 34)),
28+
29+
/**
30+
* Logs a warning message in yellow.
31+
*
32+
* @param {string} message - The warning message to log.
33+
*/
34+
warn: (message: string) => console.warn(colorize(message, 33)),
35+
36+
/**
37+
* Logs a success message in green.
38+
*
39+
* @param {string} message - The success message to log.
40+
*/
41+
success: (message: string) => console.log(colorize(message, 32))
42+
};
43+
})();
44+
45+
// Export the 'logger' object as the default export of this module.
46+
export default logger;

src/utilities/command-handler.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import { CMDArgFlag, CMDArgShape } from '../types/types';
2+
3+
/**
4+
* The default argument object.
5+
* @type {CMDArgShape}
6+
*/
7+
let argObj: CMDArgShape = {
8+
destination: '',
9+
srcArg: 'dist'
10+
}
11+
12+
// get argument from cli
13+
const processArg = process.argv;
14+
15+
processArg.forEach((arg, idx) => {
16+
const cmdArg = arg as CMDArgFlag
17+
switch (cmdArg) {
18+
case '-s':
19+
/**
20+
* Set the source argument.
21+
* @type {CMDArgShape}
22+
*/
23+
argObj = { ...argObj, srcArg: processArg[idx + 1] }
24+
break;
25+
case '-ds':
26+
/**
27+
* Set the destination path.
28+
* @type {CMDArgShape}
29+
*/
30+
argObj = { ...argObj, destination: processArg[idx + 1] }
31+
break;
32+
}
33+
})
34+
35+
/**
36+
* The final argument object.
37+
* @type {CMDArgShape}
38+
*/
39+
export default argObj;

0 commit comments

Comments
 (0)