1#!/usr/bin/env node 2//!/usr/bin/env python 3// -*- coding: utf-8 -*- 4// Copyright (c) 2024 Huawei Device Co., Ltd. 5// Licensed under the Apache License, Version 2.0 (the "License"); 6// you may not use this file except in compliance with the License. 7// You may obtain a copy of the License at 8// 9// http://www.apache.org/licenses/LICENSE-2.0 10// 11// Unless required by applicable law or agreed to in writing, software 12// distributed under the License is distributed on an "AS IS" BASIS, 13// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14// See the License for the specific language governing permissions and 15// limitations under the License. 16 17// Description 18// 19// This script is invoked by the build system and does not need to be executed directly by the developer. 20// It generates files_to_watch.gni from tsconfig.base.json. The .gni file is used by the build system to trigger a rebuild 21// if any files required to create the release version of stateMgmt.js, including tsconfig.base.json, are modified. 22// This script is automatically triggered when npm run build_release is executed. The build system also runs it automatically when needed. 23// Important: If files_to_watch.gni has changes, you should commit it to git. 24// Do not add comments or extra formatting to tsconfig.base.json, as this script will fail to process it as valid JSON. 25// Avoid adding duplicate entries to tsconfig.base.json. If duplicates are found, this script will issue a warning. 26// When this script runs, it checks if the new output would be the same as the existing one. If they are identical, the .gni file is not updated. 27// If the .gni file is updated, the --fast-rebuild option cannot be used to build the image. 28 29const fs = require('fs'); 30const JSON5 = require('json5'); 31 32function main(argv) { 33 console.log('Generating files_to_watch.gni from tsconfig.base.json'); 34 35 const uniqueLines = new Set(); 36 const duplicates = []; 37 38 // Load JSON5 config 39 let config; 40 try { 41 const fileContent = fs.readFileSync('tsconfig.base.json', 'utf-8'); 42 config = JSON5.parse(fileContent); // JSON5 allows comments and trailing commas 43 } catch (error) { 44 const errorMessage = ` 45 +-----------------------------------------------------------------------------+ 46 | ERROR | 47 | Failed to parse 'tsconfig.base.json | 48 | Ensure the file is valid JSON5 format | 49 +-----------------------------------------------------------------------------+ 50 `; 51 console.error(errorMessage); 52 process.exit(1); 53 } 54 55 // Modify file paths 56 const files = config.files || []; 57 const modifiedFiles = files.map(file => 58 file.replace( 59 'src/', 60 '//foundation/arkui/ace_engine/frameworks/bridge/declarative_frontend/state_mgmt/src/' 61 ) 62 ); 63 64 // Prepare content for files_to_watch.gni. 65 const configFileNameBase = '//foundation/arkui/ace_engine/frameworks/bridge/declarative_frontend/state_mgmt/tsconfig.base.json'; 66 const configFileNameRelease = '//foundation/arkui/ace_engine/frameworks/bridge/declarative_frontend/state_mgmt/tsconfig.release.json'; 67 68 let newContent = `# Copyright (c) 2024 Huawei Device Co., Ltd. 69# Licensed under the Apache License, Version 2.0 (the "License") 70# you may not use this file except in compliance with the License. 71# You may obtain a copy of the License at 72# 73# http://www.apache.org/licenses/LICENSE-2.0 74# 75# Unless required by applicable law or agreed to in writing, software 76# distributed under the License is distributed on an "AS IS" BASIS, 77# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 78# See the License for the specific language governing permissions and 79# limitations under the License. 80 81state_mgmt_release_files_to_watch = [ 82 "${configFileNameBase}", 83 "${configFileNameRelease}", 84`; 85 newContent += modifiedFiles.map(file => ` "${file}",`).join('\n'); 86 newContent += ` 87] 88`; 89 90 // Check if files_to_watch.gni already exists and matches the new content 91 // Its important not to write the file again with the same content. 92 // If file has new timestamp then --fast-rebuild canot be done because .gni gile get updated. 93 try { 94 const currentContent = fs.readFileSync('files_to_watch.gni', 'utf-8'); 95 if (currentContent === newContent) { 96 console.log('No changes detected in files_to_watch.gni. Skipping update.'); 97 return; 98 } 99 } catch (error) { 100 console.log('files_to_watch.gni not found. It will be created.'); 101 } 102 103 // Write to files_to_watch.gni 104 try { 105 fs.writeFileSync('files_to_watch.gni', newContent); 106 console.log('StateMgmt: File list generation done.'); 107 } catch (error) { 108 console.error('Error writing to files_to_watch.gni:'); 109 console.error(error.message); 110 process.exit(1); 111 } 112 113 // Check for duplicates in the input file list. 114 modifiedFiles.forEach(line => { 115 if (uniqueLines.has(line)) { 116 duplicates.push(line); 117 } else { 118 uniqueLines.add(line); 119 } 120 }); 121 122 if (duplicates.length > 0) { 123 console.error('ERROR: Duplicates found in tsconfig.base.json:'); 124 duplicates.forEach(duplicate => console.error(duplicate)); 125 } 126} 127 128main(process.argv); 129