From b8dff9eb194e076e005f47d84196a622a9f3a47e Mon Sep 17 00:00:00 2001 From: jaymarnz Date: Sat, 31 Dec 2022 02:14:53 +1300 Subject: [PATCH] Add optional string input to api's This allows the API's to be chained without having to create temp files. See the changes to the API section of the readme for examples --- readme.md | 27 ++++++++++++- src/inlineEnvironmentVariables.js | 6 ++- src/inlineImages.js | 6 ++- src/inlineScriptTags.js | 6 ++- src/inlineStylesheets.js | 6 ++- test/testApi/test.js | 11 +++++ test/testApi/testfiles/icons/rss-icon.svg | 3 ++ test/testApi/testfiles/index.html | 38 ++++++++++++++++++ test/testApi/testfiles/main.js | 1 + test/testApi/testfiles/myScript.js | 1 + test/testApi/testfiles/myStyle.css | 3 ++ .../testfiles/nested/myNestedScript.js | 1 + .../testfiles/nested/myNestedStylesheet.css | 3 ++ test/testApi/testfiles/red_dot.png | Bin 0 -> 85 bytes 14 files changed, 102 insertions(+), 10 deletions(-) create mode 100644 test/testApi/test.js create mode 100644 test/testApi/testfiles/icons/rss-icon.svg create mode 100644 test/testApi/testfiles/index.html create mode 100644 test/testApi/testfiles/main.js create mode 100644 test/testApi/testfiles/myScript.js create mode 100644 test/testApi/testfiles/myStyle.css create mode 100644 test/testApi/testfiles/nested/myNestedScript.js create mode 100644 test/testApi/testfiles/nested/myNestedStylesheet.css create mode 100644 test/testApi/testfiles/red_dot.png diff --git a/readme.md b/readme.md index bc9f478..46d8b7b 100644 --- a/readme.md +++ b/readme.md @@ -164,6 +164,29 @@ For convenience, if the 2nd parameter is `.`, the output will replace the input # JS API -`const {inlineEnvironmentVariables, inlineRequires, inlineScriptTags} = require(inline-scripts);` +`const {inlineEnvironmentVariables, inlineRequires, inlineScriptTags, inlineStylesheets, inlineImages} = require('inline-scripts');` -Each of the functions take a string path to the entry file as their single parameter and return a promise that resolves to the computed output. +Each of the functions take a string path to the entry file as their single parameter and returns a promise that resolves to the computed output. + +To make it easy to perform multiple operations you can pass the output of each to the input for the next operation. Rather than pass a string path to `inlineScriptTag`, `inlineStylesheets` and `inlineImage` you can pass a hash with the file path and an html string. Both the path and string must be supplied. Rather than read the file it uses the supplied string as input. The path is required to resolve relative references to the css, js or image files. +```js +{ + htmlPath: '/path-to-file.html', + htmlString: 'string-containing-html' +} +``` +`inlineEnvironmentVariables` also takes either a string with the file path or a hash with either a path or a string (if both are present then the string is used and the path is ignored): +```js +{ + jsPath: '/path-to-file.js', + jsString: 'string-containing-js' +} +``` +An example of inlining scripts, stylesheets and images: +```js +inlineScriptTags('./index.html') +.then (htmlString => inlineStylesheets({ htmlPath: './index.html', htmlString })) +.then (htmlString => inlineImages({ htmlPath: './index.html', htmlString })) +.then (htmlString => fs.writeFileSync('./dist/index.html', htmlString)); + +``` diff --git a/src/inlineEnvironmentVariables.js b/src/inlineEnvironmentVariables.js index ca4a923..f1dea4b 100644 --- a/src/inlineEnvironmentVariables.js +++ b/src/inlineEnvironmentVariables.js @@ -1,8 +1,10 @@ const fs = require('fs').promises; const path = require('path'); -let inlineJsEnvVars = async jsPath => { - let file = await fs.readFile(jsPath, 'utf8'); +let inlineJsEnvVars = async options => { + let jsString, jsPath = (typeof options === 'string') ? options : ''; + if (typeof options === 'object') ({ jsString, jsPath } = options); + const file = jsString || await fs.readFile(jsPath, 'utf8'); const envVarRegex = /process\.env\.(\w+)/; let envVarMatches = file.match(new RegExp(envVarRegex, 'g')); if (!envVarMatches) diff --git a/src/inlineImages.js b/src/inlineImages.js index 9b635fc..8c26cc1 100644 --- a/src/inlineImages.js +++ b/src/inlineImages.js @@ -3,9 +3,11 @@ const fs = require('fs').promises; const path = require('path'); -let inlineImages = async htmlPath => { +let inlineImages = async options => { const imgTagRegex = //; - let html = await fs.readFile(htmlPath, 'utf8'); + let htmlString, htmlPath = (typeof options === 'string') ? options : ''; + if (typeof options === 'object') ({ htmlString, htmlPath } = options); + const html = htmlString || await fs.readFile(htmlPath, 'utf8'); let matches = html.match(new RegExp(imgTagRegex, 'g')); if (!matches) return html; diff --git a/src/inlineScriptTags.js b/src/inlineScriptTags.js index d19699b..02a0986 100644 --- a/src/inlineScriptTags.js +++ b/src/inlineScriptTags.js @@ -3,9 +3,11 @@ const fs = require('fs').promises; const path = require('path'); -let inlineHtmlScripts = async htmlPath => { +let inlineHtmlScripts = async options => { const scriptTagRegex = / + +

line 2

+ + + +

line 3

+ + + +red dot + + \ No newline at end of file diff --git a/test/testApi/testfiles/main.js b/test/testApi/testfiles/main.js new file mode 100644 index 0000000..f986837 --- /dev/null +++ b/test/testApi/testfiles/main.js @@ -0,0 +1 @@ +console.log('port is' + process.env.PORT); \ No newline at end of file diff --git a/test/testApi/testfiles/myScript.js b/test/testApi/testfiles/myScript.js new file mode 100644 index 0000000..4f424e1 --- /dev/null +++ b/test/testApi/testfiles/myScript.js @@ -0,0 +1 @@ +console.log('hi from myScript '); diff --git a/test/testApi/testfiles/myStyle.css b/test/testApi/testfiles/myStyle.css new file mode 100644 index 0000000..da4dbaf --- /dev/null +++ b/test/testApi/testfiles/myStyle.css @@ -0,0 +1,3 @@ +p { + background: red; +} diff --git a/test/testApi/testfiles/nested/myNestedScript.js b/test/testApi/testfiles/nested/myNestedScript.js new file mode 100644 index 0000000..2ea5dd6 --- /dev/null +++ b/test/testApi/testfiles/nested/myNestedScript.js @@ -0,0 +1 @@ +console.log('hi from myNestedScript'); diff --git a/test/testApi/testfiles/nested/myNestedStylesheet.css b/test/testApi/testfiles/nested/myNestedStylesheet.css new file mode 100644 index 0000000..25190f6 --- /dev/null +++ b/test/testApi/testfiles/nested/myNestedStylesheet.css @@ -0,0 +1,3 @@ +p { + color: green; +} diff --git a/test/testApi/testfiles/red_dot.png b/test/testApi/testfiles/red_dot.png new file mode 100644 index 0000000000000000000000000000000000000000..7a3361fbec1fd1236c57bd34be595a5aaf9434eb GIT binary patch literal 85 zcmeAS@N?(olHy`uVBq!ia0vp^tRT$61|)m))t&+=8BZ6-5RU7~KmPx>KU~kIz