-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathautoFix.js
79 lines (70 loc) · 2.2 KB
/
autoFix.js
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
const a = `
import type {
DOMConversionMap,
DOMConversionOutput,
DOMExportOutput,
LexicalCommand,
LexicalNode,
NodeKey,
SerializedLexicalNode,
} from 'lexical';
import {useLexicalComposerContext} from '@lexical/react/LexicalComposerContext';
import {useLexicalNodeSelection} from '@lexical/react/useLexicalNodeSelection';
import {mergeRegister} from '@lexical/utils';
import {
$applyNodeReplacement,
$getNodeByKey,
$getSelection,
$isNodeSelection,
CLICK_COMMAND,
COMMAND_PRIORITY_LOW,
createCommand,
DecoratorNode,
KEY_BACKSPACE_COMMAND,
KEY_DELETE_COMMAND,
} from 'lexical';
`;
import { writeFile, readFile } from 'fs/promises';
import { uniq } from 'lodash-es';
import path from 'path';
import { glob, globSync, globStream, globStreamSync, Glob } from 'glob';
import chalk from 'chalk';
async function process(filename) {
// all js files, but don't look in node_modules
const jsfiles = await glob('**/' + filename, { ignore: 'node_modules/**' });
console.log(jsfiles);
const content = (await readFile(jsfiles[0], { encoding: 'utf-8' })).replaceAll('\r\n', '\n');
//console.log(content);
const r = /^(\s*import)(.|\n|\0)* from '.*';/gm;
const dollar = /\$[a-zA-Z|_]+/gm;
const sp = content.match(r);
console.log(sp);
/* let result;
while ((result = r.exec(content)) !== null) {
console.log(result);
}
*/
const importLines = sp.join('\n');
console.log(chalk.green(importLines));
const imports = importLines;
let rest = content.split(/^(import)(.|\n)* from ('.*';)$/gm).at(-1);
console.log(rest);
let new_imports = imports;
const words = uniq(Array.from(content.matchAll(dollar)).map((v) => v[0]));
console.log('--------------');
console.log(new_imports);
console.log('--------------');
console.log(words);
for (let word of words) {
const new_word = word.substring(1);
console.log(word);
new_imports = new_imports.replaceAll(word, word + ' as ' + new_word);
rest = rest.replaceAll(word, new_word);
}
new_imports = new_imports.replace(
'@lexical/react/LexicalComposerContext',
'@lexical/react/LexicalComposerContext.svelte'
);
await writeFile(jsfiles[0].replace('tsx', 'tsx'), new_imports + rest, () => console.log('wrote'));
}
process('DraggableBlockPlugin/index.tsx');