生成配置文件
src/commands.ts源代码
ts
import { window } from "vscode";
import { TemplateService } from "./TemplateService";
export type createConfigFileFunction = () => Promise<void>;
export const createConfigFile =
(templateService: TemplateService): createConfigFileFunction =>
async () => {
const folderResult = await window.showOpenDialog({
canSelectFiles: false,
canSelectFolders: true,
canSelectMany: false,
});
if (folderResult && folderResult.length === 1) {
const folderUri = folderResult[0];
await templateService.writeConfigFile(folderUri);
}
};
import { window } from "vscode";
import { TemplateService } from "./TemplateService";
export type createConfigFileFunction = () => Promise<void>;
export const createConfigFile =
(templateService: TemplateService): createConfigFileFunction =>
async () => {
const folderResult = await window.showOpenDialog({
canSelectFiles: false,
canSelectFolders: true,
canSelectMany: false,
});
if (folderResult && folderResult.length === 1) {
const folderUri = folderResult[0];
await templateService.writeConfigFile(folderUri);
}
};
创建弹窗
js
const folderResult = await window.showOpenDialog({
// 不能选择文件
canSelectFiles: false,
// 可以选择文件夹
canSelectFolders: true,
// 不能选择多个
canSelectMany: false,
});
// 选择了一个文件夹
if (folderResult && folderResult.length === 1) {
// 返回结果是一个数组,取第一个的路径
const folderUri = folderResult[0];
// 创建配置文件
await templateService.writeConfigFile(folderUri);
}
const folderResult = await window.showOpenDialog({
// 不能选择文件
canSelectFiles: false,
// 可以选择文件夹
canSelectFolders: true,
// 不能选择多个
canSelectMany: false,
});
// 选择了一个文件夹
if (folderResult && folderResult.length === 1) {
// 返回结果是一个数组,取第一个的路径
const folderUri = folderResult[0];
// 创建配置文件
await templateService.writeConfigFile(folderUri);
}
src/TemplateService.ts源代码
ts
import { TextEncoder } from "util";
import { Uri, workspace } from "vscode";
import { LoggingService } from "./LoggingService";
import { PrettierModule, PrettierOptions } from "./types";
export class TemplateService {
constructor(
private loggingService: LoggingService,
private prettierModule: PrettierModule
) {}
public async writeConfigFile(folderPath: Uri) {
const settings = { tabWidth: 2, useTabs: false };
// 拼接完整路径
const outputPath = Uri.joinPath(folderPath, ".prettierrc");
const formatterOptions: PrettierOptions = {
/* cspell: disable-next-line */
// filepath参数用于prettier解析分析,自动判断
filepath: outputPath.scheme === "file" ? outputPath.fsPath : undefined,
tabWidth: settings.tabWidth,
useTabs: settings.useTabs,
};
//
const templateSource = this.prettierModule.format(
// JSON.stringify
// 第二个参数 字符串和数字的数组,用作选择将要字符串化的对象属性的批准列表
// 第三个参数 在返回值JSON文本中添加缩进、空格和换行符,以便于阅读
JSON.stringify(settings, null, 2),
formatterOptions
);
this.loggingService.logInfo(`Writing .prettierrc to '${outputPath}'`);
// 写入文件
await workspace.fs.writeFile(
outputPath,
new TextEncoder().encode(templateSource)
);
}
}
import { TextEncoder } from "util";
import { Uri, workspace } from "vscode";
import { LoggingService } from "./LoggingService";
import { PrettierModule, PrettierOptions } from "./types";
export class TemplateService {
constructor(
private loggingService: LoggingService,
private prettierModule: PrettierModule
) {}
public async writeConfigFile(folderPath: Uri) {
const settings = { tabWidth: 2, useTabs: false };
// 拼接完整路径
const outputPath = Uri.joinPath(folderPath, ".prettierrc");
const formatterOptions: PrettierOptions = {
/* cspell: disable-next-line */
// filepath参数用于prettier解析分析,自动判断
filepath: outputPath.scheme === "file" ? outputPath.fsPath : undefined,
tabWidth: settings.tabWidth,
useTabs: settings.useTabs,
};
//
const templateSource = this.prettierModule.format(
// JSON.stringify
// 第二个参数 字符串和数字的数组,用作选择将要字符串化的对象属性的批准列表
// 第三个参数 在返回值JSON文本中添加缩进、空格和换行符,以便于阅读
JSON.stringify(settings, null, 2),
formatterOptions
);
this.loggingService.logInfo(`Writing .prettierrc to '${outputPath}'`);
// 写入文件
await workspace.fs.writeFile(
outputPath,
new TextEncoder().encode(templateSource)
);
}
}