Open Project in VSCode Script #124
Replies: 8 comments 11 replies
-
Very cool and creative! In general, I recommend Before: const choice = await kitPrompt({
placeholder: 'Which project?',
choices: [
...(await getProjects('~/code')),
...(await getProjects('~/Desktop')),
],
}) After: const choice = await arg('Which project?', [
...(await getProjects('~/code')),
...(await getProjects('~/Desktop')),
]) Why
|
Beta Was this translation helpful? Give feedback.
-
It might also be interesting to read the package.json in each project for the description/name/hint/preview. I'll put together a demo for that |
Beta Was this translation helpful? Give feedback.
-
I have made a similar script that works for my structure. All my projects live in // Menu: Open VS Code Project
// Description: Open a VS Code project
// Shortcut: cmd shift .
const projectDirPath = "~/dev";
const projects = ls(projectDirPath).map((dir) => {
const fullPath = path.join(projectDirPath, dir);
return {
name: dir,
value: fullPath,
description: fullPath,
};
});
const project = await arg("Select project:", projects);
edit(project); |
Beta Was this translation helpful? Give feedback.
-
Awesome ! I made a few changes to reflect my use cases (I have different paths for work and personal projects), works like a charm 👌 // Menu: Open Project
// Description: Opens a project in code
async function getProjects(parentDir) {
const codeDir = await ls(parentDir)
const choices = []
for (const dir of codeDir) {
if (dir.includes("node_modules")) continue
const fullPath = path.join(parentDir, dir)
if (await isFile(path.join(fullPath, "package.json"))) {
choices.push({
name: dir,
value: fullPath,
description: fullPath,
})
} else {
choices.push(...(await getProjects(fullPath)))
}
}
return choices
}
const categories = [
{ name: "Pro", path: "~/Clients" },
{ name: "Training", path: "~/Dev/Formations" },
{ name: "Projects", path: "~/Dev/Projects" },
]
const categoryChoice = await arg(
"What are you working on ?",
categories.map((category) => category.name)
)
const category =
categories.find((item) => item.name === categoryChoice)?.path ||
categories[0].path
const choice = await arg("Which project?", [...(await getProjects(category))])
edit(choice) |
Beta Was this translation helpful? Give feedback.
-
Thanks. This works great. I just noticed that the options takes time to show on my setup. Maybe because some of the folders are really deep or contains lots of files. I only have maximum of 2 level deep for my projects, so I added a check to only go that deep.
|
Beta Was this translation helpful? Give feedback.
-
When I try to open my |
Beta Was this translation helpful? Give feedback.
-
Would there be any way to force vs code to be opened via a shell? If I open with |
Beta Was this translation helpful? Give feedback.
-
Hello, I came across this post and gave the code snippet a try. Unfortunately, it didn't work as expected. However, I was able to fix the issue by changing the directory reference from ~/ to using the home() command. Here's the updated code: Copy code
...(await getProjects(home("Documents", "path.."))),
...(await getProjects(home("Documents", "path.."))), I hope this helps anyone else who may encounter similar issues. Thank you for sharing this snippet! |
Beta Was this translation helpful? Give feedback.
-
As requested by John: https://twitter.com/johnlindquist/status/1381251960817418246
I have my projects in
~/code
and temporary projects in~/Desktop
. Sometimes they're in sub-directories as well. Every project has a root-levelpackage.json
, so I put together this script to quickly open a project in vscode:Works great!
Beta Was this translation helpful? Give feedback.
All reactions