@blinkk/create-root 3.5.3 → 4.0.0-alpha.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.
Files changed (2) hide show
  1. package/dist/create-root.js +130 -17
  2. package/package.json +1 -1
@@ -1,40 +1,142 @@
1
1
  #!/usr/bin/env node
2
2
 
3
3
  // src/create-root.ts
4
- import fs from "node:fs";
4
+ import fs2 from "node:fs";
5
5
  import { createRequire } from "node:module";
6
- import path from "node:path";
6
+ import path2 from "node:path";
7
7
  import { fileURLToPath } from "node:url";
8
8
  import { Command } from "commander";
9
9
  import degit from "degit";
10
10
  import { bgGreen, black } from "kleur/colors";
11
+ import { dim as dim2 } from "kleur/colors";
12
+
13
+ // src/gcp-setup.ts
14
+ import { spawn } from "node:child_process";
15
+ import fs from "node:fs";
16
+ import path from "node:path";
17
+ import readline from "node:readline/promises";
11
18
  import { dim } from "kleur/colors";
12
- var __dirname = path.dirname(fileURLToPath(import.meta.url));
19
+ async function maybeRunGcpSetup(outputDir, options) {
20
+ if (options.skip || !process.stdin.isTTY || !await usesRootCms(outputDir)) {
21
+ return false;
22
+ }
23
+ console.log();
24
+ const ok = await confirm(
25
+ "This template uses Root CMS, which needs a Google Cloud project. Set one up now?"
26
+ );
27
+ if (!ok) {
28
+ console.log(
29
+ dim(
30
+ `\u2503 Run \`${options.packageManager} exec root-cms setup\` when you're ready.`
31
+ )
32
+ );
33
+ return false;
34
+ }
35
+ const pm = options.packageManager;
36
+ try {
37
+ console.log(dim(`\u2503 running ${pm} install...`));
38
+ await run(pm, ["install"], outputDir);
39
+ await run(pm, ["exec", "root-cms", "setup"], outputDir);
40
+ return true;
41
+ } catch (err) {
42
+ console.log();
43
+ console.log(`Google Cloud setup didn't finish: ${err.message}`);
44
+ console.log(
45
+ dim(
46
+ `\u2503 Run \`${pm} exec root-cms setup\` in the project to pick up where it left off.`
47
+ )
48
+ );
49
+ return false;
50
+ }
51
+ }
52
+ function detectPackageManager() {
53
+ const agent = process.env.npm_config_user_agent || "";
54
+ if (agent.startsWith("yarn")) {
55
+ return "yarn";
56
+ }
57
+ if (agent.startsWith("npm")) {
58
+ return "npm";
59
+ }
60
+ return "pnpm";
61
+ }
62
+ async function usesRootCms(dir) {
63
+ try {
64
+ const pkg = JSON.parse(
65
+ await fs.promises.readFile(path.join(dir, "package.json"), "utf8")
66
+ );
67
+ return Boolean(
68
+ pkg.dependencies?.["@blinkk/root-cms"] || pkg.devDependencies?.["@blinkk/root-cms"]
69
+ );
70
+ } catch {
71
+ return false;
72
+ }
73
+ }
74
+ async function confirm(question) {
75
+ const rl = readline.createInterface({
76
+ input: process.stdin,
77
+ output: process.stdout
78
+ });
79
+ try {
80
+ const answer = (await rl.question(`${question} (Y/n) `)).trim().toLowerCase();
81
+ return answer === "" || answer === "y" || answer === "yes";
82
+ } finally {
83
+ rl.close();
84
+ }
85
+ }
86
+ function run(cmd, args, cwd) {
87
+ return new Promise((resolve, reject) => {
88
+ const child = spawn(cmd, args, {
89
+ cwd,
90
+ stdio: "inherit",
91
+ // Package managers are .cmd shims on Windows.
92
+ shell: process.platform === "win32"
93
+ });
94
+ child.on("error", reject);
95
+ child.on("close", (code) => {
96
+ if (code === 0) {
97
+ resolve();
98
+ } else {
99
+ reject(new Error(`${cmd} ${args.join(" ")} exited with code ${code}`));
100
+ }
101
+ });
102
+ });
103
+ }
104
+
105
+ // src/create-root.ts
106
+ var __dirname = path2.dirname(fileURLToPath(import.meta.url));
13
107
  var require2 = createRequire(import.meta.url);
14
- var packageJson = require2(path.join(__dirname, "../package.json"));
108
+ var packageJson = require2(path2.join(__dirname, "../package.json"));
15
109
  var ROOT_VERSION = packageJson.version;
