Skip to content

Commit

Permalink
feat: delete orphaned config files on folder delete
Browse files Browse the repository at this point in the history
  • Loading branch information
hperrin committed Aug 1, 2022
1 parent e6ae85b commit 33f7baa
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 3 deletions.
8 changes: 8 additions & 0 deletions src/FileSystemAdapter/Lock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,30 @@ export default class Lock implements LockInterface {
user: User;
date: Date;
timeout: number;
guid: string;
depth: '0' | 'infinity';

constructor({
resource,
user,
date,
timeout,
guid,
depth,
}: {
resource: Resource;
user: User;
date: Date;
timeout: number;
guid: string;
depth: '0' | 'infinity';
}) {
this.resource = resource;
this.user = user;
this.date = date;
this.timeout = timeout;
this.guid = guid;
this.depth = depth;
}

async save() {
Expand Down
2 changes: 1 addition & 1 deletion src/FileSystemAdapter/Properties.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ export default class Properties implements PropertiesInterface {
try {
const props = JSON.parse((await fsp.readFile(filepath)).toString());

if (!(name in props['*'])) {
if (!('*' in props) || !(name in props['*'])) {
throw new PropertyNotFoundError(
`${name} property doesn't exist on resource.`
);
Expand Down
38 changes: 36 additions & 2 deletions src/FileSystemAdapter/Resource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,7 @@ export default class Resource implements ResourceInterface {
}

if (await this.isCollection()) {
await this.deleteOrphanedConfigFiles();
await fsp.rmdir(this.absolutePath);
} else {
await fsp.unlink(this.absolutePath);
Expand Down Expand Up @@ -293,7 +294,7 @@ export default class Resource implements ResourceInterface {
try {
const dirname = path.dirname(destinationPath);
const basename = path.basename(destinationPath);
propsFilePath = path.join(dirname, `.${basename}.nepheleprops`);
propsFilePath = path.join(dirname, `${basename}.nepheleprops`);
try {
await fsp.unlink(propsFilePath);
} catch (e: any) {
Expand Down Expand Up @@ -461,7 +462,40 @@ export default class Resource implements ResourceInterface {
} else {
const dirname = path.dirname(this.absolutePath);
const basename = path.basename(this.absolutePath);
return path.join(dirname, `.${basename}.nepheleprops`);
return path.join(dirname, `${basename}.nepheleprops`);
}
}

async deleteOrphanedConfigFiles() {
if (!(await this.isCollection())) {
throw new MethodNotSupportedError('This is not a collection.');
}

const listing = await fsp.readdir(this.absolutePath);
const files: Set<string> = new Set();
const propsFiles: Set<string> = new Set();

for (let name of listing) {
if (name === '.nepheleprops') {
continue;
}

if (name.endsWith('.nepheleprops')) {
propsFiles.add(name);
} else {
files.add(name);
}
}

for (let name of files) {
propsFiles.delete(`${name}.nepheleprops`);
}

const orphans = Array.from(propsFiles);

for (let name of orphans) {
const orphanPath = path.join(this.absolutePath, name);
await fsp.unlink(orphanPath);
}
}
}

0 comments on commit 33f7baa

Please sign in to comment.