-
Notifications
You must be signed in to change notification settings - Fork 1
/
template_options.ts
103 lines (90 loc) · 3.08 KB
/
template_options.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import { GenerationContext } from 'src/generators';
export enum PluginType { Datasource = 'datasource', Panel = 'panel' }
export enum Framework { Angular = 'angular', React = 'react' }
export enum Language { JavaScript = 'javascript', TypeScript = 'typescript' }
export enum Style { CSS = 'css', SASS = 'sass', None = 'none' }
export function getDefaultId(options: any): string {
var suffix = '';
switch(options.pluginType) {
case PluginType.Datasource:
suffix = 'datasource';
break;
case PluginType.Panel:
suffix = 'panel';
break;
default:
throw new Error('Unknown plugin type: ' + options.pluginType);
}
var s1 = options.pluginName + '-' + suffix;
return s1.toLowerCase();
}
function languageToExtension(language: Language) {
switch(language) {
case Language.JavaScript:
return 'js';
case Language.TypeScript:
return 'ts';
default:
throw new Error('Unknown language: ' + language);
}
}
export function srcExt(name: string) {
if(name.indexOf('{ext}') === -1) {
throw new Error('Template file name should contain "{ext}"');
}
return (context: GenerationContext<TemplateOptions>) =>
name.replace('{ext}', languageToExtension(context.options.language))
}
export namespace ts {
function tsCode(context: GenerationContext<TemplateOptions>) {
return (code) => context.options.language === Language.TypeScript ? code : '';
}
function tsType(context: GenerationContext<TemplateOptions>) {
return (type) => context.options.language === Language.TypeScript ? `: ${type}` : '';
}
function isTypeScript(context: GenerationContext<TemplateOptions>) {
return context.options.language === Language.TypeScript;
}
export function bind(context: GenerationContext<TemplateOptions>) {
context['tsCode'] = tsCode(context);
context['tsType'] = tsType(context);
context['isTypeScript'] = isTypeScript(context);
return context;
}
}
export class TemplateOptions {
public id: string;
public pluginName: string;
public pluginType: PluginType;
public framework: Framework;
public language: Language;
public style?: Style;
public overWriteDir: boolean;
constructor(options: any) {
this.id = options.id;
if(this.id === undefined) {
throw new Error('Missing id value');
}
this.pluginName = options.pluginName;
if(this.pluginName === undefined) {
throw new Error('Missing plugin name value');
}
this.pluginType = options.pluginType;
if(this.pluginType === undefined) {
throw new Error('Missing plugin type value');
}
this.framework = options.framework;
if(this.framework === undefined) {
throw new Error('Missing framework value');
}
this.language = options.language;
if(this.language === undefined) {
throw new Error('Missing language value');
}
this.style = options.style;
if(this.style === undefined) {
throw new Error('Missing style value');
}
this.overWriteDir = options.overWriteDir;
}
}