All files / mock monorepo.ts

91.72% Statements 155/169
85% Branches 17/20
69.23% Functions 9/13
91.72% Lines 155/169

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 1701x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 7x 7x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x     1x 1x 1x 1x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 1x 1x 2x 2x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x         1x 1x 2x 2x 2x 2x 2x 2x 2x 2x 1x 1x 1x 1x 2x 2x 2x 1x 2x 2x 2x 2x 2x 1x 1x     1x 1x     1x 1x 4x 11x 11x 3x 11x 8x 8x 11x 11x 11x 11x 2x 2x 11x 11x 11x 11x     11x 4x 4x 4x 4x 1x 1x 1x 1x 1x 1x 1x 1x     1x  
import os from "os";
import fs from "fs";
import path from "path";
import execa from "execa";
 
export class Monorepo {
  static tmpdir = os.tmpdir();
  static yarnCache = path.join(Monorepo.tmpdir, "yarn-cache-");
 
  root: string;
 
  get nodeModulesPath() {
    return path.join(this.root, "node_modules");
  }
 
  constructor(private name: string) {
    this.root = fs.mkdtempSync(path.join(Monorepo.tmpdir, `lage-monorepo-${name}-`));
  }
 
  init() {
    execa.sync("git", ["init"], { cwd: this.root });
    this.generateRepoFiles();
  }
 
  install() {
    if (!fs.existsSync(this.nodeModulesPath)) {
      fs.mkdirSync(this.nodeModulesPath, { recursive: true });
    }
 
    // pretends to perform a npm install of lage
    const lagePath = path.join(this.nodeModulesPath, "lage");
 
    if (!fs.existsSync(lagePath)) {
      fs.symlinkSync(path.join(__dirname, "..", ".."), lagePath, "junction");
    }
  }
 
  /**
   * Simulates a "yarn" call by linking internal packages and generates a yarn.lock file
   */
  linkPackages() {
    const pkgs = fs.readdirSync(path.join(this.root, "packages"));
 
    if (!fs.existsSync(this.nodeModulesPath)) {
      fs.mkdirSync(this.nodeModulesPath, { recursive: true });
    }
 
    let yarnYaml = `# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.\n# yarn lockfile v1\n`;
 
    for (const pkg of pkgs) {
      fs.symlinkSync(path.join(this.root, "packages", pkg), path.join(this.nodeModulesPath, pkg), "junction");
 
      const pkgJson = JSON.parse(fs.readFileSync(path.join(this.root, "packages", pkg, "package.json"), "utf-8"));
      const deps = pkgJson.dependencies;
 
      yarnYaml += `"${pkg}@^${pkgJson.version}":\n  version "${pkgJson.version}"\n`;
 
      if (deps) {
        yarnYaml += `  dependencies:`;
        for (const dep of Object.keys(deps)) {
          yarnYaml += `    "${dep}" "0.1.0"`;
        }
      }
    }
 
    this.commitFiles({ "yarn.lock": yarnYaml });
  }
 
  generateRepoFiles() {
    const lagePath = path.join(this.nodeModulesPath, "lage/lib/cli");
 
    this.commitFiles({
      "package.json": {
        name: this.name,
        version: "0.1.0",
        private: true,
        workspaces: ["packages/*"],
        scripts: {
          build: `node "${lagePath}" build --reporter json`,
          test: `node "${lagePath}" test --reporter json`,
          lint: `node "${lagePath}" lint --reporter json`,
        },
        devDependencies: {
          lage: path.resolve(__dirname, "..", ".."),
        },
      },
      "lage.config.js": `module.exports = {
        pipeline: {
          build: ['^build'],
          test: ['build'],
          lint: []
        }
      };`,
    });
  }
 
  setLageConfig(contents: string) {
    this.commitFiles({
      "lage.config.js": contents,
    });
  }
 
  addPackage(name: string, internalDeps: string[] = [], scripts?: { [script: string]: string }) {
    return this.commitFiles({
      [`packages/${name}/build.js`]: `console.log('building ${name}');`,
      [`packages/${name}/test.js`]: `console.log('building ${name}');`,
      [`packages/${name}/lint.js`]: `console.log('linting ${name}');`,
      [`packages/${name}/package.json`]: {
        name,
        version: "0.1.0",
        scripts: scripts || {
          build: "node ./build.js",
          test: "node ./test.js",
          lint: "node ./lint.js",
        },
        dependencies: {
          ...(internalDeps &&
            internalDeps.reduce((deps, dep) => {
              return { ...deps, [dep]: "*" };
            }, {})),
        },
      },
    });
  }
 
  clone(origin: string) {
    return execa.sync("git", ["clone", origin], { cwd: this.root });
  }
 
  push(origin: string, branch: string) {
    return execa.sync("git", ["push", origin, branch], { cwd: this.root });
  }
 
  commitFiles(files: { [name: string]: string | Object }, options: { executable?: boolean } = {}) {
    for (const [file, contents] of Object.entries(files)) {
      let out = "";
      if (typeof contents !== "string") {
        out = JSON.stringify(contents, null, 2);
      } else {
        out = contents;
      }
 
      const fullPath = path.join(this.root, file);
 
      if (!fs.existsSync(path.dirname(fullPath))) {
        fs.mkdirSync(path.dirname(fullPath), { recursive: true });
      }
 
      fs.writeFileSync(fullPath, out);
 
      if (options.executable) {
        fs.chmodSync(path.join(this.root, file), fs.constants.S_IXUSR | fs.constants.S_IRUSR | fs.constants.S_IROTH);
      }
    }
    return execa.sync("git", ["add", ...Object.keys(files)], {
      cwd: this.root,
    });
  }
 
  run(command: string, args?: string[]) {
    return execa.sync("yarn", [command, ...(args || [])], {
      cwd: this.root,
    });
  }
 
  cleanup() {
    fs.rmdirSync(this.root, { recursive: true });
  }
}