Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 8 additions & 7 deletions internal/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,17 +47,18 @@ func NewClient(
}

// UpdateDefaultProjectFiles should update any project specific files if any
func UpdateDefaultProjectFiles(fs afero.Fs, dirPath string, appDirName string) error {
func UpdateDefaultProjectFiles(fs afero.Fs, dirPath string, appDirName string, displayName string) error {
// Files and their corresponding app name replacement functions
projectFiles := []struct {
filename string
replacer func([]byte, string) []byte
name string
}{
{"manifest.json", regexReplaceAppNameInManifest},
{"manifest.js", regexReplaceAppNameInManifest},
{"manifest.ts", regexReplaceAppNameInManifest},
{"package.json", regexReplaceAppNameInPackageJSON},
{"pyproject.toml", regexReplaceAppNameInPyprojectToml},
{"manifest.json", regexReplaceAppNameInManifest, displayName},
{"manifest.js", regexReplaceAppNameInManifest, displayName},
{"manifest.ts", regexReplaceAppNameInManifest, displayName},
{"package.json", regexReplaceAppNameInPackageJSON, appDirName},
{"pyproject.toml", regexReplaceAppNameInPyprojectToml, appDirName},
}

for _, pf := range projectFiles {
Expand All @@ -67,7 +68,7 @@ func UpdateDefaultProjectFiles(fs afero.Fs, dirPath string, appDirName string) e
continue
}

fileData = pf.replacer(fileData, appDirName)
fileData = pf.replacer(fileData, pf.name)
if err := afero.WriteFile(fs, filePath, fileData, 0644); err != nil {
return err
}
Expand Down
43 changes: 21 additions & 22 deletions internal/app/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,14 @@ import (
func Test_App_UpdateDefaultProjectFiles(t *testing.T) {
tests := map[string]struct {
appDirName string
displayName string
existingFiles map[string]string
expectedFiles map[string]string
expectedErrorType error
}{
"manifest.json file exists": {
appDirName: "vibrant-butterfly-1234",
appDirName: "vibrant-butterfly-1234",
displayName: "Vibrant Butterfly 1234",
existingFiles: map[string]string{
"manifest.json": string(testdata.ManifestJSON),
},
Expand All @@ -43,7 +45,8 @@ func Test_App_UpdateDefaultProjectFiles(t *testing.T) {
expectedErrorType: nil,
},
"manifest.js file exists": {
appDirName: "vibrant-butterfly-1234",
appDirName: "vibrant-butterfly-1234",
displayName: "Vibrant Butterfly 1234",
existingFiles: map[string]string{
"manifest.js": string(testdata.ManifestJS),
},
Expand All @@ -53,7 +56,8 @@ func Test_App_UpdateDefaultProjectFiles(t *testing.T) {
expectedErrorType: nil,
},
"manifest.ts file exists": {
appDirName: "vibrant-butterfly-1234",
appDirName: "vibrant-butterfly-1234",
displayName: "Vibrant Butterfly 1234",
existingFiles: map[string]string{
"manifest.ts": string(testdata.ManifestTS),
},
Expand All @@ -63,7 +67,8 @@ func Test_App_UpdateDefaultProjectFiles(t *testing.T) {
expectedErrorType: nil,
},
"Multiple manifest files exist": {
appDirName: "vibrant-butterfly-1234",
appDirName: "vibrant-butterfly-1234",
displayName: "Vibrant Butterfly 1234",
existingFiles: map[string]string{
"manifest.json": string(testdata.ManifestJSON),
"manifest.ts": string(testdata.ManifestTS),
Expand All @@ -75,7 +80,8 @@ func Test_App_UpdateDefaultProjectFiles(t *testing.T) {
expectedErrorType: nil,
},
"package.json file exists": {
appDirName: "vibrant-butterfly-1234",
appDirName: "vibrant-butterfly-1234",
displayName: "Vibrant Butterfly 1234",
existingFiles: map[string]string{
"package.json": string(testdata.PackageJSON),
},
Expand All @@ -85,7 +91,8 @@ func Test_App_UpdateDefaultProjectFiles(t *testing.T) {
expectedErrorType: nil,
},
"pyproject.toml file exists": {
appDirName: "vibrant-butterfly-1234",
appDirName: "vibrant-butterfly-1234",
displayName: "Vibrant Butterfly 1234",
existingFiles: map[string]string{
"pyproject.toml": string(testdata.PyprojectTOML),
},
Expand All @@ -95,7 +102,8 @@ func Test_App_UpdateDefaultProjectFiles(t *testing.T) {
expectedErrorType: nil,
},
"Multiple project files exist": {
appDirName: "vibrant-butterfly-1234",
appDirName: "vibrant-butterfly-1234",
displayName: "Vibrant Butterfly 1234",
existingFiles: map[string]string{
"manifest.json": string(testdata.ManifestJSON),
"package.json": string(testdata.PackageJSON),
Expand All @@ -110,20 +118,11 @@ func Test_App_UpdateDefaultProjectFiles(t *testing.T) {
},
"No manifest files exist": {
appDirName: "vibrant-butterfly-1234",
displayName: "Vibrant Butterfly 1234",
existingFiles: map[string]string{},
expectedFiles: map[string]string{},
expectedErrorType: nil,
},
"WriteFile error": {
appDirName: "vibrant-butterfly-1234",
existingFiles: map[string]string{
"manifest.json": string(testdata.ManifestJSON),
},
expectedFiles: map[string]string{
"manifest.json": string(testdata.ManifestJSONAppName),
},
expectedErrorType: nil,
},
}

for name, tc := range tests {
Expand All @@ -146,7 +145,7 @@ func Test_App_UpdateDefaultProjectFiles(t *testing.T) {
}

// Run the tests
err := UpdateDefaultProjectFiles(fs, projectDirPath, tc.appDirName)
err := UpdateDefaultProjectFiles(fs, projectDirPath, tc.appDirName, tc.displayName)

// Assertions
require.IsType(t, err, tc.expectedErrorType)
Expand All @@ -169,22 +168,22 @@ func Test_RegexReplaceAppNameInManifest(t *testing.T) {
}{
"manifest.json is validate": {
src: testdata.ManifestJSON,
appName: "vibrant-butterfly-1234",
appName: "Vibrant Butterfly 1234",
expectedSrc: testdata.ManifestJSONAppName,
},
"manifest.js is validate": {
src: testdata.ManifestJS,
appName: "vibrant-butterfly-1234",
appName: "Vibrant Butterfly 1234",
expectedSrc: testdata.ManifestJSAppName,
},
"manifest.ts is validate": {
src: testdata.ManifestTS,
appName: "vibrant-butterfly-1234",
appName: "Vibrant Butterfly 1234",
expectedSrc: testdata.ManifestTSAppName,
},
"manifest.ts with sdk is validate": {
src: testdata.ManifestSDKTS,
appName: "vibrant-butterfly-1234",
appName: "Vibrant Butterfly 1234",
expectedSrc: testdata.ManifestSDKTSAppName,
},
}
Expand Down
2 changes: 1 addition & 1 deletion internal/pkg/create/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ func Create(ctx context.Context, clients *shared.ClientFactory, createArgs Creat
}()

// Update default project files' app name, bot name, etc
if err := app.UpdateDefaultProjectFiles(clients.Fs, projectDirPath, appDirName); err != nil {
if err := app.UpdateDefaultProjectFiles(clients.Fs, projectDirPath, appDirName, createArgs.AppName); err != nil {
return "", slackerror.Wrap(err, slackerror.ErrProjectFileUpdate)
}

Expand Down
4 changes: 2 additions & 2 deletions test/testdata/manifest-app-name.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ export default {
major_version: 2
},
display_information: {
name: 'vibrant-butterfly-1234'
name: 'Vibrant Butterfly 1234'
},
// This is a comment
features: {
Expand All @@ -13,7 +13,7 @@ export default {
messages_tab_read_only_enabled: false
},
bot_user: {
display_name: 'vibrant-butterfly-1234'
display_name: 'Vibrant Butterfly 1234'
}
},
functions: {
Expand Down
4 changes: 2 additions & 2 deletions test/testdata/manifest-app-name.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"major_version": 2
},
"display_information": {
"name": "vibrant-butterfly-1234"
"name": "Vibrant Butterfly 1234"
},
"features": {
"app_home": {
Expand All @@ -12,7 +12,7 @@
"messages_tab_read_only_enabled": false
},
"bot_user": {
"display_name": "vibrant-butterfly-1234"
"display_name": "Vibrant Butterfly 1234"
}
},
"functions": {
Expand Down
4 changes: 2 additions & 2 deletions test/testdata/manifest-app-name.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export default {
"major_version": 2
},
"display_information": {
"name": "vibrant-butterfly-1234"
"name": "Vibrant Butterfly 1234"
},
// This is a comment
"features": {
Expand All @@ -22,7 +22,7 @@ export default {
"messages_tab_read_only_enabled": false
},
"bot_user": {
"display_name": "vibrant-butterfly-1234"
"display_name": "Vibrant Butterfly 1234"
}
},
"functions": {
Expand Down
2 changes: 1 addition & 1 deletion test/testdata/manifest-sdk-app-name.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const obj = {
};

export default Manifest({
"name": "vibrant-butterfly-1234",
"name": "Vibrant Butterfly 1234",
"description": "Reverse a string",
// "runtime_environment": "slack",
"runtime": "deno1.x",
Expand Down
Loading