Skip to content

Instantly share code, notes, and snippets.

@bantic
Created September 28, 2021 10:08
Show Gist options
  • Select an option

  • Save bantic/66f6c77bf878abb2e3a50f70848b539e to your computer and use it in GitHub Desktop.

Select an option

Save bantic/66f6c77bf878abb2e3a50f70848b539e to your computer and use it in GitHub Desktop.

Revisions

  1. bantic created this gist Sep 28, 2021.
    60 changes: 60 additions & 0 deletions find-string-in-file-at-tag.mjs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,60 @@
    #!/usr/bin/env zx

    $.verbose = false;

    const MAX_v2_MINOR = 18;
    let version = { major: 3, minor: 28, patch: 0 };
    version.toString = function () {
    return `v${this.major}.${this.minor}.${this.patch}`;
    };
    version.decrementMinor = function () {
    if (this.minor > 0) {
    this.minor -= 1;
    } else {
    if (this.major === 3) {
    this.major = 2;
    this.minor = MAX_v2_MINOR;
    } else {
    throw new Error(`Cannot decrement: ${version}`);
    }
    }
    };

    async function tagExists(version) {
    try {
    let result = await $`git tag -l ${version}`;
    return true;
    } catch (p) {
    return p.exitCode === 0;
    }
    }

    async function checkForString(string, filepath, tag) {
    await $`git checkout ${tag}`;
    if (!fs.existsSync(filepath)) {
    throw new Error(`path ${filepath} doesn't exist`);
    }
    try {
    await $`cat ${filepath} | grep ${string}`;
    return true;
    } catch (p) {
    return p.exitCode === 0;
    }
    }

    while (true) {
    console.log(`Checking Version: ${version}`);
    let exists = await tagExists(version);
    if (!exists) {
    console.log(`Skipping version ${version}`);
    } else {
    let str = "index(";
    let filepath = "lib/broccoli/ember-app.js";
    let isFound = await checkForString(str, filepath, version.toString());
    if (isFound) {
    console.log(`FOUND! "${str}" at ${filepath} for version ${version}`);
    }
    }

    version.decrementMinor();
    }