-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCommandTemplateProcessor.ts
More file actions
58 lines (47 loc) · 2.17 KB
/
CommandTemplateProcessor.ts
File metadata and controls
58 lines (47 loc) · 2.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import * as path from 'path';
import WatchExecutionManager from "./WatchExecutionManager";
const baseRootRex = /:baseRoot:/g;
const watchedRootRex = /:watchedRoot:/g;
const changedAbsolutePathRex = /:changedAbsPath:/g;
const changedRelativePathRex = /:changedRelPath:/g;
const changedFileRex = /:changedFile:/g;
const changedRelFolderRex = /:changedRelFolder:/g;
const changedAbsFolderRex = /:changedAbsFolder:/g;
const changeTypeRex = /:changeType:/g;
export default class CommandTemplateProcessor {
private _commands: Array<string>;
public constructor(
private _execManager: WatchExecutionManager,
commands: string | Array<string>,
public readonly showStdout: boolean) {
this._commands = commands instanceof Array
? commands
: [ commands ];
}
public get isAllowedExecution(): boolean {
return this._execManager.isAllowedExecution;
}
public get baseRoot(): string {
return this._execManager.baseRoot;
}
public get watchRoot(): string {
return this._execManager.watchRoot;
}
public getDigestedCommands(changeType: string, changedRelativePath: string): Array<string> {
return this._commands.map(template => this.getDigestedCommand(changeType, changedRelativePath, template));
}
public getDigestedCommand(changeType: string, changedRelativePath: string, commandTemplate?: string): string {
let changedAbsolutePath = path.join(this._execManager.absoluteWatchRoot, changedRelativePath);
commandTemplate = commandTemplate || this._commands[0];
return commandTemplate
.replace(baseRootRex, this._execManager.baseRoot)
.replace(watchedRootRex, this._execManager.absoluteWatchRoot)
.replace(changedAbsolutePathRex, changedAbsolutePath)
.replace(changedRelativePathRex, changedRelativePath)
.replace(changedFileRex, path.basename(changedRelativePath))
.replace(changedRelFolderRex, path.dirname(changedRelativePath))
.replace(changedAbsFolderRex, path.dirname(changedAbsolutePath))
.replace(changeTypeRex, changeType)
.replace(/\\/g, '/');
}
}