Skip to content

Commit ff46f97

Browse files
authored
- Add another templates json file into the package tables when any json files within it are updated. The newly added templates json file is in sync and the older ones will be out of sync in the package table when a nested json file is changed (#1577)
- Removing the zcl extensions check because that is now covered by the newly added code. Not adding further tests for this because it is already covered in zcl-loader.test.js. See: test changing the zcl extension file in a top level templates json file and make sure it is re-loaded again - JIRA: ZAPP-1297
1 parent ef42589 commit ff46f97

2 files changed

Lines changed: 74 additions & 27 deletions

File tree

docs/api.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8099,6 +8099,7 @@ Get endpoint type events from the given endpoint type ID.
80998099
## JS API: generator logic
81008100

81018101
* [JS API: generator logic](#module_JS API_ generator logic)
8102+
* [~findAndReadJsonFiles(obj, basePath)](#module_JS API_ generator logic..findAndReadJsonFiles) ⇒ <code>Promise.&lt;string&gt;</code>
81028103
* [~loadGenTemplateFromFile(templatePath)](#module_JS API_ generator logic..loadGenTemplateFromFile) ⇒
81038104
* [~recordPackageIfNonexistent(db, packagePath, parentId, packageType, version, category, description)](#module_JS API_ generator logic..recordPackageIfNonexistent) ⇒
81048105
* [~loadTemplateOptionsFromJsonFile(db, packageId, category, externalPath)](#module_JS API_ generator logic..loadTemplateOptionsFromJsonFile) ⇒
@@ -8146,6 +8147,19 @@ Get endpoint type events from the given endpoint type ID.
81468147
* [~templatePromise(global, promise)](#module_JS API_ generator logic..templatePromise)
81478148
* [~deprecatedHelper(fn, explanation)](#module_JS API_ generator logic..deprecatedHelper) ⇒
81488149

8150+
<a name="module_JS API_ generator logic..findAndReadJsonFiles"></a>
8151+
8152+
### JS API: generator logic~findAndReadJsonFiles(obj, basePath) ⇒ <code>Promise.&lt;string&gt;</code>
8153+
Finds and reads JSON files referenced in a nested object.
8154+
8155+
**Kind**: inner method of [<code>JS API: generator logic</code>](#module_JS API_ generator logic)
8156+
**Returns**: <code>Promise.&lt;string&gt;</code> - - A promise that resolves to the concatenated content of all JSON files.
8157+
8158+
| Param | Type | Description |
8159+
| --- | --- | --- |
8160+
| obj | <code>Object</code> | The object to search for JSON file references. |
8161+
| basePath | <code>string</code> | The base directory to resolve relative paths. |
8162+
81498163
<a name="module_JS API_ generator logic..loadGenTemplateFromFile"></a>
81508164

81518165
### JS API: generator logic~loadGenTemplateFromFile(templatePath) ⇒
@@ -13437,6 +13451,7 @@ This module contains the API for templating. For more detailed instructions, rea
1343713451
## JS API: generator logic
1343813452

1343913453
* [JS API: generator logic](#module_JS API_ generator logic)
13454+
* [~findAndReadJsonFiles(obj, basePath)](#module_JS API_ generator logic..findAndReadJsonFiles) ⇒ <code>Promise.&lt;string&gt;</code>
1344013455
* [~loadGenTemplateFromFile(templatePath)](#module_JS API_ generator logic..loadGenTemplateFromFile) ⇒
1344113456
* [~recordPackageIfNonexistent(db, packagePath, parentId, packageType, version, category, description)](#module_JS API_ generator logic..recordPackageIfNonexistent) ⇒
1344213457
* [~loadTemplateOptionsFromJsonFile(db, packageId, category, externalPath)](#module_JS API_ generator logic..loadTemplateOptionsFromJsonFile) ⇒
@@ -13484,6 +13499,19 @@ This module contains the API for templating. For more detailed instructions, rea
1348413499
* [~templatePromise(global, promise)](#module_JS API_ generator logic..templatePromise)
1348513500
* [~deprecatedHelper(fn, explanation)](#module_JS API_ generator logic..deprecatedHelper) ⇒
1348613501

13502+
<a name="module_JS API_ generator logic..findAndReadJsonFiles"></a>
13503+
13504+
### JS API: generator logic~findAndReadJsonFiles(obj, basePath) ⇒ <code>Promise.&lt;string&gt;</code>
13505+
Finds and reads JSON files referenced in a nested object.
13506+
13507+
**Kind**: inner method of [<code>JS API: generator logic</code>](#module_JS API_ generator logic)
13508+
**Returns**: <code>Promise.&lt;string&gt;</code> - - A promise that resolves to the concatenated content of all JSON files.
13509+
13510+
| Param | Type | Description |
13511+
| --- | --- | --- |
13512+
| obj | <code>Object</code> | The object to search for JSON file references. |
13513+
| basePath | <code>string</code> | The base directory to resolve relative paths. |
13514+
1348713515
<a name="module_JS API_ generator logic..loadGenTemplateFromFile"></a>
1348813516

1348913517
### JS API: generator logic~loadGenTemplateFromFile(templatePath) ⇒
@@ -14099,6 +14127,7 @@ Function wrapper that can be used when a helper is deprecated.
1409914127
## JS API: generator logic
1410014128

1410114129
* [JS API: generator logic](#module_JS API_ generator logic)
14130+
* [~findAndReadJsonFiles(obj, basePath)](#module_JS API_ generator logic..findAndReadJsonFiles) ⇒ <code>Promise.&lt;string&gt;</code>
1410214131
* [~loadGenTemplateFromFile(templatePath)](#module_JS API_ generator logic..loadGenTemplateFromFile) ⇒
1410314132
* [~recordPackageIfNonexistent(db, packagePath, parentId, packageType, version, category, description)](#module_JS API_ generator logic..recordPackageIfNonexistent) ⇒
1410414133
* [~loadTemplateOptionsFromJsonFile(db, packageId, category, externalPath)](#module_JS API_ generator logic..loadTemplateOptionsFromJsonFile) ⇒
@@ -14146,6 +14175,19 @@ Function wrapper that can be used when a helper is deprecated.
1414614175
* [~templatePromise(global, promise)](#module_JS API_ generator logic..templatePromise)
1414714176
* [~deprecatedHelper(fn, explanation)](#module_JS API_ generator logic..deprecatedHelper) ⇒
1414814177

14178+
<a name="module_JS API_ generator logic..findAndReadJsonFiles"></a>
14179+
14180+
### JS API: generator logic~findAndReadJsonFiles(obj, basePath) ⇒ <code>Promise.&lt;string&gt;</code>
14181+
Finds and reads JSON files referenced in a nested object.
14182+
14183+
**Kind**: inner method of [<code>JS API: generator logic</code>](#module_JS API_ generator logic)
14184+
**Returns**: <code>Promise.&lt;string&gt;</code> - - A promise that resolves to the concatenated content of all JSON files.
14185+
14186+
| Param | Type | Description |
14187+
| --- | --- | --- |
14188+
| obj | <code>Object</code> | The object to search for JSON file references. |
14189+
| basePath | <code>string</code> | The base directory to resolve relative paths. |
14190+
1414914191
<a name="module_JS API_ generator logic..loadGenTemplateFromFile"></a>
1415014192

1415114193
### JS API: generator logic~loadGenTemplateFromFile(templatePath) ⇒

src-electron/generator/generation-engine.js

Lines changed: 32 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,30 @@ const dbApi = require('../db/db-api.js')
3333
const dbCache = require('../db/db-cache.js')
3434
const queryNotification = require('../db/query-package-notification.js')
3535

36+
/**
37+
* Finds and reads JSON files referenced in a nested object.
38+
*
39+
* @param {Object} obj - The object to search for JSON file references.
40+
* @param {string} basePath - The base directory to resolve relative paths.
41+
* @returns {Promise<string>} - A promise that resolves to the concatenated content of all JSON files.
42+
*/
43+
async function findAndReadJsonFiles(obj, basePath) {
44+
let additionalJsonContent = ''
45+
for (const key in obj) {
46+
if (typeof obj[key] === 'string' && obj[key].endsWith('.json')) {
47+
// If the value is a JSON file reference, read its content
48+
let jsonFilePath = path.resolve(path.join(basePath, obj[key]))
49+
if (fs.existsSync(jsonFilePath)) {
50+
additionalJsonContent += await fsPromise.readFile(jsonFilePath, 'utf8')
51+
}
52+
} else if (typeof obj[key] === 'object' && obj[key] !== null) {
53+
// If the value is an object, recursively search for JSON file references
54+
additionalJsonContent += await findAndReadJsonFiles(obj[key], basePath)
55+
}
56+
}
57+
return additionalJsonContent
58+
}
59+
3660
/**
3761
* Given a path, it will read generation template object into memory.
3862
*
@@ -42,34 +66,15 @@ const queryNotification = require('../db/query-package-notification.js')
4266
async function loadGenTemplateFromFile(templatePath) {
4367
let ret = {}
4468
ret.data = await fsPromise.readFile(templatePath, 'utf8')
45-
ret.crc = util.checksum(ret.data)
4669
ret.templateData = JSON.parse(ret.data)
47-
let zclExtension = ret.templateData.zcl
48-
let zclExtensionFileContent = ''
49-
// Adding zcl extension files to the template json crc
50-
if (zclExtension && typeof zclExtension === 'object') {
51-
for (const key of Object.keys(zclExtension)) {
52-
let extension = zclExtension[key]
53-
for (const key2 of Object.keys(extension)) {
54-
let defaultExtensionValue = extension[key2].defaults
55-
if (
56-
typeof defaultExtensionValue === 'string' ||
57-
defaultExtensionValue instanceof String
58-
) {
59-
// Data is a string, so we will treat it as a relative path to the JSON file.
60-
let externalPath = path.resolve(
61-
path.join(path.dirname(templatePath), defaultExtensionValue)
62-
)
63-
zclExtensionFileContent += await fsPromise.readFile(
64-
externalPath,
65-
'utf8'
66-
)
67-
}
68-
}
69-
}
70-
ret.crc = util.checksum(ret.data + zclExtensionFileContent)
71-
}
72-
70+
// Find the json files within the gen template json files and add them to the
71+
// crc as well.
72+
let allJsonContentInTemplatePath = await findAndReadJsonFiles(
73+
ret.templateData,
74+
path.dirname(templatePath)
75+
)
76+
let checksumData = ret.data + allJsonContentInTemplatePath
77+
ret.crc = util.checksum(checksumData)
7378
let requiredFeatureLevel = 0
7479
if ('requiredFeatureLevel' in ret.templateData) {
7580
requiredFeatureLevel = ret.templateData.requiredFeatureLevel

0 commit comments

Comments
 (0)