Skip to content

Commit

Permalink
#569 - Remove folder when using page bundle
Browse files Browse the repository at this point in the history
  • Loading branch information
estruyf committed Jul 3, 2023
1 parent bf0746d commit ba4f49a
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
- [#558](https://github.com/estruyf/vscode-front-matter/issues/558): Moved the tags and categories to a `.frontmatter/database/taxonomyDb.json` file
- [#566](https://github.com/estruyf/vscode-front-matter/issues/566): Keep the panel context on the live preview
- [#568](https://github.com/estruyf/vscode-front-matter/issues/568): Update the preview URL if the slug changes
- [#569](https://github.com/estruyf/vscode-front-matter/issues/569): Remove the page bundle folder on content removal
- [#586](https://github.com/estruyf/vscode-front-matter/issues/586): Allow to specify the content card fields
- [#588](https://github.com/estruyf/vscode-front-matter/issues/588): Added extensibility support to override card fields
- [#591](https://github.com/estruyf/vscode-front-matter/issues/591): Support for date format in the `datetime` field
Expand Down
32 changes: 30 additions & 2 deletions src/listeners/dashboard/PagesListener.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ import {
import { DashboardCommand } from '../../dashboardWebView/DashboardCommand';
import { DashboardMessage } from '../../dashboardWebView/DashboardMessage';
import { Page } from '../../dashboardWebView/models';
import { ArticleHelper, Extension, Logger, Settings } from '../../helpers';
import { ArticleHelper, Extension, Logger, parseWinPath, Settings } from '../../helpers';
import { BaseListener } from './BaseListener';
import { DataListener } from '../panel';
import Fuse from 'fuse.js';
import { PagesParser } from '../../services/PagesParser';
import { unlinkAsync } from '../../utils';
import { unlinkAsync, rmdirAsync } from '../../utils';

export class PagesListener extends BaseListener {
private static watchers: { [path: string]: FileSystemWatcher } = {};
Expand Down Expand Up @@ -114,10 +114,38 @@ export class PagesListener extends BaseListener {
return;
}

// Get the content type of the page
const article = await ArticleHelper.getFrontMatterByPath(path);
if (!article) {
return;
}
const contentType = ArticleHelper.getContentType(article.data);

Logger.info(`Deleting file: ${path}`);

await unlinkAsync(path);

// Check if the content type is a page bundle
if (contentType.pageBundle) {
const absPath = parseWinPath(path);
const folder = absPath.substring(0, absPath.lastIndexOf('/') + 1);
try {
// Check if the folder is empty
const files = await workspace.fs.readDirectory(Uri.file(folder));
if (files.length > 0) {
// Remove each file
for (const file of files) {
await unlinkAsync(`${folder}${file[0]}`);
}
}

// Delete the folder
await rmdirAsync(folder);
} catch (e) {
console.log((e as any).message);
}
}

this.lastPages = this.lastPages.filter((p) => p.fmFilePath !== path);
this.sendPageData(this.lastPages);

Expand Down
1 change: 1 addition & 0 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@ export * from './mkdirAsync';
export * from './readFileAsync';
export * from './readdirAsync';
export * from './renameAsync';
export * from './rmdirAsync';
export * from './unlinkAsync';
export * from './writeFileAsync';
4 changes: 4 additions & 0 deletions src/utils/rmdirAsync.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { promisify } from 'util';
import { rmdir as rmdirCb } from 'fs';

export const rmdirAsync = promisify(rmdirCb);

0 comments on commit ba4f49a

Please sign in to comment.