Skip to content

Commit

Permalink
create '.vscode' directory before creating 'tasks.json' (#6065)
Browse files Browse the repository at this point in the history
* getRawTasksJson modify

* add lib

* create folders while creating a file

* fix linter errors

* remove details from this version

* getRawTasksJson modified

* revert getRawTasksJson
  • Loading branch information
elahehrashedi authored Sep 1, 2020
1 parent ca777dd commit e8bc0e8
Showing 1 changed file with 47 additions and 29 deletions.
76 changes: 47 additions & 29 deletions Extension/src/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,58 +62,64 @@ export function getRawPackageJson(): any {
return rawPackageJson;
}

export function getRawTasksJson(): Promise<any> {
return new Promise<any>((resolve, reject) => {
const path: string | undefined = getTasksJsonPath();
if (!path) {
return resolve({});
}
fs.exists(path, exists => {
if (!exists) {
return resolve({});
}
const fileContents: string = fs.readFileSync(path).toString();
let rawTasks: any = {};
try {
rawTasks = jsonc.parse(fileContents);
} catch (error) {
return reject(new Error(failedToParseTasksJson));
}
resolve(rawTasks);
});
});
export async function getRawTasksJson(): Promise<any> {
const path: string | undefined = getTasksJsonPath();
if (!path) {
return {};
}
const fileExists: boolean = await checkFileExists(path);
if (!fileExists) {
return {};
}

const fileContents: string = await readFileText(path);
let rawTasks: any = {};
try {
rawTasks = jsonc.parse(fileContents);
} catch (error) {
throw new Error(failedToParseTasksJson);
}
return rawTasks;
}

export async function ensureBuildTaskExists(taskName: string): Promise<void> {
export async function ensureBuildTaskExists(taskLabel: string): Promise<void> {
const rawTasksJson: any = await getRawTasksJson();

// Ensure that the task exists in the user's task.json. Task will not be found otherwise.
if (!rawTasksJson.tasks) {
rawTasksJson.tasks = new Array();
}
// Find or create the task which should be created based on the selected "debug configuration".
let selectedTask: vscode.Task | undefined = rawTasksJson.tasks.find((task: any) => task.label && task.label === task);
let selectedTask: vscode.Task | undefined = rawTasksJson.tasks.find((task: any) => task.label && task.label === taskLabel);
if (selectedTask) {
return;
}

const buildTasks: vscode.Task[] = await getBuildTasks(false, true);
selectedTask = buildTasks.find(task => task.name === taskName);
selectedTask = buildTasks.find(task => task.name === taskLabel);
console.assert(selectedTask);
if (!selectedTask) {
throw new Error("Failed to get selectedTask in ensureBuildTaskExists()");
}

rawTasksJson.version = "2.0.0";

const selectedTask2: vscode.Task = selectedTask;
if (!rawTasksJson.tasks.find((task: any) => task.label === selectedTask2.definition.label)) {
const task: any = {
...selectedTask2.definition,
problemMatcher: selectedTask2.problemMatchers,
// Modify the current default task
rawTasksJson.tasks.forEach((task: any) => {
if (task.label === selectedTask?.definition.label) {
task.group = { kind: "build", "isDefault": true };
} else if (task.group.kind && task.group.kind === "build" && task.group.isDefault && task.group.isDefault === true) {
task.group = "build";
}
});

if (!rawTasksJson.tasks.find((task: any) => task.label === selectedTask?.definition.label)) {
const newTask: any = {
...selectedTask.definition,
problemMatcher: selectedTask.problemMatchers,
group: { kind: "build", "isDefault": true }
};
rawTasksJson.tasks.push(task);
rawTasksJson.tasks.push(newTask);
}

const settings: OtherSettings = new OtherSettings();
Expand Down Expand Up @@ -632,6 +638,18 @@ export function readFileText(filePath: string, encoding: string = "utf8"): Promi

/** Writes content to a text file */
export function writeFileText(filePath: string, content: string, encoding: string = "utf8"): Promise<void> {
const folders: string[] = filePath.split(path.sep).slice(0, -1);
if (folders.length) {
// create folder path if it doesn't exist
folders.reduce((last, folder) => {
const folderPath: string = last ? last + path.sep + folder : folder;
if (!fs.existsSync(folderPath)) {
fs.mkdirSync(folderPath);
}
return folderPath;
});
}

return new Promise<void>((resolve, reject) => {
fs.writeFile(filePath, content, { encoding }, (err) => {
if (err) {
Expand Down

0 comments on commit e8bc0e8

Please sign in to comment.