Skip to content

Commit

Permalink
fix windows bug
Browse files Browse the repository at this point in the history
  • Loading branch information
yehuozhili committed Dec 30, 2020
1 parent 5fd9178 commit c6c32cd
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 50 deletions.
52 changes: 27 additions & 25 deletions src/command/openQuestion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,33 @@ import client from "../mock";
import { selectWorkspaceFolder } from "../shared/selectWorkspaceFolder";
import { Uri, ViewColumn, window } from "vscode";
import { ListItem } from "../service";
import { filterInvalidPath } from "../shared";

export async function openQuestion(ele: ListItem): Promise<void> {
const { name, type, content } = ele;
const workspaceFolder: string = await selectWorkspaceFolder();
if (!workspaceFolder) {
return;
}
const codeTemplate = getCodeTemplate(type, content);
const finalPath = await showProblem(
resolve(workspaceFolder, name + "." + type),
codeTemplate || ""
);
await window.showTextDocument(Uri.file(finalPath), {
preview: false,
viewColumn: ViewColumn.One,
});
return;
const { name, type, content } = ele;

const workspaceFolder: string = await selectWorkspaceFolder();
if (!workspaceFolder) {
return;
}
const codeTemplate = getCodeTemplate(type, content);
const finalPath = await showProblem(
resolve(workspaceFolder, filterInvalidPath(name) + "." + type),
codeTemplate || ""
);
await window.showTextDocument(Uri.file(finalPath), {
preview: false,
viewColumn: ViewColumn.One,
});
return;
}

async function showProblem(filePath: string, codeTemplate: string) {
if (!(await fse.pathExists(filePath))) {
await fse.createFile(filePath);
await fse.writeFile(filePath, codeTemplate);
}
return filePath;
if (!(await fse.pathExists(filePath))) {
await fse.createFile(filePath);
await fse.writeFile(filePath, codeTemplate);
}
return filePath;
}

// async function getCodeTemplate(index: number) {
Expand All @@ -42,18 +44,18 @@ async function showProblem(filePath: string, codeTemplate: string) {
// }

function getCodeTemplate(type: "md" | "js", content: string) {
if (type === "md") {
return `# Problem: ${content}
if (type === "md") {
return `# Problem: ${content}
*[interview]: start
*[interview]: end
`;
} else {
return `// Problem: ${content}
} else {
return `// Problem: ${content}
// @interview start
// @interview start
`;
}
}
}
37 changes: 19 additions & 18 deletions src/service/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,40 +2,41 @@ import axios from "axios";
import { Interface } from "readline";
const baseURL = "http://daily.zhufengpeixun.com/api";
const instance = axios.create({
baseURL,
baseURL,
});

instance.interceptors.response.use((response) => {
if (response.status >= 200 && response.status < 300) {
return response.data ? response.data : {};
}
return {};
if (response.status >= 200 && response.status < 300) {
return response.data ? response.data : {};
}
return {};
});

interface IRes<T> {
data: T;
data: T;
}

interface IListQuery {
current?: number;
pageSize?: number;
current?: number;
pageSize?: number;
}

export interface ListItem {
name: string,
id: number,
publish_date: Date | string,
content: string,
type: 'md' | 'js'
name: string;
id: number;
// eslint-disable-next-line @typescript-eslint/naming-convention
publish_date: Date | string;
content: string;
type: "md" | "js";
}

type IListRes = IRes<ListItem[]>;

export const getProblemList = (
params: IListQuery = {
current: 1,
pageSize: 9999,
}
params: IListQuery = {
current: 1,
pageSize: 9999,
}
): Promise<IListRes> => {
return instance.get("/questions", { params });
return instance.get("/questions", { params });
};
19 changes: 12 additions & 7 deletions src/shared/index.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,25 @@
import { QuickPickItem, workspace, WorkspaceConfiguration } from "vscode";

export interface IQuickItemEx<T> extends QuickPickItem {
value: T;
value: T;
}

export function getWorkspaceConfiguration(): WorkspaceConfiguration {
return workspace.getConfiguration("front-end-interview");
return workspace.getConfiguration("front-end-interview");
}

export function getWorkspaceFolder(): string {
return getWorkspaceConfiguration().get<string>("workspaceFolder", "");
return getWorkspaceConfiguration().get<string>("workspaceFolder", "");
}

export enum DescriptionConfiguration {
InWebView = "In Webview",
InFileComment = "In File Comment",
Both = "Both",
None = "None",
InWebView = "In Webview",
InFileComment = "In File Comment",
Both = "Both",
None = "None",
}

export function filterInvalidPath(path: string) {
const reg = new RegExp('[\\\\/:*?"<>|]', "g");
return path.replace(reg, "");
}

0 comments on commit c6c32cd

Please sign in to comment.