Bin
2025-12-16 9e0b2ba2c317b1a86212f24cbae3195ad1f3dbfa
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
#!/usr/bin/env node
 
import yargs from "yargs/yargs";
import { hideBin } from "yargs/helpers";
import cp from "child_process";
import path from "path";
import fs from "fs";
import { stdout } from "process";
 
const packageDir = "./node_modules/@heartexlabs/ls-test";
const assetsDir = path.resolve(packageDir, "bin/assets");
const workspaceDir = path.resolve();
const relativePath = (p) => path.resolve(assetsDir, p);
 
const CREATE_DIRS = ["./cypress/support", "./specs"];
 
const CREATE_FILES = ["./cypress/support/e2e.ts", "./specs/example.cy.ts", "./cypress.config.js", "./tsconfig.json"];
 
const COPY_CONTENTS = [
  [relativePath("spec.cy.ts"), "./specs/example.cy.ts"],
  [relativePath("cypress.config.js"), "./cypress.config.js"],
  [relativePath("tsconfig.json"), "tsconfig.json"],
  [relativePath("e2e.ts"), "./cypress/support/e2e.ts"],
];
 
const runCommand = async (cmd, args, message) => {
  return new Promise((resolve) => {
    console.log(message);
 
    const command = cp.spawn(cmd, args, { shell: true });
    const err = [];
 
    command.stdout.on("data", (data) => {
      stdout.write(data);
    });
 
    command.stderr.on("data", (data) => {
      err.push(data.toString());
    });
 
    command.on("close", (code) => {
      if (code !== 0) return resolve(new Error(err.join("\n")));
      resolve();
    });
  });
};
 
yargs(hideBin(process.argv))
  .command(
    "init",
    "Initialize framework",
    () => {},
    async (args) => {
      console.log("Preparing environment");
 
      await Promise.all(CREATE_DIRS.map((dir) => runCommand("mkdir", ["-p", dir], `Creating ${dir}`)));
 
      await Promise.all(CREATE_FILES.map((file) => runCommand("touch", [file], `Creating ${file}`)));
 
      await Promise.all(
        COPY_CONTENTS.map(([source, dest]) =>
          runCommand(`/bin/cat ${source} >> ${dest}`, [], `Copying ${path.basename(dest)}`),
        ),
      );
 
      console.log("Adding test commands");
      const sourcePkg = JSON.parse(fs.readFileSync(path.resolve(assetsDir, "package.json")).toString());
      const destPkg = JSON.parse(fs.readFileSync(path.resolve(workspaceDir, "package.json")).toString());
 
      destPkg.type = "module";
      destPkg.scripts = {
        ...(destPkg.scripts ?? {}),
        ...sourcePkg.scripts,
      };
 
      fs.writeFileSync("./package.json", JSON.stringify(destPkg, null, "  "));
 
      await runCommand(
        "yarn",
        ["add", "--dev", "webpack@5", "webpack-cli@5", "typescript@5", "ts-loader@9.4"],
        "Installing packages",
      );
    },
  )
  .help(false)
  .parse();