danday74 / envsub

NPM - envsub is envsubst for Node.js
Other
62 stars 10 forks source link

Feature request: string subtitution without files #23

Open davidmoshal opened 4 years ago

davidmoshal commented 4 years ago

ie: given a string representing a .env file and a template string, return another string with substituted variables.

ps: awesome library, thanks so much for creating it !

revanth0212 commented 1 month ago

You can use the internal parser which the envsub CLI tool uses.

Quick example:

const parseEnv = require("envsub/js/envsub-parser");

const envParserConfig = {
  outputFile: null,
  options: {
    all: false,
    diff: false,
    protect: false,
    syntax: "dollar-both",
  },
  cli: false,
};

const data = {
  name: "$PATH",
};

const content = parseEnv(JSON.stringify(data), envParserConfig);

console.log(content); // "{\"name\":\"/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin\"}"

Hope this helps