@@ -4,33 +4,49 @@ import (
44 "bytes"
55
66 "github.com/pot-code/web-cli/internal/command"
7- "github.com/pot-code/web-cli/internal/shell "
7+ "github.com/pot-code/web-cli/internal/pkm "
88 "github.com/pot-code/web-cli/internal/task"
99 "github.com/pot-code/web-cli/templates"
1010 "github.com/urfave/cli/v2"
1111)
1212
1313type AddTypescriptConfig struct {
14- Target string `flag:"target" alias:"t" usage:"project target" validate:"required,oneof=node react"`
14+ Target string `flag:"target" alias:"t" usage:"project type" validate:"required,oneof=node react"`
15+ PackageManager string `flag:"pm" usage:"choose package manager" validate:"oneof=pnpm npm yarn"`
1516}
1617
1718var AddTypescriptCmd = command .NewCliCommand ("typescript" , "add typescript support" ,
18- new (AddTypescriptConfig ),
19+ & AddTypescriptConfig {
20+ PackageManager : "pnpm" ,
21+ },
1922 command .WithAlias ([]string {"ts" }),
2023).AddHandlers (
21- new (AddTypescriptToNode ),
22- new (AddTypescriptToReact ),
24+ new (AddTypescript ),
2325).BuildCommand ()
2426
25- type AddTypescriptToNode struct {}
27+ type AddTypescript struct {
28+ PackageManager string
29+ }
30+
31+ var _ command.CommandHandler = & AddTypescript {}
2632
27- var _ command.CommandHandler = & AddTypescriptToNode {}
33+ func (at * AddTypescript ) Handle (c * cli.Context , cfg interface {}) error {
34+ config := cfg .(* AddTypescriptConfig )
2835
29- func (arc * AddTypescriptToNode ) Cond (c * cli.Context ) bool {
30- return c .String ("target" ) == "node"
36+ at .PackageManager = config .PackageManager
37+
38+ if config .Target == "node" {
39+ return at .node ().Run ()
40+ }
41+ if config .Target == "react" {
42+ return at .react ().Run ()
43+ }
44+ return nil
3145}
3246
33- func (arc * AddTypescriptToNode ) Handle (c * cli.Context , cfg interface {}) error {
47+ func (at * AddTypescript ) node () task.Task {
48+ pm := pkm .NewPackageManager (at .PackageManager )
49+
3450 return task .NewParallelExecutor (
3551 []task.Task {
3652 & task.FileGenerator {
@@ -41,30 +57,26 @@ func (arc *AddTypescriptToNode) Handle(c *cli.Context, cfg interface{}) error {
4157 Name : "tsconfig.json" ,
4258 Data : bytes .NewBufferString (templates .NodeTsConfig ()),
4359 },
44- shell .YarnAddDev (
45- "typescript" ,
46- "eslint" ,
47- "@typescript-eslint/eslint-plugin" ,
48- "eslint-plugin-prettier" ,
49- "@typescript-eslint/parser" ,
50- "eslint-config-prettier" ,
51- "eslint-plugin-import" ,
52- "prettier" ,
53- "prettier-eslint" ,
60+ pm .InstallDev (
61+ []string {
62+ "typescript" ,
63+ "eslint" ,
64+ "@typescript-eslint/eslint-plugin" ,
65+ "eslint-plugin-prettier" ,
66+ "@typescript-eslint/parser" ,
67+ "eslint-config-prettier" ,
68+ "eslint-plugin-import" ,
69+ "prettier" ,
70+ "prettier-eslint" ,
71+ },
5472 ),
5573 },
56- ). Run ()
74+ )
5775}
5876
59- type AddTypescriptToReact struct {}
60-
61- var _ command.CommandHandler = & AddTypescriptToReact {}
62-
63- func (arc * AddTypescriptToReact ) Cond (c * cli.Context ) bool {
64- return c .String ("target" ) == "react"
65- }
77+ func (at * AddTypescript ) react () task.Task {
78+ pm := pkm .NewPackageManager (at .PackageManager )
6679
67- func (arc * AddTypescriptToReact ) Handle (c * cli.Context , cfg interface {}) error {
6880 return task .NewParallelExecutor (
6981 []task.Task {
7082 & task.FileGenerator {
@@ -75,23 +87,25 @@ func (arc *AddTypescriptToReact) Handle(c *cli.Context, cfg interface{}) error {
7587 Name : "tsconfig.json" ,
7688 Data : bytes .NewBufferString (templates .ReactTsConfig ()),
7789 },
78- shell .YarnAddDev (
79- "@types/react" ,
80- "@typescript-eslint/eslint-plugin" ,
81- "@typescript-eslint/parser" ,
82- "eslint" ,
83- "eslint-config-airbnb" ,
84- "eslint-config-prettier" ,
85- "eslint-import-resolver-typescript" ,
86- "eslint-plugin-import" ,
87- "eslint-plugin-jsx-a11y" ,
88- "eslint-plugin-prettier" ,
89- "eslint-plugin-react" ,
90- "eslint-plugin-react-hooks" ,
91- "prettier" ,
92- "prettier-eslint" ,
93- "typescript" ,
90+ pm .InstallDev (
91+ []string {
92+ "@types/react" ,
93+ "@typescript-eslint/eslint-plugin" ,
94+ "@typescript-eslint/parser" ,
95+ "eslint" ,
96+ "eslint-config-airbnb" ,
97+ "eslint-config-prettier" ,
98+ "eslint-import-resolver-typescript" ,
99+ "eslint-plugin-import" ,
100+ "eslint-plugin-jsx-a11y" ,
101+ "eslint-plugin-prettier" ,
102+ "eslint-plugin-react" ,
103+ "eslint-plugin-react-hooks" ,
104+ "prettier" ,
105+ "prettier-eslint" ,
106+ "typescript" ,
107+ },
94108 ),
95109 },
96- ). Run ()
110+ )
97111}
0 commit comments