@@ -8,6 +8,7 @@ import yargs from 'yargs';
88import { hideBin } from 'yargs/helpers' ;
99import path from 'path' ;
1010
11+ const componentizeVersion = '0.16.0' ;
1112const __filename = new URL ( import . meta. url ) . pathname ;
1213const __dirname = __filename . substring ( 0 , __filename . lastIndexOf ( '/' ) ) ;
1314
@@ -39,16 +40,22 @@ const args = yargs(hideBin(process.argv))
3940
4041const src = args . input ;
4142const outputPath = args . output ;
42- const inputChecksumPath = `${ src } .checksum ` ;
43+ const buildDataPath = `${ src } .buildData.json ` ;
4344
4445// Function to calculate file checksum
4546async function calculateChecksum ( filePath ) {
46- const fileBuffer = await readFile ( filePath ) ;
47- const hash = createHash ( 'sha256' ) ;
48- hash . update ( fileBuffer ) ;
49- return hash . digest ( 'hex' ) ;
47+ try {
48+ const fileBuffer = await readFile ( filePath ) ;
49+ const hash = createHash ( 'sha256' ) ;
50+ hash . update ( fileBuffer ) ;
51+ return hash . digest ( 'hex' ) ;
52+ } catch ( error ) {
53+ console . error ( `Error calculating checksum for file ${ filePath } :` , error ) ;
54+ throw error ;
55+ }
5056}
5157
58+
5259// Function to check if a file exists
5360async function fileExists ( filePath ) {
5461 try {
@@ -59,47 +66,67 @@ async function fileExists(filePath) {
5966 }
6067}
6168
62- async function getExistingChecksum ( checksumPath ) {
63- if ( await fileExists ( checksumPath ) ) {
64- return await readFile ( checksumPath , 'utf8' ) ;
69+ async function getExistingBuildData ( buildaDataPath ) {
70+ try {
71+ if ( await fileExists ( buildaDataPath ) ) {
72+ const buildData = await readFile ( buildDataPath , 'utf8' ) ;
73+ return JSON . parse ( buildData ) ;
74+ }
75+ return null ;
76+ } catch ( error ) {
77+ console . error ( `Error reading existing checksum file at ${ buildDataPath } :` , error ) ;
78+ throw error ;
6579 }
66- return null ;
6780}
6881
69- async function saveChecksum ( checksumPath , checksum ) {
70- await writeFile ( checksumPath , checksum ) ;
82+ async function saveBuildData ( buildDataPath , checksum , version ) {
83+ try {
84+ const checksumData = {
85+ version,
86+ checksum
87+ } ;
88+ await writeFile ( buildDataPath , JSON . stringify ( checksumData , null , 2 ) ) ;
89+ } catch ( error ) {
90+ console . error ( `Error saving checksum file at ${ buildDataPath } :` , error ) ;
91+ throw error ;
92+ }
7193}
7294
7395( async ( ) => {
74- const sourceChecksum = await calculateChecksum ( src ) ;
75- const existingChecksum = await getExistingChecksum ( inputChecksumPath ) ;
76-
77- if ( ( existingChecksum === sourceChecksum ) && fileExists ( outputPath ) ) {
78- console . log ( "No changes detected in source file. Skipping componentization." ) ;
79- return ;
80- }
81-
82- const source = await readFile ( src , 'utf8' ) ;
83-
84- // Check if a non-default wit directory is supplied
85- const witPath = args . witPath ? resolve ( args . witPath ) : path . join ( __dirname , 'wit' ) ;
86- if ( args . witPath ) {
87- console . log ( `Using user-provided wit in: ${ witPath } ` ) ;
96+ try {
97+ const sourceChecksum = await calculateChecksum ( src ) ;
98+ const existingBuildData = await getExistingBuildData ( buildDataPath ) ;
99+
100+ if ( existingBuildData ?. version == componentizeVersion && existingBuildData ?. checksum === sourceChecksum && await fileExists ( outputPath ) ) {
101+ console . log ( "No changes detected in source file. Skipping componentization." ) ;
102+ return ;
103+ }
104+
105+ const source = await readFile ( src , 'utf8' ) ;
106+
107+ // Check if a non-default wit directory is supplied
108+ const witPath = args . witPath ? resolve ( args . witPath ) : path . join ( __dirname , 'wit' ) ;
109+ if ( args . witPath ) {
110+ console . log ( `Using user-provided wit in: ${ witPath } ` ) ;
111+ }
112+
113+ const { component } = await componentize ( source , {
114+ sourceName : basename ( src ) ,
115+ witPath,
116+ worldName : args . triggerType ,
117+ disableFeatures : [ ] ,
118+ enableFeatures : [ "http" ] ,
119+ enableAot : args . aot
120+ } ) ;
121+
122+ await writeFile ( outputPath , component ) ;
123+
124+ // Save the checksum of the input file along with the componentize version
125+ await saveBuildData ( buildDataPath , sourceChecksum , componentizeVersion ) ;
126+
127+ console . log ( "Component successfully written." ) ;
128+ } catch ( error ) {
129+ console . error ( "An error occurred during the componentization process:" , error ) ;
130+ process . exit ( 1 ) ;
88131 }
89-
90- const { component } = await componentize ( source , {
91- sourceName : basename ( src ) ,
92- witPath,
93- worldName : args . triggerType ,
94- disableFeatures : [ ] ,
95- enableFeatures : [ "http" ] ,
96- enableAot : args . aot
97- } ) ;
98-
99- await writeFile ( outputPath , component ) ;
100-
101- // Save the checksum of the input file
102- await saveChecksum ( inputChecksumPath , sourceChecksum ) ;
103-
104- console . log ( "Component successfully written." ) ;
105- } ) ( ) ;
132+ } ) ( ) ;
0 commit comments