-
Notifications
You must be signed in to change notification settings - Fork 31
/
types.ts
216 lines (191 loc) · 5 KB
/
types.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
import pkgVersions from '@cli/pkgVersions.json';
import type { Answers, QuestionCollection } from 'inquirer';
import { taggedTemplateHelpers as helpers } from './utils';
/**
* A boolean value passed to the generator to identify the CMS
*/
type DrupalOrWP = {
[key in 'drupal' | 'wp']?: boolean;
};
/**
* Converts a kebab-case string to camel case
*/
type CamelCase<T extends string> = T extends `${infer First}-${infer Rest}`
? `${Lowercase<First>}${Capitalize<CamelCase<Rest>>}`
: Lowercase<T>;
/**
* Camel case keys to use for the package versions in BaseGeneratorData
*/
type PackageVersionKeys = `${CamelCase<keyof typeof pkgVersions>}Version`;
type PackageVersionData = {
[key in PackageVersionKeys]: string;
};
/**
* Base data that is passed to all generators
* @example
* ```
* {
* drupalKitVersion: versions['drupal-kit'],
* drupal: true
* }
* ```
*/
export type BaseGeneratorData = Partial<PackageVersionData> & DrupalOrWP;
export interface GatsbyWPData extends BaseGeneratorData {
gatsbyPnpmPlugin: boolean;
gatsby: true;
}
/**
* Generators need prompts to get user data not provided by CLI arguments
*/
export interface DecoupledKitGenerator<
Prompts = DefaultAnswers,
Data = unknown,
> {
/**
* Generator's name. This should be kebab case.
*/
name: string;
/**
* Description of the generator
*/
description: string;
/**
* An array of inquirer prompts
* @template Prompts - the type of the required user input
* @default DefaultAnswers - { outDir: string }
*/
prompts: QuestionCollection<
Prompts[] extends DefaultAnswers ? Prompts : DefaultAnswers
>[];
/**
* An array of paths to the generator's templates.
* This should be empty if the generator does not have templates.
*/
templates: string[];
/**
* An array of actions to run with the prompts and templates
*/
actions: Action[];
/**
* Any extra data that should be passed from the generator to the actions
*/
data?: Data;
/**
* Set to true if the generator is considered an addon.
* This will give priority to the templates when de-duping.
*/
addon?: boolean;
/**
* Any message(s) to be rendered after actions are successfully completed.
*/
nextSteps?: string[];
/**
* Identifies a generators compatible CMS(s).
*/
cmsType: CMSType['Drupal'] | CMSType['WordPress'] | 'any';
}
/**
* Valid CMS Type Options
*/
type CMSType = {
Drupal: 'd9' | 'd10' | 'drupal';
WordPress: 'wp' | 'wordpress';
};
type DependencyConfig = {
[key in 'dependencies' | 'devDependencies']?: Record<string, string>;
};
/**
* An action that takes in the data, templates, and an instance of handlebars
* and does an action, like installing dependencies or formatting generated code
*/
export type Action = (config: ActionConfig) => Promise<string> | string;
export type ActionRunner = (config: ActionRunnerConfig) => Promise<string>;
type VariousData = {
[key: string]: string | string[] | boolean | Record<string, unknown>;
};
/**
* Input from command line arguments, prompts, and generator data
*/
type InputIndex = DrupalOrWP &
GatsbyWPData &
PackageVersionData &
DependencyConfig &
Partial<VariousData> & {
_: string[];
appName: string;
outDir: string;
templateRootDir: string;
cmsEndpoint: string;
noInstall: boolean;
noLint: boolean;
force: boolean;
silent: boolean;
tailwindcss: boolean;
};
/**
* Input from command line arguments, prompts, and generator data
*/
export type Input = {
[Property in keyof InputIndex]: InputIndex[Property];
};
export interface TemplateData {
templateDirs: string[];
addon: boolean;
}
export interface MergedPaths {
[key: string]: { addon: boolean; base: string };
}
export interface DefaultAnswers extends Answers {
outDir: string;
}
interface ActionConfig {
data: Input;
templateData?: TemplateData[];
handlebars?: typeof Handlebars;
}
interface ActionRunnerConfig extends ActionConfig {
actions: Action[];
}
/**
* Helper utilities for template literal templates
*/
type Helpers = typeof helpers;
/**
* Arguments for the {@link TemplateFn}
*/
interface TemplateFnArgs<Data extends Input> {
data: Data;
utils: Helpers;
}
/**
* A tagged template literal function with data and utils context
*/
export declare type TemplateFn = <Data extends Input>({
data,
utils,
}: TemplateFnArgs<Data>) => string;
export interface TemplateImport {
default: TemplateFn;
}
/**
* @param arg a variable
* @returns true if the variable is a string, false otherwise
*/
export const isString = (arg: unknown): arg is string => {
return typeof arg === 'string';
};
/**
* @param value a string
* @returns true if the variable matches a Drupal alias in {@link CMSType['Drupal']}, false otherwise
*/
export const isDrupalCms = (value: string): value is CMSType['Drupal'] => {
return ['drupal', 'd9', 'd10'].includes(value);
};
/**
* @param value a string
* @returns true if the variable matches a WordPress alias in {@link CMSType['WordPress']}, false otherwise
*/
export const isWpCms = (value: string): value is CMSType['WordPress'] => {
return ['wordpress', 'wp'].includes(value);
};