Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ node_modules
node_modules/
test/

.vscode

# Logs
logs
*.log
Expand Down
50 changes: 26 additions & 24 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
{
"name": "tspath",
"version": "2.5.4",
"description": "TypeScript path alias resolver, re-writes JS files with relative paths according to @paths specified in tsconfig",
"license": "LGPL-2.1",
"scripts": {
"copy": "copyfiles package.json lib",
"name": "tspath",
"version": "2.5.4",
"description": "TypeScript path alias resolver, re-writes JS files with relative paths according to @paths specified in tsconfig",
"license": "LGPL-2.1",
"scripts": {
"copy": "copyfiles package.json lib",
"tspath": "node ./tspath.js"
},
"preferGlobal": true,
"preferGlobal": true,
"main": "index.js",
"bin": {
"bin": {
"tspath": "./tspath.js"
},
"keywords": [
"keywords": [
"tspath",
"relative",
"path",
Expand All @@ -21,26 +21,28 @@
"resolve",
"alias"
],
"author": "Patrik Forsberg <patrik.forsberg@coldmind.com>",
"repository": {
"author": "Patrik Forsberg <patrik.forsberg@coldmind.com>",
"repository": {
"type": "git",
"url": "git+https://github.com/duffman/tspath.git"
"url": "git+https://github.com/duffman/tspath.git"
},
"devDependencies": {
"@types/chai": "^4.0.4",
"@types/chai": "^4.0.4",
"@types/mocha": "^2.2.42",
"@types/node": "^8.0.25",
"chai": "^4.1.2",
"copyfiles": "^2.4.1",
"mocha": "^3.5.0",
"prettier": "^2.7.1"
"@types/node": "^8.0.25",
"@types/supports-color": "8.1.1",
"chai": "^4.1.2",
"copyfiles": "^2.4.1",
"mocha": "^3.5.0",
"prettier": "^2.7.1"
},
"dependencies": {
"chalk": "^2.3.0",
"escodegen": "^1.8.1",
"esprima": "^4.0.0",
"dependencies": {
"chalk": "^2.3.0",
"escodegen": "^1.8.1",
"esprima": "^4.0.0",
"prompt-confirm": "^1.2.0",
"typescript": "^4.4.2",
"yargs": "^11.0.0"
"supports-color": "8.1.1",
"typescript": "^4.4.2",
"yargs": "^11.0.0"
}
}
26 changes: 13 additions & 13 deletions src/parser-engine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,8 @@

=----------------------------------------------------------------= */

let esprima = require("esprima");
let escodegen = require("escodegen");
let chalk = require("chalk");
const esprima = require("esprima");
const escodegen = require("escodegen");

import { Const } from "./tspath.const";
import { Logger } from "./utils/logger";
Expand All @@ -34,6 +33,7 @@ import { JsonCommentStripper } from "./utils/json-comment-stripper";
import { ProjectOptions } from "./project-options";
import * as fs from "fs";
import * as path from "path";
import { bold, green, red, underline, yellow } from "./utils/color";

const log = console.log;
const testRun = false;
Expand Down Expand Up @@ -62,7 +62,7 @@ export class ParserEngine {

public setProjectPath(projectPath: string): boolean {
if (!Utils.isEmpty(projectPath) && !this.validateProjectPath(projectPath)) {
log(chalk.red.bold("Project Path \"" + chalk.underline(projectPath) + "\" is invalid!"));
log(red(bold(("Project Path \"" + underline(projectPath) + "\" is invalid!"))));
return false;
}

Expand Down Expand Up @@ -92,7 +92,7 @@ export class ParserEngine {
}

if (!fs.existsSync(configFile)) {
log("TypeScript Compiler - Configuration file " + chalk.underline(Const.TS_CONFIG) + " is missing!");
log("TypeScript Compiler - Configuration file " + underline(Const.TS_CONFIG) + " is missing!");
}

return result;
Expand Down Expand Up @@ -122,18 +122,18 @@ export class ParserEngine {
console.time(PROCESS_TIME);

if (!this.validateProjectPath(this.projectPath)) {
log(chalk.bold.red("Invalid project path!"));
log(bold(red("Invalid project path!")));
this.exit(10);
}

this.projectOptions = this.readConfig();
let projectName = this.readProjectName();

if (!Utils.isEmpty(projectName)) {
log(chalk.yellow("Parsing project: ") + chalk.bold(projectName) + " " + chalk.underline(this.projectPath));
log(yellow("Parsing project: ") + bold(projectName) + " " + underline(this.projectPath));
}
else {
log(chalk.yellow.bold("Parsing project at: ") + "\"" + this.projectPath + "\"");
log(yellow(bold("Parsing project at: ") + "\"" + this.projectPath + "\""));
}

this.distRoot = path.resolve(this.projectPath, this.projectOptions.outDir);
Expand Down Expand Up @@ -167,11 +167,11 @@ export class ParserEngine {
this.processFile(filename);
}

log(chalk.bold("Total files processed:"), this.nrFilesProcessed);
log(chalk.bold("Total paths processed:"), this.nrPathsProcessed);
log(bold("Total files processed:"), this.nrFilesProcessed);
log(bold("Total paths processed:"), this.nrPathsProcessed);

console.timeEnd(PROCESS_TIME);
log(chalk.bold.green("Project is prepared, now run it normally!"));
log(bold(green("Project is prepared, now run it normally!")));
}

private shouldSkipFile(filename: string): boolean {
Expand Down Expand Up @@ -313,7 +313,7 @@ export class ParserEngine {
}
}
catch (error) {
log(chalk.bold.red("Unable to write file:"), filename);
log(bold(red("Unable to write file:")), filename);
this.exit();
}
}
Expand Down Expand Up @@ -360,7 +360,7 @@ export class ParserEngine {
for (let key in reqFields) {
let field = reqFields[ key ];
if (Utils.isEmpty(field)) {
log(chalk.red.bold("Missing required field:") + " \"" + chalk.bold.underline(key) + "\"");
log(red(bold("Missing required field:")) + " \"" + bold(underline(key)) + "\"");
this.exit(22);
}
}
Expand Down
3 changes: 3 additions & 0 deletions src/test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { bold, red, underline } from "./utils/color";

console.log(`This is some ${red("red")} text and this is ${bold("bold and " + underline("underlined") )} text!`);
14 changes: 7 additions & 7 deletions src/tspath.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@

=----------------------------------------------------------------= */

let chalk = require("chalk");
let log = console.log;
let Confirm = require("prompt-confirm");
let yargs = require("yargs").argv;
const log = console.log;
const Confirm = require("prompt-confirm");
const yargs = require("yargs").argv;

import { bold, red, yellow } from "./utils/color";
import { ParserEngine } from "./parser-engine";
import { Const } from "./tspath.const";
import { JsonFile } from "./utils/json-file";
Expand All @@ -40,7 +40,7 @@ export class TSPath {
constructor() {
const pkg: any = new JsonFile("package.json");

log(chalk.yellow(`TSPath v${ Const.VERSION }`));
log(yellow(`TSPath v${ Const.VERSION }`));
let filter = ["js"];
const force: boolean = yargs.force || yargs.f;
const verbose: boolean = yargs.verbose || yargs.v;
Expand All @@ -56,7 +56,7 @@ export class TSPath {
}

if (filter.length === 0) {
log(chalk.bold.red("File filter missing!"));
log(bold(red("File filter missing!")));
process.exit(23);
}

Expand All @@ -72,7 +72,7 @@ export class TSPath {
}
});
} else {
log(chalk.bold("No project root found!"));
log(bold("No project root found!"));
}
}

Expand Down
64 changes: 64 additions & 0 deletions src/utils/color.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import * as supportsColor from "supports-color";

const ESCAPE = "\x1b[";

const formatters = {
reset: "0m",
bold: "1m",
bold_reset: "22m",
underline: "4m",
underline_reset: "24m"
}

const colors = {
"default": "39m",
"red": "31m",
"yellow": "33m",
"green": "32m",
"blue": "34m",
"magenta": "35m",
"cyan": "36m",
"red_bright": "91m"
}

const ac = (str: string) =>
supportsColor.stdout
? ESCAPE + str
: "";

const RESET_ALL = ac(formatters.reset);

// colors
const DEFAULT_COLOR = ac(colors.default);

const RED = ac(colors.red);
export const red = (str: string) => `${RED}${str}${DEFAULT_COLOR}`;

const RED_BRIGHT = ac(colors.red_bright);
export const redBright = (str: string) => `${RED_BRIGHT}${str}${DEFAULT_COLOR}`;

const YELLOW = ac(colors.yellow);
export const yellow = (str: string) => `${YELLOW}${str}${DEFAULT_COLOR}`;

const GREEN = ac(colors.green);
export const green = (str: string) => `${GREEN}${str}${DEFAULT_COLOR}`;

const BLUE = ac(colors.blue);
export const blue = (str: string) => `${BLUE}${str}${DEFAULT_COLOR}`;

const MAGENTA = ac(colors.magenta);
export const magenta = (str: string) => `${MAGENTA}${str}${DEFAULT_COLOR}`;

const CYAN = ac(colors.cyan);
export const cyan = (str: string) => `${CYAN}${str}${DEFAULT_COLOR}`;

// formatting
const BOLD = ac(formatters.bold);
const BOLD_RESET = ac(formatters.bold_reset)
export const bold = (str: string) => `${BOLD}${str}${BOLD_RESET}`;

const UNDERLINE = ac(formatters.underline);
const UNDERLINE_RESET = ac(formatters.underline_reset);
export const underline = (str: string) => `${UNDERLINE}${str}${UNDERLINE_RESET}`;


20 changes: 10 additions & 10 deletions src/utils/logger.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { blue, bold, cyan, magenta, redBright, yellow } from "./color";

/**
* @author: Patrik Forsberg <patrik.forsberg@coldmind.com>
* @date: 2022-09-28 10:07
*/
import chalk from "chalk";

const log = console.log;

export class Logger {
Expand Down Expand Up @@ -55,35 +55,35 @@ export class Logger {
}

public static logRed(logMessage: string, ...logData:any[]): void {
Logger.logBase(chalk.redBright, logMessage, logData);
Logger.logBase(redBright, logMessage, logData);
}

public static logYellow(logMessage: string, logData: any = ""): void {
log(chalk.yellow(logMessage), logData);
log(yellow(logMessage), logData);
}

public static logCyan(logMessage: string, logData: any = ""): void {
if (logData) {
log(chalk.cyan(logMessage), logData);
log(cyan(logMessage), logData);
} else {
log(chalk.cyan(logMessage));
log(cyan(logMessage));
}
}

public static logText(logMessage: string, ...logText: string[]): void {
let text = logText.join(" :: ");
log(chalk.bold.cyan(logMessage), `:: ${logText}`);
log(bold(cyan(logMessage)), `:: ${logText}`);
}

public static logBlue(logMessage: string, logData: any = ""): void {
console.log(chalk.blue(logMessage), logData);
console.log(blue(logMessage), logData);
}

public static logPurple(logMessage: string, logData: any = null): void {
if (logData == null) {
log(chalk.magenta(logMessage));
log(magenta(logMessage));
} else {
log(chalk.magenta(logMessage), logData);
log(magenta(logMessage), logData);
}
}
}
17 changes: 17 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@
resolved "https://registry.yarnpkg.com/@types/node/-/node-8.10.66.tgz#dd035d409df322acc83dff62a602f12a5783bbb3"
integrity sha512-tktOkFUA4kXx2hhhrB8bIFb5TbwzS4uOhKEmwiD+NoiL0qtP2OQ9mFldbgD4dV1djrlBYP6eBuQZiWjuHUpqFw==

"@types/supports-color@8.1.1":
version "8.1.1"
resolved "https://registry.yarnpkg.com/@types/supports-color/-/supports-color-8.1.1.tgz#1b44b1b096479273adf7f93c75fc4ecc40a61ee4"
integrity sha512-dPWnWsf+kzIG140B8z2w3fr5D03TLWbOAFQl45xUpI3vcizeXriNR5VYkWZ+WTMsUHqZ9Xlt3hrxGNANFyNQfw==

ansi-bgblack@^0.1.1:
version "0.1.1"
resolved "https://registry.yarnpkg.com/ansi-bgblack/-/ansi-bgblack-0.1.1.tgz#a68ba5007887701b6aafbe3fa0dadfdfa8ee3ca2"
Expand Down Expand Up @@ -706,6 +711,11 @@ has-flag@^3.0.0:
resolved "https://registry.yarnpkg.com/has-flag/-/has-flag-3.0.0.tgz#b5d454dc2199ae225699f3467e5a07f3b955bafd"
integrity sha512-sKJf1+ceQBr4SMkvQnBDNDtf4TXpVhVGateu0t918bl30FnbE2m4vNLX+VWe/dpjlb+HugGYzW7uQXH98HPEYw==

has-flag@^4.0.0:
version "4.0.0"
resolved "https://registry.yarnpkg.com/has-flag/-/has-flag-4.0.0.tgz#944771fd9c81c81265c4d6941860da06bb59479b"
integrity sha512-EykJT/Q1KjTWctppgIAgfSO0tKVuZUjhgMr17kqTumMl6Afv3EISleU7qZUzoXDFTAHTDC4NOoG/ZxU3EvlMPQ==

he@1.1.1:
version "1.1.1"
resolved "https://registry.yarnpkg.com/he/-/he-1.1.1.tgz#93410fd21b009735151f8868c2f271f3427e23fd"
Expand Down Expand Up @@ -1529,6 +1539,13 @@ supports-color@3.1.2:
dependencies:
has-flag "^1.0.0"

supports-color@8.1.1:
version "8.1.1"
resolved "https://registry.yarnpkg.com/supports-color/-/supports-color-8.1.1.tgz#cd6fc17e28500cff56c1b86c0a7fd4a54a73005c"
integrity sha512-MpUEN2OodtUzxvKQl72cUF7RQ5EiHsGvSsVG0ia9c5RbWGL2CI4C7EpPS8UTBIplnlzZiNuV56w+FuNxy3ty2Q==
dependencies:
has-flag "^4.0.0"

supports-color@^5.3.0:
version "5.5.0"
resolved "https://registry.yarnpkg.com/supports-color/-/supports-color-5.5.0.tgz#e2e69a44ac8772f78a1ec0b35b689df6530efc8f"
Expand Down