From c5521d15862357d528d1354ecf166e9b31b2128a Mon Sep 17 00:00:00 2001 From: Marko Vejnovic Date: Thu, 30 Mar 2023 23:42:33 -0500 Subject: [PATCH 1/6] Attempted to implement manifest.json --- handleCode.js | 183 ++++++++++++++++++++++++++++---------------------- 1 file changed, 102 insertions(+), 81 deletions(-) diff --git a/handleCode.js b/handleCode.js index 0a39c24..1e1ee47 100644 --- a/handleCode.js +++ b/handleCode.js @@ -73,39 +73,36 @@ function handleFile(file, type) { } } -function handleZip(file) { - var zip = new JSZip(); - zip.loadAsync(file) - .then(function (zip) { - $('.zip-select span').text(file.name) - if (!zip.files[`widget.ini`]) { - handleUnsupported(zip) - return - } - zip.files[`widget.ini`].async('string') - .then((data) => { - pathArray = handleIniData(data); - if (pathArray.length != 5) { - handleUnsupported(zip) - return - } - - if (!zip.files[`${pathArray[0]}`] || - !zip.files[`${pathArray[1]}`] || - !zip.files[`${pathArray[2]}`] || - !zip.files[`${pathArray[3]}`] || - !zip.files[`${pathArray[4]}`]) { - alert('This widget was not set up properly. Opening manual upload dialog...') - handleUnsupported(zip) - return - } - readFiles(zip, pathArray); - }) - }); +function loadZipAndPopulateSeInputFields(file) { + const zip = new JSZip(); + zip.loadAsync(file).then((zip) => { + $('.zip-select span').text(file.name); + + if (zip.files['widget.ini']) { + zip.files[`widget.ini`].async('string').then((data) => { + try { + readFiles(zip, parseIniManifest(data)); + } catch (err) { + alert('This widget was not set up properly. Opening manual upload dialog...') + handleUnsupported(zip); + } + }); + } else if (zip.files['manifest.json']) { + zip.files['manifest.json'].async('string').then((data) => { + try { + readFiles(zip, parseJsonManifest(data)); + } catch (err) { + alert('This widget was not set up properly. Opening manual upload dialog...') + handleUnsupported(zip); + } + }); + } else { + handleUnsupported(zip); + } + }); } function handleUnsupported(zip) { - // alert("This widget is not supported.") resetSession() waitForElm('#sigma-create-unsupported').then(() => { @@ -145,64 +142,88 @@ function handleUnsupported(zip) { }) } -function handleIniData(data) { - var lines = data.split('\n'); - var pathArray = []; - for (var i = 0; i < lines.length; i++) { - if (lines[i].includes('[HTML]')) { - var _temp = lines[i + 1].split('"'); - pathArray.push(_temp[1]); - } - - if (lines[i].includes('[CSS]')) { - var _temp = lines[i + 1].split('"'); - pathArray.push(_temp[1]); - } +/** + * This function returns the ini-based manifest of a widget. This object will + * appear as: { + * html: 'example.html', + * css: 'example.css', + * js: 'example.js', + * fields: 'example.json', + * data: 'example-data.json', + * } + * + * If a failure case has occurred, this plugin will throw an `Error`. + */ +function parseIniManifest(data) { + const iniKeyOutKeyMap = { + '[HTML]': 'html', + '[CSS]': 'css', + '[JS]': 'js', + '[FIELDS]': 'fields', + '[DATA]': 'data' + }; + + const outputFile = { + html: undefined, + css: undefined, + js: undefined, + fields: undefined, + data: undefined, + }; + + const lines = data.split('\n'); + for (let i = 0; i < lines.length; i += 1) { + Object.keys(iniKeyOutKeyMap).forEach((k) => { + if (iniKeyOutKeyMap[k] !== undefined) { + throw new Error('Malformed ini file.'); + } + if (lines[i].includes(k)) { + outputFile[iniKeyOutKeyMap[k]] = lines[i + 1].split('"'); + } + }); + } - if (lines[i].includes('[JS]')) { - var _temp = lines[i + 1].split('"'); - pathArray.push(_temp[1]); + // At this point, we should have populated every field. + Object.keys(iniKeyOutKeyMap).forEach((k) => { + if (iniKeyOutKeyMap[k] === undefined) { + throw new Error('Malformed ini file.'); } + }); - if (lines[i].includes('[FIELDS]')) { - var _temp = lines[i + 1].split('"'); - pathArray.push(_temp[1]); - } + return outputFile; +} - if (lines[i].includes('[DATA]')) { - var _temp = lines[i + 1].split('"'); - pathArray.push(_temp[1]); - } +function parseJsonManifest(data) { + const files = JSON.parse(data).fileMap; + if (!files) { + throw new Error('Malformed json file'); } - return pathArray; + return files; } -function readFiles(zip, pathArray) { - var htmlCode, cssCode, jsCode, fieldsCode, dataCode; - zip.files[`${pathArray[0]}`].async('string') - .then((data) => { - htmlCode = data; - zip.files[`${pathArray[1]}`].async('string') - .then((data) => { - cssCode = data; - zip.files[`${pathArray[2]}`].async('string') - .then((data) => { - jsCode = data; - zip.files[`${pathArray[3]}`].async('string') - .then((data) => { - fieldsCode = data; - zip.files[`${pathArray[4]}`].async('string') - .then((data) => { - dataCode = data; - code = [htmlCode, cssCode, jsCode, fieldsCode, dataCode] - window.postMessage([code, 'zip']) - return - }); - }); - }); - }); +function readFiles(zip, pathObj) { + const sourceCode = { + html: '', + css: '', + js: '', + fields: '', + data: '', + }; + let fieldsLeft = Object.keys(sourceCode).length; + Object.keys(sourceCode).forEach((k) => { + zip.files[pathObj[k]].async('string').then((data) => { + sourceCode[k] = data; + fieldsLeft--; + + if (fieldsLeft === 0) { + window.postMessage([ + [sourceCode.html, sourceCode.css, sourceCode.js, sourceCode.fields, sourceCode.data], + 'zip' + ]); + } }); + }) } function handleCode(path) { @@ -366,7 +387,7 @@ waitForElm('#zip').then(() => { $('#zip').on("change", function (evt) { var files = evt.target.files; for (var i = 0; i < files.length; i++) { - handleZip(files[i]); + loadZipAndPopulateSeInputFields(files[i]); } }); }); @@ -493,4 +514,4 @@ const dialog = `
-` \ No newline at end of file +` From f465afaba5be3ab68c25a7bbe8b488d1881aa731 Mon Sep 17 00:00:00 2001 From: Marko Vejnovic Date: Thu, 30 Mar 2023 23:46:41 -0500 Subject: [PATCH 2/6] Improved ini file parser --- handleCode.js | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/handleCode.js b/handleCode.js index 1e1ee47..e775ffb 100644 --- a/handleCode.js +++ b/handleCode.js @@ -163,13 +163,9 @@ function parseIniManifest(data) { '[DATA]': 'data' }; - const outputFile = { - html: undefined, - css: undefined, - js: undefined, - fields: undefined, - data: undefined, - }; + const outputFile = Object.fromEntries( + Object.values(iniKeyOutKeyMap).map((k) => [k, undefined]), + ); const lines = data.split('\n'); for (let i = 0; i < lines.length; i += 1) { From 44c42db4b902b5db1edd7f9b9739cead91c54e7d Mon Sep 17 00:00:00 2001 From: Marko Vejnovic Date: Thu, 30 Mar 2023 23:47:31 -0500 Subject: [PATCH 3/6] Removed comment --- handleCode.js | 12 ------------ 1 file changed, 12 deletions(-) diff --git a/handleCode.js b/handleCode.js index e775ffb..56c24d2 100644 --- a/handleCode.js +++ b/handleCode.js @@ -142,18 +142,6 @@ function handleUnsupported(zip) { }) } -/** - * This function returns the ini-based manifest of a widget. This object will - * appear as: { - * html: 'example.html', - * css: 'example.css', - * js: 'example.js', - * fields: 'example.json', - * data: 'example-data.json', - * } - * - * If a failure case has occurred, this plugin will throw an `Error`. - */ function parseIniManifest(data) { const iniKeyOutKeyMap = { '[HTML]': 'html', From 8c08a946ced057617c144f63679cb6a984169369 Mon Sep 17 00:00:00 2001 From: Marko Vejnovic Date: Thu, 30 Mar 2023 23:48:05 -0500 Subject: [PATCH 4/6] Simplified readFiles --- handleCode.js | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/handleCode.js b/handleCode.js index 56c24d2..a39348c 100644 --- a/handleCode.js +++ b/handleCode.js @@ -187,13 +187,8 @@ function parseJsonManifest(data) { } function readFiles(zip, pathObj) { - const sourceCode = { - html: '', - css: '', - js: '', - fields: '', - data: '', - }; + const sourceCode = { html: '', css: '', js: '', fields: '', data: '' }; + let fieldsLeft = Object.keys(sourceCode).length; Object.keys(sourceCode).forEach((k) => { zip.files[pathObj[k]].async('string').then((data) => { From 6ce07ef72e2a7b1c075fbfd0998e1101515b4405 Mon Sep 17 00:00:00 2001 From: Marko Vejnovic Date: Fri, 31 Mar 2023 00:13:24 -0500 Subject: [PATCH 5/6] Decided to not be a wise-guy and not change architectural crap --- handleCode.js | 178 +++++++++++++++++++++++++++----------------------- 1 file changed, 98 insertions(+), 80 deletions(-) diff --git a/handleCode.js b/handleCode.js index a39348c..696bad8 100644 --- a/handleCode.js +++ b/handleCode.js @@ -73,36 +73,57 @@ function handleFile(file, type) { } } -function loadZipAndPopulateSeInputFields(file) { - const zip = new JSZip(); - zip.loadAsync(file).then((zip) => { - $('.zip-select span').text(file.name); - - if (zip.files['widget.ini']) { - zip.files[`widget.ini`].async('string').then((data) => { - try { - readFiles(zip, parseIniManifest(data)); - } catch (err) { - alert('This widget was not set up properly. Opening manual upload dialog...') - handleUnsupported(zip); - } - }); - } else if (zip.files['manifest.json']) { - zip.files['manifest.json'].async('string').then((data) => { - try { - readFiles(zip, parseJsonManifest(data)); - } catch (err) { - alert('This widget was not set up properly. Opening manual upload dialog...') - handleUnsupported(zip); - } - }); - } else { - handleUnsupported(zip); - } - }); +function handleZip(file) { + var zip = new JSZip(); + zip.loadAsync(file) + .then(function (zip) { + $('.zip-select span').text(file.name) + if (zip.files['widget.ini']) { + zip.files[`widget.ini`].async('string').then((data) => { + pathArray = handleIniData(data); + if (pathArray.length != 5) { + handleUnsupported(zip) + return + } + + if (!zip.files[`${pathArray[0]}`] || + !zip.files[`${pathArray[1]}`] || + !zip.files[`${pathArray[2]}`] || + !zip.files[`${pathArray[3]}`] || + !zip.files[`${pathArray[4]}`]) { + alert('This widget was not set up properly. Opening manual upload dialog...') + handleUnsupported(zip) + return + } + readFiles(zip, pathArray); + }); + } else if (zip.files['manifest.json']) { + zip.files['manifest.json'].async('string').then((data) => { + try { + readFiles(zip, parseJsonManifest(data)); + } catch(err) { + alert('The included manifest.json was malformed. ' + + 'Please re-package your widget.'); + handleUnsupported(zip); + } + }); + } else { + handleUnsupported(zip); + } + }); +} + +function parseJsonManifest(jsonData) { + const json = JSON.parse(jsonData); + if (!json || !json.fileMap) { + throw new Error('Invalid manifest.json'); + } + const { html, css, js, fields, data } = json.fileMap; + return [html, css, js, fields, data]; } function handleUnsupported(zip) { + // alert("This widget is not supported.") resetSession() waitForElm('#sigma-create-unsupported').then(() => { @@ -142,67 +163,64 @@ function handleUnsupported(zip) { }) } -function parseIniManifest(data) { - const iniKeyOutKeyMap = { - '[HTML]': 'html', - '[CSS]': 'css', - '[JS]': 'js', - '[FIELDS]': 'fields', - '[DATA]': 'data' - }; - - const outputFile = Object.fromEntries( - Object.values(iniKeyOutKeyMap).map((k) => [k, undefined]), - ); - - const lines = data.split('\n'); - for (let i = 0; i < lines.length; i += 1) { - Object.keys(iniKeyOutKeyMap).forEach((k) => { - if (iniKeyOutKeyMap[k] !== undefined) { - throw new Error('Malformed ini file.'); - } - if (lines[i].includes(k)) { - outputFile[iniKeyOutKeyMap[k]] = lines[i + 1].split('"'); - } - }); - } +function handleIniData(data) { + var lines = data.split('\n'); + var pathArray = []; + for (var i = 0; i < lines.length; i++) { + if (lines[i].includes('[HTML]')) { + var _temp = lines[i + 1].split('"'); + pathArray.push(_temp[1]); + } - // At this point, we should have populated every field. - Object.keys(iniKeyOutKeyMap).forEach((k) => { - if (iniKeyOutKeyMap[k] === undefined) { - throw new Error('Malformed ini file.'); + if (lines[i].includes('[CSS]')) { + var _temp = lines[i + 1].split('"'); + pathArray.push(_temp[1]); } - }); - return outputFile; -} + if (lines[i].includes('[JS]')) { + var _temp = lines[i + 1].split('"'); + pathArray.push(_temp[1]); + } -function parseJsonManifest(data) { - const files = JSON.parse(data).fileMap; - if (!files) { - throw new Error('Malformed json file'); + if (lines[i].includes('[FIELDS]')) { + var _temp = lines[i + 1].split('"'); + pathArray.push(_temp[1]); + } + + if (lines[i].includes('[DATA]')) { + var _temp = lines[i + 1].split('"'); + pathArray.push(_temp[1]); + } } - return files; + return pathArray; } -function readFiles(zip, pathObj) { - const sourceCode = { html: '', css: '', js: '', fields: '', data: '' }; - - let fieldsLeft = Object.keys(sourceCode).length; - Object.keys(sourceCode).forEach((k) => { - zip.files[pathObj[k]].async('string').then((data) => { - sourceCode[k] = data; - fieldsLeft--; - - if (fieldsLeft === 0) { - window.postMessage([ - [sourceCode.html, sourceCode.css, sourceCode.js, sourceCode.fields, sourceCode.data], - 'zip' - ]); - } +function readFiles(zip, pathArray) { + var htmlCode, cssCode, jsCode, fieldsCode, dataCode; + zip.files[`${pathArray[0]}`].async('string') + .then((data) => { + htmlCode = data; + zip.files[`${pathArray[1]}`].async('string') + .then((data) => { + cssCode = data; + zip.files[`${pathArray[2]}`].async('string') + .then((data) => { + jsCode = data; + zip.files[`${pathArray[3]}`].async('string') + .then((data) => { + fieldsCode = data; + zip.files[`${pathArray[4]}`].async('string') + .then((data) => { + dataCode = data; + code = [htmlCode, cssCode, jsCode, fieldsCode, dataCode] + window.postMessage([code, 'zip']) + return + }); + }); + }); + }); }); - }) } function handleCode(path) { @@ -366,7 +384,7 @@ waitForElm('#zip').then(() => { $('#zip').on("change", function (evt) { var files = evt.target.files; for (var i = 0; i < files.length; i++) { - loadZipAndPopulateSeInputFields(files[i]); + handleZip(files[i]); } }); }); From e273f1d089624d63fe4e57ab27a3b4df88624ea8 Mon Sep 17 00:00:00 2001 From: Marko Vejnovic Date: Fri, 31 Mar 2023 00:14:41 -0500 Subject: [PATCH 6/6] Prioritized manifest.json over the ini file --- handleCode.js | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/handleCode.js b/handleCode.js index 696bad8..aa72025 100644 --- a/handleCode.js +++ b/handleCode.js @@ -78,7 +78,17 @@ function handleZip(file) { zip.loadAsync(file) .then(function (zip) { $('.zip-select span').text(file.name) - if (zip.files['widget.ini']) { + if (zip.files['manifest.json']) { + zip.files['manifest.json'].async('string').then((data) => { + try { + readFiles(zip, parseJsonManifest(data)); + } catch(err) { + alert('The included manifest.json was malformed. ' + + 'Please re-package your widget.'); + handleUnsupported(zip); + } + }); + } else if (zip.files['widget.ini']) { zip.files[`widget.ini`].async('string').then((data) => { pathArray = handleIniData(data); if (pathArray.length != 5) { @@ -97,16 +107,6 @@ function handleZip(file) { } readFiles(zip, pathArray); }); - } else if (zip.files['manifest.json']) { - zip.files['manifest.json'].async('string').then((data) => { - try { - readFiles(zip, parseJsonManifest(data)); - } catch(err) { - alert('The included manifest.json was malformed. ' - + 'Please re-package your widget.'); - handleUnsupported(zip); - } - }); } else { handleUnsupported(zip); }