Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add an argument to the command line --baseUrl #27

Closed
wants to merge 1 commit into from
Closed
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
10 changes: 5 additions & 5 deletions dist/json-comment-stripper.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ export declare class JsonCommentStripper {
private prevtState;
constructor();
stripComments(data: string): string;
private isQuote(char);
private setState(state);
private inState(state);
private setPrevState();
private inComment();
private isQuote;
private setState;
private inState;
private setPrevState;
private inComment;
parse(data: string): string;
}
11 changes: 7 additions & 4 deletions dist/parser-engine.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,23 @@ export declare class ParserEngine {
projectOptions: ProjectOptions;
tsConfig: any;
fileFilter: Array<string>;
constructor();
baseUrl: string;
constructor(opts: {
baseUrl?: string;
});
exit(code?: number): void;
setProjectPath(projectPath: string): boolean;
/**
* Set the accepted file extensions, ensure leading . (dot)
* @param {Array<string>} filter
*/
setFileFilter(filter: Array<string>): void;
private validateProjectPath(projectPath);
private validateProjectPath;
/**
* Attempts to read the name property form package.json
* @returns {string}
*/
private readProjectName();
private readProjectName;
/**
* Parse project and resolve paths
*/
Expand Down Expand Up @@ -69,7 +72,7 @@ export declare class ParserEngine {
* @param {string} fileExtension - ".xxx" or "xxx
* @returns {boolean}
*/
private matchExtension(fileExtension);
private matchExtension;
/**
* Recursively walking a directory structure and collect files
* @param dir
Expand Down
5 changes: 3 additions & 2 deletions dist/parser-engine.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,11 @@ const type_definitions_1 = require("./type-definitions");
const type_definitions_2 = require("./type-definitions");
const log = console.log;
class ParserEngine {
constructor() {
constructor(opts) {
this.nrFilesProcessed = 0;
this.nrPathsProcessed = 0;
this.compactMode = true;
this.baseUrl = opts.baseUrl || null;
}
exit(code = 5) {
console.log("Terminating...");
Expand Down Expand Up @@ -227,7 +228,7 @@ class ParserEngine {
this.tsConfig = JSON.parse(fileData);
let compilerOpt = this.tsConfig.compilerOptions;
let reqFields = [];
reqFields["baseUrl"] = compilerOpt.baseUrl;
reqFields["baseUrl"] = this.baseUrl || compilerOpt.baseUrl;
reqFields["outDir"] = compilerOpt.outDir;
for (let key in reqFields) {
let field = reqFields[key];
Expand Down
10 changes: 5 additions & 5 deletions dist/src/json-comment-stripper.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ export declare class JsonCommentStripper {
private prevtState;
constructor();
stripComments(data: string): string;
private isQuote(char);
private setState(state);
private inState(state);
private setPrevState();
private inComment();
private isQuote;
private setState;
private inState;
private setPrevState;
private inComment;
parse(data: string): string;
}
16 changes: 4 additions & 12 deletions dist/src/parent-file-finder.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,6 @@

=---------------------------------------------------------------=

Json Comment Stripper

Simple Parser used to strip block and line comments form a
JSON formatted string.

Worth knowing: The parser treat " and ' the same, so it´s
possible to start a string with " and end it with '

This file is part of the TypeScript Path Igniter Project:
https://github.com/duffman/ts-path-igniter

Expand Down Expand Up @@ -62,14 +54,14 @@ class ParentFileFinder {
static findFile(startPath, filename) {
let result = new FileFindResult();
let sep = path.sep;
var parts = startPath.split(sep);
var tmpStr = sep;
for (var i = 0; i < parts.length; i++) {
let parts = startPath.split(sep);
let tmpStr = sep;
for (let i = 0; i < parts.length; i++) {
tmpStr = path.resolve(tmpStr, parts[i]);
tmpStr = utils_1.Utils.ensureTrailingPathDelimiter(tmpStr);
parts[i] = tmpStr;
}
for (var i = parts.length - 1; i > 0; i--) {
for (let i = parts.length - 1; i > 0; i--) {
tmpStr = parts[i];
filename = path.resolve(tmpStr, type_definitions_1.TS_CONFIG);
if (fs.existsSync(filename)) {
Expand Down
28 changes: 24 additions & 4 deletions dist/src/parser-engine.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,39 @@ export declare class ParserEngine {
nrFilesProcessed: number;
nrPathsProcessed: number;
appRoot: string;
distRoot: string;
compactMode: boolean;
projectOptions: ProjectOptions;
tsConfig: any;
constructor();
fileFilter: Array<string>;
baseUrl: string;
constructor(opts: {
baseUrl?: string;
});
exit(code?: number): void;
setProjectPath(projectPath: string): boolean;
private validateProjectPath(projectPath);
/**
* Set the accepted file extensions, ensure leading . (dot)
* @param {Array<string>} filter
*/
setFileFilter(filter: Array<string>): void;
private validateProjectPath;
/**
* Attempts to read the name property form package.json
* @returns {string}
*/
private readProjectName();
private readProjectName;
/**
* Parse project and resolve paths
*/
execute(): void;
/**
*
* @param sourceFilename
* @param jsRequire - require in javascript source "require("jsRequire")
* @returns {string}
*/
getRelativePathForRequiredFile(sourceFilename: string, jsRequire: string): any;
getRelativePathForRequiredFile(sourceFilename: string, jsRequire: string): string;
/**
* Processes the filename specified in require("filename")
* @param node
Expand Down Expand Up @@ -53,6 +67,12 @@ export declare class ParserEngine {
* @param func
*/
traverseSynTree(ast: any, scope: any, func: any): void;
/**
* Match a given file extension with the configured extensions
* @param {string} fileExtension - ".xxx" or "xxx
* @returns {boolean}
*/
private matchExtension;
/**
* Recursively walking a directory structure and collect files
* @param dir
Expand Down
91 changes: 58 additions & 33 deletions dist/src/parser-engine.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,28 +35,35 @@ const type_definitions_1 = require("./type-definitions");
const type_definitions_2 = require("./type-definitions");
const log = console.log;
class ParserEngine {
constructor() {
constructor(opts) {
this.nrFilesProcessed = 0;
this.nrPathsProcessed = 0;
this.compactMode = true;
this.baseUrl = opts.baseUrl || null;
}
exit(code = 5) {
console.log("Terminating...");
process.exit(code);
}
setProjectPath(projectPath) {
/*
this.projectPath = process.cwd();
console.log("CURRENT:", this.projectPath);
*/
if (!utils_1.Utils.isEmpty(projectPath) && !this.validateProjectPath(projectPath)) {
log(chalk.red.bold("Project Path \"" + chalk.underline(projectPath) + "\" is invalid!"));
return false;
}
this.projectPath = projectPath;
return true;
}
/**
* Set the accepted file extensions, ensure leading . (dot)
* @param {Array<string>} filter
*/
setFileFilter(filter) {
this.fileFilter = filter.map((e) => {
return !e.startsWith(".") ? "." + e : e;
});
}
validateProjectPath(projectPath) {
var result = true;
let result = true;
let configFile = utils_1.Utils.ensureTrailingPathDelimiter(projectPath);
configFile += type_definitions_1.TS_CONFIG;
if (!fs.existsSync(projectPath)) {
Expand All @@ -72,14 +79,17 @@ class ParserEngine {
* @returns {string}
*/
readProjectName() {
var projectName = null;
var filename = path.resolve(this.projectPath, "package.json");
let projectName = null;
let filename = path.resolve(this.projectPath, "package.json");
if (fs.existsSync(filename)) {
var json = require(filename);
let json = require(filename);
projectName = json.name;
}
return projectName;
}
/**
* Parse project and resolve paths
*/
execute() {
const PROCESS_TIME = "Operation finished in";
console.time(PROCESS_TIME);
Expand All @@ -96,10 +106,10 @@ class ParserEngine {
log(chalk.yellow.bold("Parsing project at: ") + '"' + this.projectPath + '"');
}
this.appRoot = path.resolve(this.projectPath, this.projectOptions.baseUrl);
this.appRoot = path.resolve(this.appRoot, this.projectOptions.outDir);
this.distRoot = path.resolve(this.projectPath, this.projectOptions.outDir);
let fileList = new Array();
this.walkSync(this.appRoot, fileList, ".js");
for (var i = 0; i < fileList.length; i++) {
this.walkSync(this.distRoot, fileList, ".js");
for (let i = 0; i < fileList.length; i++) {
let filename = fileList[i];
this.processFile(filename);
}
Expand All @@ -116,24 +126,29 @@ class ParserEngine {
*/
getRelativePathForRequiredFile(sourceFilename, jsRequire) {
let options = this.projectOptions;
for (var alias in options.pathMappings) {
var mapping = options.pathMappings[alias];
for (let alias in options.pathMappings) {
let mapping = options.pathMappings[alias];
//TODO: Handle * properly
alias = utils_1.Utils.stripWildcard(alias);
mapping = utils_1.Utils.stripWildcard(mapping);
if (jsRequire.substring(0, alias.length) == alias) {
var result = jsRequire.replace(alias, mapping);
// 2018-06-02: Workaround for bug with same prefix Aliases e.g @db and @dbCore
// Cut alias prefix for mapping comparison
let requirePrefix = jsRequire.substring(0, jsRequire.indexOf(path.sep));
if (requirePrefix == alias) {
let result = jsRequire.replace(alias, mapping);
utils_1.Utils.replaceDoubleSlashes(result);
var absoluteJsRequire = path.join(this.appRoot, result);
var sourceDir = path.dirname(sourceFilename);
result = utils_1.Utils.ensureTrailingPathDelimiter(result);
let absoluteJsRequire = path.join(this.distRoot, result);
let sourceDir = path.dirname(sourceFilename);
let relativePath = path.relative(sourceDir, absoluteJsRequire);
/* If the path does not start with .. it´ not a sub directory
* as in ../ or ..\ so assume it´ the same dir...
*/
if (relativePath[0] != ".") {
relativePath = "./" + relativePath;
}
return relativePath;
jsRequire = relativePath;
break;
}
}
return jsRequire;
Expand Down Expand Up @@ -165,7 +180,7 @@ class ParserEngine {
this.nrFilesProcessed++;
let scope = this;
let inputSourceCode = fs.readFileSync(filename, type_definitions_2.FILE_ENCODING);
var ast = null;
let ast = null;
try {
ast = esprima.parse(inputSourceCode); //, { raw: true, tokens: true, range: true, comment: true });
}
Expand All @@ -179,7 +194,7 @@ class ParserEngine {
node.arguments[0] = scope.processJsRequire(node.arguments[0], filename);
}
});
let option = { comment: true, format: { compact: true, quotes: '"' } };
let option = { comment: true, format: { compact: this.compactMode, quotes: '"' } };
let finalSource = escodegen.generate(ast, option);
try {
this.saveFileContents(filename, finalSource);
Expand All @@ -195,7 +210,7 @@ class ParserEngine {
* @param fileContents
*/
saveFileContents(filename, fileContents) {
var error = false;
let error = false;
fs.writeFileSync(filename, fileContents, type_definitions_2.FILE_ENCODING, error);
if (error) {
throw Error("Could not save file: " + filename);
Expand All @@ -213,10 +228,10 @@ class ParserEngine {
this.tsConfig = JSON.parse(fileData);
let compilerOpt = this.tsConfig.compilerOptions;
let reqFields = [];
reqFields["baseUrl"] = compilerOpt.baseUrl;
reqFields["baseUrl"] = this.baseUrl || compilerOpt.baseUrl;
reqFields["outDir"] = compilerOpt.outDir;
for (var key in reqFields) {
var field = reqFields[key];
for (let key in reqFields) {
let field = reqFields[key];
if (utils_1.Utils.isEmpty(field)) {
log(chalk.red.bold("Missing required field:") + ' "' + chalk.bold.underline(key) + '"');
this.exit(22);
Expand All @@ -232,9 +247,9 @@ class ParserEngine {
*/
traverseSynTree(ast, scope, func) {
func(ast);
for (var key in ast) {
for (let key in ast) {
if (ast.hasOwnProperty(key)) {
var child = ast[key];
let child = ast[key];
if (typeof child === 'object' && child !== null) {
if (Array.isArray(child)) {
child.forEach(function (ast) {
Expand All @@ -248,6 +263,16 @@ class ParserEngine {
}
}
}
/**
* Match a given file extension with the configured extensions
* @param {string} fileExtension - ".xxx" or "xxx
* @returns {boolean}
*/
matchExtension(fileExtension) {
if (utils_1.Utils.isEmpty(fileExtension) || this.fileFilter.length == 0)
return false;
return this.fileFilter.indexOf(fileExtension) > -1;
}
/**
* Recursively walking a directory structure and collect files
* @param dir
Expand All @@ -256,18 +281,18 @@ class ParserEngine {
* @returns {Array<string>}
*/
walkSync(dir, filelist, fileExtension) {
var scope = this;
var files = fs.readdirSync(dir);
let scope = this;
let files = fs.readdirSync(dir);
filelist = filelist || [];
fileExtension = fileExtension === undefined ? "" : fileExtension;
for (var i = 0; i < files.length; i++) {
var file = files[i];
for (let i = 0; i < files.length; i++) {
let file = files[i];
if (fs.statSync(path.join(dir, file)).isDirectory()) {
filelist = this.walkSync(path.join(dir, file), filelist, fileExtension);
}
else {
var tmpExt = path.extname(file);
if ((fileExtension.length > 0 && tmpExt == fileExtension)
let tmpExt = path.extname(file);
if ((fileExtension.length > 0 && scope.matchExtension(fileExtension))
|| (fileExtension.length < 1)
|| (fileExtension == "*.*")) {
let fullFilename = path.join(dir, file);
Expand Down
4 changes: 2 additions & 2 deletions dist/src/tspath.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#! /usr/bin/env node
export declare class TSPath {
private engine;
constructor();
private processPath(projectPath);
parseCommandLineParam(): string;
private processPath;
}
Loading