cf-envsync 0.3.14 → 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 +222 -20
  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,41 +13735,141 @@ 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
  }
13759
+ function findFullKeyRange(text, key, searchStart = 0) {
13760
+ const needle = `"${key}"`;
13761
+ let i2 = searchStart;
13762
+ let inString = false;
13763
+ while (i2 < text.length) {
13764
+ if (inString) {
13765
+ if (text[i2] === "\\" && i2 + 1 < text.length) {
13766
+ i2 += 2;
13767
+ continue;
13768
+ }
13769
+ if (text[i2] === '"')
13770
+ inString = false;
13771
+ i2++;
13772
+ continue;
13773
+ }
13774
+ if (text[i2] === "/" && text[i2 + 1] === "/") {
13775
+ while (i2 < text.length && text[i2] !== `
13776
+ `)
13777
+ i2++;
13778
+ continue;
13779
+ }
13780
+ if (text[i2] === "/" && text[i2 + 1] === "*") {
13781
+ i2 += 2;
13782
+ while (i2 < text.length && !(text[i2] === "*" && text[i2 + 1] === "/"))
13783
+ i2++;
13784
+ i2 += 2;
13785
+ continue;
13786
+ }
13787
+ if (text.startsWith(needle, i2)) {
13788
+ const keyStart = i2;
13789
+ let j = i2 + needle.length;
13790
+ while (j < text.length && (text[j] === " " || text[j] === "\t" || text[j] === `
13791
+ ` || text[j] === "\r"))
13792
+ j++;
13793
+ if (text[j] === ":") {
13794
+ j++;
13795
+ while (j < text.length && (text[j] === " " || text[j] === "\t" || text[j] === `
13796
+ ` || text[j] === "\r"))
13797
+ j++;
13798
+ const end = findValueEnd(text, j);
13799
+ return [keyStart, end];
13800
+ }
13801
+ }
13802
+ if (text[i2] === '"') {
13803
+ inString = true;
13804
+ i2++;
13805
+ continue;
13806
+ }
13807
+ i2++;
13808
+ }
13809
+ return;
13810
+ }
13811
+ function removeJsoncKey(text, key, searchStart, searchEnd) {
13812
+ const range = findFullKeyRange(text, key, searchStart);
13813
+ if (!range || range[0] >= searchEnd)
13814
+ return;
13815
+ const [keyStart, valueEnd] = range;
13816
+ let removeStart = keyStart;
13817
+ let removeEnd = valueEnd;
13818
+ let b2 = keyStart - 1;
13819
+ while (b2 >= 0 && (text[b2] === " " || text[b2] === "\t" || text[b2] === `
13820
+ ` || text[b2] === "\r"))
13821
+ b2--;
13822
+ if (b2 >= 0 && text[b2] === ",") {
13823
+ removeStart = b2;
13824
+ } else {
13825
+ let a2 = valueEnd;
13826
+ while (a2 < text.length && (text[a2] === " " || text[a2] === "\t" || text[a2] === `
13827
+ ` || text[a2] === "\r"))
13828
+ a2++;
13829
+ if (a2 < text.length && text[a2] === ",") {
13830
+ removeEnd = a2 + 1;
13831
+ }
13832
+ let lineStart = keyStart - 1;
13833
+ while (lineStart >= 0 && (text[lineStart] === " " || text[lineStart] === "\t"))
13834
+ lineStart--;
13835
+ if (lineStart >= 0 && text[lineStart] === `
13836
+ `) {
13837
+ removeStart = lineStart;
13838
+ }
13839
+ }
13840
+ return text.slice(0, removeStart) + text.slice(removeEnd);
13841
+ }
13842
+ async function removeWranglerVars(appPath, environments, dryRun = false) {
13843
+ const configPath = findWranglerConfig(appPath);
13844
+ if (!configPath) {
13845
+ return { success: true, removedCount: 0 };
13846
+ }
13847
+ let content = await readFile(configPath);
13848
+ const parsed = JSON.parse(stripJsonc(content));
13849
+ const envSection = parsed?.env ?? {};
13850
+ let removedCount = 0;
13851
+ for (const environment of environments) {
13852
+ if (!envSection[environment]?.vars)
13853
+ continue;
13854
+ removedCount++;
13855
+ if (dryRun)
13856
+ continue;
13857
+ const envRange = findKeyRange(content, "env");
13858
+ if (!envRange)
13859
+ continue;
13860
+ const envObjRange = findKeyRange(content, environment, envRange[0]);
13861
+ if (!envObjRange)
13862
+ continue;
13863
+ const result = removeJsoncKey(content, "vars", envObjRange[0], envObjRange[1]);
13864
+ if (result !== undefined) {
13865
+ content = result;
13866
+ }
13867
+ }
13868
+ if (!dryRun && removedCount > 0) {
13869
+ await writeFile(configPath, content);
13870
+ }
13871
+ return { success: true, filePath: configPath, removedCount };
13872
+ }
13755
13873
  var init_wrangler = __esm(() => {
13756
13874
  init_dist2();
13757
13875
  init_process();
@@ -15256,6 +15374,89 @@ var init_encrypt = __esm(() => {
15256
15374
  });
15257
15375
  });
