Skip to content

Commit

Permalink
Simplified archive opening process
Browse files Browse the repository at this point in the history
  • Loading branch information
JrMasterModelBuilder committed Oct 10, 2023
1 parent c04e4e6 commit 79b3960
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 32 deletions.
35 changes: 7 additions & 28 deletions src/projector.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
import {type TranscodeEncoding} from 'node:buffer';
import {mkdir, readFile, stat, writeFile} from 'node:fs/promises';
import {mkdir, readFile, writeFile} from 'node:fs/promises';
import {join as pathJoin, dirname} from 'node:path';

import {
ArchiveDir,
ArchiveHdi,
createArchiveByFileExtension,
fsLstatExists
} from '@shockpkg/archive-files';
import {fsLstatExists} from '@shockpkg/archive-files';

import {pathRelativeBase, trimExtension} from './util';

Expand Down Expand Up @@ -95,6 +90,11 @@ export abstract class Projector {
*/
public nestXtrasConfiguration: boolean = false;

/**
* Set the nobrowse option on mounted disk images.
*/
public nobrowse = false;

/**
* Output path.
*/
Expand Down Expand Up @@ -446,27 +446,6 @@ export abstract class Projector {
*/
public abstract get lingoNewline(): string;

/**
* Open path as archive.
*
* @param path Archive path.
* @returns Archive instance.
*/
protected async _openArchive(path: string) {
const st = await stat(path);
if (st.isDirectory()) {
return new ArchiveDir(path);
}
const archive = createArchiveByFileExtension(path);
if (!archive) {
throw new Error(`Unrecognized archive format: ${path}`);
}
if (archive instanceof ArchiveHdi) {
archive.nobrowse = true;
}
return archive;
}

/**
* Write the projector skeleton from archive.
*
Expand Down
10 changes: 8 additions & 2 deletions src/projector/mac/app.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import {readFile, mkdir, rm, writeFile} from 'node:fs/promises';
import {join as pathJoin, basename, dirname} from 'node:path';

import {PathType, Entry} from '@shockpkg/archive-files';
import {
PathType,
Entry,
createArchiveByFileStatOrThrow
} from '@shockpkg/archive-files';
import {Plist} from '@shockpkg/plist-dom';

import {
Expand Down Expand Up @@ -629,7 +633,9 @@ export class ProjectorMacApp extends ProjectorMac {
return true;
};

const archive = await this._openArchive(skeleton);
const archive = await createArchiveByFileStatOrThrow(skeleton, {
nobrowse: this.nobrowse
});
await archive.read(async entry => {
if (entry.type === PathType.RESOURCE_FORK) {
return true;
Expand Down
11 changes: 9 additions & 2 deletions src/projector/windows.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import {readFile} from 'node:fs/promises';
import {join as pathJoin, dirname, basename} from 'node:path';

import {Entry, PathType, fsWalk} from '@shockpkg/archive-files';
import {
Entry,
PathType,
createArchiveByFileStatOrThrow,
fsWalk
} from '@shockpkg/archive-files';

import {pathRelativeBase, pathRelativeBaseMatch} from '../util';
import {
Expand Down Expand Up @@ -197,7 +202,9 @@ export class ProjectorWindows extends Projector {
return true;
};

const archive = await this._openArchive(skeleton);
const archive = await createArchiveByFileStatOrThrow(skeleton, {
nobrowse: this.nobrowse
});
await archive.read(async entry => {
if (entry.type === PathType.RESOURCE_FORK) {
return true;
Expand Down

0 comments on commit 79b3960

Please sign in to comment.