Skip to content

Commit

Permalink
Enhancement: Add rename and deleteFile actions to the action mode…
Browse files Browse the repository at this point in the history
…l and implement functionality in the DemoRunner #16 #17
  • Loading branch information
estruyf committed Dec 27, 2024
1 parent 591db4b commit b1e26f3
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## [0.0.47] - 2025-12-27

- [#16](https://github.com/estruyf/vscode-demo-time/issues/16): added the `deleteFile` action
- [#17](https://github.com/estruyf/vscode-demo-time/issues/17): Added the `rename` file action
- [#18](https://github.com/estruyf/vscode-demo-time/issues/18): Added the `save` and `write` actions to the `Demo Time: Add as demo step` command.

## [0.0.46] - 2025-12-20
Expand Down
2 changes: 2 additions & 0 deletions src/models/Action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ export type Action =
// File
| "create"
| "open"
| "rename"
| "deleteFile"
// Markdown
| "markdownPreview"
// Code
Expand Down
3 changes: 2 additions & 1 deletion src/models/Demos.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export interface Icons {

export interface Step {
action: Action;

path?: string;
content?: string;
contentPath?: string;
Expand All @@ -36,6 +36,7 @@ export interface Step {
args?: any;
lineInsertionDelay?: number;
setting?: Setting;
dest?: string;
}

export interface Setting {
Expand Down
51 changes: 51 additions & 0 deletions src/services/DemoRunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
TextEditorRevealType,
Uri,
WorkspaceEdit,
WorkspaceFolder,
commands,
window,
workspace,
Expand Down Expand Up @@ -519,6 +520,16 @@ export class DemoRunner {
continue;
}

if (step.action === "rename") {
DemoRunner.rename(workspaceFolder, fileUri, step);
continue;
}

if (step.action === "deleteFile") {
await DemoRunner.deleteFile(workspaceFolder, fileUri);
continue;
}

let content = step.content || "";
if (step.contentPath) {
const fileContent = await getFileContents(workspaceFolder, step.contentPath);
Expand Down Expand Up @@ -834,6 +845,46 @@ export class DemoRunner {
DemoRunner.terminal.sendText(command, true);
}

/**
* Renames a file within the specified workspace folder.
*
* @param workspaceFolder - The workspace folder containing the file to be renamed.
* @param fileUri - The URI of the file to be renamed.
* @param step - The step containing the destination path for the renamed file.
* @returns A promise that resolves when the file has been renamed.
*
* @throws Will throw an error if the destination is not specified or if the rename operation fails.
*/
private static async rename(workspaceFolder: WorkspaceFolder, fileUri: Uri, step: Step): Promise<void> {
if (!step.dest) {
Notifications.error("No destination specified");
return;
}

try {
const newUri = Uri.joinPath(workspaceFolder.uri, step.dest);
await workspace.fs.rename(fileUri, newUri);
} catch (error) {
Notifications.error((error as Error).message);
}
}

/**
* Deletes a file from the given workspace folder.
*
* @param workspaceFolder - The workspace folder containing the file to be deleted.
* @param fileUri - The URI of the file to be deleted.
* @returns A promise that resolves when the file has been deleted.
* @throws Will throw an error if the file deletion fails.
*/
private static async deleteFile(workspaceFolder: WorkspaceFolder, fileUri: Uri): Promise<void> {
try {
await workspace.fs.delete(fileUri);
} catch (error) {
Notifications.error((error as Error).message);
}
}

/**
* Retrieves the current position and range based on the provided step.
*
Expand Down

0 comments on commit b1e26f3

Please sign in to comment.