UNPKG

@live-html-editor/server

Version:

A server for @live-html-editor/browser

4 lines (3 loc) 2.59 kB
#!/usr/bin/env node !function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(require("yargs"),require("express"),require("cors"),require("body-parser"),require("fs"),require("path"),require("jsdom")):"function"==typeof define&&define.amd?define(["yargs","express","cors","body-parser","fs","path","jsdom"],t):t((e=e||self).yargs,e.express,e.cors,e.bodyParser,e.fs,e.path,e.jsdom)}(this,function(e,t,o,s,n,r,l){"use strict";t=t&&t.hasOwnProperty("default")?t.default:t,o=o&&o.hasOwnProperty("default")?o.default:o,s=s&&s.hasOwnProperty("default")?s.default:s,n=n&&n.hasOwnProperty("default")?n.default:n,r=r&&r.hasOwnProperty("default")?r.default:r;class a{constructor(){this.logs=[]}log(e){console.info(`\n${e}`),this.logs.push(e)}}function i(e){const t=e.indexOf("\n",1);return-1===t?-1!==e.indexOf("\r")?"\r":"\n":"\r"===e[t-1]?"\r\n":"\n"}const d=e.strict(!0).usage("Usage: $0 [options]").example("$0 --port=3000","Will listen on port 3000.").option("p",{alias:["port"],describe:"Listen-on Port-Number",type:"number",default:3e3,demand:!0}).help().alias("h","help").argv,c=t();c.options("/",o()),c.use(s.json()),c.post("/",o(),(e,t)=>{!function(e,t){const o=new a;try{const t=e.sourceFiles;for(const a of t){const t=r.resolve(a.path),d=r.extname(t).toLowerCase();let c;const u=n.readFileSync(t,"utf8"),f=i(u);o.log(`File: "${t}" was read. Detected line-encoding: ${s=f,"\n"===s?"LF":"\r"===s?"CR":"CRLF"}`);const p=new l.JSDOM(u,{includeNodeLocations:!0});o.log(`File: "${t}" was successfully read by \`JSDOM\`.`);let g=e.htmlDocument;const h=i(g);if(f!==h&&(g=g.replace(new RegExp(h,"g"),f)),/^\.html?$/.test(d)&&a.domPath){const e=p.window.document,t=a.domPath,s=e.querySelector(t),n=p.nodeLocation(s),r=n.startTag.endOffset;let l=n.endTag.startOffset;for(o.log(`Editable element: "${t}" was found. Its \`innerHtml\` is in: [${r}, ${l}).`);/\s/.test(u[--l]););++l,c=u.substring(0,r)+f+g+u.substring(l),o.log(`InnerHtml of Editable element: "${t}" will be replaced. Total (net) length of new document is: ${g.length} chars`)}else c=g,o.log(`Total content of the file will be replaced. Total (net) length of new document is: ${g.length} chars`);n.writeFileSync(t,c),o.log(`Contents was replaced. File: "${t}"`)}}catch(e){o.log(e.toString())}var s;t.status(200).send({logs:o.logs.join("\n\n")})}(e.body,t)}),c.use((e,t,o)=>t.status(404).send("The page not found!")),c.use((e,t,o,s)=>{console.error(e.stack),o.status(500).send("Something broke!")});const u=d.port;c.listen(u,()=>console.log(`App listening on http://127.0.0.1:${u} ...`))}); //# sourceMappingURL=bundle.umd.js.map