Skip to content

Commit

Permalink
On empty target folder, add the option to ignore a pattern.
Browse files Browse the repository at this point in the history
  • Loading branch information
Mr. Robot committed Jan 14, 2024
1 parent 2755d62 commit 65b7aae
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 4 deletions.
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"id": "bulk-exporter",
"name": "Bulk Exporter",
"version": "2.0.6",
"version": "2.0.7",
"minAppVersion": "0.15.0",
"description": "Use Dataview queries to export a set of notes with assets",
"author": "symunona",
Expand Down
2 changes: 1 addition & 1 deletion src/export/exporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ export async function exportSelection(

// If emptying target folder is set, remove all files and folders within.
if (settings.emptyTargetFolder) {
rmDirContent(settings.outputFolder)
rmDirContent(settings.outputFolder, settings.emptyTargetFolderIgnore)
}

for (const fileIndex in fileList) {
Expand Down
4 changes: 4 additions & 0 deletions src/models/bulk-export-settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ export const DEFAULT_SETTINGS: BulkExportSettings = {
outputFolder: "output",
exportQuery: "blog",
emptyTargetFolder: false,
emptyTargetFolderIgnore: '',

isPublishedField: '',
assetPath: "assets",
outputFormat: '${blog}/${slug}',
Expand All @@ -25,6 +27,8 @@ export interface BulkExportSettings {
exportQuery: string;
isPublishedField: string;
emptyTargetFolder: boolean;
emptyTargetFolderIgnore: string,

preserveWikiLinks: boolean;
normalizeSpacesInLinks: boolean;

Expand Down
29 changes: 28 additions & 1 deletion src/settings/export-settings-tab.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,10 +165,37 @@ export class OutputSettingTab extends PluginSettingTab {
.setValue(settings.emptyTargetFolder)
.onChange(async (value) => {
settings.emptyTargetFolder = value;
if (value){
ignorePatterns.settingEl.show()
} else {
ignorePatterns.settingEl.hide()
}
await this.plugin.saveSettingsWithRefresh();
})
);

const linkToGlobDocs = createEl('a', { href: 'https://globster.xyz/', text: 'Glob pattern matcher' })
const ignoreInfo = createSpan({ text: `Files in the root folder matching this pattern will NOT be deleted. E.g. ignore 'engine' and 'rest' folders, just type in {engine,rest} - ` })
ignoreInfo.append(linkToGlobDocs)
const ignoreMatcherElement = document.createDocumentFragment()
ignoreMatcherElement.append(ignoreInfo)

const ignorePatterns = new Setting(containerEl)
.setName("Ignore Delete Glob Pattern")
.setDesc(ignoreMatcherElement)
.addText((text) =>
text
.setPlaceholder("*")
.setValue(settings.emptyTargetFolderIgnore)
.onChange(async (value) => {
settings.emptyTargetFolderIgnore = value
await this.plugin.saveSettingsWithRefresh();
})
);
if (!settings.emptyTargetFolder){ ignorePatterns.settingEl.hide() }




containerEl.createEl('h2', { text: 'Preview' })

Expand All @@ -195,7 +222,7 @@ export class OutputSettingTab extends PluginSettingTab {
.setPlaceholder("*")
.setValue(settings.headerFieldsToShow.join(', '))
.onChange(async (value) => {
settings.headerFieldsToShow = value.split(',').map(v=>v.trim()).filter(v=>v);
settings.headerFieldsToShow = value.split(',').map(v => v.trim()).filter(v => v);
await this.plugin.saveSettingsWithRefresh();
})
);
Expand Down
1 change: 1 addition & 0 deletions src/ui/button-with-loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ export class ButtonWithLoader {
loadingIcon.style.display = 'none'
errorIcon.style.display = 'block'
onError(e)
console.error(e)
}
button.disabled = false
})
Expand Down
18 changes: 17 additions & 1 deletion src/utils/delete-folder-content.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,25 @@
import { existsSync, lstatSync, readdirSync, rmSync, unlinkSync } from "fs";
import { join } from "path";
import { globSync } from 'glob'
import { log } from "./log";

export function rmDirContent(directoryPath: string) {
export function rmDirContent(directoryPath: string, ignorePattern: string) {
if (existsSync(directoryPath)) {
const fileMap: {[key: string]: boolean} = {}
if (ignorePattern){
const matching =
globSync(ignorePattern, {cwd: directoryPath})
.forEach((filePath)=>fileMap[filePath] = true)
console.warn(matching)
}

readdirSync(directoryPath).forEach((file) => {
// Keep if!
if (fileMap[file]) {
log('Not deleting file/folder as it is matching ignore rule: ', file)
return
}

const curPath = join(directoryPath, file);

if (lstatSync(curPath).isDirectory()) {
Expand Down

0 comments on commit 65b7aae

Please sign in to comment.