t1kit 0.1.0 → 0.1.1

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.
@@ -93,6 +93,10 @@ export const SEMANTIC_SUBMODULE_PATH = 'src/semantic-ui';
93
93
  export const SEMANTIC_THEME_TS = 'front-semantic-theme-ts';
94
94
  export const SEMANTIC_THEME_BRANCH = 'main';
95
95
 
96
+ export const isThemeTsRemoteUrl = url => /front-semantic-theme-ts/.test(url || '');
97
+
98
+ export const isLegacyThemeRemoteUrl = url => /front-semantic-theme(?!-ts\b)/.test(url || '');
99
+
96
100
  export const patchGitmodules = source =>
97
101
  source.replace(/(url\s*=\s*.*)front-semantic-theme(?!-ts)(\s|$)/g, '$1front-semantic-theme-ts$2');
98
102
 
@@ -118,8 +122,8 @@ export const parseSemanticSubmodule = source => {
118
122
  return null;
119
123
  };
120
124
 
121
- export const semanticAddUrl = source => {
122
- const parsed = parseSemanticSubmodule(source || '');
125
+ export const semanticAddUrl = (source, fallback = '') => {
126
+ const parsed = parseSemanticSubmodule(source || '') || parseSemanticSubmodule(fallback || '');
123
127
 
124
128
  if (parsed?.url) {
125
129
  return parsed.url.replace(/front-semantic-theme(?!-ts\b)/, SEMANTIC_THEME_TS);
@@ -174,31 +178,279 @@ export const patchPackageJson = source => {
174
178
  return `${JSON.stringify(pkg, null, '\t')}\n`;
175
179
  };
176
180
 
181
+ const newlineOf = source => (source.includes('\r\n') ? '\r\n' : '\n');
182
+
183
+ const withNewlines = (text, nl) => (nl === '\r\n' ? text.replaceAll('\n', '\r\n') : text);
184
+
185
+ const lineIndent = (source, index) => {
186
+ const lineStart = source.lastIndexOf('\n', index - 1) + 1;
187
+
188
+ return source.slice(lineStart, index).match(/^[ \t]*/)?.[0] ?? '';
189
+ };
190
+
191
+ const skipComment = (source, index) => {
192
+ if (source[index + 1] === '/') {
193
+ const nl = source.indexOf('\n', index);
194
+
195
+ return nl < 0 ? source.length : nl;
196
+ }
197
+
198
+ const close = source.indexOf('*/', index + 2);
199
+
200
+ return close < 0 ? source.length : close + 2;
201
+ };
202
+
203
+ const insideCommentOrString = source => {
204
+ const hidden = new Uint8Array(source.length);
205
+
206
+ for (let i = 0; i < source.length; i += 1) {
207
+ const char = source[i];
208
+
209
+ if (char === "'" || char === '"' || char === '`') {
210
+ const end = skipQuoted(source, i);
211
+ // The opening quote of a code token stays visible, so a real key
212
+ // ('@typescript-eslint/no-explicit-any': ...) is not treated as string text.
213
+ if (end > i + 1) hidden.fill(1, i + 1, end);
214
+ i = end - 1;
215
+ continue;
216
+ }
217
+
218
+ if (char === '/' && (source[i + 1] === '/' || source[i + 1] === '*')) {
219
+ const end = skipComment(source, i);
220
+ hidden.fill(1, i, end);
221
+ i = end - 1;
222
+ }
223
+ }
224
+
225
+ return index => hidden[index] === 1;
226
+ };
227
+
228
+ const braceClose = (source, open) => {
229
+ let depth = 0;
230
+
231
+ for (let i = open; i < source.length; i += 1) {
232
+ const char = source[i];
233
+
234
+ if (char === "'" || char === '"' || char === '`') {
235
+ i = skipQuoted(source, i) - 1;
236
+ continue;
237
+ }
238
+
239
+ if (char === '/' && (source[i + 1] === '/' || source[i + 1] === '*')) {
240
+ const end = skipComment(source, i);
241
+ if (end >= source.length) return -1;
242
+ i = end - 1;
243
+ continue;
244
+ }
245
+
246
+ if (char === '{') depth += 1;
247
+ else if (char === '}') {
248
+ depth -= 1;
249
+ if (depth === 0) return i;
250
+ }
251
+ }
252
+
253
+ return -1;
254
+ };
255
+
256
+ const braceDepthAt = (source, index) => {
257
+ let depth = 0;
258
+
259
+ for (let i = 0; i < index; i += 1) {
260
+ const char = source[i];
261
+
262
+ if (char === "'" || char === '"' || char === '`') {
263
+ i = skipQuoted(source, i) - 1;
264
+ continue;
265
+ }
266
+
267
+ if (char === '/' && (source[i + 1] === '/' || source[i + 1] === '*')) {
268
+ const end = skipComment(source, i);
269
+ if (end >= source.length) break;
270
+ i = end - 1;
271
+ continue;
272
+ }
273
+
274
+ if (char === '{') depth += 1;
275
+ else if (char === '}') depth -= 1;
276
+ }
277
+
278
+ return depth;
279
+ };
280
+
281
+ const findProperty = (
282
+ source,
283
+ key,
284
+ { withBrace = false, from = 0, to = source.length, hidden = insideCommentOrString(source) } = {},
285
+ ) => {
286
+ const re = new RegExp(`\\b${key}\\s*:${withBrace ? '\\s*\\{' : ''}`, 'g');
287
+ re.lastIndex = from;
288
+ let match = re.exec(source);
289
+
290
+ while (match && match.index < to) {
291
+ if (!hidden(match.index)) return match;
292
+ match = re.exec(source);
293
+ }
294
+
295
+ return null;
296
+ };
297
+
298
+ const findShallowestBlock = (source, key) => {
299
+ const hidden = insideCommentOrString(source);
300
+ const re = new RegExp(`\\b${key}\\s*:\\s*\\{`, 'g');
301
+ let match = re.exec(source);
302
+ let best = null;
303
+
304
+ while (match) {
305
+ if (!hidden(match.index)) {
306
+ const open = match.index + match[0].lastIndexOf('{');
307
+ const close = braceClose(source, open);
308
+
309
+ if (close > open) {
310
+ const depth = braceDepthAt(source, match.index);
311
+ if (!best || depth < best.depth) best = { open, close, depth };
312
+ }
313
+ }
314
+
315
+ match = re.exec(source);
316
+ }
317
+
318
+ return best;
319
+ };
320
+
321
+ const findBlockInside = (source, parent, key) => {
322
+ const match = findProperty(source, key, { withBrace: true, from: parent.open + 1, to: parent.close });
323
+
324
+ if (!match) return null;
325
+
326
+ const open = match.index + match[0].lastIndexOf('{');
327
+ const close = braceClose(source, open);
328
+
329
+ if (close < 0 || close > parent.close) return null;
330
+
331
+ return { open, close };
332
+ };
333
+
334
+ const spliceAfterOpen = (source, open, lines) => {
335
+ const close = braceClose(source, open);
336
+ if (close < 0) return source;
337
+
338
+ const nl = newlineOf(source);
339
+ const inner = source.slice(open + 1, close);
340
+ const keyIndent = lineIndent(source, open);
341
+ const childIndent = inner.match(/\n([ \t]+)\S/)?.[1] ?? `${keyIndent}\t`;
342
+ const rendered = lines.map(line => `${childIndent}${line}`).join(nl);
343
+
344
+ if (!inner.includes('\n')) {
345
+ if (lines.length === 1) {
346
+ const property = inner.trim() ? lines[0] : lines[0].replace(/,$/, '');
347
+ const rest = inner.startsWith(' ') || !inner.trim() ? inner : ` ${inner}`;
348
+
349
+ return `${source.slice(0, open + 1)} ${property}${rest}${source.slice(close)}`;
350
+ }
351
+
352
+ const tail = inner.trim() ? `${nl}${childIndent}${inner.trim()}` : '';
353
+
354
+ return `${source.slice(0, open + 1)}${nl}${rendered}${tail}${nl}${keyIndent}${source.slice(close)}`;
355
+ }
356
+
357
+ return `${source.slice(0, open + 1)}${nl}${rendered}${source.slice(open + 1)}`;
358
+ };
359
+
360
+ const loaderMapsJsToJsx = (source, loader) => {
361
+ let text = '';
362
+
363
+ for (let i = loader.open + 1; i < loader.close; i += 1) {
364
+ const char = source[i];
365
+
366
+ if (char === "'" || char === '"' || char === '`') {
367
+ const end = Math.min(skipQuoted(source, i), loader.close);
368
+ text += source.slice(i, end);
369
+ i = end - 1;
370
+ continue;
371
+ }
372
+
373
+ if (char === '/' && (source[i + 1] === '/' || source[i + 1] === '*')) {
374
+ i = Math.min(skipComment(source, i), loader.close) - 1;
375
+ continue;
376
+ }
377
+
378
+ text += char;
379
+ }
380
+
381
+ return /['"]\.js['"]\s*:\s*['"]jsx['"]/.test(text);
382
+ };
383
+
384
+ const insertFreshOptimizeDeps = source => {
385
+ const replacement = withNewlines(
386
+ "exclude: ['node_modules', 'dist'],\n\t},\n\toptimizeDeps: {\n\t\tesbuildOptions: {\n\t\t\tloader: {\n\t\t\t\t'.js': 'jsx',\n\t\t\t},\n\t\t},\n\t},\n\tserver:",
387
+ newlineOf(source),
388
+ );
389
+
390
+ return source.replace(/exclude:\s*\['node_modules', 'dist'\],\r?\n\t\},\r?\n\tserver:/, replacement);
391
+ };
392
+
393
+ const ensureOptimizeDepsJsxLoader = source => {
394
+ const block = findShallowestBlock(source, 'optimizeDeps');
395
+
396
+ if (!block) {
397
+ if (findProperty(source, 'optimizeDeps')) return source;
398
+
399
+ return insertFreshOptimizeDeps(source);
400
+ }
401
+
402
+ const esbuild = findBlockInside(source, block, 'esbuildOptions');
403
+
404
+ if (!esbuild) {
405
+ return spliceAfterOpen(source, block.open, [
406
+ 'esbuildOptions: {',
407
+ '\tloader: {',
408
+ "\t\t'.js': 'jsx',",
409
+ '\t},',
410
+ '},',
411
+ ]);
412
+ }
413
+
414
+ const loader = findBlockInside(source, esbuild, 'loader');
415
+
416
+ if (!loader) {
417
+ return spliceAfterOpen(source, esbuild.open, ['loader: {', "\t'.js': 'jsx',", '},']);
418
+ }
419
+
420
+ if (loaderMapsJsToJsx(source, loader)) return source;
421
+
422
+ return spliceAfterOpen(source, loader.open, ["'.js': 'jsx',"]);
423
+ };
424
+
177
425
  export const patchViteConfig = (source, { hasJsxTransformer } = { hasJsxTransformer: true }) => {
178
426
  let next = source;
427
+ const nl = newlineOf(next);
179
428
 
180
429
  if (hasJsxTransformer && !next.includes('jsxTransformer')) {
181
430
  next = next.replace(
182
431
  /import eslint from 'vite-plugin-eslint';/,
183
- "import eslint from 'vite-plugin-eslint';\nimport transformJsxInJs from './src/modules/plugins/jsxTransformer';",
432
+ withNewlines(
433
+ "import eslint from 'vite-plugin-eslint';\nimport transformJsxInJs from './src/modules/plugins/jsxTransformer';",
434
+ nl,
435
+ ),
184
436
  );
185
437
  }
186
438
 
187
439
  if (hasJsxTransformer && !next.includes('transformJsxInJs(')) {
188
- next = next.replace(/plugins:\s*\[\s*\n\s*eslint\(/, 'plugins: [\n\t\ttransformJsxInJs(mode === \'production\'),\n\t\teslint(');
189
- }
190
-
191
- if (!next.includes('optimizeDeps:')) {
192
- next = next.replace(
193
- /exclude:\s*\['node_modules', 'dist'\],\n\t},\n\tserver:/,
194
- "exclude: ['node_modules', 'dist'],\n\t},\n\toptimizeDeps: {\n\t\tesbuildOptions: {\n\t\t\tloader: {\n\t\t\t\t'.js': 'jsx',\n\t\t\t},\n\t\t},\n\t},\n\tserver:",
440
+ next = next.replace(/plugins:\s*\[\s*\r?\n(\s*)eslint\(/, (_match, indent) =>
441
+ withNewlines(`plugins: [\n${indent}transformJsxInJs(mode === 'production'),\n${indent}eslint(`, nl),
195
442
  );
196
443
  }
197
444
 
445
+ next = ensureOptimizeDepsJsxLoader(next);
446
+
198
447
  if (!next.includes("find: 'fonts'")) {
199
448
  next = next.replace(
200
- /alias:\s*\[\s*\n\s*\{\s*\n\s*find: '\.\.\/\.\.\/theme\.config'/,
201
- "alias: [\n\t\t\t{\n\t\t\t\tfind: 'fonts',\n\t\t\t\treplacement: resolve(__dirname, 'src/semantic-ui/site/globals/fonts'),\n\t\t\t},\n\t\t\t{\n\t\t\t\tfind: '../../theme.config'",
449
+ /alias:\s*\[\s*\r?\n\s*\{\s*\r?\n\s*find: '\.\.\/\.\.\/theme\.config'/,
450
+ withNewlines(
451
+ "alias: [\n\t\t\t{\n\t\t\t\tfind: 'fonts',\n\t\t\t\treplacement: resolve(__dirname, 'src/semantic-ui/site/globals/fonts'),\n\t\t\t},\n\t\t\t{\n\t\t\t\tfind: '../../theme.config'",
452
+ nl,
453
+ ),
202
454
  );
203
455
  }
204
456
 
@@ -207,22 +459,107 @@ export const patchViteConfig = (source, { hasJsxTransformer } = { hasJsxTransfor
207
459
 
208
460
  const insert = replacement => () => replacement;
209
461
 
462
+ const replaceOutsideCommentOrString = (source, pattern, replacer) => {
463
+ const literal = typeof pattern === 'string';
464
+ const sourcePattern = literal ? pattern.replace(/[.*+?^${}()|[\]\\]/g, '\\$&') : pattern.source;
465
+ const flags = literal || !pattern.flags.includes('g') ? 'g' : pattern.flags;
466
+ const re = new RegExp(sourcePattern, flags);
467
+ const hidden = insideCommentOrString(source);
468
+ const matches = [];
469
+ let match = re.exec(source);
470
+
471
+ while (match) {
472
+ if (!hidden(match.index)) {
473
+ matches.push(match);
474
+ if (literal || !pattern.flags.includes('g')) break;
475
+ } else {
476
+ let resume = match.index;
477
+
478
+ while (resume < source.length && hidden(resume)) resume += 1;
479
+ re.lastIndex = resume > match.index ? resume : match.index + 1;
480
+ }
481
+
482
+ match = re.exec(source);
483
+ }
484
+
485
+ if (!matches.length) return source;
486
+
487
+ const apply = item => {
488
+ const replacement = typeof replacer === 'function' ? replacer(...item, item.index, source) : replacer;
489
+
490
+ return `${source.slice(0, item.index)}${replacement}${source.slice(item.index + item[0].length)}`;
491
+ };
492
+
493
+ if (literal || !pattern.flags.includes('g')) return apply(matches[0]);
494
+
495
+ return matches.reduceRight((next, item) => {
496
+ const replacement = typeof replacer === 'function' ? replacer(...item, item.index, source) : replacer;
497
+
498
+ return `${next.slice(0, item.index)}${replacement}${next.slice(item.index + item[0].length)}`;
499
+ }, source);
500
+ };
501
+
502
+ const appearsInCode = (source, pattern) => {
503
+ const literal = typeof pattern === 'string';
504
+ const sourcePattern = literal ? pattern.replace(/[.*+?^${}()|[\]\\]/g, '\\$&') : pattern.source;
505
+ const flags = literal || !pattern.flags.includes('g') ? 'g' : pattern.flags;
506
+ const re = new RegExp(sourcePattern, flags);
507
+ const hidden = insideCommentOrString(source);
508
+ let match = re.exec(source);
509
+
510
+ while (match) {
511
+ if (!hidden(match.index)) return true;
512
+
513
+ let resume = match.index;
514
+
515
+ while (resume < source.length && hidden(resume)) resume += 1;
516
+ re.lastIndex = resume > match.index ? resume : match.index + 1;
517
+ match = re.exec(source);
518
+ }
519
+
520
+ return false;
521
+ };
522
+
523
+ const insertEslintConstants = source => {
524
+ if (!source || appearsInCode(source, 'const importNoUnresolvedIgnore')) return source;
525
+
526
+ const nl = newlineOf(source);
527
+ const block = withNewlines(ESLINT_CONSTANTS, nl);
528
+ const afterTsPlugin = replaceOutsideCommentOrString(
529
+ source,
530
+ /(import tsPlugin from '@typescript-eslint\/eslint-plugin';)(\r?\n)/,
531
+ (_match, importLine, lineEnd) => `${importLine}${lineEnd}${block}`,
532
+ );
533
+
534
+ if (afterTsPlugin !== source) return afterTsPlugin;
535
+
536
+ const beforeExport = replaceOutsideCommentOrString(
537
+ source,
538
+ /(\r?\n)(export default \[)/,
539
+ (_match, lineEnd, exp) => `${lineEnd}${block}${lineEnd}${exp}`,
540
+ );
541
+
542
+ if (beforeExport !== source) return beforeExport;
543
+
544
+ if (!appearsInCode(source, /export default|tsPlugin|import\/no-unresolved/)) return source;
545
+
546
+ throw new Error(
547
+ 'Не смог вставить const importNoUnresolvedIgnore в eslint.config.js. Нужен import tsPlugin или `export default [`.',
548
+ );
549
+ };
550
+
210
551
  export const patchEslintConfig = source => {
211
552
  let next = source;
212
553
 
213
554
  if (!next.includes("from 'node:fs'")) {
214
- next = next.replace("import js from '@eslint/js';", insert(`${ESLINT_HELPER}import js from '@eslint/js';`));
555
+ next = replaceOutsideCommentOrString(next, "import js from '@eslint/js';", insert(`${ESLINT_HELPER}import js from '@eslint/js';`));
215
556
  }
216
557
 
217
- if (!next.includes('importNoUnresolvedIgnore')) {
218
- next = next.replace(
219
- "import tsPlugin from '@typescript-eslint/eslint-plugin';\n",
220
- insert(`import tsPlugin from '@typescript-eslint/eslint-plugin';\n${ESLINT_CONSTANTS}`),
221
- );
222
- }
558
+ next = insertEslintConstants(next);
223
559
 
224
560
  if (!next.includes("'artifacts/**'")) {
225
- next = next.replace(
561
+ next = replaceOutsideCommentOrString(
562
+ next,
226
563
  "ignores: ['dist/**', 'build/**', 'coverage/**', 'node_modules/**', '.git', '.vscode', '.idea']",
227
564
  insert(`ignores: [
228
565
  'dist/**',
@@ -238,24 +575,30 @@ export const patchEslintConfig = source => {
238
575
  );
239
576
  }
240
577
 
241
- next = next.replace(
578
+ next = replaceOutsideCommentOrString(
579
+ next,
242
580
  /ignore:\s*\[[^\]]*semantic-ui-css[^\]]*\]/g,
243
581
  insert('ignore: importNoUnresolvedIgnore'),
244
582
  );
245
583
 
246
- next = next.replace(
584
+ next = replaceOutsideCommentOrString(
585
+ next,
247
586
  /languageOptions:\s*\{\s*parser:\s*tsParser,[\s\S]*?parserOptions:\s*\{\s*project:\s*'[^']+',\s*\},\s*\},/,
248
587
  insert(TS_LANGUAGE_OPTIONS),
249
588
  );
250
589
 
251
- next = next.replace(
252
- /'jsx-a11y\/click-events-have-key-events': 'off',\n(\t\t\t'import\/prefer-default-export': 'off',)/,
253
- (_match, importRule) =>
254
- `'jsx-a11y/click-events-have-key-events': 'off',\n\t\t\t'jsx-a11y/no-autofocus': 'off',\n${importRule}`,
590
+ next = replaceOutsideCommentOrString(
591
+ next,
592
+ /'jsx-a11y\/click-events-have-key-events': 'off',\r?\n(\t\t\t'import\/prefer-default-export': 'off',)/,
593
+ (match, importRule) => {
594
+ const nl = match.includes('\r\n') ? '\r\n' : '\n';
595
+ return `'jsx-a11y/click-events-have-key-events': 'off',${nl}\t\t\t'jsx-a11y/no-autofocus': 'off',${nl}${importRule}`;
596
+ },
255
597
  );
256
598
 
257
599
  if (!next.includes("'@typescript-eslint/no-redeclare'")) {
258
- next = next.replace(
600
+ next = replaceOutsideCommentOrString(
601
+ next,
259
602
  "'@typescript-eslint/no-explicit-any': 'warn',",
260
603
  insert(`'@typescript-eslint/no-explicit-any': 'warn',
261
604
  'no-redeclare': 'off',
@@ -272,7 +615,8 @@ export const patchEslintConfig = source => {
272
615
  }
273
616
 
274
617
  if (next.includes("files: ['**/*.{ts,tsx}']") && !next.includes('typescript: { alwaysTryTypes: true }')) {
275
- next = next.replace(
618
+ next = replaceOutsideCommentOrString(
619
+ next,
276
620
  /'import\/resolver': \{\s*node: \{\s*extensions: \['\.js', '\.jsx', '\.ts', '\.tsx', '\.mjs'\],\s*moduleDirectory: \['node_modules'\],\s*typescript: \{\},\s*\},\s*typescript: \{\s*alwaysTryTypes: true,\s*\},\s*\}/,
277
621
  insert(`'import/resolver': {
278
622
  typescript: { alwaysTryTypes: true },
@@ -284,7 +628,8 @@ export const patchEslintConfig = source => {
284
628
  }
285
629
 
286
630
  if (!next.includes("'prettier/prettier': 'off'")) {
287
- next = next.replace(
631
+ next = replaceOutsideCommentOrString(
632
+ next,
288
633
  /files: \['src\/semantic-ui\/\*\*\/\*\.\{js,jsx,ts,tsx\}'\],\s*rules: \{\s*'react-hooks\/rules-of-hooks': 'off',/,
289
634
  insert(`files: ['src/semantic-ui/**/*.{js,jsx,ts,tsx}'],
290
635
  rules: {