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
34 changes: 34 additions & 0 deletions src/commands/clean.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { cancel, confirm, isCancel, log, outro } from '@clack/prompts';
import { command } from 'cleye';
import colors from 'picocolors';

import { cleanWorkspaces } from '../operations/clean-workspaces';

export default command(
{
name: 'clean',

help: {
description: 'Clean all workspaces codew stored',
},
},
async () => {
try {
const confirmed = await confirm({
message: `Are you sure to clean all workspaces?`,
initialValue: false,
});
if (!confirmed || isCancel(confirmed)) {
cancel(`Cancelled`);
process.exit(0);
}

await cleanWorkspaces();

outro(colors.cyan('✔ Successfully cleaned.'));
process.exit(0);
} catch (e) {
log.error(`${e}`);
}
}
);
6 changes: 6 additions & 0 deletions src/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,9 @@ export async function insertWorkspace(workspace: Workspace) {
db.data.workspaces.push(workspace);
await db.write();
}

export async function dropWorkspaces() {
const db = await getDb();
db.data.workspaces = [];
await db.write();
}
3 changes: 2 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { log } from '@clack/prompts';
import { cli } from 'cleye';

import * as pkg from '../package.json';
import clean from './commands/clean';
import list from './commands/list';
import { checkDir } from './operations/check-dir';
import { createWorkspace } from './operations/create-workspace';
Expand All @@ -25,7 +26,7 @@ cli(
examples: ['codew .'],
},

commands: [list],
commands: [list, clean],
},
async (argv) => {
const path = argv._.path;
Expand Down
10 changes: 10 additions & 0 deletions src/operations/clean-workspaces.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { rm } from 'node:fs/promises';

import { workspaceDir } from '../config';
import { dropWorkspaces } from '../db';

export async function cleanWorkspaces() {
await rm(workspaceDir, { recursive: true, force: true });

await dropWorkspaces();
}
65 changes: 65 additions & 0 deletions test/operations/clean-workspaces.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import { existsSync } from 'node:fs';
import { mkdir, readFile, rm } from 'node:fs/promises';
import { basename, resolve } from 'node:path';

import { afterEach, beforeEach, expect, test, vi } from 'vitest';

import { cleanWorkspaces } from '../../src/operations/clean-workspaces';
import { createWorkspace } from '../../src/operations/create-workspace';

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('cleanWorkspaces', async () => {
await createWorkspace('dir-1');

expect(
existsSync(
resolve(mocks.testHomedir(), '.codew/workspaces/dir-1.code-workspace')
)
).toBe(true);
const dbContent = await readFile(
resolve(mocks.testHomedir(), '.codew/db.json'),
{
encoding: 'utf-8',
}
);
const db = JSON.parse(dbContent);
expect(db.workspaces.length).toBe(1);

await cleanWorkspaces();

expect(
existsSync(
resolve(mocks.testHomedir(), '.codew/workspaces/dir-1.code-workspace')
)
).toBe(false);
const dbContent2 = await readFile(
resolve(mocks.testHomedir(), '.codew/db.json'),
{
encoding: 'utf-8',
}
);
const db2 = JSON.parse(dbContent2);
expect(db2.workspaces.length).toBe(0);
});