diff --git a/src/commands/list.ts b/src/commands/list.ts new file mode 100644 index 0000000..21d478c --- /dev/null +++ b/src/commands/list.ts @@ -0,0 +1,20 @@ +import { command } from 'cleye'; +import colors from 'picocolors'; + +import { getWorkspaces } from '../operations/get-workspaces'; + +export default command( + { + name: 'list', + }, + async () => { + const workspaces = await getWorkspaces(); + const msg = [ + `${colors.cyan('All workspaces')} (total: ${workspaces.length})`, + ...workspaces.map( + (w) => `${colors.white(w.workspace)} ${colors.dim(w.path)}` + ), + ].join('\n'); + console.log(msg); + } +); diff --git a/src/db.ts b/src/db.ts index 3c33452..ba80383 100644 --- a/src/db.ts +++ b/src/db.ts @@ -3,8 +3,8 @@ import { JSONFile } from 'lowdb/node'; import { codewHomeDir } from './config'; -type Workspace = { path: string; workspace: string }; -type Data = { +export type Workspace = { path: string; workspace: string }; +export type Data = { workspaces: Workspace[]; }; diff --git a/src/index.ts b/src/index.ts index 5af151e..f366fe1 100644 --- a/src/index.ts +++ b/src/index.ts @@ -4,38 +4,44 @@ import { log } from '@clack/prompts'; import { cli } from 'cleye'; import * as pkg from '../package.json'; +import list from './commands/list'; import { checkDir } from './operations/check-dir'; import { createWorkspace } from './operations/create-workspace'; import { getWorkspace } from './operations/get-workspace'; import { openWorkspace } from './operations/open-workspace'; -const argv = cli({ - name: 'codew', +cli( + { + name: 'codew', - version: pkg.version, + version: pkg.version, - parameters: [''], + parameters: [''], - flags: {}, + flags: {}, - help: { - description: pkg.description, - examples: ['codew .'], - }, -}); - -const path = argv._.path; + help: { + description: pkg.description, + examples: ['codew .'], + }, -try { - await checkDir(path); - - const workspace = await getWorkspace(path); - if (workspace) { - await openWorkspace(workspace); - } else { - const workspace = await createWorkspace(path); - await openWorkspace(workspace); + commands: [list], + }, + async (argv) => { + const path = argv._.path; + + try { + await checkDir(path); + + const workspace = await getWorkspace(path); + if (workspace) { + await openWorkspace(workspace); + } else { + const workspace = await createWorkspace(path); + await openWorkspace(workspace); + } + } catch (e) { + log.error(`${e}`); + } } -} catch (e) { - log.error(`${e}`); -} +); diff --git a/src/operations/get-workspaces.ts b/src/operations/get-workspaces.ts new file mode 100644 index 0000000..738ab4c --- /dev/null +++ b/src/operations/get-workspaces.ts @@ -0,0 +1,6 @@ +import { readWorkspaces, Workspace } from '../db'; +export async function getWorkspaces(): Promise> { + const workspaces = await readWorkspaces(); + + return workspaces; +} diff --git a/test/operations/get-workspaces.test.ts b/test/operations/get-workspaces.test.ts new file mode 100644 index 0000000..a9d4a56 --- /dev/null +++ b/test/operations/get-workspaces.test.ts @@ -0,0 +1,43 @@ +import { existsSync } from 'node:fs'; +import { mkdir, rm } from 'node:fs/promises'; +import { basename, resolve } from 'node:path'; + +import { afterEach, beforeEach, expect, test, vi } from 'vitest'; + +import { createWorkspace } from '../../src/operations/create-workspace'; +import { getWorkspaces } from '../../src/operations/get-workspaces'; + +const mocks = vi.hoisted(() => { + return { + testHomedir: () => resolve('test-tmp', basename(import.meta.url)), + }; +}); + +beforeEach(async () => { + if (existsSync(mocks.testHomedir())) { + await rm(mocks.testHomedir(), { recursive: true, force: true }); + } + await mkdir(mocks.testHomedir(), { recursive: true }); + + vi.mock('node:os', () => { + return { + homedir: () => mocks.testHomedir(), + }; + }); +}); + +afterEach(async () => { + vi.restoreAllMocks(); +}); + +test('getWorkspaces', async () => { + expect((await getWorkspaces()).length).toBe(0); + + await createWorkspace('dir-1'); + const workspaces = await getWorkspaces(); + expect(workspaces.length).toBe(1); + + expect(workspaces.at(0)?.workspace).toBe( + resolve(mocks.testHomedir(), '.codew/workspaces/dir-1.code-workspace') + ); +});