@cleartrip/frontguard 1.0.0 → 1.0.1

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/cli.js CHANGED
@@ -2628,23 +2628,18 @@ If **Yes**, list tools and what they touched (helps reviewers run a stricter fir
2628
2628
  ## AI assistance (optional detail)
2629
2629
  - [ ] I have reviewed every AI-suggested line for security, auth, and product correctness
2630
2630
  `;
2631
- var FRONTGUARD_STEP_YAML = ` - step:
2632
- name: FrontGuard \u2014 report + PR comment
2633
- caches:
2634
- - node
2635
- artifacts:
2636
- - frontguard-report.html
2637
- - frontguard-report.md
2638
- - frontguard-pr-comment.partial.md
2639
- script:
2640
- - corepack enable
2641
- - yarn install --immutable || yarn install
2642
- - |
2643
- yarn frontguard run --markdown \\
2644
- --htmlOut frontguard-report.html \\
2645
- --prCommentOut frontguard-pr-comment.partial.md \\
2646
- > frontguard-report.md
2647
- - test -n "\${BITBUCKET_ACCESS_TOKEN:-}" || { echo "Missing secured var BITBUCKET_ACCESS_TOKEN"; exit 1; }
2631
+ async function detectPackageManager(cwd) {
2632
+ const [hasYarnLock, hasPnpmLock, hasNpmLock] = await Promise.all([
2633
+ fileExists(path6.join(cwd, "yarn.lock")),
2634
+ fileExists(path6.join(cwd, "pnpm-lock.yaml")),
2635
+ fileExists(path6.join(cwd, "package-lock.json"))
2636
+ ]);
2637
+ if (hasYarnLock) return "yarn";
2638
+ if (hasPnpmLock) return "pnpm";
2639
+ if (hasNpmLock) return "npm";
2640
+ return "npm";
2641
+ }
2642
+ var UPLOAD_AND_COMMENT_SCRIPT = ` - test -n "\${BITBUCKET_ACCESS_TOKEN:-}" || { echo "Missing secured var BITBUCKET_ACCESS_TOKEN"; exit 1; }
2648
2643
  - |
2649
2644
  python3 << 'PY'
2650
2645
  import json
@@ -2700,20 +2695,70 @@ var FRONTGUARD_STEP_YAML = ` - step:
2700
2695
  --header 'Content-Type: application/json' \\
2701
2696
  --header "Authorization: Bearer \${BITBUCKET_ACCESS_TOKEN}" \\
2702
2697
  --data @frontguard-payload.json`;
2698
+ function buildFrontGuardStepYaml(pm) {
2699
+ const installLines = [];
2700
+ const runLine = [];
2701
+ if (pm === "yarn") {
2702
+ installLines.push(
2703
+ " - corepack enable",
2704
+ " - yarn install --immutable || YARN_ENABLE_IMMUTABLE_INSTALLS=false yarn install"
2705
+ );
2706
+ runLine.push(
2707
+ " - |",
2708
+ " yarn frontguard run --markdown \\",
2709
+ " --htmlOut frontguard-report.html \\",
2710
+ " --prCommentOut frontguard-pr-comment.partial.md \\",
2711
+ " > frontguard-report.md"
2712
+ );
2713
+ } else if (pm === "pnpm") {
2714
+ installLines.push(
2715
+ " - corepack enable",
2716
+ " - pnpm install --frozen-lockfile || pnpm install"
2717
+ );
2718
+ runLine.push(
2719
+ " - |",
2720
+ " pnpm exec frontguard run --markdown \\",
2721
+ " --htmlOut frontguard-report.html \\",
2722
+ " --prCommentOut frontguard-pr-comment.partial.md \\",
2723
+ " > frontguard-report.md"
2724
+ );
2725
+ } else {
2726
+ installLines.push(" - npm ci || npm install");
2727
+ runLine.push(
2728
+ " - |",
2729
+ " npx frontguard run --markdown \\",
2730
+ " --htmlOut frontguard-report.html \\",
2731
+ " --prCommentOut frontguard-pr-comment.partial.md \\",
2732
+ " > frontguard-report.md"
2733
+ );
2734
+ }
2735
+ return ` - step:
2736
+ name: FrontGuard \u2014 report + PR comment
2737
+ caches:
2738
+ - node
2739
+ artifacts:
2740
+ - frontguard-report.html
2741
+ - frontguard-report.md
2742
+ - frontguard-pr-comment.partial.md
2743
+ script:
2744
+ ${installLines.join("\n")}
2745
+ ${runLine.join("\n")}
2746
+ ${UPLOAD_AND_COMMENT_SCRIPT}`;
2747
+ }
2703
2748
  var FRONTGUARD_MARKER = "FrontGuard";
2704
2749
  function hasFrontGuardStep(content) {
2705
2750
  return content.includes(FRONTGUARD_MARKER);
2706
2751
  }
2707
- function mergePipelineStep(existing) {
2752
+ function mergePipelineStep(existing, stepYaml) {
2708
2753
  if (hasFrontGuardStep(existing)) {
2709
2754
  return existing;
2710
2755
  }
2711
2756
  const lines = existing.split("\n");
2712
2757
  const prSectionIdx = findPullRequestsSection(lines);
2713
2758
  if (prSectionIdx !== null) {
2714
- return injectStepIntoPrSection(lines, prSectionIdx);
2759
+ return injectStepIntoPrSection(lines, prSectionIdx, stepYaml);
2715
2760
  }
2716
- return appendPrSection(lines);
2761
+ return appendPrSection(lines, stepYaml);
2717
2762
  }
2718
2763
  function findPullRequestsSection(lines) {
2719
2764
  for (let i3 = 0; i3 < lines.length; i3++) {
@@ -2742,19 +2787,19 @@ function findLastStepEnd(lines, afterBranch) {
2742
2787
  }
2743
2788
  return lastContentLine;
2744
2789
  }
2745
- function injectStepIntoPrSection(lines, prIdx) {
2790
+ function injectStepIntoPrSection(lines, prIdx, stepYaml) {
2746
2791
  const branchIdx = findBranchPattern(lines, prIdx);
2747
2792
  if (branchIdx !== null) {
2748
2793
  const insertAfter = findLastStepEnd(lines, branchIdx);
2749
2794
  const before = lines.slice(0, insertAfter + 1);
2750
2795
  const after = lines.slice(insertAfter + 1);
2751
- return [...before, FRONTGUARD_STEP_YAML, ...after].join("\n");
2796
+ return [...before, stepYaml, ...after].join("\n");
2752
2797
  }
2753
2798
  const result = [...lines];
2754
- result.splice(prIdx + 1, 0, " '**':", FRONTGUARD_STEP_YAML);
2799
+ result.splice(prIdx + 1, 0, " '**':", stepYaml);
2755
2800
  return result.join("\n");
2756
2801
  }
2757
- function appendPrSection(lines) {
2802
+ function appendPrSection(lines, stepYaml) {
2758
2803
  const pipelinesIdx = lines.findIndex((l3) => /^pipelines:\s*$/.test(l3));
2759
2804
  if (pipelinesIdx === -1) {
2760
2805
  return [
@@ -2763,7 +2808,7 @@ function appendPrSection(lines) {
2763
2808
  "pipelines:",
2764
2809
  " pull-requests:",
2765
2810
  " '**':",
2766
- FRONTGUARD_STEP_YAML
2811
+ stepYaml
2767
2812
  ].join("\n");
2768
2813
  }
2769
2814
  let insertAt = pipelinesIdx + 1;
@@ -2787,7 +2832,7 @@ function appendPrSection(lines) {
2787
2832
  ...before,
2788
2833
  " pull-requests:",
2789
2834
  " '**':",
2790
- FRONTGUARD_STEP_YAML,
2835
+ stepYaml,
2791
2836
  ...after
2792
2837
  ].join("\n");
2793
2838
  }
@@ -2804,6 +2849,7 @@ function findEndOfLastSection(lines, pipelinesIdx) {
2804
2849
  }
2805
2850
  async function initFrontGuard(cwd) {
2806
2851
  const actions = [];
2852
+ const pm = await detectPackageManager(cwd);
2807
2853
  const cfgCandidates = [
2808
2854
  "frontguard.config.mjs",
2809
2855
  "frontguard.config.js",
@@ -2826,15 +2872,16 @@ async function initFrontGuard(cwd) {
2826
2872
  } else {
2827
2873
  actions.push("pull_request_template.md already exists (skipped)");
2828
2874
  }
2875
+ const stepYaml = buildFrontGuardStepYaml(pm);
2829
2876
  const pipelinePath = path6.join(cwd, "bitbucket-pipelines.yml");
2830
2877
  if (await fileExists(pipelinePath)) {
2831
2878
  const existing = await fs.readFile(pipelinePath, "utf8");
2832
2879
  if (hasFrontGuardStep(existing)) {
2833
2880
  actions.push("bitbucket-pipelines.yml already has FrontGuard step (skipped)");
2834
2881
  } else {
2835
- const merged = mergePipelineStep(existing);
2882
+ const merged = mergePipelineStep(existing, stepYaml);
2836
2883
  await fs.writeFile(pipelinePath, merged, "utf8");
2837
- actions.push("merged FrontGuard step into bitbucket-pipelines.yml pull-requests");
2884
+ actions.push(`merged FrontGuard step into bitbucket-pipelines.yml (${pm})`);
2838
2885
  }
2839
2886
  } else {
2840
2887
  await fs.writeFile(
@@ -2848,12 +2895,12 @@ async function initFrontGuard(cwd) {
2848
2895
  "pipelines:",
2849
2896
  " pull-requests:",
2850
2897
  " '**':",
2851
- FRONTGUARD_STEP_YAML,
2898
+ stepYaml,
2852
2899
  ""
2853
2900
  ].join("\n"),
2854
2901
  "utf8"
2855
2902
  );
2856
- actions.push("created bitbucket-pipelines.yml with FrontGuard step");
2903
+ actions.push(`created bitbucket-pipelines.yml with FrontGuard step (${pm})`);
2857
2904
  }
2858
2905
  return actions;
2859
2906
  }