-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #687 from magieno/add-essential-classes-to-cli
- Adds pathManager and shellManager.
- Loading branch information
Showing
6 changed files
with
162 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
export * from "./command-not-found.error"; | ||
export * from "./command-not-found.error"; | ||
export * from "./path-already-contains-filename.error"; |
10 changes: 10 additions & 0 deletions
10
packages/cli/src/errors/path-already-contains-filename.error.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
export class PathAlreadyContainsFilenameError extends Error { | ||
public constructor(message: string) { | ||
super(message); | ||
|
||
// Set the prototype explicitly. | ||
// As specified in the documentation in TypeScript | ||
// https://github.com/Microsoft/TypeScript/wiki/Breaking-Changes#extending-built-ins-like-error-array-and-map-may-no-longer-work | ||
Object.setPrototypeOf(this, PathAlreadyContainsFilenameError.prototype); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,3 @@ | ||
export * from "./console.manager"; | ||
export * from "./console.manager"; | ||
export * from "./path.manager"; | ||
export * from "./path.manager"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import "reflect-metadata" | ||
import {PathManager} from "./path.manager"; | ||
import {PathAlreadyContainsFilenameError} from "../errors/path-already-contains-filename.error"; | ||
|
||
describe("Path Manager", () => { | ||
it("should properly append a path that begins with a '/'.", () => { | ||
const pathManager = new PathManager(); | ||
|
||
const path = pathManager.getPathRelativeToCurrentExecutionDirectory("/allo"); | ||
|
||
const pathParts = path.split("/").reverse(); | ||
|
||
expect(pathParts[0]).toBe("allo") | ||
expect(pathParts[1]).toBe("cli") | ||
expect(pathParts[2]).toBe("packages") | ||
}) | ||
|
||
it("should properly append a path that ends with a '/'.", () => { | ||
const pathManager = new PathManager(); | ||
|
||
const path = pathManager.getPathRelativeToCurrentExecutionDirectory("/allo/"); | ||
|
||
const pathParts = path.split("/").reverse(); | ||
|
||
expect(pathParts[0]).toBe("allo") | ||
expect(pathParts[1]).toBe("cli") | ||
expect(pathParts[2]).toBe("packages") | ||
}) | ||
|
||
it("should throw an error when passing two filenames", () => { | ||
const pathManager = new PathManager(); | ||
|
||
expect(() => pathManager.getPathRelativeToCurrentExecutionDirectory("/allo/test.txt", "data.bin")).toThrow(PathAlreadyContainsFilenameError); | ||
}) | ||
|
||
it("should properly append a path and filename", () => { | ||
const pathManager = new PathManager(); | ||
|
||
const path = pathManager.getPathRelativeToCurrentExecutionDirectory("/allo/", "test.txt"); | ||
|
||
const pathParts = path.split("/").reverse(); | ||
|
||
expect(pathParts[0]).toBe("test.txt") | ||
expect(pathParts[1]).toBe("allo") | ||
expect(pathParts[2]).toBe("cli") | ||
expect(pathParts[3]).toBe("packages") | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import {injectable} from "tsyringe"; | ||
import {PathAlreadyContainsFilenameError} from "../errors/path-already-contains-filename.error"; | ||
|
||
@injectable() | ||
export class PathManager { | ||
getCurrentExecutionDirectory(): string { | ||
return process.cwd(); | ||
} | ||
|
||
getPathRelativeToCurrentExecutionDirectory(path: string, filename?: string): string { | ||
const currentExecutionDirectory = this.getCurrentExecutionDirectory(); | ||
|
||
// If it starts with a "/", remove this character. | ||
path = path.replace(/^\//, ''); | ||
|
||
// If it ends with a "/", remove this character. | ||
path = path.replace(/\/$/, ''); | ||
|
||
// If path already ends with a filename and a filename was also provided, throw an error | ||
if (filename) { | ||
if (path.match(/\.\w+$/)) { | ||
throw new PathAlreadyContainsFilenameError(`The path '${path}' already contains a filename. Cannot add filename: '${filename}'.`) | ||
} | ||
|
||
filename = filename.replace(/^\//, ''); | ||
|
||
path += `/${filename}` | ||
} | ||
|
||
return currentExecutionDirectory + "/" + path; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import {exec, spawn} from "child_process"; | ||
import {ConsoleManager} from "./console.manager"; | ||
import {injectable} from "tsyringe"; | ||
import {PathManager} from "./path.manager"; | ||
|
||
@injectable() | ||
export class ShellManager { | ||
constructor(private readonly consoleManager: ConsoleManager, private readonly pathManager: PathManager) { | ||
} | ||
|
||
execute(command: string, options?: { | ||
directory?: string, | ||
streamStdout?: boolean, | ||
maxBuffer?: number, | ||
}): Promise<string> { | ||
return new Promise<string>((resolve, reject) => { | ||
const env = process.env; | ||
let finalCommand = command; | ||
|
||
const streamStdout = options?.streamStdout ?? false; | ||
const directory = options?.directory; | ||
|
||
if(directory) { | ||
const absoluteDirectory = this.pathManager.getPathRelativeToCurrentExecutionDirectory(directory); | ||
finalCommand = "cd " + absoluteDirectory + " && " + command; | ||
} | ||
|
||
this.consoleManager.writeLine(finalCommand); | ||
|
||
if(streamStdout) { | ||
const child = spawn(finalCommand, [], { shell: true, env }); | ||
child.stdout.on('data', (data) => { | ||
this.consoleManager.writeLine(`${data}`); | ||
}); | ||
|
||
child.stderr.on('data', (data) => { | ||
this.consoleManager.writeLine(`Stderr: ${data}`); | ||
}); | ||
|
||
child.on('close', (code) => { | ||
this.consoleManager.writeLine(`Command exited with code ${code}`); | ||
|
||
if(code !== 0) { | ||
return reject(code); | ||
} | ||
|
||
return resolve(code + ""); | ||
}); | ||
} | ||
|
||
return exec(finalCommand, {env, maxBuffer: options?.maxBuffer}, (error, stdout, stderr) => { | ||
if (error && error.code) { | ||
this.consoleManager.writeLine("Error: " + error.message); | ||
return reject(error); | ||
} | ||
|
||
if (stderr) { | ||
this.consoleManager.writeLine("Stderr: " + stderr); | ||
return resolve(stderr); | ||
} | ||
|
||
this.consoleManager.writeLine(stdout); | ||
return resolve(stdout); | ||
}) | ||
}) | ||
} | ||
} |