forked from forcedotcom/salesforcedx-templates
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsfdxGenerator.ts
77 lines (71 loc) · 2.43 KB
/
sfdxGenerator.ts
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/*
* Copyright (c) 2020, salesforce.com, inc.
* All rights reserved.
* Licensed under the BSD 3-Clause license.
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/
import * as fs from 'fs';
import * as path from 'path';
import * as Generator from 'yeoman-generator';
import { TemplateService } from '../service/templateService';
import { TemplateOptions } from '../utils/types';
/**
* Base class for generators
*/
export abstract class SfdxGenerator<
TOptions extends TemplateOptions
> extends Generator<Generator.GeneratorOptions> {
options!: TOptions & {
apiversion: string;
outputdir: string;
};
/**
* Set by sourceRootWithPartialPath called in generator
*/
public builtInTemplatesRootPath?: string;
constructor(args: string | string[], options: TOptions) {
super(args, options);
this.options.apiversion =
this.options.apiversion ?? TemplateService.getDefaultApiVersion();
this.options.outputdir = this.options.outputdir ?? process.cwd();
this.validateOptions();
}
/**
* Validate provided options
*/
public abstract validateOptions(): void;
/**
* Set source root to built-in templates or custom templates root if available.
* @param partialPath the relative path from the templates folder to templates root folder.
*/
public sourceRootWithPartialPath(partialPath: string): void {
this.builtInTemplatesRootPath = path.join(
__dirname,
'..',
'templates',
partialPath
);
const { customTemplatesRootPath } = TemplateService.getInstance();
if (!customTemplatesRootPath) {
this.sourceRoot(path.join(this.builtInTemplatesRootPath));
} else {
if (fs.existsSync(path.join(customTemplatesRootPath, partialPath))) {
this.sourceRoot(path.join(customTemplatesRootPath, partialPath));
}
}
}
public templatePath(...paths: string[]): string {
// The template paths are relative to the generator's source root
// If we have set a custom template root, the source root should have already been set.
// Otherwise we'll fallback to the built-in templates
const customPath = super.templatePath(...paths);
if (fs.existsSync(customPath)) {
return customPath;
} else {
// files that are builtin and not in the custom template folder
return super.templatePath(
path.join(this.builtInTemplatesRootPath!, ...paths)
);
}
}
}