@@ -15,10 +15,31 @@ Object.defineProperty(exports, "__esModule", { value: true });
1515exports . updateComponentSummaryApi = exports . writeComponentMetadataApi = exports . writeComponentApi = exports . getAPIPath = void 0 ;
1616const fs_extra_1 = __importDefault ( require ( "fs-extra" ) ) ;
1717const path_1 = __importDefault ( require ( "path" ) ) ;
18- function updateObject ( target , source ) {
19- return Object . entries ( source ) . reduce ( ( acc , [ key , value ] ) => {
20- if ( value !== undefined && value !== null && value !== '' ) {
21- acc [ key ] = value ;
18+ /**
19+ * Merges values from a source object into a target object, returning a new object.
20+ * For each key present in either object:
21+ * - If the key is listed in preserveKeys and the source value is undefined, null, or an empty string,
22+ * the target's value is preserved.
23+ * - Otherwise, the value from the source is used (even if undefined, null, or empty string).
24+ * This is useful for partial updates where some properties should not be overwritten unless explicitly set.
25+ *
26+ * @param target - The original object to merge into
27+ * @param source - The object containing new values
28+ * @param preserveKeys - Keys for which the target's value should be preserved if the source value is undefined, null, or empty string
29+ * @returns A new object with merged values
30+ */
31+ function updateObject ( target , source , preserveKeys = [ ] ) {
32+ // Collect all unique keys from both target and source
33+ const allKeys = Array . from ( new Set ( [ ...Object . keys ( target ) , ...Object . keys ( source ) ] ) ) ;
34+ return allKeys . reduce ( ( acc , key ) => {
35+ const sourceValue = source [ key ] ;
36+ const targetValue = target [ key ] ;
37+ // Preserve existing values for specified keys when source value is undefined
38+ if ( preserveKeys . includes ( key ) && ( sourceValue === undefined || sourceValue === null || sourceValue === '' ) ) {
39+ acc [ key ] = targetValue ;
40+ }
41+ else {
42+ acc [ key ] = sourceValue ;
2243 }
2344 return acc ;
2445 } , Object . assign ( { } , target ) ) ;
@@ -42,22 +63,23 @@ const writeComponentSummaryAPI = (handoff, componentData) => __awaiter(void 0, v
4263 componentData . sort ( ( a , b ) => a . title . localeCompare ( b . title ) ) ;
4364 yield fs_extra_1 . default . writeFile ( path_1 . default . resolve ( ( 0 , exports . getAPIPath ) ( handoff ) , 'components.json' ) , JSON . stringify ( componentData , null , 2 ) ) ;
4465} ) ;
45- const writeComponentApi = ( id_1 , component_1 , version_1 , handoff_1 , ...args_1 ) => __awaiter ( void 0 , [ id_1 , component_1 , version_1 , handoff_1 , ...args_1 ] , void 0 , function * ( id , component , version , handoff , isPartialUpdate = false ) {
66+ const writeComponentApi = ( id_1 , component_1 , version_1 , handoff_1 , ...args_1 ) => __awaiter ( void 0 , [ id_1 , component_1 , version_1 , handoff_1 , ...args_1 ] , void 0 , function * ( id , component , version , handoff , preserveKeys = [ ] ) {
4667 const outputDirPath = path_1 . default . resolve ( ( 0 , exports . getAPIPath ) ( handoff ) , 'component' , id ) ;
47- if ( isPartialUpdate ) {
48- const outputFilePath = path_1 . default . resolve ( outputDirPath , `${ version } .json` ) ;
49- if ( fs_extra_1 . default . existsSync ( outputFilePath ) ) {
50- const existingJson = yield fs_extra_1 . default . readFile ( outputFilePath , 'utf8' ) ;
51- if ( existingJson ) {
52- try {
53- const existingData = JSON . parse ( existingJson ) ;
54- const mergedData = updateObject ( existingData , component ) ;
55- yield fs_extra_1 . default . writeFile ( path_1 . default . resolve ( outputDirPath , `${ version } .json` ) , JSON . stringify ( mergedData , null , 2 ) ) ;
56- return ;
57- }
58- catch ( _ ) {
59- // Unable to parse existing file
60- }
68+ const outputFilePath = path_1 . default . resolve ( outputDirPath , `${ version } .json` ) ;
69+ if ( fs_extra_1 . default . existsSync ( outputFilePath ) ) {
70+ const existingJson = yield fs_extra_1 . default . readFile ( outputFilePath , 'utf8' ) ;
71+ if ( existingJson ) {
72+ try {
73+ const existingData = JSON . parse ( existingJson ) ;
74+ // Special case: always allow page to be cleared when undefined
75+ // This handles the case where page slices are removed
76+ const finalPreserveKeys = component . page === undefined ? preserveKeys . filter ( ( key ) => key !== 'page' ) : preserveKeys ;
77+ const mergedData = updateObject ( existingData , component , finalPreserveKeys ) ;
78+ yield fs_extra_1 . default . writeFile ( path_1 . default . resolve ( outputDirPath , `${ version } .json` ) , JSON . stringify ( mergedData , null , 2 ) ) ;
79+ return ;
80+ }
81+ catch ( _ ) {
82+ // Unable to parse existing file
6183 }
6284 }
6385 }
0 commit comments