Skip to content

Commit 47d3eba

Browse files
committed
feat: 🎸 handle strucutre generator edge cases
handle new Module creation for a lower structures;
1 parent 1e5ad45 commit 47d3eba

5 files changed

Lines changed: 69 additions & 0 deletions

File tree

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import Generator from "yeoman-generator";
2+
import {
3+
getModuleQuestion,
4+
isNewModule,
5+
moduleInputQuestion
6+
} from "../../src/fsModules";
7+
8+
export = class StructureInnerGenerator extends Generator {
9+
public async prompting() {
10+
let answer = await this.prompt(await getModuleQuestion());
11+
12+
if (isNewModule(answer.module)) {
13+
answer = await this.prompt(moduleInputQuestion);
14+
}
15+
16+
this.composeWith(require.resolve(`../structure-${this.options.task}`), {
17+
module: answer.module
18+
});
19+
}
20+
};

‎src/defaultConfig.ts‎

Whitespace-only changes.

‎src/fsModules.ts‎

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import { lstatSync, readdirSync } from "fs";
2+
import { ChoiceType, Question } from "inquirer";
3+
import { join } from "path";
4+
5+
const isDirectory = ({ path }: any) => lstatSync(path).isDirectory();
6+
7+
const getDirectories = (baseDir: any) =>
8+
readdirSync(baseDir)
9+
.map((dirName: string) => ({ path: join(baseDir, dirName), name: dirName }))
10+
.filter(isDirectory);
11+
12+
function getModules() {
13+
return getDirectories("./src");
14+
}
15+
16+
export async function getModulesToChoices(): Promise<any> {
17+
const modules = await getModules();
18+
19+
return modules
20+
.map((dir: any) => ({ name: dir.name, value: dir.name }))
21+
.concat({ name: "a new one", value: "new" });
22+
}
23+
24+
const NEW_MODULE = "new";
25+
26+
const modulesListQuestion = (modules: ChoiceType[]): Question => ({
27+
type: "list",
28+
name: "module",
29+
message: `In which Module do you want to put the Component`,
30+
choices: modules
31+
});
32+
33+
export const moduleInputQuestion: Question = {
34+
type: "input",
35+
name: "module",
36+
message: `What's the Module name to put the Component into?`,
37+
validate: (input: any) => !!input || "Provide a name"
38+
};
39+
40+
export async function getModuleQuestion() {
41+
const modules = await getModulesToChoices();
42+
43+
return modules.length ? modulesListQuestion(modules) : moduleInputQuestion;
44+
}
45+
46+
export function isNewModule(moduleName: string): boolean {
47+
return moduleName === NEW_MODULE;
48+
}

‎src/questions.ts‎

Whitespace-only changes.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const validateName = (input: any) => !!input || "Provide a name";

0 commit comments

Comments
 (0)