diff --git a/client/src/driver/BitbakeESDK.ts b/client/src/driver/BitbakeESDK.ts index 0a4450c59..71e070259 100644 --- a/client/src/driver/BitbakeESDK.ts +++ b/client/src/driver/BitbakeESDK.ts @@ -76,11 +76,13 @@ export function generateTasksDefinitions (workspace: DevtoolWorkspaceInfo, bitba dependsOn: [`BitBake Build ${recipeName}`] }) } - const tasks = vscodeTasks.tasks - if (tasks === undefined) { - vscodeTasks.tasks = newTasks - } else { - mergeJsonArray(tasks, newTasks) + if (typeof vscodeTasks === 'object' && vscodeTasks !== null && 'tasks' in vscodeTasks) { + const tasks = vscodeTasks.tasks + if (tasks === undefined || tasks === null) { + vscodeTasks.tasks = newTasks + } else if (Array.isArray(tasks)) { + mergeJsonArray(tasks, newTasks) + } } saveJsonFile(vscodeTasksPath, vscodeTasks) logger.info(`Generated ${vscodeTasksPath} for ${recipeName}`) @@ -102,11 +104,14 @@ export async function generateCPPProperties (workspace: DevtoolWorkspaceInfo, bi compilerArgs: CXX?.split(/\s+/).slice(1).concat(CXXFLAGS?.split(/\s+/) ?? []) } - const configurations = vscodeCppProperties.configurations + if (typeof vscodeCppProperties === 'object' && vscodeCppProperties !== null && 'configurations' in vscodeCppProperties) { + const configurations = vscodeCppProperties.configurations if (configurations === undefined) { vscodeCppProperties.configurations = [configuration] - } else { + } else if(Array.isArray(configurations)) { mergeJsonArray(configurations, [configuration]) } + } + saveJsonFile(vscodeCppPropertiesPath, vscodeCppProperties) } diff --git a/client/src/utils/JSONFile.ts b/client/src/utils/JSONFile.ts index 34c1f3306..e1cf0b069 100644 --- a/client/src/utils/JSONFile.ts +++ b/client/src/utils/JSONFile.ts @@ -4,44 +4,56 @@ * ------------------------------------------------------------------------------------------ */ import fs from 'fs' +import { logger } from '../lib/src/utils/OutputLogger' -export function loadJsonFile (path: string): any { +export function loadJsonFile (path: string): unknown { try { if (!fs.existsSync(path)) { return {} } return JSON.parse(fs.readFileSync(path, 'utf-8')) - } catch (e: any) { + } catch (e) { throw new Error(`Error while reading JSON file ${path}: ${e}`) } } -export function saveJsonFile (path: string, json: any): void { +export function saveJsonFile (path: string, json: unknown): void { fs.writeFileSync(path, JSON.stringify(json, null, 2), 'utf-8') } -export function setJsonProperty (json: any, property: string, value: any): any { - if (value === undefined) { +export function setJsonProperty (json: unknown, property: string, value: unknown): unknown { + if (typeof json !== 'object' || json === null) { + logger.warn('[setJsonProperty] json is not an object') + return + } + const typedJson = json as Record + if (value === undefined && property in json) { // We want to make sure undefined workspace properties are kept as undefined // Warning: this will not work if the property is not a string - json[property] = '' + typedJson[property] = '' return json } - json[property] = value + typedJson[property] = value return json } /// Merge tasks with the same label -export function mergeJsonArray (tasks: any, newTasks: any): any { +export function mergeJsonArray (tasks: unknown[], newTasks: unknown[]): unknown[] { for (const newTask of newTasks) { + if (typeof newTask !== 'object' || newTask === null || !('label' in newTask)) { + continue + } let found = false for (const task of tasks) { + if (typeof task !== 'object' || task === null || !('label' in task)) { + continue + } if (task.label === newTask.label) { found = true Object.assign(task, newTask) for (const key in task) { if (!(key in newTask)) { - delete task[key] + delete (task as Record)[key] } } }