-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmod.ts
194 lines (175 loc) · 5.8 KB
/
mod.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
import { join, relative, dirname } from 'node:path';
import { mkdir, stat, writeFile, cp } from 'node:fs/promises';
import { createInterface } from 'node:readline/promises';
import { scanDirectory } from './scanDir.ts';
import { transformFileContent } from './transformFileContent.ts';
import { getComponentVariations } from './getComponentVariations.ts';
import process from 'node:process';
import { STARTER_TEMPLATES, type TemplateType } from './types.ts';
/**
* Prompts user to select a starter template using a CLI menu
* @returns Promise resolving to selected template type
* @throws Error if stdin/stdout interaction fails
*/
async function promptForTemplate(): Promise<TemplateType> {
const rl = createInterface({
input: process.stdin,
output: process.stdout,
terminal: false,
});
try {
const templates = Object.entries(STARTER_TEMPLATES);
console.log('======= SELECT A TEMPLATE =======');
templates.forEach(([_key, { name }], index) => {
console.log(`${index + 1}) ${name}`);
});
const input = await rl.question(
'Enter the number of the template you want to generate: '
);
return templates[parseInt(input.trim() || '1', 10) - 1][0] as TemplateType;
} finally {
rl.close();
}
}
/**
* Creates a new project from a starter template
* @param outputPath Path where the template should be created
* @param templateType Optional template type. If not provided, user will be prompted
*/
async function createStarterTemplate(
outputPath: string,
templateType?: TemplateType
): Promise<void> {
const template = templateType || (await promptForTemplate());
const templatePath = STARTER_TEMPLATES[template].path;
try {
await stat(templatePath);
await mkdir(outputPath, { recursive: true });
await cp(templatePath, outputPath, { recursive: true });
console.log(`Created ${template} template in ${outputPath}`);
} catch (error) {
if (error instanceof Error) {
throw new Error(`Failed to create template: ${error.message}`);
}
throw error;
}
}
/**
* Converts a directory containing a component into a Plop template directory.
* Looks for a PascalCase file as the main component and transforms all related files.
* @param sourcePath Path to the source directory containing the component
* @param outputPath Path where the template directory should be created
* @throws Error if source path doesn't exist or no PascalCase component file is found
*/
export async function convertToTemplate(
sourcePath: string,
outputPath: string
) {
try {
const sourceInfo = await stat(sourcePath);
if (!sourceInfo.isDirectory()) {
throw new Error('Source path must be a directory');
}
} catch (error) {
if (error instanceof Error) {
throw new Error('Source path must be a directory');
}
throw error;
}
// Create output directory if it doesn't exist
await mkdir(outputPath, { recursive: true });
// Scan the directory
const files = await scanDirectory(sourcePath);
// Find the main component name (look for PascalCase file)
const mainComponentFile = files.find((file) =>
/^[A-Z][a-zA-Z0-9]*\.(tsx?|jsx?)$/.test(file.name)
);
if (!mainComponentFile) {
throw new Error(
'Could not find main component file (should be PascalCase)'
);
}
const componentName = mainComponentFile.name.split('.')[0];
const componentInfo = getComponentVariations(componentName);
// Process each file
for (const file of files) {
if (file.isDirectory) {
const relativePath = relative(sourcePath, file.path);
await mkdir(join(outputPath, relativePath), { recursive: true });
continue;
}
// Transform content using the main component info
const transformedContent = await transformFileContent(
file.path,
componentInfo
);
// Transform filename using the appropriate pattern from componentInfo
let newFileName = file.name;
if (newFileName.includes(componentInfo.variations.pascal)) {
newFileName = newFileName.replace(
componentInfo.variations.pascal,
'{{pascalCaseName}}'
);
} else if (newFileName.includes(componentInfo.variations.camel)) {
newFileName = newFileName.replace(
componentInfo.variations.camel,
'{{camelCaseName}}'
);
} else if (newFileName.includes(componentInfo.variations.kebab)) {
newFileName = newFileName.replace(
componentInfo.variations.kebab,
'{{kebabCaseName}}'
);
} else if (newFileName.includes(componentInfo.variations.snake)) {
newFileName = newFileName.replace(
componentInfo.variations.snake,
'{{snakeCaseName}}'
);
}
newFileName += '.hbs';
// Create output file
const relativePath = relative(sourcePath, file.path);
const outputFilePath = join(outputPath, relativePath);
const outputFileDir = dirname(outputFilePath);
await mkdir(outputFileDir, { recursive: true });
await writeFile(join(outputFileDir, newFileName), transformedContent);
}
}
if (import.meta.main) {
const args = process.argv.slice(2);
if (!args.length) {
console.log('Usage: dir-to-plop <source-directory> <output-directory>');
console.log(' or: dir-to-plop --starter <output-directory>');
console.log(
' or: dir-to-plop --starter-react-component <output-directory>'
);
process.exit(1);
}
try {
if (args[0] === '--starter') {
if (!args[1]) {
console.error('Error: Output directory is required');
process.exit(1);
}
await createStarterTemplate(args[1]);
} else if (args[0] === '--starter-react-component') {
if (!args[1]) {
console.error('Error: Output directory is required');
process.exit(1);
}
await createStarterTemplate(args[1], 'ReactComponent');
} else if (args[0] && args[1]) {
await convertToTemplate(args[0], args[1]);
} else {
console.error('Error: Invalid arguments');
process.exit(1);
}
} catch (error) {
if (error instanceof Error) {
console.error(`Error: ${error.message}`);
} else {
console.error('An unknown error occurred');
}
process.exit(1);
}
}