15258
15376
 
15377
+ // src/commands/clean.ts
15378
+ var exports_clean = {};
15379
+ __export(exports_clean, {
15380
+ default: () => clean_default
15381
+ });
15382
+ import { join as join9, relative as relative4 } from "node:path";
15383
+ import { unlinkSync } from "node:fs";
15384
+ var clean_default;
15385
+ var init_clean = __esm(() => {
15386
+ init_dist();
15387
+ init_dist2();
15388
+ init_config();
15389
+ init_wrangler();
15390
+ init_fs();
15391
+ clean_default = defineCommand({
15392
+ meta: {
15393
+ name: "clean",
15394
+ description: "Remove generated files (.dev.vars, wrangler.jsonc vars)"
15395
+ },
15396
+ args: {
15397
+ "dry-run": {
15398
+ type: "boolean",
15399
+ description: "Show what would be removed without removing",
15400
+ default: false
15401
+ }
15402
+ },
15403
+ async run({ args }) {
15404
+ const rawConfig = await loadConfig();
15405
+ const errors = validateConfig(rawConfig);
15406
+ if (errors.length > 0) {
15407
+ for (const err of errors)
15408
+ consola.error(err);
15409
+ process.exit(1);
15410
+ }
15411
+ const config = resolveConfig(rawConfig);
15412
+ const apps = Object.values(config.apps);
15413
+ if (apps.length === 0) {
15414
+ consola.warn("No apps to process.");
15415
+ return;
15416
+ }
15417
+ const dryRun = args["dry-run"];
15418
+ let removedFiles = 0;
15419
+ let removedVars = 0;
15420
+ for (const app of apps) {
15421
+ for (const devFileName of app.devFiles) {
15422
+ const filePath = join9(app.absolutePath, devFileName);
15423
+ if (!fileExists(filePath))
15424
+ continue;
15425
+ const rel = relative4(config.projectRoot, filePath);
15426
+ if (dryRun) {
15427
+ consola.log(` Would remove ${rel}`);
15428
+ } else {
15429
+ unlinkSync(filePath);
15430
+ consola.log(` Removed ${rel}`);
15431
+ }
15432
+ removedFiles++;
15433
+ }
15434
+ const result = await removeWranglerVars(app.absolutePath, config.environments, dryRun);
15435
+ if (result.success && result.removedCount > 0) {
15436
+ const rel = relative4(config.projectRoot, result.filePath);
15437
+ if (dryRun) {
15438
+ consola.log(` Would remove ${result.removedCount} vars section(s) from ${rel}`);
15439
+ } else {
15440
+ consola.log(` Removed ${result.removedCount} vars section(s) from ${rel}`);
15441
+ }
15442
+ removedVars += result.removedCount;
15443
+ }
15444
+ }
15445
+ if (removedFiles === 0 && removedVars === 0) {
15446
+ consola.info("Nothing to clean.");
15447
+ return;
15448
+ }
15449
+ const action = dryRun ? "Would remove" : "Removed";
15450
+ const parts = [];
15451
+ if (removedFiles > 0)
15452
+ parts.push(`${removedFiles} file(s)`);
15453
+ if (removedVars > 0)
15454
+ parts.push(`${removedVars} vars section(s)`);
15455
+ consola.success(`${action} ${parts.join(", ")}.`);
15456
+ }
15457
+ });
15458
+ });
15459
+
15259
15460
  // src/index.ts
15260
15461
  init_dist();
15261
15462
  var main = defineCommand({
@@ -15274,7 +15475,8 @@ var main = defineCommand({
15274
15475
  normalize: () => Promise.resolve().then(() => (init_normalize(), exports_normalize)).then((m2) => m2.default),
15275
15476
  merge: () => Promise.resolve().then(() => (init_merge(), exports_merge)).then((m2) => m2.default),
15276
15477
  list: () => Promise.resolve().then(() => (init_list(), exports_list)).then((m2) => m2.default),
15277
- encrypt: () => Promise.resolve().then(() => (init_encrypt(), exports_encrypt)).then((m2) => m2.default)
15478
+ encrypt: () => Promise.resolve().then(() => (init_encrypt(), exports_encrypt)).then((m2) => m2.default),
15479
+ clean: () => Promise.resolve().then(() => (init_clean(), exports_clean)).then((m2) => m2.default)
15278
15480
  }
15279
15481
  });
15280
15482
  runMain(main);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cf-envsync",
3
- "version": "0.3.14",
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": {