Skip to content

Commit

Permalink
feat: added initialisation flag for IntelliJ Deno Config (#2045)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>

* Update internal/init/init.go

Co-authored-by: Han Qiao <[email protected]>

* fix: deno config functions reduces

* chore: use utils function to write file

---------

Co-authored-by: Han Qiao <[email protected]>
Co-authored-by: Qiao Han <[email protected]>
  • Loading branch information
3 people authored Mar 19, 2024
1 parent 37feaff commit fc79fd2
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 17 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

# IDE
/.vscode
/.idea

# NPM
node_modules
Expand Down
16 changes: 11 additions & 5 deletions cmd/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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") + ".")
Expand All @@ -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)
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
22 changes: 20 additions & 2 deletions internal/init/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,22 @@ 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
//go:embed templates/.vscode/extensions.json
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 {
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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
}
40 changes: 33 additions & 7 deletions internal/init/init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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) {
Expand All @@ -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)
})
Expand All @@ -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)
})
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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) {
Expand Down
6 changes: 6 additions & 0 deletions internal/init/templates/.idea/deno.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit fc79fd2

Please sign in to comment.