Skip to content

Commit dff5acb

Browse files
authored
feat: allow --run to take any command (#219)
* feat: allow run to work with any command * chore: fix broken tests, remove obsolete tests * chore: remove unused import * feat: add help and readme
1 parent 0d759fe commit dff5acb

4 files changed

Lines changed: 29 additions & 36 deletions

File tree

readme.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,22 @@ To use SWA CLI with your local dev server, follow these two steps:
7070
swa start http://<APP_DEV_SERVER_HOST>:<APP_DEV_SERVER_PORT>
7171
```
7272

73+
Instead of starting a dev server separately, you can provide the startup command to the CLI.
74+
75+
```bash
76+
# npm start script (React)
77+
swa start http://localhost:3000 --run "npm start"
78+
79+
# dotnet watch (Blazor)
80+
swa start http://localhost:5000 --run "dotnet watch run"
81+
82+
# Jekyll
83+
swa start http://localhost:4000 --run "jekyll serve"
84+
85+
# custom script
86+
swa start http://localhost:4200 --run "./startup.sh"
87+
```
88+
7389
Here is a list of the default ports used by some popular dev servers:
7490

7591
| Tool | Port | Command |

src/cli/index.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ exports.run = async function () {
4343
.option("--ssl-cert <sslCertLocation>", "SSL certificate (.crt) to use for serving HTTPS", DEFAULT_CONFIG.sslCert)
4444
.option("--ssl-key <sslKeyLocation>", "SSL key (.key) to use for serving HTTPS", DEFAULT_CONFIG.sslKey)
4545

46-
.addOption(new Option("--run <startupScript>", "run a external program or npm/yarn script on startup").default(DEFAULT_CONFIG.run).hideHelp())
46+
.option("--run <startupScript>", "run a command at startup", DEFAULT_CONFIG.run)
4747

4848
.action(async (context: string = `.${path.sep}`, options: SWACLIConfig) => {
4949
options = {
@@ -67,9 +67,6 @@ exports.run = async function () {
6767
`
6868
Examples:
6969
70-
Serve static content from the current folder
71-
swa start
72-
7370
Serve static content from a specific folder
7471
swa start ./output-folder
7572
@@ -81,6 +78,9 @@ Examples:
8178
8279
Serve static content and run an API from another folder
8380
swa start ./output-folder --api ./api
81+
82+
Use a custom command to run framework development server at startup
83+
swa start http://localhost:3000 --run "npm start"
8484
`
8585
);
8686

src/core/utils/cli.spec.ts

Lines changed: 4 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -140,30 +140,10 @@ describe("createStartupScriptCommand()", () => {
140140
expect(cmd).toBe("/foo/script.sh");
141141
});
142142
});
143-
describe("non-valid use cases", () => {
144-
it("should handle non-valid npm patterns", () => {
145-
const cmd = createStartupScriptCommand("npm", {});
146-
expect(cmd).toBe(null);
147-
});
148-
it("should handle non-valid yarn patterns", () => {
149-
const cmd = createStartupScriptCommand("yarn", {});
150-
expect(cmd).toBe(null);
151-
});
152-
it("should handle non-valid npx patterns", () => {
153-
const cmd = createStartupScriptCommand("npx", {});
154-
expect(cmd).toBe(null);
155-
});
156-
it("should handle non-existant scripts (relative)", () => {
157-
const cmd = createStartupScriptCommand("script.sh", {});
158-
expect(cmd).toBe(null);
159-
});
160-
it("should handle non-existant scripts (asbolute)", () => {
161-
const cmd = createStartupScriptCommand("/foo/bar/script.sh", {});
162-
expect(cmd).toBe(null);
163-
});
164-
it("should handle non-existant scripts (asbolute)", () => {
165-
const cmd = createStartupScriptCommand(`"npm:µ˜¬…˚πº–ª¶§∞¢£¢™§_)(*!#˜%@)`, {});
166-
expect(cmd).toBe(null);
143+
describe("custom command", () => {
144+
it("should return custom command", () => {
145+
const cmd = createStartupScriptCommand("dotnet watch run", {});
146+
expect(cmd).toBe("dotnet watch run");
167147
});
168148
});
169149
});

src/core/utils/cli.ts

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import path from "path";
22
import fs from "fs";
3-
import { logger } from "./logger";
43

54
/**
65
* Parse process.argv and retrieve a specific flag value.
@@ -92,14 +91,12 @@ export function createStartupScriptCommand(startupScript: string, options: SWACL
9291
if (!path.isAbsolute(startupScript)) {
9392
const { appLocation } = options;
9493
const cwd = appLocation || process.cwd();
95-
startupScript = path.resolve(cwd, startupScript);
96-
}
97-
98-
if (fs.existsSync(startupScript)) {
99-
return startupScript;
100-
} else {
101-
logger.error(`Script file "${startupScript}" was not found.`, true);
94+
const absoluteStartupScript = path.resolve(cwd, startupScript);
95+
if (fs.existsSync(absoluteStartupScript)) {
96+
startupScript = absoluteStartupScript;
97+
}
10298
}
99+
return startupScript;
103100
}
104101
return null;
105102
}

0 commit comments

Comments
 (0)