Skip to content

Commit

Permalink
Overhauled file index:
Browse files Browse the repository at this point in the history
	Renamed `FileIndex` to `Index`
	The index JSON format now supports versioning and uses "flat" entries
	`Index` now preloads all files when loaded
	Removed `SyncIndexFS` and `AsyncIndexFS`
	Added `IndexFS.reloadFiles` (and `*Sync`)
Renamed `FetchFS.preloadFile` to `FetchFS.preload`
Removed `FetchFS.empty`
Renamed `FetchFS.prefixUrl` to `FetchFS.naseUrl` for consistancy with `FetchOptions`
Added optional generic type to `StatsLike`
  • Loading branch information
james-pre committed May 17, 2024
1 parent 615c05a commit 16b192e
Show file tree
Hide file tree
Showing 9 changed files with 300 additions and 637 deletions.
50 changes: 31 additions & 19 deletions scripts/make-index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import { parseArgs } from 'util';
import { statSync, readdirSync, writeFileSync } from 'fs';
import { join } from 'path/posix';
import { resolve } from 'path';
import { relative, resolve } from 'path';
import { minimatch } from 'minimatch';

const { values: options, positionals } = parseArgs({
Expand Down Expand Up @@ -38,10 +38,12 @@ if (options.quiet && options.verbose) {
process.exit();
}

function pathToPosix(path) {
function fixSlash(path) {
return path.replaceAll('\\', '/');
}

const resolvedRoot = root || '.';

const colors = {
reset: 0,
black: 30,
Expand All @@ -66,36 +68,46 @@ function color(color, text) {
return `\x1b[${colors[color]}m${text}\x1b[0m`;
}

function listing(path, seen = new Set()) {
const entries = new Map();

function computeEntries(path) {
try {
if (options.verbose) console.log(`${color('blue', 'list')} ${path}`);
if (options.ignore.some(pattern => minimatch(path, pattern))) {
if (!options.quiet) console.log(`${color('yellow', 'skip')} ${path}`);
return;
}

const stats = statSync(path);
entries.set('/' + relative(resolvedRoot, path), stats);

if (stats.isFile()) {
if (options.verbose) console.log(`${color('green', 'file')} ${path}`);
return null;
if (options.verbose) {
console.log(`${color('green', 'file')} ${path}`);
}
return;
}

const entries = {};
for (const file of readdirSync(path)) {
const full = join(path, file);
if (options.ignore.some(pattern => minimatch(full, pattern))) {
if (!options.quiet) console.log(`${color('yellow', 'skip')} ${full}`);
continue;
}

entries[file] = listing(full, seen);
computeEntries(join(path, file));
}
if (options.verbose) {
console.log(`${color('bright_green', ' dir')} ${path}`);
}
if (options.verbose) console.log(`${color('bright_green', ' dir')} ${path}`);
return entries;
} catch (e) {
if (!options.quiet) {
console.log(`${color('red', 'fail')} ${path}: ${e.message}`);
}
}
}

const rootListing = listing(pathToPosix(root));
if (!options.quiet) console.log('Generated listing for ' + pathToPosix(resolve(root)));
computeEntries(resolvedRoot);
if (!options.quiet) {
console.log('Generated listing for ' + fixSlash(resolve(root)));
}

const index = {
version: 1,
entries: Object.fromEntries(entries),
};

writeFileSync(options.output, JSON.stringify(rootListing));
writeFileSync(options.output, JSON.stringify(index));
Loading

0 comments on commit 16b192e

Please sign in to comment.