cf-envsync 0.3.15 → 0.3.16

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/dist/index.js +23 -19
  2. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -13695,6 +13695,24 @@ function detectIndent(content) {
13695
13695
  const match = content.match(/\n(\s+)"/);
13696
13696
  return match ? match[1] : " ";
13697
13697
  }
13698
+ function reindentJson(json, baseIndent) {
13699
+ return json.replace(/\n/g, `
13700
+ ${baseIndent}`);
13701
+ }
13702
+ function insertIntoObject(content, objEnd, key, value, keyIndent, braceIndent) {
13703
+ const closingBrace = objEnd - 1;
13704
+ let pos = closingBrace - 1;
13705
+ while (pos >= 0 && (content[pos] === " " || content[pos] === "\t" || content[pos] === `
13706
+ ` || content[pos] === "\r"))
13707
+ pos--;
13708
+ const needsComma = pos >= 0 && content[pos] !== "," && content[pos] !== "{";
13709
+ const comma = needsComma ? "," : "";
13710
+ pos++;
13711
+ const insertion = `${comma}
13712
+ ${keyIndent}"${key}": ${value}
13713
+ ${braceIndent}`;
13714
+ return content.slice(0, pos) + insertion + content.slice(closingBrace);
13715
+ }
13698
13716
  async function updateWranglerVars(appPath, environment, vars) {
13699
13717
  const configPath = findWranglerConfig(appPath);
13700
13718
  if (!configPath) {
@@ -13706,7 +13724,7 @@ async function updateWranglerVars(appPath, environment, vars) {
13706
13724
  const envSection = parsed?.env ?? {};
13707
13725
  const existingVars = envSection[environment]?.vars ?? {};
13708
13726
  const mergedVars = { ...existingVars, ...vars };
13709
- const varsJson = JSON.stringify(mergedVars, null, indent);
13727
+ const varsJson = reindentJson(JSON.stringify(mergedVars, null, indent), indent.repeat(3));
13710
13728
  const envRange = findKeyRange(content, "env");
13711
13729
  if (envRange) {
13712
13730
  const envObjRange = findKeyRange(content, environment, envRange[0]);
@@ -13717,38 +13735,24 @@ async function updateWranglerVars(appPath, environment, vars) {
13717
13735
  await writeFile(configPath, content);
13718
13736
  return { success: true, filePath: configPath, updatedCount: Object.keys(vars).length };
13719
13737
  }
13720
- const insertAt2 = envObjRange[1] - 1;
13721
- const envIndent3 = indent.repeat(3);
13722
- const insertion3 = `,
13723
- ${envIndent3}"vars": ${varsJson}
13724
- ${indent.repeat(2)}`;
13725
- content = content.slice(0, insertAt2) + insertion3 + content.slice(insertAt2);
13738
+ content = insertIntoObject(content, envObjRange[1], "vars", varsJson, indent.repeat(3), indent.repeat(2));
13726
13739
  await writeFile(configPath, content);
13727
13740
  return { success: true, filePath: configPath, updatedCount: Object.keys(vars).length };
13728
13741
  }
13729
- const insertAt = envRange[1] - 1;
13730
- const envIndent2 = indent.repeat(2);
13731
13742
  const inner2 = `{
13732
13743
  ${indent.repeat(3)}"vars": ${varsJson}
13733
- ${envIndent2}}`;
13734
- const insertion2 = `,
13735
- ${envIndent2}"${environment}": ${inner2}
13736
- ${indent}`;
13737
- content = content.slice(0, insertAt) + insertion2 + content.slice(insertAt);
13744
+ ${indent.repeat(2)}}`;
13745
+ content = insertIntoObject(content, envRange[1], environment, inner2, indent.repeat(2), indent);
13738
13746
  await writeFile(configPath, content);
13739
13747
  return { success: true, filePath: configPath, updatedCount: Object.keys(vars).length };
13740
13748
  }
13741
13749
  const lastBrace = content.lastIndexOf("}");
13742
- const envIndent = indent;
13743
13750
  const inner = `{
13744
13751
  ${indent.repeat(2)}"${environment}": ` + `{
13745
13752
  ${indent.repeat(3)}"vars": ${varsJson}
13746
13753
  ${indent.repeat(2)}}
13747
13754
  ${indent}}`;
13748
- const insertion = `,
13749
- ${envIndent}"env": ${inner}
13750
- `;
13751
- content = content.slice(0, lastBrace) + insertion + content.slice(lastBrace);
13755
+ content = insertIntoObject(content, lastBrace + 1, "env", inner, indent, "");
13752
13756
  await writeFile(configPath, content);
13753
13757
  return { success: true, filePath: configPath, updatedCount: Object.keys(vars).length };
13754
13758
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cf-envsync",
3
- "version": "0.3.15",
3
+ "version": "0.3.16",
4
4
  "description": "Sync .env files to Cloudflare Workers secrets, .dev.vars, and more",
5
5
  "type": "module",
6
6
  "exports": {