@@ -2,7 +2,7 @@ import fs from 'node:fs';
22import os from 'node:os' ;
33import path from 'node:path' ;
44import { cwd } from 'node:process' ;
5- import { emptyDirSync , readFile , readJson , readJsonSync , writeFile , writeJson } from '@tomjs/node' ;
5+ import { readJson , readJsonSync , writeFile , writeJson } from '@tomjs/node' ;
66import type { IExtensionManifest } from '@tomjs/vscode-types' ;
77import chalk from 'chalk' ;
88import chokidar from 'chokidar' ;
@@ -11,9 +11,8 @@ import { formatCode, getDtsOutputPath, logger } from './utils';
1111
1212const ROOT = cwd ( ) ;
1313
14- const DTS_CACHE_DIR = path . join ( ROOT , 'node_modules' , '.cache/@tomjs/vscode-dev' ) ;
15- const DTS_CACHE_NLS_PATH = path . join ( DTS_CACHE_DIR , 'nls.d.ts' ) ;
16- const DTS_CACHE_PKG_PATH = path . join ( DTS_CACHE_DIR , 'pkg.d.ts' ) ;
14+ let nlsCode = '' ;
15+ let pkgCode = '' ;
1716
1817function createWatcher ( paths : string | string [ ] , callback : ( ) => Promise < any > ) {
1918 const watchPaths = Array . isArray ( paths ) ? paths : [ paths ] ;
@@ -51,8 +50,6 @@ function createWatcher(paths: string | string[], callback: () => Promise<any>) {
5150}
5251
5352export async function generateCode ( opts : CLIOptions ) {
54- emptyDirSync ( DTS_CACHE_DIR ) ;
55-
5653 opts . cwd = opts . cwd ?? ROOT ;
5754 opts . locales = path . join ( opts . cwd , opts . locales ?? 'locales' ) ;
5855 opts . lang = opts . lang ?? 'en' ;
@@ -109,7 +106,7 @@ async function genNlsDts(keys: string[], opts: CLIOptions) {
109106 const nslKeys = [ ...new Set ( keys ) ] ;
110107 nslKeys . sort ( ) ;
111108
112- const code = /* ts */ `
109+ nlsCode = /* ts */ `
113110import '@tomjs/vscode';
114111
115112declare module '@tomjs/vscode' {
@@ -127,7 +124,6 @@ declare module '@tomjs/vscode' {
127124}
128125 ` ;
129126
130- await writeFile ( DTS_CACHE_NLS_PATH , code ) ;
131127 await mergeDts ( opts ) ;
132128
133129 logger . success ( `generate ${ chalk . green ( opts . dtsName ) } [package.nls.json]` ) ;
@@ -223,34 +219,21 @@ async function genPackageDts(opts: CLIOptions) {
223219 logger . error ( e ?. message ) ;
224220 }
225221
226- const code = /* ts */ `
222+ pkgCode = /* ts */ `
227223declare module 'vscode' {
228224 ${ getCommandDts ( pkg , opts ) }
229225
230226 ${ getViewDts ( pkg ) }
231227}
232228` ;
233229
234- await writeFile ( DTS_CACHE_PKG_PATH , code ) ;
235230 await mergeDts ( opts ) ;
236231
237232 logger . success ( `generate ${ chalk . green ( opts . dtsName ) } [package.json]` ) ;
238233}
239234
240235async function mergeDts ( opts : CLIOptions ) {
241- const codes = await Promise . all (
242- [ DTS_CACHE_NLS_PATH , DTS_CACHE_PKG_PATH ] . map ( async filePath => {
243- if ( ! fs . existsSync ( filePath ) ) {
244- return '' ;
245- }
246- return readFile ( filePath ) ;
247- } ) ,
248- ) ;
249-
236+ const codes = [ nlsCode , pkgCode ] . filter ( s => s ) ;
250237 const code = await formatCode ( codes . join ( '\n\n' ) , opts . cwd ! ) ;
251-
252- await writeFile (
253- path . join ( getDtsOutputPath ( opts . cwd ! ) , opts . dtsName ! ) ,
254- `// generated by @tomjs/vscode-dev\n${ code } ` ,
255- ) ;
238+ await writeFile ( getDtsOutputPath ( opts ) , `// generated by @tomjs/vscode-dev\n${ code } ` ) ;
256239}
0 commit comments