From fc79fd2460cd91d25af1dbe69b96665a32d43c5b Mon Sep 17 00:00:00 2001 From: Yashas Majmudar <64674824+yashas-hm@users.noreply.github.com> Date: Tue, 19 Mar 2024 09:06:51 +0530 Subject: [PATCH] feat: added initialisation flag for IntelliJ Deno Config (#2045) * update: libraries update, gitignore update for intellij * feat: added flag --with-intellij-settings to create and insert settings for deno in IntelliJ IDEA * test: added test cases for intellij deno config * fix: lint issue * fix: go mod tidy * Update internal/init/templates/.idea/deno.xml Co-authored-by: Han Qiao * Update internal/init/init.go Co-authored-by: Han Qiao * fix: deno config functions reduces * chore: use utils function to write file --------- Co-authored-by: Han Qiao Co-authored-by: Qiao Han --- .gitignore | 1 + cmd/init.go | 16 +++++++---- go.mod | 2 +- go.sum | 4 +-- internal/init/init.go | 22 ++++++++++++-- internal/init/init_test.go | 40 +++++++++++++++++++++----- internal/init/templates/.idea/deno.xml | 6 ++++ 7 files changed, 74 insertions(+), 17 deletions(-) create mode 100644 internal/init/templates/.idea/deno.xml diff --git a/.gitignore b/.gitignore index f829b5144..e119d9725 100644 --- a/.gitignore +++ b/.gitignore @@ -21,6 +21,7 @@ # IDE /.vscode +/.idea # NPM node_modules diff --git a/cmd/init.go b/cmd/init.go index 511dda249..57b224e75 100644 --- a/cmd/init.go +++ b/cmd/init.go @@ -11,10 +11,10 @@ import ( ) var ( - createVscodeSettings = new(bool) - useOrioleDB bool - - initCmd = &cobra.Command{ + createVscodeSettings = new(bool) + useOrioleDB bool + createIntellijSettings = new(bool) + initCmd = &cobra.Command{ GroupID: groupLocalDev, Use: "init", Short: "Initialize a local project", @@ -35,7 +35,12 @@ var ( if !cmd.Flags().Changed("with-vscode-settings") && !cmd.Flags().Changed("with-vscode-workspace") { createVscodeSettings = nil } - return _init.Run(fsys, createVscodeSettings, useOrioleDB) + + if !cmd.Flags().Changed("with-intellij-settings") { + createIntellijSettings = nil + } + + return _init.Run(fsys, createVscodeSettings, createIntellijSettings, useOrioleDB) }, PostRun: func(cmd *cobra.Command, args []string) { fmt.Println("Finished " + utils.Aqua("supabase init") + ".") @@ -48,6 +53,7 @@ func init() { flags.BoolVar(createVscodeSettings, "with-vscode-workspace", false, "Generate VS Code workspace.") cobra.CheckErr(flags.MarkHidden("with-vscode-workspace")) flags.BoolVar(createVscodeSettings, "with-vscode-settings", false, "Generate VS Code settings for Deno.") + flags.BoolVar(createIntellijSettings, "with-intellij-settings", false, "Generate IntelliJ IDEA settings for Deno.") flags.BoolVar(&useOrioleDB, "use-orioledb", false, "Use OrioleDB storage engine for Postgres") rootCmd.AddCommand(initCmd) } diff --git a/go.mod b/go.mod index 47292d280..502f064cd 100644 --- a/go.mod +++ b/go.mod @@ -225,7 +225,7 @@ require ( github.com/matttproud/golang_protobuf_extensions v1.0.1 // indirect github.com/mbilski/exhaustivestruct v1.2.0 // indirect github.com/mgechev/revive v1.3.7 // indirect - github.com/microcosm-cc/bluemonday v1.0.25 // indirect + github.com/microcosm-cc/bluemonday v1.0.26 // indirect github.com/miekg/pkcs11 v1.1.1 // indirect github.com/mitchellh/go-homedir v1.1.0 // indirect github.com/mitchellh/hashstructure/v2 v2.0.2 // indirect diff --git a/go.sum b/go.sum index 5d3babdb4..0827aaade 100644 --- a/go.sum +++ b/go.sum @@ -711,8 +711,8 @@ github.com/mbilski/exhaustivestruct v1.2.0/go.mod h1:OeTBVxQWoEmB2J2JCHmXWPJ0aks github.com/mgechev/revive v1.3.7 h1:502QY0vQGe9KtYJ9FpxMz9rL+Fc/P13CI5POL4uHCcE= github.com/mgechev/revive v1.3.7/go.mod h1:RJ16jUbF0OWC3co/+XTxmFNgEpUPwnnA0BRllX2aDNA= github.com/microcosm-cc/bluemonday v1.0.21/go.mod h1:ytNkv4RrDrLJ2pqlsSI46O6IVXmZOBBD4SaJyDwwTkM= -github.com/microcosm-cc/bluemonday v1.0.25 h1:4NEwSfiJ+Wva0VxN5B8OwMicaJvD8r9tlJWm9rtloEg= -github.com/microcosm-cc/bluemonday v1.0.25/go.mod h1:ZIOjCQp1OrzBBPIJmfX4qDYFuhU02nx4bn030ixfHLE= +github.com/microcosm-cc/bluemonday v1.0.26 h1:xbqSvqzQMeEHCqMi64VAs4d8uy6Mequs3rQ0k/Khz58= +github.com/microcosm-cc/bluemonday v1.0.26/go.mod h1:JyzOCs9gkyQyjs+6h10UEVSe02CGwkhd72Xdqh78TWs= github.com/miekg/pkcs11 v1.0.2/go.mod h1:XsNlhZGX73bx86s2hdc/FuaLm2CPZJemRLMA+WTFxgs= github.com/miekg/pkcs11 v1.1.1 h1:Ugu9pdy6vAYku5DEpVWVFPYnzV+bxB+iRdbuFSu7TvU= github.com/miekg/pkcs11 v1.1.1/go.mod h1:XsNlhZGX73bx86s2hdc/FuaLm2CPZJemRLMA+WTFxgs= diff --git a/internal/init/init.go b/internal/init/init.go index f2b2e0a54..76161cf67 100644 --- a/internal/init/init.go +++ b/internal/init/init.go @@ -17,6 +17,8 @@ var ( vscodeDir = ".vscode" extensionsPath = filepath.Join(vscodeDir, "extensions.json") settingsPath = filepath.Join(vscodeDir, "settings.json") + intellijDir = ".idea" + denoPath = filepath.Join(intellijDir, "deno.xml") //go:embed templates/.gitignore initGitignore []byte @@ -24,11 +26,13 @@ var ( vscodeExtensions string //go:embed templates/.vscode/settings.json vscodeSettings string + //go:embed templates/.idea/deno.xml + intelliJDeno string errAlreadyInitialized = errors.Errorf("Project already initialized. Remove %s to reinitialize.", utils.Bold(utils.ConfigPath)) ) -func Run(fsys afero.Fs, createVscodeSettings *bool, useOrioleDB bool) error { +func Run(fsys afero.Fs, createVscodeSettings *bool, createIntellijSettings *bool, useOrioleDB bool) error { // Sanity checks. { if _, err := fsys.Stat(utils.ConfigPath); err == nil { @@ -60,12 +64,18 @@ func Run(fsys afero.Fs, createVscodeSettings *bool, useOrioleDB bool) error { if *createVscodeSettings { return writeVscodeConfig(fsys) } + } else if createIntellijSettings != nil { + if *createIntellijSettings { + return writeIntelliJConfig(fsys) + } } else { if isVscode := utils.PromptYesNo("Generate VS Code settings for Deno?", false, os.Stdin); isVscode { return writeVscodeConfig(fsys) } + if isIntelliJ := utils.PromptYesNo("Generate IntelliJ Settings for Deno?", false, os.Stdin); isIntelliJ { + return writeIntelliJConfig(fsys) + } } - return nil } @@ -156,3 +166,11 @@ func writeVscodeConfig(fsys afero.Fs) error { fmt.Println("Generated VS Code settings in " + utils.Bold(settingsPath) + ". Please install the recommended extension!") return nil } + +func writeIntelliJConfig(fsys afero.Fs) error { + if err := utils.WriteFile(denoPath, []byte(intelliJDeno), fsys); err != nil { + return err + } + fmt.Println("Generated IntelliJ settings in " + utils.Bold(denoPath) + ". Please install the Deno plugin!") + return nil +} diff --git a/internal/init/init_test.go b/internal/init/init_test.go index 7d3ceeb99..ec4a2a154 100644 --- a/internal/init/init_test.go +++ b/internal/init/init_test.go @@ -18,7 +18,7 @@ func TestInitCommand(t *testing.T) { fsys := &afero.MemMapFs{} require.NoError(t, fsys.Mkdir(".git", 0755)) // Run test - assert.NoError(t, Run(fsys, nil, false)) + assert.NoError(t, Run(fsys, nil, nil, false)) // Validate generated config.toml exists, err := afero.Exists(fsys, utils.ConfigPath) assert.NoError(t, err) @@ -38,6 +38,10 @@ func TestInitCommand(t *testing.T) { exists, err = afero.Exists(fsys, extensionsPath) assert.NoError(t, err) assert.False(t, exists) + // Validate intellij settings file isn't generated + exists, err = afero.Exists(fsys, denoPath) + assert.NoError(t, err) + assert.False(t, exists) }) t.Run("throws error when config file exists", func(t *testing.T) { @@ -46,14 +50,14 @@ func TestInitCommand(t *testing.T) { _, err := fsys.Create(utils.ConfigPath) require.NoError(t, err) // Run test - assert.Error(t, Run(fsys, nil, false)) + assert.Error(t, Run(fsys, nil, nil, false)) }) t.Run("throws error on permission denied", func(t *testing.T) { // Setup in-memory fs fsys := &fstest.StatErrorFs{DenyPath: utils.ConfigPath} // Run test - err := Run(fsys, nil, false) + err := Run(fsys, nil, nil, false) // Check error assert.ErrorIs(t, err, os.ErrPermission) }) @@ -62,14 +66,14 @@ func TestInitCommand(t *testing.T) { // Setup read-only fs fsys := afero.NewReadOnlyFs(afero.NewMemMapFs()) // Run test - assert.Error(t, Run(fsys, nil, false)) + assert.Error(t, Run(fsys, nil, nil, false)) }) t.Run("throws error on seed failure", func(t *testing.T) { // Setup in-memory fs fsys := &fstest.CreateErrorFs{DenyPath: utils.SeedDataPath} // Run test - err := Run(fsys, nil, false) + err := Run(fsys, nil, nil, false) // Check error assert.ErrorIs(t, err, os.ErrPermission) }) @@ -78,7 +82,7 @@ func TestInitCommand(t *testing.T) { // Setup in-memory fs fsys := &afero.MemMapFs{} // Run test - assert.NoError(t, Run(fsys, boolPointer(true), false)) + assert.NoError(t, Run(fsys, boolPointer(true), nil, false)) // Validate generated vscode settings exists, err := afero.Exists(fsys, settingsPath) assert.NoError(t, err) @@ -92,7 +96,7 @@ func TestInitCommand(t *testing.T) { // Setup in-memory fs fsys := &afero.MemMapFs{} // Run test - assert.NoError(t, Run(fsys, boolPointer(false), false)) + assert.NoError(t, Run(fsys, boolPointer(false), nil, false)) // Validate vscode settings file isn't generated exists, err := afero.Exists(fsys, settingsPath) assert.NoError(t, err) @@ -101,6 +105,28 @@ func TestInitCommand(t *testing.T) { assert.NoError(t, err) assert.False(t, exists) }) + + t.Run("creates intellij deno file", func(t *testing.T) { + // Setup in-memory fs + fsys := &afero.MemMapFs{} + // Run test + assert.NoError(t, Run(fsys, nil, boolPointer(true), false)) + // Validate generated intellij deno config + exists, err := afero.Exists(fsys, denoPath) + assert.NoError(t, err) + assert.True(t, exists) + }) + + t.Run("does not create intellij deno file", func(t *testing.T) { + // Setup in-memory fs + fsys := &afero.MemMapFs{} + // Run test + assert.NoError(t, Run(fsys, nil, boolPointer(false), false)) + // Validate intellij deno config file isn't generated + exists, err := afero.Exists(fsys, denoPath) + assert.NoError(t, err) + assert.False(t, exists) + }) } func TestUpdateGitIgnore(t *testing.T) { diff --git a/internal/init/templates/.idea/deno.xml b/internal/init/templates/.idea/deno.xml new file mode 100644 index 000000000..70e99b911 --- /dev/null +++ b/internal/init/templates/.idea/deno.xml @@ -0,0 +1,6 @@ + + + + +