Skip to content

Commit 27a91e5

Browse files
committed
feat: add post generator
1 parent eefadd8 commit 27a91e5

File tree

7 files changed

+39
-4
lines changed

7 files changed

+39
-4
lines changed

src/actions/CreateIgnoreFileAction.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@ import { withCurrentDir, writeContentToFile } from "../process";
33
import { Action } from "./Action";
44

55
export class CreateIgnoreFileAction extends Action {
6-
constructor(private fileName: string) {
6+
constructor(private fileName: string, private content = ignoreFiles) {
77
super();
88
}
99

1010
async exec(): Promise<void> {
11-
await writeContentToFile(withCurrentDir(this.fileName), ignoreFiles);
11+
await writeContentToFile(withCurrentDir(this.fileName), this.content);
1212
}
1313
}

src/configs.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,12 @@ coverage
4141
te?mp
4242
`;
4343

44+
export const postIgnoreFiles = `
45+
# Editors
46+
.idea
47+
.vscode
48+
`;
49+
4450
export const flowConfigs = `
4551
[ignore]
4652
<PROJECT_ROOT>/node_modules/.*

src/generators/GeneratorFactory.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { GeneratorType, InquirerConfigs } from "../types";
44
import { ESLintGenerator } from "./ESLintGenerator";
55
import { FlowGenerator } from "./FlowGenerator";
66
import { Generator } from "./Generator";
7+
import { PostGenerator } from "./PostGenerator";
78
import { PrettierGenerator } from "./PrettierGenerator";
89
import { Yarn2Generator } from "./Yarn2Generator";
910

@@ -39,6 +40,10 @@ export class GeneratorFactory {
3940
return new FlowGenerator(userConfigs, formatter, packager);
4041
}
4142

43+
if (type === "post") {
44+
return new PostGenerator(userConfigs, formatter, packager);
45+
}
46+
4247
throw new Error(`"${type}" generator not implemented.`);
4348
}
4449
}

src/generators/PostGenerator.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { Action } from "../actions/Action";
2+
import { CreateIgnoreFileAction } from "../actions/CreateIgnoreFileAction";
3+
import { postIgnoreFiles } from "../configs";
4+
import { Formatter } from "../formatters/Formatter";
5+
import { Packager } from "../packagers/Packager";
6+
import { InquirerConfigs } from "../types";
7+
import { Generator } from "./Generator";
8+
9+
export class PostGenerator extends Generator {
10+
generateConfigs(userConfigs: InquirerConfigs): [object, string[]] {
11+
return [{}, []];
12+
}
13+
14+
initActions(
15+
configs: string,
16+
packages: string[],
17+
userConfigs: InquirerConfigs,
18+
formatter: Formatter,
19+
packager: Packager,
20+
): Action[] {
21+
return [new CreateIgnoreFileAction(".gitignore", postIgnoreFiles)];
22+
}
23+
}

src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ class CreateJavascriptProject extends Command {
2727
this.parse(CreateJavascriptProject);
2828
await checkPackageJson();
2929
const answers = await collectAnswers();
30+
answers.configurations.push("post");
3031

3132
if (answers.configurations.includes("flow") || answers.framework !== "none") {
3233
answers.module = "esm";

src/process.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ export async function writeContentToFile(file: string, content: string, flag = "
6565

6666
if (stats) {
6767
if (stats.isDirectory()) {
68-
throw new Error(`Cannot write content to ${file}`);
68+
throw new Error(`Cannot write content to a folder (${file})`);
6969
}
7070

7171
await writeFile(file, `\n${content.trim()}\n`, { flag });

src/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ export type Environment = "browser" | "node";
88

99
export type ConfigsFileFormat = "js" | "json" | "yml";
1010

11-
export type GeneratorType = "yarn2" | "eslint" | "prettier" | "flow";
11+
export type GeneratorType = "yarn2" | "eslint" | "prettier" | "flow" | "post";
1212

1313
export type PackagerType = "yarn" | "npm";
1414

0 commit comments

Comments
 (0)