Skip to content

Commit

Permalink
chore: first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
akulsr0 committed Oct 9, 2022
0 parents commit b233ca3
Show file tree
Hide file tree
Showing 5 changed files with 264 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules/
16 changes: 16 additions & 0 deletions index.js
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())();
173 changes: 173 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions package.json
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"
}
}
59 changes: 59 additions & 0 deletions utils.js
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);
}

0 comments on commit b233ca3

Please sign in to comment.