-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit b233ca3
Showing
5 changed files
with
264 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
node_modules/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { | ||
fetchResponseAsText, | ||
getStaticScriptsUrl, | ||
getEnvFromBundles, | ||
} from "./utils.js"; | ||
|
||
const [, , url] = process.argv; | ||
|
||
export default async function main() { | ||
const markupText = await fetchResponseAsText(url); | ||
const staticScriptsUrl = getStaticScriptsUrl(markupText, url); | ||
const result = await getEnvFromBundles(staticScriptsUrl); | ||
console.log(result); | ||
} | ||
|
||
(async () => main())(); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
{ | ||
"name": "get-react-envs", | ||
"version": "0.0.1", | ||
"description": "", | ||
"type": "module", | ||
"main": "index.js", | ||
"scripts": {}, | ||
"keywords": [], | ||
"author": "", | ||
"license": "ISC", | ||
"dependencies": { | ||
"cheerio": "^1.0.0-rc.12", | ||
"node-fetch": "^3.2.10" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import fetch from "node-fetch"; | ||
import { load } from "cheerio"; | ||
|
||
/** | ||
* Fetchs text response for a url | ||
* | ||
* @param {String} url Url to be fetched | ||
* @returns {Promise<String>} Text Response | ||
*/ | ||
export async function fetchResponseAsText(url) { | ||
const response = await fetch(url); | ||
const text = await response.text(); | ||
return text; | ||
} | ||
|
||
/** | ||
* Gets an array of all static script urls | ||
* | ||
* @param {String} markup MarkupString | ||
* @returns {Array<string>} Array of static script urls | ||
*/ | ||
export function getStaticScriptsUrl(markup, url) { | ||
const $ = load(markup); | ||
const bundleUrls = $("script") | ||
.map((_, el) => { | ||
if (!el.attribs.src) return undefined; | ||
const _url = url.endsWith("/") ? url.slice(0, url.length - 1) : url; | ||
const _src = el.attribs.src.startsWith(".") | ||
? el.attribs.src.slice(1) | ||
: el.attribs.src; | ||
return _src.startsWith("http") ? _src : _url + _src; | ||
}) | ||
.filter(Boolean) | ||
.toArray(); | ||
return bundleUrls; | ||
} | ||
|
||
/** | ||
* Searches for React environment variables and | ||
* returns array of env. objects | ||
* | ||
* @param {Array<string>} urls Script Urls | ||
* @returns {Promise<Array<Record<string, string>>>} Array of founded enviroment objects | ||
*/ | ||
export async function getEnvFromBundles(urls) { | ||
const results = await Promise.all( | ||
urls.map(async (url) => { | ||
const jsText = await fetchResponseAsText(url); | ||
const envs = jsText.match(/REACT_APP[\w]+:[\S]+?('|"|$)/g); | ||
if (envs) { | ||
const data = envs | ||
.map((envStr) => envStr.split(/:(.*)/s).slice(0, 2)) | ||
.map(([k, v]) => [k, v.replace(/"/g, "")]); | ||
return Object.fromEntries(data); | ||
} | ||
}) | ||
); | ||
return results.filter(Boolean); | ||
} |