// Preview: docs // Menu: Open Project // Description: Opens a project in vscode // Shortcut: command shift . import '@johnlindquist/kit' import { rmdirSync, existsSync } from 'fs' const envPath = await env('PROJECT_DIR') const projectDir = home(envPath) const isGit = (input: string | Record<string, string>) => typeof input == 'string' ? input.includes('github.com') : false const unfilteredProjectList = await readdir(projectDir) const projectList = unfilteredProjectList.filter( (value: string) => !value.includes('.') ) let { projects, write } = await db('projects', { projects: projectList }) projects.sort((a: string, b: string) => { if (a < b) { return -1 } if (a > b) { return 1 } return 0 }) projects.forEach(async (val: string) => { if (val.includes('.')) { projects.pop(val) await write() } }) projectList.forEach(async (value: string) => { if (!projects.includes(value)) { projects.push(value) await write() } }) async function addExistingF (input: string) { const pathToFolder = await selectFolder() const dirname = path.basename(pathToFolder) const pathToDir = path.resolve(projectDir, dirname) const dirExists = existsSync(pathToDir) if (dirExists) { projects.push(input) await write() await submit('exit') } } async function gitCloneRepo (input: string, state: any) { if (isGit(input)) { await term({ command: `git clone ${input}`, cwd: projectDir, shortcuts: [ { name: 'Exit', key: `${cmd}+w`, bar: 'right', onPress: async () => { await mainScript() } }, { name: 'Back to Main Menu', key: `${cmd}+enter`, bar: 'right', onPress: async () => { await submit('exit') } } ] }) } else { throw new Error('Not a git repository!') } } async function removeProjectActionHandler (input: string, state: any) { let project = state?.focused?.value rmdirSync(path.resolve(projectDir, project), { recursive: true }) let indexOfProject = projects.indexOf(project) projects.splice(indexOfProject, 1) await write() await submit('exit') } async function purgeFromList (input: string, state: any) { let project = state?.focused?.value let indexOfProject = projects.indexOf(project) projects.splice(indexOfProject, 1) await write() await submit('exit') } async function createF (input: string, state: any) { const dir = path.resolve(projectDir, input) const exists = await isDir(dir) if (exists) { return `${input} already exists` } else { const { code, stderr, stdout } = mkdir(dir) if (stderr) { throw stderr } } const initorNot = await arg('Create git repo?', ['yes', 'no']) if (initorNot === 'yes') { cd(dir) execLog('git init') } projects.push(input) await write() edit('', dir) } const specialChoices = [ { miss: true, name: 'Clone Git Repository', onSubmit: gitCloneRepo }, { miss: true, name: 'Add folder to list', onSubmit: addExistingF }, { miss: true, name: 'Create Project folder', onSubmit: createF } ] while (true) { const projList = projects.map(project => project.split('\\').pop()) const previewList = projList.concat(specialChoices) let project = await arg( { placeholder: 'Projects', actions: [ { name: 'Remove Project', onAction: removeProjectActionHandler }, { name: 'Remove from List', onAction: purgeFromList } ] }, previewList ) const isExistingProject = projects.filter((e: string) => e.includes(project)).length !== 0 if (project !== 'exit') { if (isExistingProject) { edit('', path.resolve(projectDir, project)) } } }