Skip to content

Commit

Permalink
squash!
Browse files Browse the repository at this point in the history
Signed-off-by: Nikola <[email protected]>
  • Loading branch information
NikolaRHristov committed Apr 16, 2024
1 parent a6ca573 commit 4c70f63
Show file tree
Hide file tree
Showing 14 changed files with 126 additions and 82 deletions.
17 changes: 10 additions & 7 deletions Source/Class/Error/File.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
export class FileError extends Step {
constructor(
public readonly step: string,
public readonly path: string,
message: string
) {
super(step, `Error processing ${path}: ${message}`);
export default class FileError extends (await import("@Class/Error/Step.js"))
.default {
public override readonly Step;
public readonly Path;

constructor(Step: string, Path: string, Message: string) {
super(Step, `Error processing ${Path}: ${Message}`);

this.Step = Step;
this.Path = Path;
}
}
6 changes: 3 additions & 3 deletions Source/Class/Error/FileNotFound.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export class FileNotFoundError extends FileError {
constructor(step: string, path: string) {
super(step, path, `Not found`);
export default class extends (await import("@Class/Error/File.js")).default {
constructor(Step: string, Path: string) {
super(Step, Path, "Not found");
}
}
15 changes: 9 additions & 6 deletions Source/Class/Error/InvaliadAlias.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
export class InvalidAliasError extends Step {
constructor(
public readonly step: string,
public readonly alias: string
) {
super(step, `The alias ${alias} is not permitted`);
export default class extends (await import("@Class/Error/Step.js")).default {
public override readonly Step: string;
public readonly Alias: string;

constructor(Step: string, Alias: string) {
super(Step, `The alias ${Alias} is not permitted`);

this.Step = Step;
this.Alias = Alias;
}
}
15 changes: 9 additions & 6 deletions Source/Class/Error/InvalidAliasError.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
export class InvalidAliasError extends Step {
constructor(
public readonly step: string,
public readonly alias: string
) {
super(step, `The alias ${alias} is not permitted`);
export default class extends (await import("@Class/Error/Step.js")).default {
public override readonly Step: string;
public readonly Alias: string;

constructor(Step: string, Alias: string) {
super(Step, `The alias ${Alias} is not permitted`);

this.Step = Step;
this.Alias = Alias;
}
}
11 changes: 6 additions & 5 deletions Source/Class/Error/Step.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
export default class extends Error {
constructor(
public readonly step: string,
message: string
) {
super(message);
public readonly Step: string;

constructor(Step: string, Message: string) {
super(Message);

this.Step = Step;
}
}
13 changes: 8 additions & 5 deletions Source/Class/Error/TSConfigPropertyError.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
export default class extends (await import("@Class/Error/Step.js")).default {
constructor(
public override readonly step: string,
public readonly property: string
) {
super(step, `${property} is not set in tsconfig`);
public override readonly Step: string;
public readonly Property: string;

constructor(Step: string, Property: string) {
super(Step, `${Property} is not set in tsconfig`);

this.Step = Step;
this.Property = Property;
}
}
12 changes: 6 additions & 6 deletions Source/Class/Logger.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
import { bold, dim, green, red } from "ansi-colors";

export type LoggerLevel = "verbose" | "info" | "error";
export type Level = "verbose" | "info" | "error";

export default class {
public readonly level;
public readonly Level;

constructor(level: LoggerLevel) {
this.level = level;
constructor(level: Level) {
this.Level = level;
}

verbose(...args: (string | undefined)[]) {
if (this.level === "verbose") {
if (this.Level === "verbose") {
console.log(...args);
}
}

info(...args: (string | number)[]) {
if (["verbose", "info"].includes(this.level)) {
if (["verbose", "info"].includes(this.Level)) {
console.log(...args);
}
}
Expand Down
16 changes: 8 additions & 8 deletions Source/Class/Resolve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { bold } from "ansi-colors";

import Step from "@Class/Error/Step.js";
import Logger from "@Class/Logger.js";
import type ProgramOptions from "@Interface/ProgramOptions.js";
import type Interface from "@Interface/ProgramOptions.js";

import Apply from "@Function/Apply.js";
import Compute from "@Function/Compute.js";
Expand All @@ -15,15 +15,15 @@ import Load from "@Function/Load.js";
import Path from "@Function/Resolve/Path.js";

export const main = () => {
const options = Create().parse().opts<ProgramOptions>();
const options = Create().parse().opts<Interface>();

const logger = new Logger(options.verbose ? "verbose" : "info");
const logger = new Logger(options.Verbose ? "verbose" : "info");

logger.verbose();
logger.fancyParams("options", options);

try {
const tsConfig = Load(options.project);
const tsConfig = Load(options.Project);

const { rootDir, outDir, baseUrl, paths } = tsConfig.options ?? {};

Expand All @@ -39,13 +39,13 @@ export const main = () => {
logger.fancyParams("programPaths", programPaths);

const aliases = Compute(
programPaths.basePath,
programPaths.Base,
tsConfig?.options?.paths ?? {}
);

logger.fancyParams("aliases", aliases);

const files = Get(programPaths.outPath, options.ext);
const files = Get(programPaths.Target, options.Extension);

logger.fancyParams("filesToProcess", files);

Expand All @@ -56,7 +56,7 @@ export const main = () => {
changes.map(({ file, changes }) => ({ file, changes }))
);

if (options.noEmit) {
if (options.NoEmit) {
logger.info(
bold("Resolve:"),
"discovered",
Expand All @@ -71,7 +71,7 @@ export const main = () => {
} catch (_Error) {
if (_Error instanceof Step) {
logger.fancyError(
`Error during step '${bold(_Error.step)}'`,
`Error during step '${bold(_Error.Step)}'`,
_Error.message
);
} else {
Expand Down
12 changes: 6 additions & 6 deletions Source/Function/Resolve/Path.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,20 @@ import { dirname, resolve } from "path";
import Step from "@Class/Error/Step";
import TSConfigPropertyError from "@Class/Error/TSConfigPropertyError.js";

import type ProgramOptions from "@Interface/ProgramOptions.js";
import type Interface from "@Interface/ProgramOptions.js";
import type ProgramPaths from "@Interface/ProgramPaths.js";
import type TSConfig from "@Interface/TSConfig.js";

/**
* Resolve paths provided to the program to absolute paths.
*/
export const _Function = (
options: Pick<ProgramOptions, "out" | "project" | "src">,
options: Pick<Interface, "out" | "project" | "src">,
tsConfig: TSConfig
): ProgramPaths => {
const { baseUrl = "", outDir, paths } = tsConfig.options ?? {};

const out = options.out ?? outDir;
const out = options.Out ?? outDir;

if (!out) {
throw new Step(
Expand All @@ -32,17 +32,17 @@ export const _Function = (
);
}

const configFile = resolve(process.cwd(), options.project);
const configFile = resolve(process.cwd(), options.Project);

const configPath = dirname(configFile);

const basePath = resolve(configPath, baseUrl);

const srcPath = resolve(options.src ?? tsConfig?.options?.rootDir ?? "src");
const srcPath = resolve(options.Source ?? tsConfig?.options?.rootDir ?? "src");

const outPath = resolve(out);

return { basePath, configPath, configFile, srcPath, outPath };
return { Base: basePath, PathConfig: configPath, FileConfig: configFile, Source: srcPath, Target: outPath };
};

export default _Function;
10 changes: 6 additions & 4 deletions Source/Interface/Alias.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
export interface Alias {
export default interface Interface {
/** The original path alias. */
alias: string;
Alias: string;

/** The alias prefix that has been matched. */
prefix: string;
Prefix: string;

/** The paths that the alias points to. */
aliasPaths: string[];
Path: string[];
}
14 changes: 8 additions & 6 deletions Source/Interface/Change.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import type { TextChange } from "@Interface/TextChange.js";

export interface Change {
export default interface Interface {
/** The source of the file being changed. */
file: string;
File: string;

/** The new content of the file. */
text: string;
Text: string;

/** A list of text changes in the file. */
changes?: TextChange[];
Change?: TextChange[];
}

import type TextChange from "@Interface/TextChange.js";
25 changes: 18 additions & 7 deletions Source/Interface/ProgramOptions.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,43 @@
export default interface ProgramOptions {
export default interface Interface {
/**
* Path to the project's tsconfig file. Defaults to "tsconfig.json"
* if not provided.
*
*/
project: string;
Project: string;

/**
* Path to the source directory. Defaults to `compilerOptions.rootDir`
* from tsconfig. If `rootDir` is not defined in tsconfig, it will
* default to "src".
*
*/
src?: string;
Source?: string;

/**
* Path to the output directory. Defaults to `compilerOptions.outDir`
* from tsconfig if not provided.
*
*/
out?: string;
Target?: string;

/**
* A list of file extensions that will be matched for replacement.
* Defaults to `["js", "d.ts"]` to handle js and type declaration
* files.
*
*/
ext: string[];
Extension: string[];

/**
* If `true`, verbose logs will be printed for degugging.
*
*/
verbose: boolean;
Verbose: boolean;

/**
* If `true`, changes will not be emitted.
*
*/
noEmit: boolean;
NoEmit: boolean;
}
35 changes: 25 additions & 10 deletions Source/Interface/ProgramPaths.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,31 @@
export default interface Interface {
/** Absolute path to `baseUrl` as defined in the tsconfig file. */
basePath: string;
/**
* Absolute path to `baseUrl` as defined in the tsconfig file.
*
*/
Base: string;

/** Absolute path to the directory the tsconfig file is in. */
configPath: string;
/**
* Absolute path to the directory the tsconfig file is in.
*
*/
PathConfig: string;

/** Absolute path to the tsconfig file. */
configFile: string;
/**
* Absolute path to the tsconfig file.
*
*/
FileConfig: string;

/** Absolute path to the source directory. */
srcPath: string;
/**
* Absolute path to the source directory.
*
*/
Source: string;

/** Absolute path to the output directory. */
outPath: string;
/**
* Absolute path to the output directory.
*
*/
Target: string;
}
7 changes: 4 additions & 3 deletions Source/Interface/TextChange.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export interface TextChange {
export default interface Interface {
/** The original text. */
original: string;
Original: string;

/** The modified text. */
modified: string;
Modify: string;
}

0 comments on commit 4c70f63

Please sign in to comment.