Skip to content

Commit a0c35ae

Browse files
enhancement/issue 1518 split side effect from CLI entry point to dedicated bin file (#1520)
1 parent 4b04ea4 commit a0c35ae

File tree

170 files changed

+236
-229
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

170 files changed

+236
-229
lines changed

greenwood.config.test-types.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { Config, ExternalSourcePage } from "@greenwood/cli";
1+
import { type Config, type ExternalSourcePage, run } from "@greenwood/cli";
22
import { greenwoodPluginAdapterAws } from "@greenwood/plugin-adapter-aws";
33
import { greenwoodPluginAdapterVercel } from "@greenwood/plugin-adapter-vercel";
44
import { greenwoodPluginAdapterNetlify } from "@greenwood/plugin-adapter-netlify";
@@ -18,7 +18,8 @@ import { greenwoodPluginRendererPuppeteer } from "@greenwood/plugin-renderer-pup
1818
import { getContentByRoute } from "@greenwood/cli/src/data/client.js";
1919

2020
const foo = await getContentByRoute("foo");
21-
console.log(foo);
21+
22+
console.log({ run, foo });
2223

2324
import ChildrenQuery from "@greenwood/plugin-graphql/src/queries/children.gql";
2425
import CollectionQuery from "@greenwood/plugin-graphql/src/queries/collection.gql";

package.json

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
"repository": "https://github.com/ProjectEvergreen/greenwood",
66
"author": "Owen Buckley <[email protected]>",
77
"license": "MIT",
8-
"main": "./packages/cli/src/index.js",
98
"type": "module",
109
"workspaces": {
1110
"packages": [
@@ -17,9 +16,9 @@
1716
"lerna": "lerna",
1817
"clean": "rimraf --glob ./packages/**/.greenwood/** ./packages/**/public/** ./coverage",
1918
"clean:deps": "rimraf **/node_modules/**",
20-
"build": "cross-env __GWD_ROLLUP_MODE__=strict node . build",
21-
"serve": "node . serve",
22-
"develop": "node . develop",
19+
"build": "cross-env __GWD_ROLLUP_MODE__=strict greenwood build",
20+
"serve": "greenwood serve",
21+
"develop": "greenwood develop",
2322
"test": "cross-env BROWSERSLIST_IGNORE_OLD_DATA=true __GWD_ROLLUP_MODE__=strict NODE_NO_WARNINGS=1 c8 mocha --exclude \"./packages/**/test/cases/loaders-*/**\" --exclude \"./packages/**/test/cases/*.typescript*/**\" \"./packages/**/**/*.spec.js\"",
2423
"test:loaders": "cross-env BROWSERSLIST_IGNORE_OLD_DATA=true __GWD_ROLLUP_MODE__=strict NODE_NO_WARNINGS=1 node --import $(pwd)/test/test-register.js ./node_modules/mocha/bin/mocha --exclude \"./packages/**/test/cases/*.typescript*/**\" \"./packages/**/**/*.spec.js\"",
2524
"test:loaders:win": "cross-env BROWSERSLIST_IGNORE_OLD_DATA=true __GWD_ROLLUP_MODE__=strict NODE_NO_WARNINGS=1 node --import file:\\\\%cd%\\test\\test-register.js ./node_modules/mocha/bin/mocha --exclude \"./packages/init/test/cases/**\" --exclude \"./packages/**/test/cases/*.typescript*/**\" \"./packages/**/**/*.spec.js\"",

packages/cli/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
"./src/*": "./src/*"
3939
},
4040
"bin": {
41-
"greenwood": "./src/index.js"
41+
"greenwood": "./src/bin.js"
4242
},
4343
"files": [
4444
"src/"

packages/cli/src/bin.js

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#!/usr/bin/env node
2+
3+
import program from "commander";
4+
import { run } from "./index.js";
5+
6+
const greenwoodPackageJson = (
7+
await import(new URL("../package.json", import.meta.url), { with: { type: "json" } })
8+
).default;
9+
10+
let command = "";
11+
12+
console.info("-------------------------------------------------------");
13+
console.info(`Welcome to Greenwood (v${greenwoodPackageJson.version}) ♻️`);
14+
console.info("-------------------------------------------------------");
15+
16+
program
17+
.version(greenwoodPackageJson.version)
18+
.arguments("<script-mode>")
19+
.usage("<script-mode> [options]");
20+
21+
program
22+
.command("build")
23+
.description("Build a static site for production.")
24+
.action((cmd) => {
25+
command = cmd._name;
26+
});
27+
28+
program
29+
.command("develop")
30+
.description("Start a local development server.")
31+
.action((cmd) => {
32+
command = cmd._name;
33+
});
34+
35+
program
36+
.command("serve")
37+
.description("View a production build locally with a basic web server.")
38+
.action((cmd) => {
39+
command = cmd._name;
40+
});
41+
42+
program
43+
.command("eject")
44+
.option("-a, --all", "eject all configurations including babel, postcss, browserslistrc")
45+
.description("Eject greenwood configurations.")
46+
.action((cmd) => {
47+
command = cmd._name;
48+
});
49+
50+
program.parse(process.argv);
51+
52+
if (program.parse.length === 0) {
53+
program.help();
54+
}
55+
56+
run(command);

packages/cli/src/index.js

Lines changed: 3 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,6 @@
1-
#!/usr/bin/env node
2-
31
import { generateCompilation } from "./lifecycles/compile.js";
4-
import fs from "node:fs/promises";
5-
import program from "commander";
6-
7-
const greenwoodPackageJson = JSON.parse(
8-
await fs.readFile(new URL("../package.json", import.meta.url), "utf-8"),
9-
);
10-
let cmdOption = {};
11-
let command = "";
12-
13-
console.info("-------------------------------------------------------");
14-
console.info(`Welcome to Greenwood (v${greenwoodPackageJson.version}) ♻️`);
15-
console.info("-------------------------------------------------------");
16-
17-
program
18-
.version(greenwoodPackageJson.version)
19-
.arguments("<script-mode>")
20-
.usage("<script-mode> [options]");
21-
22-
program
23-
.command("build")
24-
.description("Build a static site for production.")
25-
.action((cmd) => {
26-
command = cmd._name;
27-
});
28-
29-
program
30-
.command("develop")
31-
.description("Start a local development server.")
32-
.action((cmd) => {
33-
command = cmd._name;
34-
});
352

36-
program
37-
.command("serve")
38-
.description("View a production build locally with a basic web server.")
39-
.action((cmd) => {
40-
command = cmd._name;
41-
});
42-
43-
program
44-
.command("eject")
45-
.option("-a, --all", "eject all configurations including babel, postcss, browserslistrc")
46-
.description("Eject greenwood configurations.")
47-
.action((cmd) => {
48-
command = cmd._name;
49-
cmdOption.all = cmd.all;
50-
});
51-
52-
program.parse(process.argv);
53-
54-
if (program.parse.length === 0) {
55-
program.help();
56-
}
57-
58-
const run = async () => {
3+
async function run(command) {
594
process.env.__GWD_COMMAND__ = command;
605

616
try {
@@ -92,6 +37,6 @@ const run = async () => {
9237
console.error(err);
9338
process.exit(1);
9439
}
95-
};
40+
}
9641

97-
run();
42+
export { run };

packages/cli/src/types/index.d.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,3 +57,9 @@ export type {
5757
GetLayout,
5858
GetFrontmatter,
5959
};
60+
61+
export type CLI_COMMAND = "develop" | "build" | "serve";
62+
63+
declare module "@greenwood/cli" {
64+
export const run: (CLI_COMMAND) => Promise<void>;
65+
}

packages/cli/test/cases/build.config.active-frontmatter/build.config.active-frontmatter.spec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ const expect = chai.expect;
3434

3535
describe("Build Greenwood With: ", function () {
3636
const LABEL = "Active Frontmatter";
37-
const cliPath = path.join(process.cwd(), "packages/cli/src/index.js");
37+
const cliPath = path.join(process.cwd(), "packages/cli/src/bin.js");
3838
const outputPath = fileURLToPath(new URL(".", import.meta.url));
3939
let runner;
4040

packages/cli/test/cases/build.config.default/build.config.default.spec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import { fileURLToPath } from "node:url";
2222

2323
describe("Build Greenwood With: ", function () {
2424
const LABEL = "Empty Configuration and Default Workspace";
25-
const cliPath = path.join(process.cwd(), "packages/cli/src/index.js");
25+
const cliPath = path.join(process.cwd(), "packages/cli/src/bin.js");
2626
const outputPath = fileURLToPath(new URL(".", import.meta.url));
2727
let runner;
2828

packages/cli/test/cases/build.config.error-dev-server-extensions/build.config.error-dev-server-extensions.spec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import { fileURLToPath } from "node:url";
2626
const expect = chai.expect;
2727

2828
describe("Build Greenwood With: ", function () {
29-
const cliPath = path.join(process.cwd(), "packages/cli/src/index.js");
29+
const cliPath = path.join(process.cwd(), "packages/cli/src/bin.js");
3030
const outputPath = fileURLToPath(new URL(".", import.meta.url));
3131
let runner;
3232

packages/cli/test/cases/build.config.error-dev-server-hud/build.config.error-dev-server-hud.spec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import { fileURLToPath } from "node:url";
2626
const expect = chai.expect;
2727

2828
describe("Build Greenwood With: ", function () {
29-
const cliPath = path.join(process.cwd(), "packages/cli/src/index.js");
29+
const cliPath = path.join(process.cwd(), "packages/cli/src/bin.js");
3030
const outputPath = fileURLToPath(new URL(".", import.meta.url));
3131
let runner;
3232

0 commit comments

Comments
 (0)