@hairy/lnv
Version:
Loading environment variables in Next.js and other frameworks can be quite cumbersome, and using dotenv or vault at runtime is also inconvenient. That's why my created this tool
60 lines (57 loc) • 1.54 kB
JavaScript
import {createRequire as __createRequire} from 'module';var require=__createRequire(import.meta.url);
// src/cli/utils/index.ts
function extractSingleQuotes(argv) {
const parsed = [];
let currentString = "";
let inQuotes = false;
let quoteChar = "";
for (const arg of argv) {
if (arg.startsWith("'") || arg.startsWith("`")) {
if (inQuotes) {
currentString += ` ${arg.slice(1)}`;
} else {
inQuotes = true;
quoteChar = arg[0];
currentString += arg.slice(1);
}
} else if (arg.endsWith("'") || arg.endsWith("`")) {
if (inQuotes && quoteChar === arg[arg.length - 1]) {
currentString += ` ${arg.slice(0, -1)}`;
parsed.push(currentString.trim());
currentString = "";
inQuotes = false;
} else {
parsed.push(arg);
}
} else {
inQuotes ? currentString += ` ${arg}` : parsed.push(arg);
}
}
currentString && parsed.push(currentString.trim());
return parsed;
}
function extractTailCommands(argv) {
const news = [];
const runs = [];
let is = false;
for (const arg of argv) {
if (is && arg === ";") {
news.push(runs.join(" "));
is = false;
continue;
}
is ? runs.push(arg) : news.push(arg);
if (arg === "-r" || arg === "--run")
is = true;
if (arg === "--") {
news.splice(news.length - 1, 1, "-r");
is = true;
}
}
is && runs.length && news.push(runs.join(" "));
return news;
}
export {
extractSingleQuotes,
extractTailCommands
};