@voltkit/create-volt 0.1.14 → 0.1.15

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,5 @@
1
+ import type { IndexRange, PropertyValueRange } from './types.js';
2
+ export declare function findDefineConfigObjectBounds(source: string): IndexRange | null;
3
+ export declare function findPropertyValueEnd(source: string, valueStart: number): number;
4
+ export declare function parsePropertyAt(source: string, startIndex: number): PropertyValueRange | null;
5
+ export declare function findTopLevelPropertyValueRange(objectLiteral: string, propertyName: string): IndexRange | null;
@@ -0,0 +1,133 @@
1
+ import { findMatchingBrace, isIdentifierPart, isIdentifierStart, skipQuotedString, skipTokenIfStringOrComment, skipWhitespaceAndComments, } from './scanner.js';
2
+ export function findDefineConfigObjectBounds(source) {
3
+ const needle = 'defineConfig';
4
+ let searchStart = 0;
5
+ while (searchStart < source.length) {
6
+ const callIndex = source.indexOf(needle, searchStart);
7
+ if (callIndex === -1) {
8
+ return null;
9
+ }
10
+ let index = skipWhitespaceAndComments(source, callIndex + needle.length);
11
+ if (source[index] !== '(') {
12
+ searchStart = callIndex + needle.length;
13
+ continue;
14
+ }
15
+ index = skipWhitespaceAndComments(source, index + 1);
16
+ if (source[index] !== '{') {
17
+ searchStart = callIndex + needle.length;
18
+ continue;
19
+ }
20
+ const closeIndex = findMatchingBrace(source, index);
21
+ return closeIndex === -1 ? null : { start: index, end: closeIndex + 1 };
22
+ }
23
+ return null;
24
+ }
25
+ export function findPropertyValueEnd(source, valueStart) {
26
+ let index = valueStart;
27
+ let curlyDepth = 0;
28
+ let squareDepth = 0;
29
+ let parenDepth = 0;
30
+ while (index < source.length) {
31
+ const nextIndex = skipTokenIfStringOrComment(source, index);
32
+ if (nextIndex !== null) {
33
+ index = nextIndex;
34
+ continue;
35
+ }
36
+ const current = source[index];
37
+ if (current === '{') {
38
+ curlyDepth += 1;
39
+ }
40
+ else if (current === '}') {
41
+ if (curlyDepth === 0 && squareDepth === 0 && parenDepth === 0) {
42
+ return index;
43
+ }
44
+ curlyDepth = Math.max(0, curlyDepth - 1);
45
+ }
46
+ else if (current === '[') {
47
+ squareDepth += 1;
48
+ }
49
+ else if (current === ']') {
50
+ squareDepth = Math.max(0, squareDepth - 1);
51
+ }
52
+ else if (current === '(') {
53
+ parenDepth += 1;
54
+ }
55
+ else if (current === ')') {
56
+ parenDepth = Math.max(0, parenDepth - 1);
57
+ }
58
+ else if (current === ',' && curlyDepth === 0 && squareDepth === 0 && parenDepth === 0) {
59
+ return index;
60
+ }
61
+ index += 1;
62
+ }
63
+ return source.length;
64
+ }
65
+ export function parsePropertyAt(source, startIndex) {
66
+ let index = skipWhitespaceAndComments(source, startIndex);
67
+ if (index >= source.length) {
68
+ return null;
69
+ }
70
+ let key;
71
+ if (source[index] === "'" || source[index] === '"') {
72
+ const endQuote = skipQuotedString(source, index);
73
+ if (endQuote <= index + 1 || endQuote > source.length) {
74
+ return null;
75
+ }
76
+ key = source.slice(index + 1, endQuote - 1);
77
+ index = endQuote;
78
+ }
79
+ else if (isIdentifierStart(source[index])) {
80
+ const keyStart = index;
81
+ index += 1;
82
+ while (index < source.length && isIdentifierPart(source[index])) {
83
+ index += 1;
84
+ }
85
+ key = source.slice(keyStart, index);
86
+ }
87
+ else {
88
+ return null;
89
+ }
90
+ index = skipWhitespaceAndComments(source, index);
91
+ if (source[index] !== ':') {
92
+ return null;
93
+ }
94
+ const valueStart = index + 1;
95
+ return {
96
+ key,
97
+ start: valueStart,
98
+ end: findPropertyValueEnd(source, valueStart),
99
+ };
100
+ }
101
+ export function findTopLevelPropertyValueRange(objectLiteral, propertyName) {
102
+ let index = 1;
103
+ let depth = 1;
104
+ while (index < objectLiteral.length) {
105
+ const nextIndex = skipTokenIfStringOrComment(objectLiteral, index);
106
+ if (nextIndex !== null) {
107
+ index = nextIndex;
108
+ continue;
109
+ }
110
+ if (objectLiteral[index] === '{') {
111
+ depth += 1;
112
+ index += 1;
113
+ continue;
114
+ }
115
+ if (objectLiteral[index] === '}') {
116
+ depth -= 1;
117
+ index += 1;
118
+ continue;
119
+ }
120
+ if (depth === 1) {
121
+ const property = parsePropertyAt(objectLiteral, index);
122
+ if (property) {
123
+ if (property.key === propertyName) {
124
+ return { start: property.start, end: property.end };
125
+ }
126
+ index = property.end;
127
+ continue;
128
+ }
129
+ }
130
+ index += 1;
131
+ }
132
+ return null;
133
+ }
@@ -0,0 +1,2 @@
1
+ export declare function replacePropertyValue(objectLiteral: string, propertyName: string, replacementLiteral: string): string;
2
+ export declare function replaceWindowTitle(configObject: string, displayNameLiteral: string): string;
@@ -0,0 +1,35 @@
1
+ import { findMatchingBrace, skipWhitespaceAndComments } from './scanner.js';
2
+ import { findTopLevelPropertyValueRange } from './parsing.js';
3
+ export function replacePropertyValue(objectLiteral, propertyName, replacementLiteral) {
4
+ const propertyRange = findTopLevelPropertyValueRange(objectLiteral, propertyName);
5
+ if (!propertyRange) {
6
+ return objectLiteral;
7
+ }
8
+ const currentValue = objectLiteral.slice(propertyRange.start, propertyRange.end);
9
+ const leadingWhitespace = currentValue.match(/^\s*/u)?.[0] ?? '';
10
+ return (objectLiteral.slice(0, propertyRange.start) +
11
+ leadingWhitespace +
12
+ replacementLiteral +
13
+ objectLiteral.slice(propertyRange.end));
14
+ }
15
+ export function replaceWindowTitle(configObject, displayNameLiteral) {
16
+ const windowRange = findTopLevelPropertyValueRange(configObject, 'window');
17
+ if (!windowRange) {
18
+ return configObject;
19
+ }
20
+ const windowValue = configObject.slice(windowRange.start, windowRange.end);
21
+ const objectStart = skipWhitespaceAndComments(windowValue, 0);
22
+ if (windowValue[objectStart] !== '{') {
23
+ return configObject;
24
+ }
25
+ const objectEnd = findMatchingBrace(windowValue, objectStart);
26
+ if (objectEnd === -1) {
27
+ return configObject;
28
+ }
29
+ const windowObject = windowValue.slice(objectStart, objectEnd + 1);
30
+ const updatedWindowObject = replacePropertyValue(windowObject, 'title', displayNameLiteral);
31
+ const updatedWindowValue = windowValue.slice(0, objectStart) + updatedWindowObject + windowValue.slice(objectEnd + 1);
32
+ return (configObject.slice(0, windowRange.start) +
33
+ updatedWindowValue +
34
+ configObject.slice(windowRange.end));
35
+ }
@@ -0,0 +1,9 @@
1
+ export declare function isIdentifierStart(character: string): boolean;
2
+ export declare function isIdentifierPart(character: string): boolean;
3
+ export declare function isWhitespace(character: string): boolean;
4
+ export declare function skipQuotedString(source: string, startIndex: number): number;
5
+ export declare function skipLineComment(source: string, startIndex: number): number;
6
+ export declare function skipBlockComment(source: string, startIndex: number): number;
7
+ export declare function skipTokenIfStringOrComment(source: string, startIndex: number): number | null;
8
+ export declare function skipWhitespaceAndComments(source: string, startIndex: number): number;
9
+ export declare function findMatchingBrace(source: string, openBraceIndex: number): number;
@@ -0,0 +1,92 @@
1
+ export function isIdentifierStart(character) {
2
+ return /^[A-Za-z_$]$/u.test(character);
3
+ }
4
+ export function isIdentifierPart(character) {
5
+ return /^[A-Za-z0-9_$]$/u.test(character);
6
+ }
7
+ export function isWhitespace(character) {
8
+ return character === ' ' || character === '\t' || character === '\n' || character === '\r';
9
+ }
10
+ export function skipQuotedString(source, startIndex) {
11
+ const quote = source[startIndex];
12
+ let index = startIndex + 1;
13
+ while (index < source.length) {
14
+ if (source[index] === '\\') {
15
+ index += 2;
16
+ continue;
17
+ }
18
+ if (source[index] === quote) {
19
+ return index + 1;
20
+ }
21
+ index += 1;
22
+ }
23
+ return source.length;
24
+ }
25
+ export function skipLineComment(source, startIndex) {
26
+ let index = startIndex + 2;
27
+ while (index < source.length && source[index] !== '\n') {
28
+ index += 1;
29
+ }
30
+ return index;
31
+ }
32
+ export function skipBlockComment(source, startIndex) {
33
+ let index = startIndex + 2;
34
+ while (index + 1 < source.length && !(source[index] === '*' && source[index + 1] === '/')) {
35
+ index += 1;
36
+ }
37
+ return Math.min(index + 2, source.length);
38
+ }
39
+ export function skipTokenIfStringOrComment(source, startIndex) {
40
+ const current = source[startIndex];
41
+ const next = source[startIndex + 1];
42
+ if (current === "'" || current === '"' || current === '`') {
43
+ return skipQuotedString(source, startIndex);
44
+ }
45
+ if (current === '/' && next === '/') {
46
+ return skipLineComment(source, startIndex);
47
+ }
48
+ if (current === '/' && next === '*') {
49
+ return skipBlockComment(source, startIndex);
50
+ }
51
+ return null;
52
+ }
53
+ export function skipWhitespaceAndComments(source, startIndex) {
54
+ let index = startIndex;
55
+ while (index < source.length) {
56
+ if (isWhitespace(source[index])) {
57
+ index += 1;
58
+ continue;
59
+ }
60
+ const nextIndex = skipTokenIfStringOrComment(source, index);
61
+ if (nextIndex === null || nextIndex <= index) {
62
+ return index;
63
+ }
64
+ if (source[index] === "'" || source[index] === '"' || source[index] === '`') {
65
+ return index;
66
+ }
67
+ index = nextIndex;
68
+ }
69
+ return index;
70
+ }
71
+ export function findMatchingBrace(source, openBraceIndex) {
72
+ let depth = 0;
73
+ let index = openBraceIndex;
74
+ while (index < source.length) {
75
+ const nextIndex = skipTokenIfStringOrComment(source, index);
76
+ if (nextIndex !== null) {
77
+ index = nextIndex;
78
+ continue;
79
+ }
80
+ if (source[index] === '{') {
81
+ depth += 1;
82
+ }
83
+ else if (source[index] === '}') {
84
+ depth -= 1;
85
+ if (depth === 0) {
86
+ return index;
87
+ }
88
+ }
89
+ index += 1;
90
+ }
91
+ return -1;
92
+ }
@@ -0,0 +1,7 @@
1
+ export interface IndexRange {
2
+ start: number;
3
+ end: number;
4
+ }
5
+ export interface PropertyValueRange extends IndexRange {
6
+ key: string;
7
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -1,258 +1,5 @@
1
- function isIdentifierStart(character) { return /^[A-Za-z_$]$/u.test(character); }
2
- function isIdentifierPart(character) { return /^[A-Za-z0-9_$]$/u.test(character); }
3
- function isWhitespace(character) {
4
- return character === ' ' || character === '\t' || character === '\n' || character === '\r';
5
- }
6
- function skipQuotedString(source, startIndex) {
7
- const quote = source[startIndex];
8
- let index = startIndex + 1;
9
- while (index < source.length) {
10
- if (source[index] === '\\') {
11
- index += 2;
12
- continue;
13
- }
14
- if (source[index] === quote) {
15
- return index + 1;
16
- }
17
- index += 1;
18
- }
19
- return source.length;
20
- }
21
- function skipLineComment(source, startIndex) {
22
- let index = startIndex + 2;
23
- while (index < source.length && source[index] !== '\n') {
24
- index += 1;
25
- }
26
- return index;
27
- }
28
- function skipBlockComment(source, startIndex) {
29
- let index = startIndex + 2;
30
- while (index + 1 < source.length && !(source[index] === '*' && source[index + 1] === '/')) {
31
- index += 1;
32
- }
33
- return Math.min(index + 2, source.length);
34
- }
35
- function skipTokenIfStringOrComment(source, startIndex) {
36
- const current = source[startIndex];
37
- const next = source[startIndex + 1];
38
- if (current === '\'' || current === '"' || current === '`') {
39
- return skipQuotedString(source, startIndex);
40
- }
41
- if (current === '/' && next === '/') {
42
- return skipLineComment(source, startIndex);
43
- }
44
- if (current === '/' && next === '*') {
45
- return skipBlockComment(source, startIndex);
46
- }
47
- return null;
48
- }
49
- function skipWhitespaceAndComments(source, startIndex) {
50
- let index = startIndex;
51
- while (index < source.length) {
52
- if (isWhitespace(source[index])) {
53
- index += 1;
54
- continue;
55
- }
56
- const nextIndex = skipTokenIfStringOrComment(source, index);
57
- if (nextIndex === null || nextIndex <= index) {
58
- return index;
59
- }
60
- if (source[index] === '\'' || source[index] === '"' || source[index] === '`') {
61
- return index;
62
- }
63
- index = nextIndex;
64
- }
65
- return index;
66
- }
67
- function findMatchingBrace(source, openBraceIndex) {
68
- let depth = 0;
69
- let index = openBraceIndex;
70
- while (index < source.length) {
71
- const nextIndex = skipTokenIfStringOrComment(source, index);
72
- if (nextIndex !== null) {
73
- index = nextIndex;
74
- continue;
75
- }
76
- if (source[index] === '{') {
77
- depth += 1;
78
- }
79
- else if (source[index] === '}') {
80
- depth -= 1;
81
- if (depth === 0) {
82
- return index;
83
- }
84
- }
85
- index += 1;
86
- }
87
- return -1;
88
- }
89
- function findDefineConfigObjectBounds(source) {
90
- const needle = 'defineConfig';
91
- let searchStart = 0;
92
- while (searchStart < source.length) {
93
- const callIndex = source.indexOf(needle, searchStart);
94
- if (callIndex === -1) {
95
- return null;
96
- }
97
- let index = skipWhitespaceAndComments(source, callIndex + needle.length);
98
- if (source[index] !== '(') {
99
- searchStart = callIndex + needle.length;
100
- continue;
101
- }
102
- index = skipWhitespaceAndComments(source, index + 1);
103
- if (source[index] !== '{') {
104
- searchStart = callIndex + needle.length;
105
- continue;
106
- }
107
- const closeIndex = findMatchingBrace(source, index);
108
- return closeIndex === -1 ? null : { start: index, end: closeIndex + 1 };
109
- }
110
- return null;
111
- }
112
- function findPropertyValueEnd(source, valueStart) {
113
- let index = valueStart;
114
- let curlyDepth = 0;
115
- let squareDepth = 0;
116
- let parenDepth = 0;
117
- while (index < source.length) {
118
- const nextIndex = skipTokenIfStringOrComment(source, index);
119
- if (nextIndex !== null) {
120
- index = nextIndex;
121
- continue;
122
- }
123
- const current = source[index];
124
- if (current === '{') {
125
- curlyDepth += 1;
126
- }
127
- else if (current === '}') {
128
- if (curlyDepth === 0 && squareDepth === 0 && parenDepth === 0) {
129
- return index;
130
- }
131
- curlyDepth = Math.max(0, curlyDepth - 1);
132
- }
133
- else if (current === '[') {
134
- squareDepth += 1;
135
- }
136
- else if (current === ']') {
137
- squareDepth = Math.max(0, squareDepth - 1);
138
- }
139
- else if (current === '(') {
140
- parenDepth += 1;
141
- }
142
- else if (current === ')') {
143
- parenDepth = Math.max(0, parenDepth - 1);
144
- }
145
- else if (current === ',' && curlyDepth === 0 && squareDepth === 0 && parenDepth === 0) {
146
- return index;
147
- }
148
- index += 1;
149
- }
150
- return source.length;
151
- }
152
- function parsePropertyAt(source, startIndex) {
153
- let index = skipWhitespaceAndComments(source, startIndex);
154
- if (index >= source.length) {
155
- return null;
156
- }
157
- let key;
158
- if (source[index] === '\'' || source[index] === '"') {
159
- const endQuote = skipQuotedString(source, index);
160
- if (endQuote <= index + 1 || endQuote > source.length) {
161
- return null;
162
- }
163
- key = source.slice(index + 1, endQuote - 1);
164
- index = endQuote;
165
- }
166
- else if (isIdentifierStart(source[index])) {
167
- const keyStart = index;
168
- index += 1;
169
- while (index < source.length && isIdentifierPart(source[index])) {
170
- index += 1;
171
- }
172
- key = source.slice(keyStart, index);
173
- }
174
- else {
175
- return null;
176
- }
177
- index = skipWhitespaceAndComments(source, index);
178
- if (source[index] !== ':') {
179
- return null;
180
- }
181
- const valueStart = index + 1;
182
- return {
183
- key,
184
- start: valueStart,
185
- end: findPropertyValueEnd(source, valueStart),
186
- };
187
- }
188
- function findTopLevelPropertyValueRange(objectLiteral, propertyName) {
189
- let index = 1;
190
- let depth = 1;
191
- while (index < objectLiteral.length) {
192
- const nextIndex = skipTokenIfStringOrComment(objectLiteral, index);
193
- if (nextIndex !== null) {
194
- index = nextIndex;
195
- continue;
196
- }
197
- if (objectLiteral[index] === '{') {
198
- depth += 1;
199
- index += 1;
200
- continue;
201
- }
202
- if (objectLiteral[index] === '}') {
203
- depth -= 1;
204
- index += 1;
205
- continue;
206
- }
207
- if (depth === 1) {
208
- const property = parsePropertyAt(objectLiteral, index);
209
- if (property) {
210
- if (property.key === propertyName) {
211
- return { start: property.start, end: property.end };
212
- }
213
- index = property.end;
214
- continue;
215
- }
216
- }
217
- index += 1;
218
- }
219
- return null;
220
- }
221
- function replacePropertyValue(objectLiteral, propertyName, replacementLiteral) {
222
- const propertyRange = findTopLevelPropertyValueRange(objectLiteral, propertyName);
223
- if (!propertyRange) {
224
- return objectLiteral;
225
- }
226
- const currentValue = objectLiteral.slice(propertyRange.start, propertyRange.end);
227
- const leadingWhitespace = currentValue.match(/^\s*/u)?.[0] ?? '';
228
- return (objectLiteral.slice(0, propertyRange.start)
229
- + leadingWhitespace
230
- + replacementLiteral
231
- + objectLiteral.slice(propertyRange.end));
232
- }
233
- function replaceWindowTitle(configObject, displayNameLiteral) {
234
- const windowRange = findTopLevelPropertyValueRange(configObject, 'window');
235
- if (!windowRange) {
236
- return configObject;
237
- }
238
- const windowValue = configObject.slice(windowRange.start, windowRange.end);
239
- const objectStart = skipWhitespaceAndComments(windowValue, 0);
240
- if (windowValue[objectStart] !== '{') {
241
- return configObject;
242
- }
243
- const objectEnd = findMatchingBrace(windowValue, objectStart);
244
- if (objectEnd === -1) {
245
- return configObject;
246
- }
247
- const windowObject = windowValue.slice(objectStart, objectEnd + 1);
248
- const updatedWindowObject = replacePropertyValue(windowObject, 'title', displayNameLiteral);
249
- const updatedWindowValue = (windowValue.slice(0, objectStart)
250
- + updatedWindowObject
251
- + windowValue.slice(objectEnd + 1));
252
- return (configObject.slice(0, windowRange.start)
253
- + updatedWindowValue
254
- + configObject.slice(windowRange.end));
255
- }
1
+ import { findDefineConfigObjectBounds } from './config-updater/parsing.js';
2
+ import { replaceWindowTitle, replacePropertyValue } from './config-updater/replacement.js';
256
3
  export function updateVoltConfigContent(configContent, displayName) {
257
4
  const configBounds = findDefineConfigObjectBounds(configContent);
258
5
  if (!configBounds) {
@@ -261,7 +8,7 @@ export function updateVoltConfigContent(configContent, displayName) {
261
8
  const displayNameLiteral = JSON.stringify(displayName);
262
9
  const configObject = configContent.slice(configBounds.start, configBounds.end);
263
10
  const updatedConfigObject = replaceWindowTitle(replacePropertyValue(configObject, 'name', displayNameLiteral), displayNameLiteral);
264
- return (configContent.slice(0, configBounds.start)
265
- + updatedConfigObject
266
- + configContent.slice(configBounds.end));
11
+ return (configContent.slice(0, configBounds.start) +
12
+ updatedConfigObject +
13
+ configContent.slice(configBounds.end));
267
14
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@voltkit/create-volt",
3
- "version": "0.1.14",
3
+ "version": "0.1.15",
4
4
  "license": "BSL-1.1",
5
5
  "repository": {
6
6
  "type": "git",