-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathutils.ts
289 lines (265 loc) · 7.24 KB
/
utils.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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
import * as vscode from "vscode";
import * as path from "path";
import { getLocator } from "locate-character";
// Easy helper for building text boxes
export function createAsyncTextBox(
title: string,
description: string,
defaultValue?: string
): Promise<string> {
return new Promise((resolve, reject) => {
// Create box obj with options
const box = vscode.window.createInputBox();
box.title = title;
box.prompt = description;
box.value = defaultValue || "";
// Display a message box to the user
box.show();
// Return value if there is one
box.onDidAccept((e) => {
resolve(box.value);
box.dispose();
});
// If cancelled then return nothing...
box.onDidHide(() => {
reject(undefined);
});
});
}
// Easy helper for building option boxes
interface CustomQuickPickItem<T> {
id: T;
label: string;
description?: string;
picked?: boolean;
}
export function createAsyncQuickPick<T>(
title: string,
items: CustomQuickPickItem<T>[]
): Promise<T> {
return new Promise((resolve, reject) => {
// Create box obj with options
const box = vscode.window.createQuickPick();
box.title = title;
box.items = items;
// Display a message box to the user
box.show();
// Return value if there is one
box.onDidAccept((e) => {
resolve(
items.find(({ label }) => box.activeItems[0].label === label)?.id
);
box.dispose();
});
// If cancelled then return nothing...
box.onDidHide(() => {
reject(undefined);
});
});
}
// Function to get selected text
export function getSelectedText(): string {
const editor = vscode.window.activeTextEditor;
const selection = editor?.selection;
if (selection) {
const range = new vscode.Range(selection?.start, selection?.end);
return editor?.document.getText(range) ?? "";
}
return "";
}
interface RegexSearch {
content: string;
range: vscode.Range;
}
// Apply a regex to text and get the range
export const applyRegexSearch = (
text: string,
regexp: RegExp
): RegexSearch | null => {
const locator = getLocator(text);
const selection = text.match(regexp);
if (selection && selection.index) {
const start = locator(selection.index);
const end = locator(selection.index + selection[0].length);
const startPos = new vscode.Position(start.line, start.column);
const endPos = new vscode.Position(end.line, end.column);
return {
content: selection[0],
range: new vscode.Range(startPos, endPos),
};
}
return null;
};
// Get all text from a position to end of file
export const getTextRange = (
document: vscode.TextDocument,
range: vscode.Range
) =>
document.getText(
new vscode.Range(range.start, new vscode.Position(document.lineCount, 0))
);
// Function to open a filename
export function openFile(filename: string | undefined) {
if (filename) {
return vscode.workspace.openTextDocument(filename).then((document) => {
vscode.window.showTextDocument(document);
});
}
}
// Function to extract useful imformation from path
export function extractFilename(currentFilePath: string) {
const ext = path.extname(currentFilePath);
const filename = path.basename(currentFilePath, ext) ?? "";
return {
filename,
directory: path.dirname(currentFilePath),
recommendedComponentName: toPascalCase(filename),
};
}
// Function to convert camal-case to PascalCase
function toPascalCase(str: string) {
return str.replace(/(\-|^)([a-z])/gi, function (
match,
delimiter,
hyphenated
) {
return hyphenated.toUpperCase();
});
}
// Function to check if file is JS
export function isJavascriptFile(filepath?: string) {
if (filepath) {
const activeDocumentExt = path.extname(filepath);
const documentIsJs =
[".js", ".jsx", ".ts", ".tsx"].indexOf(activeDocumentExt) !== -1;
return documentIsJs;
}
return false;
}
// Function to check if file is TS
export function isTypescriptFile(filepath?: string) {
if (filepath) {
const activeDocumentExt = path.extname(filepath);
const documentIsTs = [".ts", ".tsx"].indexOf(activeDocumentExt) !== -1;
return documentIsTs;
}
return false;
}
export interface SvgConversionOptions {
componentName?: string;
reactEnviroment?: string;
isIcon?: boolean;
isExpo?: boolean;
isTypescript?: boolean;
isOverwriting?: boolean;
useMemo?: boolean;
template?: string;
fileLocation?: string | undefined;
originalFilename?: string | undefined;
}
// Ask the details we need to create the component
export async function askConverterOptions(
isSavingFile: boolean = true,
uri: vscode.Uri | null = null,
askIfOverwrite = true
): Promise<SvgConversionOptions> {
// Obtain a URI to recommend a saving place
const currentUri: string | undefined = (
uri || vscode.window.activeTextEditor?.document.uri
)?.fsPath;
const { directory, recommendedComponentName } = extractFilename(
currentUri ?? vscode.workspace.rootPath ?? ""
);
// Display a message box to the user
const componentName = await createAsyncTextBox(
"Component Name",
"What do you want to name the component? ",
recommendedComponentName
);
// Where to save the file
let isOverwriting = false;
let fileLocation: string = "";
if (isSavingFile) {
if (askIfOverwrite) {
// Are we creating extra or overwriting the open file
isOverwriting = await createAsyncQuickPick<boolean>(
"Do you want to overwrite current file?",
[
{
id: true,
label: "Yes",
description: "Convert this .svg to a .js file",
},
{
id: false,
label: "No",
description: "Create a seperate .js file and keep this one",
},
]
);
}
fileLocation = await createAsyncTextBox(
"File Location",
"Where do you want to save the file? ",
`${directory}/${componentName}.js`
);
}
// What type of react?
const reactEnviroment: string = await createAsyncQuickPick(
"Which React enviroment?",
[
{
id: "web",
label: "React JS",
description: "Works only in web enviroments, No imports nessacery",
},
{
id: "native",
label: "React Native",
description:
"Works in RN enviroments only, Uses react-native-svg library",
},
]
);
let isExpo = false;
if (reactEnviroment === "native") {
isExpo = await createAsyncQuickPick("Are you using Expo?", [
{
id: true,
label: "Yes",
description: "",
},
{
id: false,
label: "No",
description: "Click here if you have ejected out of Expo",
},
]);
}
// What type of react?
const isIcon: boolean = await createAsyncQuickPick("Is this SVG an icon?", [
{
id: false,
label: "No",
description: "This is a shape, pattern or other non-icon image",
picked: true,
},
{
id: true,
label: "Yes",
description:
"Yes, this is an icon and I want to resize using font sizes.",
},
]);
return {
componentName,
fileLocation,
reactEnviroment,
isOverwriting,
isExpo,
isIcon,
useMemo: true,
isTypescript: isTypescriptFile(fileLocation),
originalFilename: currentUri,
};
}