Skip to content

Commit c87e791

Browse files
Allow users to pass the base and project main directory as options (#5)
1 parent 60a2e0d commit c87e791

File tree

3 files changed

+75
-8
lines changed

3 files changed

+75
-8
lines changed

README.md

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,12 +92,20 @@ Scaffold a new resource in your project.
9292

9393
```bash
9494
Options:
95-
--help Show help [boolean]
96-
--version Show version number [boolean]
97-
-r, --resource The name of the resource to scaffold [array] [required]
95+
--help Show help [boolean]
96+
--version Show version number [boolean]
97+
-r, --resource The name of the resource to scaffold [array] [default: []]
98+
-s, --service The name of the service to scaffold [array] [default: []]
99+
-p, --repository The name of the repository to scaffold [array] [default: []]
100+
-b, --base-dir The base directory for the project [string] [default: ""]
101+
-m, --main-dir The main directory for the project [string] [default: "src"]
98102

99103
Examples:
100104
scaffold -r product Scaffold a project with a single "product" domain
101105
scaffold -r product -r user Scaffold a project with two domains: "product" an
102106
d "user"
107+
scaffold -s product Scaffold a project with a single "product" servic
108+
e
109+
110+
For more information, visit the documentation.
103111
```

__tests__/unit/commands.test.ts

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ describe("Scaffold command", () => {
4242
.mockResolvedValue({ success: true });
4343
});
4444

45-
it("should scaffold a new resource", async () => {
45+
it("should scaffold a new resource in the default main directory", async () => {
4646
await new Promise((resolve, reject) => {
4747
parser.parse(
4848
`scaffold ${resources.map((resource) => `-r ${resource}`).join(" ")}`,
@@ -70,6 +70,40 @@ describe("Scaffold command", () => {
7070
}
7171
});
7272

73+
it("should scaffold a new resource in the user defined main directory", async () => {
74+
const customMainDirectory = "src/app";
75+
const customBaseDirectory = "/home/user/projects/my-app";
76+
77+
await new Promise((resolve, reject) => {
78+
parser.parse(
79+
`scaffold -m ${customMainDirectory} -b ${customBaseDirectory} ${resources
80+
.map((resource) => `-r ${resource}`)
81+
.join(" ")}`,
82+
{},
83+
(error, _, output) => {
84+
if (error) {
85+
reject(error);
86+
}
87+
resolve(output);
88+
},
89+
);
90+
});
91+
92+
expect(createLayersSpy).toHaveBeenCalledWith(
93+
customBaseDirectory,
94+
customMainDirectory,
95+
defaultLayers,
96+
);
97+
for (const resource of resources) {
98+
expect(createFilesSpy).toHaveBeenCalledWith(
99+
customBaseDirectory,
100+
customMainDirectory,
101+
defaultLayers,
102+
resource,
103+
);
104+
}
105+
});
106+
73107
it("should scaffold a new service", async () => {
74108
await new Promise((resolve, reject) => {
75109
parser.parse(

src/commands/scaffold.ts

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,15 @@ import { basePath, defaultLayers } from "../constants";
88
* @property {Array} resource The name of the resource to scaffold
99
* @property {Array} service The name of the service to scaffold
1010
* @property {Array} repository The name of the repository to scaffold
11+
* @property {string} baseDir The base directory for the project
12+
* @property {string} mainDir The main directory for the project
1113
*/
1214
type A = {
1315
resource: string[];
1416
service: string[];
1517
repository: string[];
18+
"base-dir": string;
19+
"main-dir": string;
1620
};
1721

1822
const command = "scaffold";
@@ -40,6 +44,18 @@ const builder = (yargs: yargs.Argv) =>
4044
description: "The name of the repository to scaffold",
4145
default: [],
4246
})
47+
.option("base-dir", {
48+
alias: "b",
49+
type: "string",
50+
description: "The base directory for the project",
51+
default: basePath,
52+
})
53+
.option("main-dir", {
54+
alias: "m",
55+
type: "string",
56+
description: "The main directory for the project",
57+
default: "src",
58+
})
4359
.example(
4460
"scaffold -r product",
4561
'Scaffold a project with a single "product" domain',
@@ -57,19 +73,28 @@ const builder = (yargs: yargs.Argv) =>
5773
const handler = async (args: yargs.ArgumentsCamelCase<A>): Promise<void> => {
5874
const defaultMainDirectory = await getDefaultMainDirectory();
5975

60-
await createLayersIfNotExists(basePath, defaultMainDirectory, defaultLayers);
76+
await createLayersIfNotExists(
77+
args["base-dir"],
78+
args["main-dir"],
79+
defaultLayers,
80+
);
6181

6282
const results = await Promise.all([
6383
...args.resource.map(async (resource) => {
6484
return createFiles(
65-
basePath,
66-
defaultMainDirectory,
85+
args["base-dir"],
86+
args["main-dir"],
6787
defaultLayers,
6888
resource,
6989
);
7090
}),
7191
...args.service.map(async (service) => {
72-
return createFiles(basePath, defaultMainDirectory, ["service"], service);
92+
return createFiles(
93+
args["base-dir"],
94+
args["main-dir"],
95+
["service"],
96+
service,
97+
);
7398
}),
7499
]);
75100

0 commit comments

Comments
 (0)