Skip to content

Commit d933c00

Browse files
authored
Merge pull request #26 from sonicbaume/async-file-adapter
Change file adapter functions to async
2 parents ccb73ae + e9e3676 commit d933c00

44 files changed

Lines changed: 608 additions & 545 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

src/cli/index.ts

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,18 @@ import {
1111
import { ComparisonAnalyzer, MetricsCalculator } from '../utilities/analytics';
1212
import { CellScanningOrder, ScanningSelectionMethod } from '../types/aac';
1313
import { defaultFileAdapter, extname } from '../utils/io';
14+
import { readFileSync } from 'node:fs';
1415

15-
const { pathExists, isDirectory, join, readTextFromInput, basename, writeTextToPath } =
16-
defaultFileAdapter;
16+
const { pathExists, isDirectory, join, basename, writeTextToPath } = defaultFileAdapter;
1717

1818
// Helper function to detect format from file/folder path
19-
function detectFormat(filePath: string): string {
19+
async function detectFormat(filePath: string): Promise<string> {
2020
// Check if it's a folder ending with .ascconfig
21-
if (pathExists(filePath) && isDirectory(filePath) && filePath.endsWith('.ascconfig')) {
21+
if (
22+
(await pathExists(filePath)) &&
23+
(await isDirectory(filePath)) &&
24+
filePath.endsWith('.ascconfig')
25+
) {
2226
return 'ascconfig';
2327
}
2428

@@ -84,7 +88,7 @@ function parseFilteringOptions(options: {
8488
}
8589

8690
// Set version from package.json
87-
const packageJson = JSON.parse(readTextFromInput(join(__dirname, '../../package.json'))) as {
91+
const packageJson = JSON.parse(readFileSync(join(__dirname, '../../package.json'), 'utf8')) as {
8892
version: string;
8993
};
9094
program.version(packageJson.version);
@@ -116,7 +120,7 @@ program
116120
const filteringOptions = parseFilteringOptions(options);
117121

118122
// Auto-detect format if not specified
119-
const format = options.format || detectFormat(file);
123+
const format = options.format || (await detectFormat(file));
120124
const processor = getProcessor(format, filteringOptions);
121125
const tree = await processor.loadIntoTree(file);
122126

@@ -170,7 +174,7 @@ program
170174
const filteringOptions = parseFilteringOptions(options);
171175

172176
// Auto-detect format if not specified
173-
const format = options.format || detectFormat(file);
177+
const format = options.format || (await detectFormat(file));
174178
const processor = getProcessor(format, filteringOptions);
175179
const texts = await processor.extractTexts(file);
176180

@@ -237,7 +241,7 @@ program
237241
const filteringOptions = parseFilteringOptions(options);
238242

239243
// Auto-detect input format
240-
const inputFormat = detectFormat(input);
244+
const inputFormat = await detectFormat(input);
241245
const inputProcessor = getProcessor(inputFormat, filteringOptions);
242246

243247
// Load the tree (handle both files and folders)
@@ -293,7 +297,7 @@ program
293297
) => {
294298
try {
295299
// Auto-detect format if not specified
296-
const format = options.format || detectFormat(file);
300+
const format = options.format || (await detectFormat(file));
297301

298302
// Get processor with gridset password if provided
299303
const processorOptions: ProcessorOptions = {};
@@ -388,7 +392,7 @@ program
388392
.option('--encryption <mode>', 'Encryption label for baton export', 'none')
389393
.option('--version <version>', 'Baton export version', '1.0')
390394
.action(
391-
(
395+
async (
392396
input: string,
393397
options: {
394398
format?: string;
@@ -401,7 +405,7 @@ program
401405
}
402406
) => {
403407
try {
404-
if (!pathExists(input)) {
408+
if (!(await pathExists(input))) {
405409
throw new Error(`File not found: ${input}`);
406410
}
407411

@@ -412,9 +416,9 @@ program
412416

413417
let entries;
414418
if (normalizedSource === 'grid3' || (normalizedSource === 'auto' && isGrid3Db)) {
415-
entries = readGrid3History(input);
419+
entries = await readGrid3History(input);
416420
} else if (normalizedSource === 'snap' || (normalizedSource === 'auto' && isSnap)) {
417-
entries = readSnapUsage(input);
421+
entries = await readSnapUsage(input);
418422
} else {
419423
throw new Error('Unable to detect history source. Use --source grid3 or --source snap.');
420424
}
@@ -435,7 +439,7 @@ program
435439

436440
const output = JSON.stringify(payload, null, 2);
437441
if (options.out) {
438-
writeTextToPath(options.out, output);
442+
await writeTextToPath(options.out, output);
439443
} else {
440444
console.log(output);
441445
}
@@ -493,7 +497,7 @@ program
493497
) => {
494498
try {
495499
const filteringOptions = parseFilteringOptions(options);
496-
const format = options.format || detectFormat(file);
500+
const format = options.format || (await detectFormat(file));
497501
const processor = getProcessor(format, filteringOptions);
498502
const tree = await processor.loadIntoTree(file);
499503

@@ -575,7 +579,7 @@ program
575579

576580
const output = options.pretty ? JSON.stringify(result, null, 2) : JSON.stringify(result);
577581
if (options.out) {
578-
writeTextToPath(options.out, output);
582+
await writeTextToPath(options.out, output);
579583
} else {
580584
console.log(output);
581585
}

src/core/baseProcessor.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ export interface ProcessorConfig {
7777
fileAdapter: FileAdapter;
7878

7979
// Adapter for handling encoding/decoding zip files
80-
zipAdapter: (input?: ProcessorInput) => Promise<ZipAdapter>;
80+
zipAdapter: (input?: ProcessorInput, fileAdapter?: FileAdapter) => Promise<ZipAdapter>;
8181
}
8282
export type ProcessorOptions = Partial<ProcessorConfig>;
8383

src/processors/applePanelsProcessor.ts

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ class ApplePanelsProcessor extends BaseProcessor {
220220
async loadIntoTree(filePathOrBuffer: ProcessorInput): Promise<AACTree> {
221221
const { readBinaryFromInput, readTextFromInput, pathExists, getFileSize, join } =
222222
this.options.fileAdapter;
223-
await Promise.resolve();
223+
224224
const filename =
225225
typeof filePathOrBuffer === 'string' ? getBasename(filePathOrBuffer) : 'upload.plist';
226226
let buffer: Uint8Array;
@@ -234,8 +234,8 @@ class ApplePanelsProcessor extends BaseProcessor {
234234
'Resources',
235235
'PanelDefinitions.plist'
236236
);
237-
if (pathExists(panelDefsPath)) {
238-
buffer = readBinaryFromInput(panelDefsPath);
237+
if (await pathExists(panelDefsPath)) {
238+
buffer = await readBinaryFromInput(panelDefsPath);
239239
} else {
240240
const validation = buildValidationResultFromMessage({
241241
filename,
@@ -248,13 +248,13 @@ class ApplePanelsProcessor extends BaseProcessor {
248248
throw new ValidationFailureError('Apple Panels file not found', validation);
249249
}
250250
} else {
251-
buffer = readBinaryFromInput(filePathOrBuffer);
251+
buffer = await readBinaryFromInput(filePathOrBuffer);
252252
}
253253
} else {
254-
buffer = readBinaryFromInput(filePathOrBuffer);
254+
buffer = await readBinaryFromInput(filePathOrBuffer);
255255
}
256256

257-
const content = readTextFromInput(buffer);
257+
const content = await readTextFromInput(buffer);
258258
const parsedData = plist.parse(content) as ApplePanelsParsedDocument;
259259

260260
let panelsData: ApplePanelsPanel[] = [];
@@ -393,10 +393,12 @@ class ApplePanelsProcessor extends BaseProcessor {
393393
filename,
394394
filesize:
395395
typeof filePathOrBuffer === 'string'
396-
? (() => {
397-
return pathExists(filePathOrBuffer) ? getFileSize(filePathOrBuffer) : 0;
396+
? await (async () => {
397+
return (await pathExists(filePathOrBuffer))
398+
? await getFileSize(filePathOrBuffer)
399+
: 0;
398400
})()
399-
: readBinaryFromInput(filePathOrBuffer).byteLength,
401+
: (await readBinaryFromInput(filePathOrBuffer)).byteLength,
400402
format: 'applepanels',
401403
message: err?.message || 'Failed to parse Apple Panels file',
402404
type: 'parse',
@@ -466,16 +468,16 @@ class ApplePanelsProcessor extends BaseProcessor {
466468
await this.saveFromTree(tree, outputPath);
467469

468470
if (outputPath.endsWith('.plist')) {
469-
return readBinaryFromInput(outputPath);
471+
return await readBinaryFromInput(outputPath);
470472
}
471473
const configPath = outputPath.endsWith('.ascconfig') ? outputPath : `${outputPath}.ascconfig`;
472474
const panelDefsPath = join(configPath, 'Contents', 'Resources', 'PanelDefinitions.plist');
473-
return readBinaryFromInput(panelDefsPath);
475+
return await readBinaryFromInput(panelDefsPath);
474476
}
475477

476478
async saveFromTree(tree: AACTree, outputPath: string): Promise<void> {
477479
const { writeTextToPath, pathExists, mkDir, join, dirname } = this.options.fileAdapter;
478-
await Promise.resolve();
480+
479481
// Support two output modes:
480482
// 1) Single-file .plist (PanelDefinitions.plist content written directly)
481483
// 2) Apple Panels bundle folder (*.ascconfig) with Contents/Resources structure
@@ -490,9 +492,9 @@ class ApplePanelsProcessor extends BaseProcessor {
490492
contentsPath = join(configPath, 'Contents');
491493
resourcesPath = join(contentsPath, 'Resources');
492494

493-
if (!pathExists(configPath)) mkDir(configPath, { recursive: true });
494-
if (!pathExists(contentsPath)) mkDir(contentsPath, { recursive: true });
495-
if (!pathExists(resourcesPath)) mkDir(resourcesPath, { recursive: true });
495+
if (!(await pathExists(configPath))) await mkDir(configPath, { recursive: true });
496+
if (!(await pathExists(contentsPath))) await mkDir(contentsPath, { recursive: true });
497+
if (!(await pathExists(resourcesPath))) await mkDir(resourcesPath, { recursive: true });
496498

497499
// Create Info.plist (bundle mode only)
498500
const infoPlist = {
@@ -510,11 +512,11 @@ class ApplePanelsProcessor extends BaseProcessor {
510512
`Generated by AAC Processors${tree.metadata?.author ? ` - Author: ${tree.metadata.author}` : ''}`,
511513
};
512514
const infoPlistContent = plist.build(infoPlist);
513-
writeTextToPath(join(contentsPath, 'Info.plist'), infoPlistContent);
515+
await writeTextToPath(join(contentsPath, 'Info.plist'), infoPlistContent);
514516

515517
// Create AssetIndex.plist (empty)
516518
const assetIndexContent = plist.build({});
517-
writeTextToPath(join(resourcesPath, 'AssetIndex.plist'), assetIndexContent);
519+
await writeTextToPath(join(resourcesPath, 'AssetIndex.plist'), assetIndexContent);
518520
}
519521

520522
// Build PanelDefinitions content from tree
@@ -656,11 +658,11 @@ class ApplePanelsProcessor extends BaseProcessor {
656658
if (isSinglePlist) {
657659
// Write single PanelDefinitions.plist file directly
658660
const dir = dirname(outputPath);
659-
if (!pathExists(dir)) mkDir(dir, { recursive: true });
660-
writeTextToPath(outputPath, panelDefsContent);
661+
if (!(await pathExists(dir))) await mkDir(dir, { recursive: true });
662+
await writeTextToPath(outputPath, panelDefsContent);
661663
} else {
662664
// Write into bundle structure
663-
writeTextToPath(join(resourcesPath, 'PanelDefinitions.plist'), panelDefsContent);
665+
await writeTextToPath(join(resourcesPath, 'PanelDefinitions.plist'), panelDefsContent);
664666
}
665667
}
666668

0 commit comments

Comments
 (0)