@commercebuild/extension 0.0.21 → 0.0.22

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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/scripts/cli.js +37 -8
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@commercebuild/extension",
3
- "version": "0.0.21",
3
+ "version": "0.0.22",
4
4
  "types": "./types/index.d.ts",
5
5
  "exports": {
6
6
  ".": {
package/scripts/cli.js CHANGED
@@ -1,6 +1,6 @@
1
1
  #!/usr/bin/env node
2
2
 
3
- import { execFileSync, execSync } from "child_process";
3
+ import { execFileSync } from "child_process";
4
4
  import { existsSync } from "fs";
5
5
  import { createRequire } from "module";
6
6
  import path from "path";
@@ -9,6 +9,23 @@ import { displayError } from "./utils.mjs";
9
9
  const __filename = fileURLToPath(import.meta.url);
10
10
  const __dirname = path.dirname(__filename);
11
11
  const args = process.argv.slice(2);
12
+ // Resolve tools from THIS package, not from whatever the project hoisted.
13
+ const require = createRequire(import.meta.url);
14
+
15
+ /**
16
+ * Absolute path of a dependency's `bin` script. Goes through its
17
+ * package.json ("./package.json" is exported by every package we need,
18
+ * while "./bin/*" usually is not) and reads the declared bin entry, so a
19
+ * relocation of the script inside the package cannot break us.
20
+ */
21
+ function binOf(pkg, name) {
22
+ const manifestPath = require.resolve(`${pkg}/package.json`);
23
+ const manifest = require(manifestPath);
24
+ const bin =
25
+ typeof manifest.bin === "string" ? manifest.bin : manifest.bin?.[name];
26
+ if (!bin) throw new Error(`${pkg} declares no "${name}" bin`);
27
+ return path.join(path.dirname(manifestPath), bin);
28
+ }
12
29
 
13
30
  const USAGE =
14
31
  "Usage: commercebuild-extension <dev | build | type-check>\n" +
@@ -30,12 +47,25 @@ function hasAdminEntry() {
30
47
  );
31
48
  }
32
49
 
50
+ /**
51
+ * Run vite without a shell: the project path goes to vite as one argument,
52
+ * so folders like "my-app(2)" or "My Apps" (spaces, parentheses — `open`
53
+ * numbers duplicate downloads that way) cannot be mis-parsed. Exit status
54
+ * is propagated by the caller through the thrown error.
55
+ */
56
+ function vite(viteArgs, env = process.env) {
57
+ execFileSync(process.execPath, [binOf("vite", "vite"), ...viteArgs], {
58
+ stdio: "inherit",
59
+ env,
60
+ });
61
+ }
62
+
33
63
  function build() {
34
- execSync(`vite build --config ${viteConfigPath}`, { stdio: "inherit" });
64
+ vite(["build", "--config", viteConfigPath]);
35
65
  if (hasAdminEntry()) {
36
- execSync(`vite build --config ${viteConfigPath}`, {
37
- stdio: "inherit",
38
- env: { ...process.env, CB_BUILD_TARGET: "admin" },
66
+ vite(["build", "--config", viteConfigPath], {
67
+ ...process.env,
68
+ CB_BUILD_TARGET: "admin",
39
69
  });
40
70
  }
41
71
  }
@@ -48,8 +78,7 @@ function build() {
48
78
  * involved, so the resolved path needs no quoting.
49
79
  */
50
80
  function typeCheck() {
51
- const require = createRequire(import.meta.url);
52
- const tsc = require.resolve("typescript/bin/tsc");
81
+ const tsc = binOf("typescript", "tsc");
53
82
  try {
54
83
  execFileSync(process.execPath, [tsc, "--noEmit", "-p", "."], {
55
84
  stdio: "inherit",
@@ -62,7 +91,7 @@ function typeCheck() {
62
91
  if (args.includes("dev")) {
63
92
  try {
64
93
  build();
65
- execSync(`vite --config ${viteConfigPath}`, { stdio: "inherit" });
94
+ vite(["--config", viteConfigPath]);
66
95
  } catch (error) {
67
96
  displayError("Failed to start commercebuild extension server:", error);
68
97
  }