Skip to content
Merged
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
20 changes: 20 additions & 0 deletions src/commands/list.ts
Original file line number Diff line number Diff line change
@@ -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);
}
);
4 changes: 2 additions & 2 deletions src/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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[];
};

Expand Down
54 changes: 30 additions & 24 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: ['<path>'],
parameters: ['<path>'],

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}`);
}
);
6 changes: 6 additions & 0 deletions src/operations/get-workspaces.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { readWorkspaces, Workspace } from '../db';
export async function getWorkspaces(): Promise<ReadonlyArray<Workspace>> {
const workspaces = await readWorkspaces();

return workspaces;
}
43 changes: 43 additions & 0 deletions test/operations/get-workspaces.test.ts
Original file line number Diff line number Diff line change
@@ -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')
);
});