Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit f667785

Browse files
committed
fix: vscode-dev d.ts
1 parent cfaaefc commit f667785

File tree

3 files changed

+19
-31
lines changed

3 files changed

+19
-31
lines changed

packages/vscode-dev/src/gen.ts

Lines changed: 7 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import fs from 'node:fs';
22
import os from 'node:os';
33
import path from 'node:path';
44
import { cwd } from 'node:process';
5-
import { emptyDirSync, readFile, readJson, readJsonSync, writeFile, writeJson } from '@tomjs/node';
5+
import { readJson, readJsonSync, writeFile, writeJson } from '@tomjs/node';
66
import type { IExtensionManifest } from '@tomjs/vscode-types';
77
import chalk from 'chalk';
88
import chokidar from 'chokidar';
@@ -11,9 +11,8 @@ import { formatCode, getDtsOutputPath, logger } from './utils';
1111

1212
const 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

1817
function 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

5352
export 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 */ `
113110
import '@tomjs/vscode';
114111
115112
declare 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 */ `
227223
declare 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

240235
async 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
}

packages/vscode-dev/src/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ export interface CLIOptions {
2424
*/
2525
dtsDir?: string;
2626
/**
27-
* generate d.ts file name, default is "vscode-i18n.d.ts"
27+
* generate d.ts file name, default is "vscode.d.ts"
2828
*/
2929
dtsName?: string;
3030
/**

packages/vscode-dev/src/utils.ts

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import fs from 'node:fs';
22
import path from 'node:path';
33
import Logger from '@tomjs/logger';
4+
import { mkdirpSync } from '@tomjs/node';
5+
import type { CLIOptions } from './types';
46

57
export const logger = new Logger({ directory: 'vscode-dev/logs' });
68

@@ -44,12 +46,7 @@ export const formatCode = async (code: string, cwd: string) => {
4446
return code.replace(/^\s+/gm, '').replace(/\n/g, '');
4547
};
4648

47-
/**
48-
* Get dts output path
49-
* @param cwd
50-
* @returns
51-
*/
52-
export function getDtsOutputPath(cwd: string) {
49+
function getDtsDir(cwd: string) {
5350
const folders = ['types', 'extension', 'src'];
5451
for (const folder of folders) {
5552
const dir = path.join(cwd, folder);
@@ -59,3 +56,11 @@ export function getDtsOutputPath(cwd: string) {
5956
}
6057
return cwd;
6158
}
59+
60+
export function getDtsOutputPath(opts: CLIOptions) {
61+
const filePath = opts.dtsDir ? path.join(opts.cwd!, opts.dtsDir) : getDtsDir(opts.cwd!);
62+
if (!fs.existsSync(filePath)) {
63+
mkdirpSync(filePath);
64+
}
65+
return path.join(filePath, opts.dtsName || 'vscode.d.ts');
66+
}

0 commit comments

Comments
 (0)