1+ const { TextDocument } = require ( 'vscode-languageserver-textdocument' ) ;
2+ const { createConnection, TextDocuments, ProposedFeatures } = require ( 'vscode-languageserver/node' ) ;
3+ const ts = require ( 'typescript' ) ;
4+ const fs = require ( 'fs/promises' ) ;
5+ const path = require ( 'path' ) ;
6+
7+ async function decoupleFile ( inputFilePath ) {
8+ // Read the source file
9+ const sourceCode = await fs . readFile ( inputFilePath , 'utf-8' ) ;
10+
11+ // Create output directory
12+ const outputDir = path . join ( path . dirname ( inputFilePath ) , 'decoupled' ) ;
13+ await fs . mkdir ( outputDir , { recursive : true } ) ;
14+
15+ // Parse the source file
16+ const sourceFile = ts . createSourceFile (
17+ inputFilePath ,
18+ sourceCode ,
19+ ts . ScriptTarget . Latest ,
20+ true
21+ ) ;
22+
23+ // Store different sections
24+ const sections = new Map ( ) ;
25+
26+ // Visit nodes and collect classes and major sections
27+ function visit ( node ) {
28+ if ( ts . isClassDeclaration ( node ) && node . name ) {
29+ // Handle classes
30+ const className = node . name . text ;
31+ const classCode = sourceCode . substring ( node . pos , node . end ) ;
32+ sections . set ( `${ className } .js` , classCode ) ;
33+ }
34+ else if ( ts . isFunctionDeclaration ( node ) && node . name ) {
35+ // Handle named functions
36+ const functionName = node . name . text ;
37+ const functionCode = sourceCode . substring ( node . pos , node . end ) ;
38+ sections . set ( `${ functionName } .js` , functionCode ) ;
39+ }
40+ else if ( ts . isVariableStatement ( node ) ) {
41+ // Handle major variable declarations
42+ const declarations = node . declarationList . declarations ;
43+ for ( const decl of declarations ) {
44+ if ( decl . name && ts . isIdentifier ( decl . name ) ) {
45+ const varName = decl . name . text ;
46+ const varCode = sourceCode . substring ( node . pos , node . end ) ;
47+ sections . set ( `${ varName } .js` , varCode ) ;
48+ }
49+ }
50+ }
51+
52+ ts . forEachChild ( node , visit ) ;
53+ }
54+
55+ // Start the visitor pattern
56+ visit ( sourceFile ) ;
57+
58+ // Create index.js to re-export everything
59+ let indexContent = '' ;
60+
61+ // Write sections to files
62+ for ( const [ filename , content ] of sections ) {
63+ const filePath = path . join ( outputDir , filename ) ;
64+ await fs . writeFile ( filePath , content ) ;
65+
66+ // Add to index.js
67+ const moduleName = path . basename ( filename , '.js' ) ;
68+ indexContent += `const { ${ moduleName } } = require('./${ filename } ');\n` ;
69+ }
70+
71+ // Export all modules in index.js
72+ indexContent += '\nmodule.exports = {\n' ;
73+ for ( const [ filename ] of sections ) {
74+ const moduleName = path . basename ( filename , '.js' ) ;
75+ indexContent += ` ${ moduleName } ,\n` ;
76+ }
77+ indexContent += '};\n' ;
78+
79+ await fs . writeFile ( path . join ( outputDir , 'index.js' ) , indexContent ) ;
80+
81+ // Create package.json
82+ const packageJson = {
83+ name : "decoupled-extension" ,
84+ version : "1.0.0" ,
85+ description : "Decoupled version of extension.js" ,
86+ main : "index.js" ,
87+ dependencies : {
88+ "vscode-languageserver" : "^8.0.0" ,
89+ "vscode-languageserver-textdocument" : "^1.0.8" ,
90+ "typescript" : "^4.9.0"
91+ }
92+ } ;
93+
94+ await fs . writeFile (
95+ path . join ( outputDir , 'package.json' ) ,
96+ JSON . stringify ( packageJson , null , 2 )
97+ ) ;
98+ }
99+
100+ // Function to analyze code sections using LSP
101+ async function analyzeSections ( document ) {
102+ const connection = createConnection ( ProposedFeatures . all ) ;
103+ const documents = new TextDocuments ( TextDocument ) ;
104+
105+ documents . listen ( connection ) ;
106+ connection . listen ( ) ;
107+
108+ // Add LSP-specific analysis here if needed
109+ // This can be extended to use LSP features for better code understanding
110+ }
111+
112+ // Main execution
113+ async function main ( ) {
114+ const inputFile = process . argv [ 2 ] || 'extension.js' ;
115+
116+ try {
117+ await decoupleFile ( inputFile ) ;
118+ console . log ( 'Successfully decoupled the file into separate modules!' ) ;
119+ } catch ( error ) {
120+ console . error ( 'Error decoupling file:' , error ) ;
121+ }
122+ }
123+
124+ main ( ) ;
0 commit comments