Skip to content

Commit 6610083

Browse files
ImCesarJessicaSachstgriesser
authored
fix: drag and drop to be correct directory (#18400)
Co-authored-by: Jessica Sachs <jess@jessicasachs.io> Co-authored-by: Tim Griesser <tgriesser10@gmail.com>
1 parent bda7e5e commit 6610083

File tree

5 files changed

+41
-26
lines changed

5 files changed

+41
-26
lines changed

packages/data-context/src/actions/ProjectActions.ts

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -110,21 +110,33 @@ export class ProjectActions {
110110
}
111111

112112
async addProject (args: MutationAddProjectArgs) {
113-
const dirStat = await this.ctx.fs.stat(args.path)
113+
const projectRoot = await this.getDirectoryPath(args.path)
114114

115-
if (!dirStat.isDirectory()) {
116-
throw new Error(`Cannot add ${args.path} as a project, it is not a directory`)
117-
}
118-
119-
const found = this.projects.find((x) => x.projectRoot === args.path)
115+
const found = this.projects.find((x) => x.projectRoot === projectRoot)
120116

121117
if (!found) {
122-
this.projects.push({ projectRoot: args.path })
123-
this.api.insertProjectToCache(args.path)
118+
this.projects.push({ projectRoot })
119+
this.api.insertProjectToCache(projectRoot)
124120
}
125121

126122
if (args.open) {
127-
await this.setActiveProject(args.path)
123+
await this.setActiveProject(projectRoot)
124+
}
125+
}
126+
127+
private async getDirectoryPath (projectRoot: string) {
128+
try {
129+
const { dir, base } = path.parse(projectRoot)
130+
const fullPath = path.join(dir, base)
131+
const dirStat = await this.ctx.fs.stat(fullPath)
132+
133+
if (dirStat.isDirectory()) {
134+
return fullPath
135+
}
136+
137+
return dir
138+
} catch (exception) {
139+
throw Error(`Cannot add ${projectRoot} to projects as it does not exist in the file system`)
128140
}
129141
}
130142

packages/frontend-shared/src/locales/en-US.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@
8686
"empty": {
8787
"title": "Welcome to Cypress!",
8888
"helper": "Add your first project below to start testing with Cypress.",
89-
"dropText": "Drag your project here or {0}",
89+
"dropText": "Drag your project directory here or {0}",
9090
"browseManually": "browse manually."
9191
},
9292
"searchPlaceholder": "Search Projects",

packages/launchpad/src/global/GlobalEmpty.vue

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@
4747
</template>
4848

4949
<script lang="ts" setup>
50-
import { getDirectoryPath } from '../utils/getDirectoryPath'
5150
import { useI18n } from '@cy/i18n'
5251
import IconPlaceholder from '~icons/icons8/circle-thin'
5352
import { FileSelector, Dropzone } from 'vue3-file-selector'
@@ -62,9 +61,12 @@ const emits = defineEmits<{
6261
(e: 'addProject', value: string): void
6362
}>()
6463
64+
type WebkitFile = File & { path: string }
6565
watch(files, (newVal) => {
6666
if (newVal.length) {
67-
emits('addProject', getDirectoryPath(newVal as FileList))
67+
const file = newVal[0] as WebkitFile
68+
69+
emits('addProject', file.path)
6870
}
6971
})
7072

packages/launchpad/src/global/GlobalPageHeader.vue

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727

2828
<script lang="ts" setup>
2929
import { ref } from 'vue'
30-
import { getDirectoryPath } from '../utils/getDirectoryPath'
3130
import Button from '@cy/components/Button.vue'
3231
import Input from '@cy/components/Input.vue'
3332
import IconPlus from '~icons/mdi/plus'
@@ -52,9 +51,22 @@ function handleButtonClick () {
5251
5352
function handleFileSelection (e: Event) {
5453
const target = e.target as HTMLInputElement
55-
const dirPath = getDirectoryPath(target.files)
54+
const files = target.files
55+
const path = getFilePath(files)
5656
57-
emits('addProject', dirPath)
57+
emits('addProject', path)
58+
}
59+
60+
type WebkitFile = File & { path: string }
61+
function getFilePath (files: FileList | null) {
62+
if (files) {
63+
const file = files[0] as WebkitFile
64+
const path = file?.path ?? ''
65+
66+
return path
67+
}
68+
69+
return ''
5870
}
5971
6072
const localValue = useModelWrapper(props, emits, 'modelValue')

packages/launchpad/src/utils/getDirectoryPath.ts

Lines changed: 0 additions & 11 deletions
This file was deleted.

0 commit comments

Comments
 (0)