Skip to content

Commit

Permalink
Added support for watching over file changes in the source directory …
Browse files Browse the repository at this point in the history
…to automaticly trigger the transpile process
  • Loading branch information
wartoshika committed Nov 19, 2018
1 parent 4cc51da commit ef6144b
Show file tree
Hide file tree
Showing 9 changed files with 271 additions and 39 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Expand Up @@ -8,7 +8,7 @@ The qhun-transpiler project uses [Semantic Versioning](https://semver.org/spec/v

## Implemented but unreleased

- Nothing
- Added support for watching over file changes in the source directory to automaticly trigger the transpile process. `-w` flag on the cli.

## **0.7.2** released 2018-11-18

Expand Down
60 changes: 39 additions & 21 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Expand Up @@ -31,6 +31,7 @@
"@types/chai": "^4.1.4",
"@types/command-line-args": "^5.0.0",
"@types/command-line-usage": "^5.0.1",
"@types/md5": "^2.1.33",
"@types/mkdirp": "^0.5.2",
"@types/mocha": "^5.2.4",
"@types/mock-fs": "^3.6.30",
Expand Down Expand Up @@ -58,6 +59,7 @@
"command-line-args": "^5.0.2",
"command-line-usage": "^5.0.5",
"mkdirp": "^0.5.1",
"md5": "^2.2.1",
"typescript": "^3.0.3",
"typescript-mix": "^3.1.3"
}
Expand Down
80 changes: 71 additions & 9 deletions src/cli/CommandLine.ts
Expand Up @@ -10,6 +10,8 @@ import { Compiler } from "../compiler/Compiler";
import { CommandLineColors } from "./CommandLineColors";
import { ValidationError } from "../error/ValidationError";
import { ExternalModuleService } from "../compiler/ExternalModuleService";
import { FileWatcher } from "./FileWatcher";
import { Logger } from "./Logger";

// tslint:disable-next-line
const packageJson = require("../../package.json");
Expand All @@ -18,7 +20,8 @@ declare type ProgramArguments = {
help: boolean,
project: string,
target: string,
file: string
file: string,
watch: boolean
};

export class CommandLine {
Expand Down Expand Up @@ -49,6 +52,11 @@ export class CommandLine {
type: String,
description: "The file that shoule be transpiled",
typeLabel: "<file.ts>"
}, {
name: "watch",
alias: "w",
type: Boolean,
description: "Watches over edited and created files to automaticly trigger the transpiling process"
}
];

Expand All @@ -57,6 +65,16 @@ export class CommandLine {
*/
private programArguments: ProgramArguments;

/**
* the current project reference
*/
private currentProject: Project;

/**
* the file watcher instance
*/
private fileWatcher: FileWatcher;

/**
* @param args the arguments
*/
Expand All @@ -71,9 +89,9 @@ export class CommandLine {
}

/**
* executes the command line tool with the given arguments
* prepares the execution of the transpiling process
*/
public execute(): boolean {
public prepare(): boolean {

// evaluate help page
if (this.programArguments.help || this.args.length === 0) {
Expand All @@ -89,17 +107,49 @@ export class CommandLine {
return false;
}

// print an execution header
this.printProgramExecuteInfo();

// save project reference
this.currentProject = project;

// watch for file changes
if (this.programArguments.watch) {

this.fileWatcher = new FileWatcher(this.currentProject, this.execute.bind(this));
}
}

/**
* executes the command line tool with the given arguments
*/
public execute(): boolean {

// check if state is prepared
if (!this.currentProject) {
return false;
}

// construct the compiler
const compiler = new Compiler(project);
const compiler = new Compiler(this.currentProject);

// start everything else
const result = compiler.compile(project.parsedCommandLine.fileNames);
const result = compiler.compile(this.currentProject.parsedCommandLine.fileNames);

// print the final result
this.printResult(result);

return result !== false;
}

/**
* test if the cli is watching over files
*/
public isWatchingFiles(): boolean {

return !!this.fileWatcher;
}

/**
* get the project config from either json reader or argument reader
*/
Expand Down Expand Up @@ -132,7 +182,7 @@ export class CommandLine {
if (e instanceof ValidationError) {

// write the error
console.error(`${CommandLineColors.RED}${e.message}${CommandLineColors.RESET}`);
Logger.error(e.message, undefined, CommandLineColors.RED);

// no transpiling!
return false;
Expand Down Expand Up @@ -177,14 +227,26 @@ export class CommandLine {
// only print something about external modules when some are referenced
if (embededModules.length > 0) {
embededModules.forEach(moduleName => {
console.log(`${CommandLineColors.GREEN}%s${CommandLineColors.RESET}`, `Added ${moduleName} as external module.`);
Logger.log(`Added ${moduleName} as external module.`, undefined, CommandLineColors.GREEN);
});
}

console.log(`${CommandLineColors.GREEN}%s${CommandLineColors.RESET}`, `Successfully transpiled ${result} files.`);
Logger.log(`Successfully transpiled ${result} files.`, undefined, CommandLineColors.GREEN);
} else {
console.log(`${CommandLineColors.RED}%s${CommandLineColors.RESET}`, `An error occured while transpiling your files.`);
Logger.log(`An error occured while transpiling your files.`, undefined, CommandLineColors.GREEN);
}
}

/**
* print some program metadata for the command line
*/
private printProgramExecuteInfo(): void {

// read package.json file
const packageObject = packageJson;

Logger.log();
Logger.log(`${packageObject.name} (${packageObject.version})`, "");
Logger.log();
}
}

0 comments on commit ef6144b

Please sign in to comment.