16
110
  async function main() {
17
111
  console.log(`\u{1F331} ${bgGreen(black(" root.js "))} v${ROOT_VERSION}`);
18
112
  const program = new Command("create-root");
19
113
  program.version(packageJson.version);
20
114
  program.argument("[dir]", "output dir");
21
- program.option("--template [template]", "template to use", "starter");
115
+ program.option(
116
+ "--template [template]",
117
+ 'template to use: "starter" (Root CMS) or "minimal" (hello world)',
118
+ "starter"
119
+ );
22
120
  program.option(
23
121
  "--repo [repo]",
24
122
  "github repo to pull from",
25
123
  "blinkk/rootjs/examples"
26
124
  );
125
+ program.option(
126
+ "--skip-gcp-setup",
127
+ "don't offer to set up Google Cloud for Root CMS templates"
128
+ );
27
129
  program.action(async (dir, options) => {
28
130
  if (!dir) {
29
131
  dir = process.cwd();
30
132
  }
31
- const outputDir = path.resolve(dir || ".");
133
+ const outputDir = path2.resolve(dir || ".");
32
134
  const repo = options.repo || "blinkk/rootjs/examples";
33
135
  const template = options.template || "starter";
34
- const githubPath = path.join(repo, template);
136
+ const githubPath = path2.join(repo, template);
35
137
  console.log();
36
- console.log(`${dim("\u2503")} github path: ${githubPath}`);
37
- console.log(`${dim("\u2503")} output dir: ${maybeRelativePath(outputDir)}`);
138
+ console.log(`${dim2("\u2503")} github path: ${githubPath}`);
139
+ console.log(`${dim2("\u2503")} output dir: ${maybeRelativePath(outputDir)}`);
38
140
  console.log();
39
141
  const emitter = degit(githubPath, {
40
142
  cache: false,
@@ -42,20 +144,31 @@ async function main() {
42
144
  verbose: false
43
145
  });
44
146
  await emitter.clone(outputDir);
45
- await updatePackageJson(path.join(outputDir, "package.json"));
46
- console.log("done!");
47
- console.log("next: run `pnpm install` or `yarn install`");
147
+ await updatePackageJson(path2.join(outputDir, "package.json"));
148
+ const ranSetup = await maybeRunGcpSetup(outputDir, {
149
+ skip: options.skipGcpSetup,
150
+ packageManager: detectPackageManager()
151
+ });
152
+ console.log("done! next steps:");
153
+ console.log();
154
+ if (outputDir !== process.cwd()) {
155
+ console.log(` cd ${maybeRelativePath(outputDir)}`);
156
+ }
157
+ if (!ranSetup) {
158
+ console.log(" pnpm install");
159
+ }
160
+ console.log(" pnpm dev");
48
161
  });
49
162
  await program.parseAsync(process.argv);
50
163
  }
51
164
  async function updatePackageJson(packageJsonPath) {
52
- const str = await fs.promises.readFile(packageJsonPath, "utf-8");
165
+ const str = await fs2.promises.readFile(packageJsonPath, "utf-8");
53
166
  const packageJson2 = JSON.parse(str);
54
- packageJson2.name = path.basename(path.dirname(packageJsonPath));
167
+ packageJson2.name = path2.basename(path2.dirname(packageJsonPath));
55
168
  packageJson2.version = "1.0.0";
56
169
  updateWorkspaceDeps(packageJson2.dependencies);
57
170
  updateWorkspaceDeps(packageJson2.peerDependencies);
58
- await fs.promises.writeFile(
171
+ await fs2.promises.writeFile(
59
172
  packageJsonPath,
60
173
  JSON.stringify(packageJson2, null, 2),
61
174
  "utf-8"
@@ -73,9 +186,9 @@ function updateWorkspaceDeps(deps) {
73
186
  }
74
187
  function maybeRelativePath(filePath) {
75
188
  const cwd = process.cwd();
76
- const fullPath = path.resolve(filePath);
189
+ const fullPath = path2.resolve(filePath);
77
190
  if (fullPath.startsWith(cwd)) {
78
- return `./${path.relative(cwd, fullPath)}`;
191
+ return `./${path2.relative(cwd, fullPath)}`;
79
192
  }
80
193
  return fullPath;
81
194
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@blinkk/create-root",
3
- "version": "3.5.3",
3
+ "version": "4.0.0-alpha.0",
4
4
  "description": "",
5
5
  "author": "s@blinkk.com",
6
6
  "license": "MIT",