cf-envsync 0.3.12 → 0.3.14

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.
package/dist/index.js CHANGED
@@ -2286,9 +2286,6 @@ function validateConfig(config) {
2286
2286
  if (!app.path) {
2287
2287
  errors.push(`App "${name}" is missing "path" field`);
2288
2288
  }
2289
- if (!app.workers || Object.keys(app.workers).length === 0) {
2290
- errors.push(`App "${name}" is missing "workers" mapping`);
2291
- }
2292
2289
  if ((!app.secrets || app.secrets.length === 0) && (!app.vars || app.vars.length === 0)) {
2293
2290
  errors.push(`App "${name}" has no "secrets" or "vars" declared`);
2294
2291
  }
@@ -2355,7 +2352,7 @@ function resolveApps(config, appNames) {
2355
2352
  return resolved;
2356
2353
  }
2357
2354
  function getWorkerName(app, environment) {
2358
- return app.workers[environment];
2355
+ return app.workers?.[environment];
2359
2356
  }
2360
2357
  function resolveEnvFilePath(pattern, env2) {
2361
2358
  if (env2 === "local")
@@ -12491,7 +12488,7 @@ var require_ignore = __commonJS((exports, module) => {
12491
12488
  TMP_KEY_IGNORE = Symbol.for("node-ignore");
12492
12489
  }
12493
12490
  var KEY_IGNORE = TMP_KEY_IGNORE;
12494
- var define2 = (object, key, value) => Object.defineProperty(object, key, { value });
12491
+ var define = (object, key, value) => Object.defineProperty(object, key, { value });
12495
12492
  var REGEX_REGEXP_RANGE = /([0-z])-([0-z])/g;
12496
12493
  var RETURN_FALSE = () => false;
12497
12494
  var sanitizeRange = (range) => range.replace(REGEX_REGEXP_RANGE, (match, from, to) => from.charCodeAt(0) <= to.charCodeAt(0) ? match : EMPTY);
@@ -12634,7 +12631,7 @@ var require_ignore = __commonJS((exports, module) => {
12634
12631
  ignoreCase = ignorecase,
12635
12632
  allowRelativePaths = false
12636
12633
  } = {}) {
12637
- define2(this, KEY_IGNORE, true);
12634
+ define(this, KEY_IGNORE, true);
12638
12635
  this._rules = [];
12639
12636
  this._ignoreCase = ignoreCase;
12640
12637
  this._allowRelativePaths = allowRelativePaths;
@@ -13440,152 +13437,6 @@ Done!`);
13440
13437
  });
13441
13438
  });
13442
13439
 
13443
- // node_modules/jsonc-parser/lib/umd/main.js
13444
- var require_main3 = __commonJS((exports, module) => {
13445
- (function(factory) {
13446
- if (typeof module === "object" && typeof module.exports === "object") {
13447
- var v2 = factory(__require, exports);
13448
- if (v2 !== undefined)
13449
- module.exports = v2;
13450
- } else if (typeof define === "function" && define.amd) {
13451
- define(["require", "exports", "./impl/format", "./impl/edit", "./impl/scanner", "./impl/parser"], factory);
13452
- }
13453
- })(function(require2, exports2) {
13454
- Object.defineProperty(exports2, "__esModule", { value: true });
13455
- exports2.applyEdits = exports2.modify = exports2.format = exports2.printParseErrorCode = exports2.ParseErrorCode = exports2.stripComments = exports2.visit = exports2.getNodeValue = exports2.getNodePath = exports2.findNodeAtOffset = exports2.findNodeAtLocation = exports2.parseTree = exports2.parse = exports2.getLocation = exports2.SyntaxKind = exports2.ScanError = exports2.createScanner = undefined;
13456
- const formatter = require2("./impl/format");
13457
- const edit = require2("./impl/edit");
13458
- const scanner = require2("./impl/scanner");
13459
- const parser = require2("./impl/parser");
13460
- exports2.createScanner = scanner.createScanner;
13461
- var ScanError;
13462
- (function(ScanError2) {
13463
- ScanError2[ScanError2["None"] = 0] = "None";
13464
- ScanError2[ScanError2["UnexpectedEndOfComment"] = 1] = "UnexpectedEndOfComment";
13465
- ScanError2[ScanError2["UnexpectedEndOfString"] = 2] = "UnexpectedEndOfString";
13466
- ScanError2[ScanError2["UnexpectedEndOfNumber"] = 3] = "UnexpectedEndOfNumber";
13467
- ScanError2[ScanError2["InvalidUnicode"] = 4] = "InvalidUnicode";
13468
- ScanError2[ScanError2["InvalidEscapeCharacter"] = 5] = "InvalidEscapeCharacter";
13469
- ScanError2[ScanError2["InvalidCharacter"] = 6] = "InvalidCharacter";
13470
- })(ScanError || (exports2.ScanError = ScanError = {}));
13471
- var SyntaxKind;
13472
- (function(SyntaxKind2) {
13473
- SyntaxKind2[SyntaxKind2["OpenBraceToken"] = 1] = "OpenBraceToken";
13474
- SyntaxKind2[SyntaxKind2["CloseBraceToken"] = 2] = "CloseBraceToken";
13475
- SyntaxKind2[SyntaxKind2["OpenBracketToken"] = 3] = "OpenBracketToken";
13476
- SyntaxKind2[SyntaxKind2["CloseBracketToken"] = 4] = "CloseBracketToken";
13477
- SyntaxKind2[SyntaxKind2["CommaToken"] = 5] = "CommaToken";
13478
- SyntaxKind2[SyntaxKind2["ColonToken"] = 6] = "ColonToken";
13479
- SyntaxKind2[SyntaxKind2["NullKeyword"] = 7] = "NullKeyword";
13480
- SyntaxKind2[SyntaxKind2["TrueKeyword"] = 8] = "TrueKeyword";
13481
- SyntaxKind2[SyntaxKind2["FalseKeyword"] = 9] = "FalseKeyword";
13482
- SyntaxKind2[SyntaxKind2["StringLiteral"] = 10] = "StringLiteral";
13483
- SyntaxKind2[SyntaxKind2["NumericLiteral"] = 11] = "NumericLiteral";
13484
- SyntaxKind2[SyntaxKind2["LineCommentTrivia"] = 12] = "LineCommentTrivia";
13485
- SyntaxKind2[SyntaxKind2["BlockCommentTrivia"] = 13] = "BlockCommentTrivia";
13486
- SyntaxKind2[SyntaxKind2["LineBreakTrivia"] = 14] = "LineBreakTrivia";
13487
- SyntaxKind2[SyntaxKind2["Trivia"] = 15] = "Trivia";
13488
- SyntaxKind2[SyntaxKind2["Unknown"] = 16] = "Unknown";
13489
- SyntaxKind2[SyntaxKind2["EOF"] = 17] = "EOF";
13490
- })(SyntaxKind || (exports2.SyntaxKind = SyntaxKind = {}));
13491
- exports2.getLocation = parser.getLocation;
13492
- exports2.parse = parser.parse;
13493
- exports2.parseTree = parser.parseTree;
13494
- exports2.findNodeAtLocation = parser.findNodeAtLocation;
13495
- exports2.findNodeAtOffset = parser.findNodeAtOffset;
13496
- exports2.getNodePath = parser.getNodePath;
13497
- exports2.getNodeValue = parser.getNodeValue;
13498
- exports2.visit = parser.visit;
13499
- exports2.stripComments = parser.stripComments;
13500
- var ParseErrorCode;
13501
- (function(ParseErrorCode2) {
13502
- ParseErrorCode2[ParseErrorCode2["InvalidSymbol"] = 1] = "InvalidSymbol";
13503
- ParseErrorCode2[ParseErrorCode2["InvalidNumberFormat"] = 2] = "InvalidNumberFormat";
13504
- ParseErrorCode2[ParseErrorCode2["PropertyNameExpected"] = 3] = "PropertyNameExpected";
13505
- ParseErrorCode2[ParseErrorCode2["ValueExpected"] = 4] = "ValueExpected";
13506
- ParseErrorCode2[ParseErrorCode2["ColonExpected"] = 5] = "ColonExpected";
13507
- ParseErrorCode2[ParseErrorCode2["CommaExpected"] = 6] = "CommaExpected";
13508
- ParseErrorCode2[ParseErrorCode2["CloseBraceExpected"] = 7] = "CloseBraceExpected";
13509
- ParseErrorCode2[ParseErrorCode2["CloseBracketExpected"] = 8] = "CloseBracketExpected";
13510
- ParseErrorCode2[ParseErrorCode2["EndOfFileExpected"] = 9] = "EndOfFileExpected";
13511
- ParseErrorCode2[ParseErrorCode2["InvalidCommentToken"] = 10] = "InvalidCommentToken";
13512
- ParseErrorCode2[ParseErrorCode2["UnexpectedEndOfComment"] = 11] = "UnexpectedEndOfComment";
13513
- ParseErrorCode2[ParseErrorCode2["UnexpectedEndOfString"] = 12] = "UnexpectedEndOfString";
13514
- ParseErrorCode2[ParseErrorCode2["UnexpectedEndOfNumber"] = 13] = "UnexpectedEndOfNumber";
13515
- ParseErrorCode2[ParseErrorCode2["InvalidUnicode"] = 14] = "InvalidUnicode";
13516
- ParseErrorCode2[ParseErrorCode2["InvalidEscapeCharacter"] = 15] = "InvalidEscapeCharacter";
13517
- ParseErrorCode2[ParseErrorCode2["InvalidCharacter"] = 16] = "InvalidCharacter";
13518
- })(ParseErrorCode || (exports2.ParseErrorCode = ParseErrorCode = {}));
13519
- function printParseErrorCode(code) {
13520
- switch (code) {
13521
- case 1:
13522
- return "InvalidSymbol";
13523
- case 2:
13524
- return "InvalidNumberFormat";
13525
- case 3:
13526
- return "PropertyNameExpected";
13527
- case 4:
13528
- return "ValueExpected";
13529
- case 5:
13530
- return "ColonExpected";
13531
- case 6:
13532
- return "CommaExpected";
13533
- case 7:
13534
- return "CloseBraceExpected";
13535
- case 8:
13536
- return "CloseBracketExpected";
13537
- case 9:
13538
- return "EndOfFileExpected";
13539
- case 10:
13540
- return "InvalidCommentToken";
13541
- case 11:
13542
- return "UnexpectedEndOfComment";
13543
- case 12:
13544
- return "UnexpectedEndOfString";
13545
- case 13:
13546
- return "UnexpectedEndOfNumber";
13547
- case 14:
13548
- return "InvalidUnicode";
13549
- case 15:
13550
- return "InvalidEscapeCharacter";
13551
- case 16:
13552
- return "InvalidCharacter";
13553
- }
13554
- return "<unknown ParseErrorCode>";
13555
- }
13556
- exports2.printParseErrorCode = printParseErrorCode;
13557
- function format(documentText, range, options) {
13558
- return formatter.format(documentText, range, options);
13559
- }
13560
- exports2.format = format;
13561
- function modify(text, path, value, options) {
13562
- return edit.setProperty(text, path, value, options);
13563
- }
13564
- exports2.modify = modify;
13565
- function applyEdits(text, edits) {
13566
- let sortedEdits = edits.slice(0).sort((a2, b2) => {
13567
- const diff = a2.offset - b2.offset;
13568
- if (diff === 0) {
13569
- return a2.length - b2.length;
13570
- }
13571
- return diff;
13572
- });
13573
- let lastModifiedOffset = text.length;
13574
- for (let i2 = sortedEdits.length - 1;i2 >= 0; i2--) {
13575
- let e2 = sortedEdits[i2];
13576
- if (e2.offset + e2.length <= lastModifiedOffset) {
13577
- text = edit.applyEdit(text, e2);
13578
- } else {
13579
- throw new Error("Overlapping edit");
13580
- }
13581
- lastModifiedOffset = e2.offset;
13582
- }
13583
- return text;
13584
- }
13585
- exports2.applyEdits = applyEdits;
13586
- });
13587
- });
13588
-
13589
13440
  // src/utils/process.ts
13590
13441
  import { spawn } from "node:child_process";
13591
13442
  async function exec(command, options = {}) {
@@ -13632,6 +13483,160 @@ var init_process = () => {};
13632
13483
 
13633
13484
  // src/core/wrangler.ts
13634
13485
  import { join as join6 } from "node:path";
13486
+ function stripJsonc(text) {
13487
+ let result = "";
13488
+ let i2 = 0;
13489
+ let inString = false;
13490
+ while (i2 < text.length) {
13491
+ if (inString) {
13492
+ result += text[i2];
13493
+ if (text[i2] === "\\" && i2 + 1 < text.length) {
13494
+ result += text[i2 + 1];
13495
+ i2 += 2;
13496
+ continue;
13497
+ }
13498
+ if (text[i2] === '"')
13499
+ inString = false;
13500
+ i2++;
13501
+ continue;
13502
+ }
13503
+ if (text[i2] === '"') {
13504
+ inString = true;
13505
+ result += text[i2];
13506
+ i2++;
13507
+ continue;
13508
+ }
13509
+ if (text[i2] === "/" && text[i2 + 1] === "/") {
13510
+ while (i2 < text.length && text[i2] !== `
13511
+ `)
13512
+ i2++;
13513
+ continue;
13514
+ }
13515
+ if (text[i2] === "/" && text[i2 + 1] === "*") {
13516
+ i2 += 2;
13517
+ while (i2 < text.length && !(text[i2] === "*" && text[i2 + 1] === "/"))
13518
+ i2++;
13519
+ i2 += 2;
13520
+ continue;
13521
+ }
13522
+ result += text[i2];
13523
+ i2++;
13524
+ }
13525
+ return result.replace(/,(\s*[}\]])/g, "$1");
13526
+ }
13527
+ function findValueEnd(text, start) {
13528
+ const open = text[start];
13529
+ const close = open === "{" ? "}" : open === "[" ? "]" : undefined;
13530
+ if (!close) {
13531
+ let i3 = start;
13532
+ if (text[i3] === '"') {
13533
+ i3++;
13534
+ while (i3 < text.length) {
13535
+ if (text[i3] === "\\" && i3 + 1 < text.length) {
13536
+ i3 += 2;
13537
+ continue;
13538
+ }
13539
+ if (text[i3] === '"')
13540
+ return i3 + 1;
13541
+ i3++;
13542
+ }
13543
+ return i3;
13544
+ }
13545
+ while (i3 < text.length && text[i3] !== "," && text[i3] !== "}" && text[i3] !== "]")
13546
+ i3++;
13547
+ return i3;
13548
+ }
13549
+ let depth = 1;
13550
+ let i2 = start + 1;
13551
+ let inStr = false;
13552
+ while (i2 < text.length && depth > 0) {
13553
+ if (inStr) {
13554
+ if (text[i2] === "\\" && i2 + 1 < text.length) {
13555
+ i2 += 2;
13556
+ continue;
13557
+ }
13558
+ if (text[i2] === '"')
13559
+ inStr = false;
13560
+ i2++;
13561
+ continue;
13562
+ }
13563
+ if (text[i2] === '"') {
13564
+ inStr = true;
13565
+ i2++;
13566
+ continue;
13567
+ }
13568
+ if (text[i2] === "/" && text[i2 + 1] === "/") {
13569
+ while (i2 < text.length && text[i2] !== `
13570
+ `)
13571
+ i2++;
13572
+ continue;
13573
+ }
13574
+ if (text[i2] === "/" && text[i2 + 1] === "*") {
13575
+ i2 += 2;
13576
+ while (i2 < text.length && !(text[i2] === "*" && text[i2 + 1] === "/"))
13577
+ i2++;
13578
+ i2 += 2;
13579
+ continue;
13580
+ }
13581
+ if (text[i2] === open)
13582
+ depth++;
13583
+ if (text[i2] === close)
13584
+ depth--;
13585
+ i2++;
13586
+ }
13587
+ return i2;
13588
+ }
13589
+ function findKeyRange(text, key, searchStart = 0) {
13590
+ const needle = `"${key}"`;
13591
+ let i2 = searchStart;
13592
+ let inString = false;
13593
+ while (i2 < text.length) {
13594
+ if (inString) {
13595
+ if (text[i2] === "\\" && i2 + 1 < text.length) {
13596
+ i2 += 2;
13597
+ continue;
13598
+ }
13599
+ if (text[i2] === '"')
13600
+ inString = false;
13601
+ i2++;
13602
+ continue;
13603
+ }
13604
+ if (text[i2] === "/" && text[i2 + 1] === "/") {
13605
+ while (i2 < text.length && text[i2] !== `
13606
+ `)
13607
+ i2++;
13608
+ continue;
13609
+ }
13610
+ if (text[i2] === "/" && text[i2 + 1] === "*") {
13611
+ i2 += 2;
13612
+ while (i2 < text.length && !(text[i2] === "*" && text[i2 + 1] === "/"))
13613
+ i2++;
13614
+ i2 += 2;
13615
+ continue;
13616
+ }
13617
+ if (text.startsWith(needle, i2)) {
13618
+ let j = i2 + needle.length;
13619
+ while (j < text.length && (text[j] === " " || text[j] === "\t" || text[j] === `
13620
+ ` || text[j] === "\r"))
13621
+ j++;
13622
+ if (text[j] === ":") {
13623
+ j++;
13624
+ while (j < text.length && (text[j] === " " || text[j] === "\t" || text[j] === `
13625
+ ` || text[j] === "\r"))
13626
+ j++;
13627
+ const end = findValueEnd(text, j);
13628
+ return [j, end];
13629
+ }
13630
+ }
13631
+ if (text[i2] === '"') {
13632
+ inString = true;
13633
+ i2++;
13634
+ continue;
13635
+ }
13636
+ i2++;
13637
+ }
13638
+ return;
13639
+ }
13635
13640
  async function checkWrangler() {
13636
13641
  const result = await exec(["npx", "wrangler", "--version"]);
13637
13642
  return result.success;
@@ -13686,28 +13691,71 @@ function findWranglerConfig(appPath) {
13686
13691
  }
13687
13692
  return;
13688
13693
  }
13694
+ function detectIndent(content) {
13695
+ const match = content.match(/\n(\s+)"/);
13696
+ return match ? match[1] : " ";
13697
+ }
13689
13698
  async function updateWranglerVars(appPath, environment, vars) {
13690
13699
  const configPath = findWranglerConfig(appPath);
13691
13700
  if (!configPath) {
13692
13701
  return { success: false, updatedCount: 0 };
13693
13702
  }
13694
- const content = await readFile(configPath);
13695
- const config = import_jsonc_parser.parse(content);
13696
- const envSection = config?.env ?? {};
13703
+ let content = await readFile(configPath);
13704
+ const indent = detectIndent(content);
13705
+ const parsed = JSON.parse(stripJsonc(content));
13706
+ const envSection = parsed?.env ?? {};
13697
13707
  const existingVars = envSection[environment]?.vars ?? {};
13698
13708
  const mergedVars = { ...existingVars, ...vars };
13699
- const fmt = { tabSize: 2, insertSpaces: true };
13700
- const edits = import_jsonc_parser.modify(content, ["env", environment, "vars"], mergedVars, { formattingOptions: fmt });
13701
- const updated = import_jsonc_parser.applyEdits(content, edits);
13702
- await writeFile(configPath, updated);
13709
+ const varsJson = JSON.stringify(mergedVars, null, indent);
13710
+ const envRange = findKeyRange(content, "env");
13711
+ if (envRange) {
13712
+ const envObjRange = findKeyRange(content, environment, envRange[0]);
13713
+ if (envObjRange) {
13714
+ const varsRange = findKeyRange(content, "vars", envObjRange[0]);
13715
+ if (varsRange) {
13716
+ content = content.slice(0, varsRange[0]) + varsJson + content.slice(varsRange[1]);
13717
+ await writeFile(configPath, content);
13718
+ return { success: true, filePath: configPath, updatedCount: Object.keys(vars).length };
13719
+ }
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);
13726
+ await writeFile(configPath, content);
13727
+ return { success: true, filePath: configPath, updatedCount: Object.keys(vars).length };
13728
+ }
13729
+ const insertAt = envRange[1] - 1;
13730
+ const envIndent2 = indent.repeat(2);
13731
+ const inner2 = `{
13732
+ ${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);
13738
+ await writeFile(configPath, content);
13739
+ return { success: true, filePath: configPath, updatedCount: Object.keys(vars).length };
13740
+ }
13741
+ const lastBrace = content.lastIndexOf("}");
13742
+ const envIndent = indent;
13743
+ const inner = `{
13744
+ ${indent.repeat(2)}"${environment}": ` + `{
13745
+ ${indent.repeat(3)}"vars": ${varsJson}
13746
+ ${indent.repeat(2)}}
13747
+ ${indent}}`;
13748
+ const insertion = `,
13749
+ ${envIndent}"env": ${inner}
13750
+ `;
13751
+ content = content.slice(0, lastBrace) + insertion + content.slice(lastBrace);
13752
+ await writeFile(configPath, content);
13703
13753
  return { success: true, filePath: configPath, updatedCount: Object.keys(vars).length };
13704
13754
  }
13705
- var import_jsonc_parser;
13706
13755
  var init_wrangler = __esm(() => {
13707
13756
  init_dist2();
13708
13757
  init_process();
13709
13758
  init_fs();
13710
- import_jsonc_parser = __toESM(require_main3(), 1);
13711
13759
  });
13712
13760
 
13713
13761
  // src/commands/push.ts
@@ -14359,11 +14407,13 @@ function generateConfigTS(config) {
14359
14407
  for (const [name, app] of Object.entries(config.apps)) {
14360
14408
  lines.push(` ${quoteKey(name)}: {`);
14361
14409
  lines.push(` path: ${JSON.stringify(app.path)},`);
14362
- lines.push(` workers: {`);
14363
- for (const [env2, worker] of Object.entries(app.workers)) {
14364
- lines.push(` ${quoteKey(env2)}: ${JSON.stringify(worker)},`);
14410
+ if (app.workers && Object.keys(app.workers).length > 0) {
14411
+ lines.push(` workers: {`);
14412
+ for (const [env2, worker] of Object.entries(app.workers)) {
14413
+ lines.push(` ${quoteKey(env2)}: ${JSON.stringify(worker)},`);
14414
+ }
14415
+ lines.push(` },`);
14365
14416
  }
14366
- lines.push(` },`);
14367
14417
  if (app.secrets?.length) {
14368
14418
  lines.push(` secrets: ${JSON.stringify(app.secrets)},`);
14369
14419
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cf-envsync",
3
- "version": "0.3.12",
3
+ "version": "0.3.14",
4
4
  "description": "Sync .env files to Cloudflare Workers secrets, .dev.vars, and more",
5
5
  "type": "module",
6
6
  "exports": {
@@ -33,8 +33,7 @@
33
33
  "@dotenvx/dotenvx": "^1.52.0",
34
34
  "citty": "^0.2.1",
35
35
  "consola": "^3.4.2",
36
- "jiti": "^2.4.2",
37
- "jsonc-parser": "^3.3.1"
36
+ "jiti": "^2.4.2"
38
37
  },
39
38
  "devDependencies": {
40
39
  "@types/node": "^22"
@@ -35,8 +35,8 @@ export interface EnvSyncConfig {
35
35
  export interface AppConfig {
36
36
  /** Path to app directory relative to project root */
37
37
  path: string;
38
- /** Cloudflare Worker names per environment */
39
- workers: Record<string, string>;
38
+ /** Cloudflare Worker names per environment (omit for non-worker apps) */
39
+ workers?: Record<string, string>;
40
40
  /** Secret keys for this app (pushed via wrangler secret) */
41
41
  secrets?: string[];
42
42
  /** Var keys for this app (non-secret env vars) */