From ef2c00ede1b2c1cff94195fec4c913770a7e2a70 Mon Sep 17 00:00:00 2001 From: underdorff18 Date: Sun, 20 Nov 2022 18:51:49 -0500 Subject: [PATCH] added error checking for watcher and file reader --- JS/1234.xlsx | Bin 5187 -> 5209 bytes JS/excelreader.js | 59 +++++++++++++++++++++++++++++++++++++++------- 2 files changed, 50 insertions(+), 9 deletions(-) diff --git a/JS/1234.xlsx b/JS/1234.xlsx index 41708a425797a2fb2b390cbfeb993953cfeb1f25..60e483ccfb5265e760b75bb14ccaa9bdee5f6c13 100644 GIT binary patch delta 2980 zcmZ8jc{mi>8y>QZeawt4Yql}Atc~TW$(CJ%35hJpGMb2FNs~3knjx};#*z>fLWq%_ z?E92muD!Aw`lar3Z~eZ%-gC}-p7TA=d%o}enk2>~ST7o$pn(E_Kp;S|#s~e9m4k}r z7`%p2$x;7A1e+6rIF#vx9O&L;X&G~TXl|ZNnb>*eVYkze5Mg7-Ah{@lm5T4Rx4j$7 zF0J}$<#M^$etZ(_pq_kZXa259hM!AkOvnXk(dn)Sp;}zjU77;yk9IeF&uFLwBPHJp zYpXV$l`ac&X{0xO9x{^-b)xm55f4h0Ovc^fJi>u4{u4dJ0tW=N)@soFvLOZzzHxPQJ*io5zJ}weX!)LkP zFGZnrT^$;8a8x8HLm|f5%^A$=9({i;uJ88FGj9X%b^bZwkyR*TqSWj@8wSmK9SzLO zw3A0p&FjIXM=f}>(|Yh%N;L*DF-=MLZu-HfIYOtabR_6Acb-J+OhA zg0&_F@rFT)r7k^o+udCRP1oUl*{GV~>3eF+hLrxi6Um0@)5@(N8BNus-gIz4!w^eUSW$UhUZDDQ6=hJ!b z-Y5obeVj~L?S7?-rmw7*O2=k2a58ow-m>9Raar(kboBvk4qgtUnjXgV#QE8_V2pYg z(UM{6Lnb0u|ACVK!*USy>oQlBR{jr*rWqzL2(0V7VxKrTbL>JRZjI-Ro}(*rba&b! zNbj;-G^7XakEB66sQ~~t2mtVB4FG-%?jP9C<21pYjtggeb3z4Fw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N@Rbb_)qM1neJh7HzP&7Lu5Z9wyfkdT-=IZO>2*CRR&_Hd}Y#(* z4v^6|yt;V(Y9z|(t*8SPO-TbS4&o!jd3ReD+Cgf)zMXUZ$c;-Pus4;-d^S4jn2V$F z_5nk7maEcHP7V!hN~v7_vghFXc)!f>;Jr=wk8anK5piRtf_@CnP0BRRsRz*6I@`Kf zG)T`j?`HS}P9%;wWj0|@z_k3+F80pvQC*VIqj^Zh+z5O! z+UZiGia^Asjba47j{AK1h~Z-b(!{*sYWTAycg#?!7dQs*`?4acJ;rx){AMWArs}(% zHSI`};EQ$?q(v6c;;oFyc#^g*m6_L|Nj>x2n_WXQ+FUb<)ljPQ`EU2x-YMl+PZZ@@ zE>T5trL<1zrvn!vIVK~&&4NA$eO-CwoGX>wua;DZeN;+8TmFXSdy~_qK6A#|0sHWI z;?Xo|Zjh5ZYuJaqH&XM7#PX$4rjAZj?VWu7t>Hs9dO1fLb@wZ_KtIdi#Bevj9+YA1 zfP;VMUzP*KX~}!^AMF{;m;TSR2tZE#EkZ3I8r(m1>Hi6wM1m;8f1dn4bs~V@&4e@s zLH?Z%hKd54NHS2lUn##ggTU_@fC-WhRQea@_m}ej3zI}Dhf4m!Xv&in77k*e2S4Kb l7eya2umb=GqT@Hikdy>uK+Ip4{|{&vTiyTw diff --git a/JS/excelreader.js b/JS/excelreader.js index a64c251..a99896f 100644 --- a/JS/excelreader.js +++ b/JS/excelreader.js @@ -10,18 +10,40 @@ const { all } = require('express/lib/application'); //const myfilepath = Path.join(__dirname, 'Inventory', 'samplepc1234.xlsx'); function readInventoryFile(filepath) { + //Checking for correct file type + const fileinfo = Path.parse(filepath); + if (fileinfo.ext !== ".xlsx") { + return {error: "Given file is not xlsx format"} + } + let workbook = xlsx.readFileSync(filepath); let sheetNames = workbook.SheetNames; let sheetData = workbook.Sheets[sheetNames[0]]; + let system; + //Here is where we use known cell locations of the specs needed to build the systems file - let serialnum = sheetData.D1.v; - let model = sheetData.D2.v; - let OS = sheetData.D3.v; - let price = sheetData.D4.v; - let specs = [sheetData.D5.v, sheetData.D6.v, sheetData.D7.v, sheetData.D8.v]; + try { + system = new System( + sheetData.D1.v, + sheetData.D2.v, + sheetData.D3.v, + sheetData.D4.v, + [sheetData.D5.v, sheetData.D6.v, sheetData.D7.v, sheetData.D8.v]); + } + catch (error) { + console.error(error); + } + + //Checking if file is formatted correctly + if (!system) { + return {error: "One or more necessary values are not defined"} + } + if (parseInt(fileinfo.name) !== system.serialnum) { + return {error: "filename does not match serial number"}; + } - return new System(serialnum, model, OS, price, specs); + return system; } @@ -29,18 +51,37 @@ var inventoryWatcher = chokidar.watch(Path.join(__dirname, "Inventory"), { ignored: /(^|[\/\\])\../, persistent: true, awaitWriteFinish: true, + ignoreInitial: true }); console.log(`Watching directory for changes...` ) inventoryWatcher .on('add', function(filepath) { - dbtools.insertSystem(readInventoryFile(filepath)); + const system = readInventoryFile(filepath); + if (system.error) { + console.error(system.error); + } + else { + dbtools.insertSystem(system); + } + }) .on('change', function(filepath) { - dbtools.updateSystem(readInventoryFile(filepath)); + const system = readInventoryFile(filepath); + if (system.error) { + console.error(system.error); + } + else { + dbtools.updateSystem(system); + } }) .on('unlink', function(filepath) { - dbtools.deleteSystem(parseInt(Path.basename(filepath, '.xlsx'))); + if (Path.extname(filepath) !== ".xlsx") { + console.log("inventoryWatcher: a non xlsx file has been deleted"); + } + else { + dbtools.deleteSystem(parseInt(Path.basename(filepath, '.xlsx'))); + } }) .on('error', function(error) { console.error('Error happened', error);