create-vanilla-vite 1.1.0 → 1.2.0

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.
@@ -1,28 +1,73 @@
1
- import { readFile } from "node:fs/promises";
2
- import { join } from "node:path";
1
+ import { readFile, writeFile } from "node:fs/promises";
2
+ import { existsSync } from "node:fs";
3
+ import { join, relative } from "node:path";
3
4
  import { fileURLToPath } from "node:url";
4
5
 
5
6
  const root = fileURLToPath(new URL("../", import.meta.url));
6
7
  const project = JSON.parse(await readFile(join(root, "package.json"), "utf8"));
8
+ const installStatePath = join(root, ".install-state");
9
+ if (process.argv.includes("--complete")) {
10
+ await writeFile(installStatePath, "installed\n");
11
+ process.exit(0);
12
+ }
13
+ const workspaces = await Promise.all(
14
+ project.workspaces.map(async (workspace) => ({
15
+ path: join(root, workspace),
16
+ manifest: JSON.parse(await readFile(join(root, workspace, "package.json"), "utf8")),
17
+ })),
18
+ );
19
+ const declaredDependencies = [
20
+ ...Object.keys(project.dependencies ?? {}),
21
+ ...Object.keys(project.devDependencies ?? {}),
22
+ ...workspaces.flatMap(({ manifest }) => [
23
+ ...Object.keys(manifest.dependencies ?? {}),
24
+ ...Object.keys(manifest.devDependencies ?? {}),
25
+ ]),
26
+ ];
27
+ const hasInstalledDependencies = declaredDependencies.some(
28
+ (dependency) =>
29
+ existsSync(join(root, "node_modules", dependency)) ||
30
+ workspaces.some(({ path }) => existsSync(join(path, "node_modules", dependency))),
31
+ );
32
+ let installState;
33
+ try {
34
+ installState = (await readFile(installStatePath, "utf8")).trim();
35
+ } catch (error) {
36
+ if (error.code !== "ENOENT") throw error;
37
+ }
38
+ const freshInstall = installState === undefined && process.env.npm_command === "install";
7
39
 
8
- if (process.env.VANILLA_VITE_ALLOW_INSTALL !== "1") {
40
+ if (
41
+ process.env.VANILLA_VITE_ALLOW_INSTALL !== "1" &&
42
+ process.env.CI === undefined &&
43
+ process.env.npm_command !== "ci" &&
44
+ !freshInstall &&
45
+ (installState === "installing" || installState === "installed" || hasInstalledDependencies)
46
+ ) {
9
47
  console.error(
10
48
  "\nDirect installs are disabled to keep dependencies in the intended workspace.\n" +
11
- "Use `pnpm adD <package>` or `pnpm add-dependency <package>`; with npm, use `npm run adD -- <package>` or `npm run add-dependency -- <package>`.",
49
+ "Use `pnpm adD <package>` or `pnpm add-dependency <package>`; with npm, use `npm run adD -- <package>` or `npm run add-dependency -- <package>`.\n" +
50
+ "The adD commands are the supported way to add packages.",
12
51
  );
13
52
  process.exit(1);
14
53
  }
15
54
 
55
+ if (
56
+ process.env.VANILLA_VITE_ALLOW_INSTALL === "1" ||
57
+ process.env.CI !== undefined ||
58
+ process.env.npm_command === "ci" ||
59
+ freshInstall
60
+ ) {
61
+ await writeFile(installStatePath, "installing\n");
62
+ }
63
+
16
64
  console.log("Installing workspace dependencies:");
17
- for (const workspace of project.workspaces) {
18
- const manifest = JSON.parse(
19
- await readFile(join(root, workspace, "package.json"), "utf8"),
20
- );
65
+ for (const { path, manifest } of workspaces) {
21
66
  const dependencies = {
22
67
  ...manifest.dependencies,
23
68
  ...manifest.devDependencies,
24
69
  };
25
- console.log(`\n${manifest.name} (${workspace}):`);
70
+ console.log(`\n${manifest.name} (${relative(root, path)}):`);
26
71
  if (!Object.keys(dependencies).length) {
27
72
  console.log(" No dependencies declared.");
28
73
  continue;
@@ -11,7 +11,7 @@ const { workspaces } = JSON.parse(
11
11
  const workspace = workspaces.find((name) => name.endsWith(`-${app}`));
12
12
 
13
13
  if (!workspace || !["client", "server"].includes(app)) {
14
- console.error("Usage: npm|pnpm client|server [ -dev | add <package...> ]");
14
+ console.error("Usage: npm|pnpm client|server [dev|-dev]");
15
15
  process.exit(1);
16
16
  }
17
17
 
@@ -19,19 +19,14 @@ const packageManager = process.env.npm_config_user_agent?.startsWith("pnpm/")
19
19
  ? "pnpm"
20
20
  : "npm";
21
21
  let commandArgs;
22
- if (args[0] === "add" && args.length > 1) {
23
- commandArgs =
24
- packageManager === "pnpm"
25
- ? ["--filter", workspace, "add", ...args.slice(1)]
26
- : ["install", ...args.slice(1), "--workspace", workspace];
27
- } else if (args.length === 0 || (args.length === 1 && args[0] === "-dev")) {
28
- const script = args[0] === "-dev" ? "dev" : "start";
22
+ if (args.length === 0 || (args.length === 1 && ["dev", "-dev"].includes(args[0]))) {
23
+ const script = args.length === 1 ? "dev" : "start";
29
24
  commandArgs =
30
25
  packageManager === "pnpm"
31
26
  ? ["--filter", workspace, "run", script]
32
27
  : ["run", script, "--workspace", workspace];
33
28
  } else {
34
- console.error(`Usage: ${packageManager} ${app} [ -dev | add <package...> ]`);
29
+ console.error(`Usage: ${packageManager} ${app} [dev|-dev]`);
35
30
  process.exit(1);
36
31
  }
37
32