MCPcopy
hub / github.com/graphile/starter / add

Function add

scripts/lib/dotenv.js:53–80  ·  view source on GitHub ↗
(varName, defaultValue, comment)

Source from the content-addressed store, hash-verified

51 };
52
53 function add(varName, defaultValue, comment) {
54 const SET = `\n${varName}=`;
55 const encodedValue = encodeDotenvValue(
56 varName in answers ? answers[varName] : defaultValue || ""
57 );
58 const pos = data.indexOf(SET);
59 if (pos >= 0) {
60 /* Replace this value with the new value */
61
62 // Where's the next newline (or the end of the file if there is none)
63 let nlpos = data.indexOf("\n", pos + 1);
64 if (nlpos < 0) {
65 nlpos = data.length;
66 }
67
68 // Surgical editing
69 data =
70 data.substr(0, pos + SET.length) + encodedValue + data.substr(nlpos);
71 } else {
72 /* This value didn't already exist; add it to the end */
73
74 if (comment) {
75 data += `\n\n${comment}`;
76 }
77
78 data += `${SET}${encodedValue}`;
79 }
80 }
81
82 await callback(add);
83

Callers 1

_setup_utils.jsFile · 0.85

Calls 1

encodeDotenvValueFunction · 0.85

Tested by

no test coverage detected