Skip to content

Commit

Permalink
Fix: eslint issues on client/src/utils/JSONFile.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
idillon-sfl committed Sep 17, 2024
1 parent 520e858 commit dd1d0b0
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 16 deletions.
19 changes: 12 additions & 7 deletions client/src/driver/BitbakeESDK.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}`)
Expand All @@ -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)
}
30 changes: 21 additions & 9 deletions client/src/utils/JSONFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, unknown>
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<string, unknown>)[key]
}
}
}
Expand Down

0 comments on commit dd1d0b0

Please sign in to comment.