static-dir-list 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +122 -0
- package/dist/chunk-ZH66Q66Y.js +210 -0
- package/dist/cli.cjs +349 -0
- package/dist/cli.d.cts +1 -0
- package/dist/cli.d.ts +1 -0
- package/dist/cli.js +121 -0
- package/dist/index.cjs +244 -0
- package/dist/index.d.cts +22 -0
- package/dist/index.d.ts +22 -0
- package/dist/index.js +6 -0
- package/package.json +54 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
# static-dir-list
|
|
2
|
+
|
|
3
|
+
A Node.js library and CLI tool that generates static HTML directory listings for a given folder. It recursively scans directories and creates `index.html` files for easy browsing without requiring any server-side logic.
|
|
4
|
+
|
|
5
|
+
## Live Demo
|
|
6
|
+
|
|
7
|
+
You can see an example of the generated output here:
|
|
8
|
+
|
|
9
|
+
https://hugo-olabi.github.io/static-dir-list/
|
|
10
|
+
|
|
11
|
+
## Features
|
|
12
|
+
|
|
13
|
+
* Recursive directory traversal
|
|
14
|
+
* Static HTML output (no JavaScript required in the browser)
|
|
15
|
+
* Supports custom output directory
|
|
16
|
+
* Ignore files support (similar to `.gitignore`)
|
|
17
|
+
* Configurable root path for generated links
|
|
18
|
+
* Simple CLI and programmatic API
|
|
19
|
+
|
|
20
|
+
## Installation
|
|
21
|
+
|
|
22
|
+
```bash
|
|
23
|
+
npm install static-dir-list
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
## Usage
|
|
27
|
+
|
|
28
|
+
### CLI
|
|
29
|
+
|
|
30
|
+
Basic usage:
|
|
31
|
+
|
|
32
|
+
```bash
|
|
33
|
+
npx static-dir-list <source-dir> [output-dir]
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
* `<source-dir>`: Directory to scan and generate listings from (required)
|
|
37
|
+
* `[output-dir]`: Directory where the HTML files will be written (defaults to `source-dir`)
|
|
38
|
+
|
|
39
|
+
### Options
|
|
40
|
+
|
|
41
|
+
#### Ignore files
|
|
42
|
+
|
|
43
|
+
You can provide one or more ignore files:
|
|
44
|
+
|
|
45
|
+
```bash
|
|
46
|
+
npx static-dir-list ./public/files -if .gitignore -if .staticignore
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
or
|
|
50
|
+
|
|
51
|
+
```bash
|
|
52
|
+
npx static-dir-list ./public/files --ignore-file .gitignore
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
Each ignore file should contain patterns (one per line). Empty lines and lines starting with `#` are ignored.
|
|
56
|
+
|
|
57
|
+
#### Root path
|
|
58
|
+
|
|
59
|
+
Set a custom root path for generated links:
|
|
60
|
+
|
|
61
|
+
```bash
|
|
62
|
+
npx static-dir-list ./public/files -root /files/
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
This is useful when your files are served from a subpath.
|
|
66
|
+
|
|
67
|
+
### Examples
|
|
68
|
+
|
|
69
|
+
Generate listings in-place:
|
|
70
|
+
|
|
71
|
+
```bash
|
|
72
|
+
npx static-dir-list ./public/files
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
Generate listings into a separate directory:
|
|
76
|
+
|
|
77
|
+
```bash
|
|
78
|
+
npx static-dir-list ./public/files ./dist/files
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
Use ignore file and custom root:
|
|
82
|
+
|
|
83
|
+
```bash
|
|
84
|
+
npx static-dir-list ./public/files ./dist/files -if .gitignore -root /files/
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
## Library Usage
|
|
88
|
+
|
|
89
|
+
You can also use it programmatically:
|
|
90
|
+
|
|
91
|
+
```ts
|
|
92
|
+
import { generateStaticListing } from 'static-dir-list';
|
|
93
|
+
|
|
94
|
+
await generateStaticListing(
|
|
95
|
+
'./public/files', // source directory
|
|
96
|
+
'./dist/files', // output directory
|
|
97
|
+
['node_modules'], // optional ignore list
|
|
98
|
+
'/files/' // optional root path
|
|
99
|
+
);
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
### API
|
|
103
|
+
|
|
104
|
+
```ts
|
|
105
|
+
generateStaticListing(
|
|
106
|
+
sourceDir: string,
|
|
107
|
+
outputDir: string,
|
|
108
|
+
ignoreList?: string[],
|
|
109
|
+
rootLocation?: string
|
|
110
|
+
): Promise<{ filesListed: number; dirsListed: number }>
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
## Behavior
|
|
114
|
+
|
|
115
|
+
* Generates an `index.html` file inside each directory
|
|
116
|
+
* Preserves directory structure in the output directory
|
|
117
|
+
* If no ignore list is provided, all files are included
|
|
118
|
+
* Ignore patterns are applied before traversal
|
|
119
|
+
|
|
120
|
+
## License
|
|
121
|
+
|
|
122
|
+
MIT
|
|
@@ -0,0 +1,210 @@
|
|
|
1
|
+
// src/index.ts
|
|
2
|
+
import fs from "fs";
|
|
3
|
+
import path from "path";
|
|
4
|
+
import ignore from "ignore";
|
|
5
|
+
|
|
6
|
+
// src/static.ts
|
|
7
|
+
var style = `<style>
|
|
8
|
+
body { font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif; padding: 20px; color: #333; line-height: 1.5; background-color: #fff; }
|
|
9
|
+
h1 { font-size: 1.5rem; border-bottom: 1px solid #eee; padding-bottom: 0.75rem; margin-bottom: 1.5rem; }
|
|
10
|
+
table { width: 100%; border-collapse: collapse; }
|
|
11
|
+
th, td { text-align: left; padding: 0.75rem; border-bottom: 1px solid #f0f0f0; }
|
|
12
|
+
th { background: #fafafa; font-weight: 600; font-size: 0.875rem; text-transform: uppercase; letter-spacing: 0.05em; color: #666; cursor: pointer; user-select: none; position: relative; }
|
|
13
|
+
th:hover { background: #f0f0f0; }
|
|
14
|
+
th.sort-asc::after { content: ' \u2191'; position: absolute; }
|
|
15
|
+
th.sort-desc::after { content: ' \u2193'; position: absolute; }
|
|
16
|
+
.detailsColumn { text-align: right; color: #888; font-size: 0.875rem; font-variant-numeric: tabular-nums; }
|
|
17
|
+
.icon { text-decoration: none; color: #2563eb; font-weight: 500; }
|
|
18
|
+
.icon:hover { text-decoration: underline; color: #1d4ed8; }
|
|
19
|
+
.dir::before { content: "\u{1F4C1} "; margin-right: 0.5rem; }
|
|
20
|
+
.file::before { content: "\u{1F4C4} "; margin-right: 0.5rem; }
|
|
21
|
+
.up::before { content: "\u2B06\uFE0F "; margin-right: 0.5rem; }
|
|
22
|
+
#parentDirLinkBox { margin-bottom: 1rem; }
|
|
23
|
+
|
|
24
|
+
@media (prefers-color-scheme: dark) {
|
|
25
|
+
body { background-color: #121212; color: #e0e0e0; }
|
|
26
|
+
h1 { border-bottom-color: #333; }
|
|
27
|
+
th { background: #1e1e1e; color: #aaa; border-bottom-color: #333; }
|
|
28
|
+
th:hover { background: #2a2a2a; }
|
|
29
|
+
td { border-bottom-color: #222; }
|
|
30
|
+
.detailsColumn { color: #999; }
|
|
31
|
+
.icon { color: #60a5fa; }
|
|
32
|
+
.icon:hover { color: #93c5fd; }
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
@media (max-width: 640px) {
|
|
36
|
+
.detailsColumn { display: none; }
|
|
37
|
+
}
|
|
38
|
+
</style>`;
|
|
39
|
+
var script = ` <script>
|
|
40
|
+
function sortTable(n) {
|
|
41
|
+
const table = document.getElementById("tbody");
|
|
42
|
+
const headers = document.querySelectorAll("th");
|
|
43
|
+
let rows = Array.from(table.rows);
|
|
44
|
+
let dir = headers[n].getAttribute("data-dir") === "asc" ? "desc" : "asc";
|
|
45
|
+
|
|
46
|
+
// Reset headers
|
|
47
|
+
headers.forEach(h => {
|
|
48
|
+
h.classList.remove("sort-asc", "sort-desc");
|
|
49
|
+
h.removeAttribute("data-dir");
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
headers[n].classList.add(dir === "asc" ? "sort-asc" : "sort-desc");
|
|
53
|
+
headers[n].setAttribute("data-dir", dir);
|
|
54
|
+
|
|
55
|
+
rows.sort((a, b) => {
|
|
56
|
+
const aIsDir = a.getAttribute("data-is-dir") === "true";
|
|
57
|
+
const bIsDir = b.getAttribute("data-is-dir") === "true";
|
|
58
|
+
|
|
59
|
+
if (aIsDir && !bIsDir) return -1;
|
|
60
|
+
if (!aIsDir && bIsDir) return 1;
|
|
61
|
+
|
|
62
|
+
let x = a.cells[n].getAttribute("data-value");
|
|
63
|
+
let y = b.cells[n].getAttribute("data-value");
|
|
64
|
+
|
|
65
|
+
if (n > 0) { // Size or Date (numeric)
|
|
66
|
+
x = parseFloat(x);
|
|
67
|
+
y = parseFloat(y);
|
|
68
|
+
} else { // Name (string)
|
|
69
|
+
x = x.toLowerCase();
|
|
70
|
+
y = y.toLowerCase();
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
if (x < y) return dir === "asc" ? -1 : 1;
|
|
74
|
+
if (x > y) return dir === "asc" ? 1 : -1;
|
|
75
|
+
return 0;
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
rows.forEach(row => table.appendChild(row));
|
|
79
|
+
}
|
|
80
|
+
</script>`;
|
|
81
|
+
|
|
82
|
+
// src/html.ts
|
|
83
|
+
var head = (title) => `<head>
|
|
84
|
+
<meta charset="UTF-8">
|
|
85
|
+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
86
|
+
<title>${title}</title>
|
|
87
|
+
${style}
|
|
88
|
+
</head>`;
|
|
89
|
+
var body = (title, parentLink, rows) => `
|
|
90
|
+
<body>
|
|
91
|
+
<h1 id="header">${title}</h1>
|
|
92
|
+
|
|
93
|
+
${parentLink ? `
|
|
94
|
+
<div id="parentDirLinkBox">
|
|
95
|
+
<a id="parentDirLink" class="icon up" href="${parentLink}">
|
|
96
|
+
<span id="parentDirText">[parent directory]</span>
|
|
97
|
+
</a>
|
|
98
|
+
</div>` : ""}
|
|
99
|
+
|
|
100
|
+
<table>
|
|
101
|
+
<thead>
|
|
102
|
+
<tr class="header" id="theader">
|
|
103
|
+
<th onclick="sortTable(0)">Name</th>
|
|
104
|
+
<th onclick="sortTable(1)" class="detailsColumn">Size</th>
|
|
105
|
+
<th onclick="sortTable(2)" class="detailsColumn">Date Modified</th>
|
|
106
|
+
</tr>
|
|
107
|
+
</thead>
|
|
108
|
+
<tbody id="tbody">
|
|
109
|
+
${rows}
|
|
110
|
+
</tbody>
|
|
111
|
+
</table>
|
|
112
|
+
|
|
113
|
+
${script}
|
|
114
|
+
</body>`;
|
|
115
|
+
|
|
116
|
+
// src/index.ts
|
|
117
|
+
function formatSize(bytes) {
|
|
118
|
+
if (bytes === 0) return "";
|
|
119
|
+
const units = ["B", "kB", "MB", "GB", "TB"];
|
|
120
|
+
const i = Math.floor(Math.log(bytes) / Math.log(1024));
|
|
121
|
+
return `${(bytes / Math.pow(1024, i)).toFixed(2)} ${units[i]}`.replace(/\.00\s/, " ");
|
|
122
|
+
}
|
|
123
|
+
function generateRowHtml(file) {
|
|
124
|
+
const iconClass = file.isDir ? "dir" : "file";
|
|
125
|
+
const href = file.isDir ? `${file.name}/` : file.name;
|
|
126
|
+
const value = file.isDir ? `${file.name}/` : file.name;
|
|
127
|
+
return `<tr data-is-dir="${file.isDir}">
|
|
128
|
+
<td data-value="${value}"><a class="icon ${iconClass}" href="${href}">${value}</a></td>
|
|
129
|
+
<td class="detailsColumn" data-value="${file.size}">${file.sizeStr}</td>
|
|
130
|
+
<td class="detailsColumn" data-value="${file.mtime}">${file.mtimeStr}</td>
|
|
131
|
+
</tr>`;
|
|
132
|
+
}
|
|
133
|
+
function generateHtml(rootLocation, files, relativePath) {
|
|
134
|
+
const title = `Index of ${"/" + relativePath || "/"}`;
|
|
135
|
+
const parentPath = relativePath ? path.dirname(relativePath) : null;
|
|
136
|
+
const parentLink = parentPath === "." ? rootLocation : parentPath ? `${rootLocation}${parentPath}/` : null;
|
|
137
|
+
const rows = files.map(generateRowHtml).join("");
|
|
138
|
+
return `
|
|
139
|
+
<!DOCTYPE html>
|
|
140
|
+
<html lang="en">
|
|
141
|
+
${head(title)}
|
|
142
|
+
|
|
143
|
+
${body(title, parentLink, rows)}
|
|
144
|
+
|
|
145
|
+
</html>`;
|
|
146
|
+
}
|
|
147
|
+
async function generateStaticListing(baseDir, outputDir, ignoreList, rootLocation = "/") {
|
|
148
|
+
const absoluteBaseDir = path.isAbsolute(baseDir) ? baseDir : path.resolve(process.cwd(), baseDir);
|
|
149
|
+
const absoluteOutputDir = path.isAbsolute(outputDir) ? outputDir : path.resolve(process.cwd(), outputDir);
|
|
150
|
+
var filesListed = 0;
|
|
151
|
+
var dirsListed = 0;
|
|
152
|
+
if (!fs.existsSync(absoluteBaseDir)) {
|
|
153
|
+
throw new Error(`Source directory not found: ${absoluteBaseDir}`);
|
|
154
|
+
}
|
|
155
|
+
const ig = ignoreList ? ignore().add(ignoreList) : null;
|
|
156
|
+
const traverse = (currentDir, relativePath) => {
|
|
157
|
+
const items = fs.readdirSync(currentDir);
|
|
158
|
+
const filesInfo = [];
|
|
159
|
+
for (const item of items) {
|
|
160
|
+
const fullPath = path.join(currentDir, item);
|
|
161
|
+
const itemRelativePath = path.join(relativePath, item);
|
|
162
|
+
if (ig && ig.ignores(itemRelativePath)) {
|
|
163
|
+
continue;
|
|
164
|
+
}
|
|
165
|
+
const stats = fs.statSync(fullPath);
|
|
166
|
+
const isDir = stats.isDirectory();
|
|
167
|
+
filesInfo.push({
|
|
168
|
+
name: item,
|
|
169
|
+
isDir,
|
|
170
|
+
size: isDir ? 0 : stats.size,
|
|
171
|
+
sizeStr: isDir ? "" : formatSize(stats.size),
|
|
172
|
+
mtime: stats.mtimeMs,
|
|
173
|
+
mtimeStr: stats.mtime.toLocaleString()
|
|
174
|
+
});
|
|
175
|
+
if (isDir) {
|
|
176
|
+
dirsListed++;
|
|
177
|
+
traverse(fullPath, itemRelativePath);
|
|
178
|
+
} else {
|
|
179
|
+
filesListed++;
|
|
180
|
+
const targetFilePath = path.join(absoluteOutputDir, itemRelativePath);
|
|
181
|
+
const targetFileDir = path.dirname(targetFilePath);
|
|
182
|
+
if (!fs.existsSync(targetFileDir)) {
|
|
183
|
+
fs.mkdirSync(targetFileDir, { recursive: true });
|
|
184
|
+
}
|
|
185
|
+
fs.copyFileSync(fullPath, targetFilePath);
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
const html = generateHtml(rootLocation, filesInfo.sort((a, b) => {
|
|
189
|
+
if (a.isDir && !b.isDir) return -1;
|
|
190
|
+
if (!a.isDir && b.isDir) return 1;
|
|
191
|
+
return a.name.localeCompare(b.name);
|
|
192
|
+
}), relativePath);
|
|
193
|
+
const targetDir = path.join(absoluteOutputDir, relativePath);
|
|
194
|
+
if (!fs.existsSync(targetDir)) {
|
|
195
|
+
fs.mkdirSync(targetDir, { recursive: true });
|
|
196
|
+
}
|
|
197
|
+
const outputFilePath = path.join(targetDir, "index.html");
|
|
198
|
+
fs.writeFileSync(outputFilePath, html, "utf8");
|
|
199
|
+
console.log(`Generated: ${outputFilePath} (${filesInfo.length} items)`);
|
|
200
|
+
};
|
|
201
|
+
traverse(absoluteBaseDir, "");
|
|
202
|
+
return {
|
|
203
|
+
filesListed,
|
|
204
|
+
dirsListed
|
|
205
|
+
};
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
export {
|
|
209
|
+
generateStaticListing
|
|
210
|
+
};
|
package/dist/cli.cjs
ADDED
|
@@ -0,0 +1,349 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
"use strict";
|
|
3
|
+
var __create = Object.create;
|
|
4
|
+
var __defProp = Object.defineProperty;
|
|
5
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
6
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
7
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
8
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
9
|
+
var __copyProps = (to, from, except, desc) => {
|
|
10
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
11
|
+
for (let key of __getOwnPropNames(from))
|
|
12
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
13
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
14
|
+
}
|
|
15
|
+
return to;
|
|
16
|
+
};
|
|
17
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
18
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
19
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
20
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
21
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
22
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
23
|
+
mod
|
|
24
|
+
));
|
|
25
|
+
|
|
26
|
+
// src/index.ts
|
|
27
|
+
var import_node_fs = __toESM(require("fs"), 1);
|
|
28
|
+
var import_node_path = __toESM(require("path"), 1);
|
|
29
|
+
var import_ignore = __toESM(require("ignore"), 1);
|
|
30
|
+
|
|
31
|
+
// src/static.ts
|
|
32
|
+
var style = `<style>
|
|
33
|
+
body { font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif; padding: 20px; color: #333; line-height: 1.5; background-color: #fff; }
|
|
34
|
+
h1 { font-size: 1.5rem; border-bottom: 1px solid #eee; padding-bottom: 0.75rem; margin-bottom: 1.5rem; }
|
|
35
|
+
table { width: 100%; border-collapse: collapse; }
|
|
36
|
+
th, td { text-align: left; padding: 0.75rem; border-bottom: 1px solid #f0f0f0; }
|
|
37
|
+
th { background: #fafafa; font-weight: 600; font-size: 0.875rem; text-transform: uppercase; letter-spacing: 0.05em; color: #666; cursor: pointer; user-select: none; position: relative; }
|
|
38
|
+
th:hover { background: #f0f0f0; }
|
|
39
|
+
th.sort-asc::after { content: ' \u2191'; position: absolute; }
|
|
40
|
+
th.sort-desc::after { content: ' \u2193'; position: absolute; }
|
|
41
|
+
.detailsColumn { text-align: right; color: #888; font-size: 0.875rem; font-variant-numeric: tabular-nums; }
|
|
42
|
+
.icon { text-decoration: none; color: #2563eb; font-weight: 500; }
|
|
43
|
+
.icon:hover { text-decoration: underline; color: #1d4ed8; }
|
|
44
|
+
.dir::before { content: "\u{1F4C1} "; margin-right: 0.5rem; }
|
|
45
|
+
.file::before { content: "\u{1F4C4} "; margin-right: 0.5rem; }
|
|
46
|
+
.up::before { content: "\u2B06\uFE0F "; margin-right: 0.5rem; }
|
|
47
|
+
#parentDirLinkBox { margin-bottom: 1rem; }
|
|
48
|
+
|
|
49
|
+
@media (prefers-color-scheme: dark) {
|
|
50
|
+
body { background-color: #121212; color: #e0e0e0; }
|
|
51
|
+
h1 { border-bottom-color: #333; }
|
|
52
|
+
th { background: #1e1e1e; color: #aaa; border-bottom-color: #333; }
|
|
53
|
+
th:hover { background: #2a2a2a; }
|
|
54
|
+
td { border-bottom-color: #222; }
|
|
55
|
+
.detailsColumn { color: #999; }
|
|
56
|
+
.icon { color: #60a5fa; }
|
|
57
|
+
.icon:hover { color: #93c5fd; }
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
@media (max-width: 640px) {
|
|
61
|
+
.detailsColumn { display: none; }
|
|
62
|
+
}
|
|
63
|
+
</style>`;
|
|
64
|
+
var script = ` <script>
|
|
65
|
+
function sortTable(n) {
|
|
66
|
+
const table = document.getElementById("tbody");
|
|
67
|
+
const headers = document.querySelectorAll("th");
|
|
68
|
+
let rows = Array.from(table.rows);
|
|
69
|
+
let dir = headers[n].getAttribute("data-dir") === "asc" ? "desc" : "asc";
|
|
70
|
+
|
|
71
|
+
// Reset headers
|
|
72
|
+
headers.forEach(h => {
|
|
73
|
+
h.classList.remove("sort-asc", "sort-desc");
|
|
74
|
+
h.removeAttribute("data-dir");
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
headers[n].classList.add(dir === "asc" ? "sort-asc" : "sort-desc");
|
|
78
|
+
headers[n].setAttribute("data-dir", dir);
|
|
79
|
+
|
|
80
|
+
rows.sort((a, b) => {
|
|
81
|
+
const aIsDir = a.getAttribute("data-is-dir") === "true";
|
|
82
|
+
const bIsDir = b.getAttribute("data-is-dir") === "true";
|
|
83
|
+
|
|
84
|
+
if (aIsDir && !bIsDir) return -1;
|
|
85
|
+
if (!aIsDir && bIsDir) return 1;
|
|
86
|
+
|
|
87
|
+
let x = a.cells[n].getAttribute("data-value");
|
|
88
|
+
let y = b.cells[n].getAttribute("data-value");
|
|
89
|
+
|
|
90
|
+
if (n > 0) { // Size or Date (numeric)
|
|
91
|
+
x = parseFloat(x);
|
|
92
|
+
y = parseFloat(y);
|
|
93
|
+
} else { // Name (string)
|
|
94
|
+
x = x.toLowerCase();
|
|
95
|
+
y = y.toLowerCase();
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
if (x < y) return dir === "asc" ? -1 : 1;
|
|
99
|
+
if (x > y) return dir === "asc" ? 1 : -1;
|
|
100
|
+
return 0;
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
rows.forEach(row => table.appendChild(row));
|
|
104
|
+
}
|
|
105
|
+
</script>`;
|
|
106
|
+
|
|
107
|
+
// src/html.ts
|
|
108
|
+
var head = (title) => `<head>
|
|
109
|
+
<meta charset="UTF-8">
|
|
110
|
+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
111
|
+
<title>${title}</title>
|
|
112
|
+
${style}
|
|
113
|
+
</head>`;
|
|
114
|
+
var body = (title, parentLink, rows) => `
|
|
115
|
+
<body>
|
|
116
|
+
<h1 id="header">${title}</h1>
|
|
117
|
+
|
|
118
|
+
${parentLink ? `
|
|
119
|
+
<div id="parentDirLinkBox">
|
|
120
|
+
<a id="parentDirLink" class="icon up" href="${parentLink}">
|
|
121
|
+
<span id="parentDirText">[parent directory]</span>
|
|
122
|
+
</a>
|
|
123
|
+
</div>` : ""}
|
|
124
|
+
|
|
125
|
+
<table>
|
|
126
|
+
<thead>
|
|
127
|
+
<tr class="header" id="theader">
|
|
128
|
+
<th onclick="sortTable(0)">Name</th>
|
|
129
|
+
<th onclick="sortTable(1)" class="detailsColumn">Size</th>
|
|
130
|
+
<th onclick="sortTable(2)" class="detailsColumn">Date Modified</th>
|
|
131
|
+
</tr>
|
|
132
|
+
</thead>
|
|
133
|
+
<tbody id="tbody">
|
|
134
|
+
${rows}
|
|
135
|
+
</tbody>
|
|
136
|
+
</table>
|
|
137
|
+
|
|
138
|
+
${script}
|
|
139
|
+
</body>`;
|
|
140
|
+
|
|
141
|
+
// src/index.ts
|
|
142
|
+
function formatSize(bytes) {
|
|
143
|
+
if (bytes === 0) return "";
|
|
144
|
+
const units = ["B", "kB", "MB", "GB", "TB"];
|
|
145
|
+
const i = Math.floor(Math.log(bytes) / Math.log(1024));
|
|
146
|
+
return `${(bytes / Math.pow(1024, i)).toFixed(2)} ${units[i]}`.replace(/\.00\s/, " ");
|
|
147
|
+
}
|
|
148
|
+
function generateRowHtml(file) {
|
|
149
|
+
const iconClass = file.isDir ? "dir" : "file";
|
|
150
|
+
const href = file.isDir ? `${file.name}/` : file.name;
|
|
151
|
+
const value = file.isDir ? `${file.name}/` : file.name;
|
|
152
|
+
return `<tr data-is-dir="${file.isDir}">
|
|
153
|
+
<td data-value="${value}"><a class="icon ${iconClass}" href="${href}">${value}</a></td>
|
|
154
|
+
<td class="detailsColumn" data-value="${file.size}">${file.sizeStr}</td>
|
|
155
|
+
<td class="detailsColumn" data-value="${file.mtime}">${file.mtimeStr}</td>
|
|
156
|
+
</tr>`;
|
|
157
|
+
}
|
|
158
|
+
function generateHtml(rootLocation2, files, relativePath) {
|
|
159
|
+
const title = `Index of ${"/" + relativePath || "/"}`;
|
|
160
|
+
const parentPath = relativePath ? import_node_path.default.dirname(relativePath) : null;
|
|
161
|
+
const parentLink = parentPath === "." ? rootLocation2 : parentPath ? `${rootLocation2}${parentPath}/` : null;
|
|
162
|
+
const rows = files.map(generateRowHtml).join("");
|
|
163
|
+
return `
|
|
164
|
+
<!DOCTYPE html>
|
|
165
|
+
<html lang="en">
|
|
166
|
+
${head(title)}
|
|
167
|
+
|
|
168
|
+
${body(title, parentLink, rows)}
|
|
169
|
+
|
|
170
|
+
</html>`;
|
|
171
|
+
}
|
|
172
|
+
async function generateStaticListing(baseDir, outputDir2, ignoreList2, rootLocation2 = "/") {
|
|
173
|
+
const absoluteBaseDir = import_node_path.default.isAbsolute(baseDir) ? baseDir : import_node_path.default.resolve(process.cwd(), baseDir);
|
|
174
|
+
const absoluteOutputDir = import_node_path.default.isAbsolute(outputDir2) ? outputDir2 : import_node_path.default.resolve(process.cwd(), outputDir2);
|
|
175
|
+
var filesListed = 0;
|
|
176
|
+
var dirsListed = 0;
|
|
177
|
+
if (!import_node_fs.default.existsSync(absoluteBaseDir)) {
|
|
178
|
+
throw new Error(`Source directory not found: ${absoluteBaseDir}`);
|
|
179
|
+
}
|
|
180
|
+
const ig = ignoreList2 ? (0, import_ignore.default)().add(ignoreList2) : null;
|
|
181
|
+
const traverse = (currentDir, relativePath) => {
|
|
182
|
+
const items = import_node_fs.default.readdirSync(currentDir);
|
|
183
|
+
const filesInfo = [];
|
|
184
|
+
for (const item of items) {
|
|
185
|
+
const fullPath = import_node_path.default.join(currentDir, item);
|
|
186
|
+
const itemRelativePath = import_node_path.default.join(relativePath, item);
|
|
187
|
+
if (ig && ig.ignores(itemRelativePath)) {
|
|
188
|
+
continue;
|
|
189
|
+
}
|
|
190
|
+
const stats = import_node_fs.default.statSync(fullPath);
|
|
191
|
+
const isDir = stats.isDirectory();
|
|
192
|
+
filesInfo.push({
|
|
193
|
+
name: item,
|
|
194
|
+
isDir,
|
|
195
|
+
size: isDir ? 0 : stats.size,
|
|
196
|
+
sizeStr: isDir ? "" : formatSize(stats.size),
|
|
197
|
+
mtime: stats.mtimeMs,
|
|
198
|
+
mtimeStr: stats.mtime.toLocaleString()
|
|
199
|
+
});
|
|
200
|
+
if (isDir) {
|
|
201
|
+
dirsListed++;
|
|
202
|
+
traverse(fullPath, itemRelativePath);
|
|
203
|
+
} else {
|
|
204
|
+
filesListed++;
|
|
205
|
+
const targetFilePath = import_node_path.default.join(absoluteOutputDir, itemRelativePath);
|
|
206
|
+
const targetFileDir = import_node_path.default.dirname(targetFilePath);
|
|
207
|
+
if (!import_node_fs.default.existsSync(targetFileDir)) {
|
|
208
|
+
import_node_fs.default.mkdirSync(targetFileDir, { recursive: true });
|
|
209
|
+
}
|
|
210
|
+
import_node_fs.default.copyFileSync(fullPath, targetFilePath);
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
const html = generateHtml(rootLocation2, filesInfo.sort((a, b) => {
|
|
214
|
+
if (a.isDir && !b.isDir) return -1;
|
|
215
|
+
if (!a.isDir && b.isDir) return 1;
|
|
216
|
+
return a.name.localeCompare(b.name);
|
|
217
|
+
}), relativePath);
|
|
218
|
+
const targetDir = import_node_path.default.join(absoluteOutputDir, relativePath);
|
|
219
|
+
if (!import_node_fs.default.existsSync(targetDir)) {
|
|
220
|
+
import_node_fs.default.mkdirSync(targetDir, { recursive: true });
|
|
221
|
+
}
|
|
222
|
+
const outputFilePath = import_node_path.default.join(targetDir, "index.html");
|
|
223
|
+
import_node_fs.default.writeFileSync(outputFilePath, html, "utf8");
|
|
224
|
+
console.log(`Generated: ${outputFilePath} (${filesInfo.length} items)`);
|
|
225
|
+
};
|
|
226
|
+
traverse(absoluteBaseDir, "");
|
|
227
|
+
return {
|
|
228
|
+
filesListed,
|
|
229
|
+
dirsListed
|
|
230
|
+
};
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
// src/cli.ts
|
|
234
|
+
var import_node_fs2 = __toESM(require("fs"), 1);
|
|
235
|
+
var import_node_path2 = __toESM(require("path"), 1);
|
|
236
|
+
var import_meta = {};
|
|
237
|
+
var color = {
|
|
238
|
+
reset: "\x1B[0m",
|
|
239
|
+
bold: "\x1B[1m",
|
|
240
|
+
red: "\x1B[31m",
|
|
241
|
+
yellow: "\x1B[33m",
|
|
242
|
+
green: "\x1B[32m",
|
|
243
|
+
cyan: "\x1B[36m"
|
|
244
|
+
};
|
|
245
|
+
function printHelp() {
|
|
246
|
+
console.log(`
|
|
247
|
+
${color.bold}static-dir-list${color.reset}
|
|
248
|
+
|
|
249
|
+
Generate static HTML directory listings for a folder.
|
|
250
|
+
|
|
251
|
+
${color.bold}Usage:${color.reset}
|
|
252
|
+
static-dir-list <source-dir> [output-dir] [options]
|
|
253
|
+
|
|
254
|
+
${color.bold}Arguments:${color.reset}
|
|
255
|
+
<source-dir> Directory to scan (required)
|
|
256
|
+
[output-dir] Directory to write output (default: source-dir)
|
|
257
|
+
|
|
258
|
+
${color.bold}Options:${color.reset}
|
|
259
|
+
-if, --ignore-file <file> Path to ignore file (can be used multiple times)
|
|
260
|
+
-root <path> Root path for generated links (default: /)
|
|
261
|
+
-h, --help Show this help message
|
|
262
|
+
-v, --version Show CLI version
|
|
263
|
+
|
|
264
|
+
${color.bold}Examples:${color.reset}
|
|
265
|
+
static-dir-list ./public/files
|
|
266
|
+
static-dir-list ./public/files ./dist/files
|
|
267
|
+
static-dir-list ./public/files -if .gitignore
|
|
268
|
+
static-dir-list ./public/files ./dist -if .gitignore -root /files/
|
|
269
|
+
|
|
270
|
+
${color.bold}Notes:${color.reset}
|
|
271
|
+
- Ignore files use one pattern per line
|
|
272
|
+
- Empty lines and lines starting with "#" are ignored
|
|
273
|
+
- Multiple ignore files are merged
|
|
274
|
+
`);
|
|
275
|
+
}
|
|
276
|
+
function printVersion() {
|
|
277
|
+
try {
|
|
278
|
+
const pkgPath = new URL("../package.json", import_meta.url);
|
|
279
|
+
const pkg = JSON.parse(import_node_fs2.default.readFileSync(pkgPath, "utf8"));
|
|
280
|
+
console.log(`static-dir-list v${pkg.version}`);
|
|
281
|
+
} catch {
|
|
282
|
+
console.log("static-dir-list (version unknown)");
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
var args = process.argv.slice(2);
|
|
286
|
+
if (args.length === 0 || args.includes("-h") || args.includes("--help")) {
|
|
287
|
+
printHelp();
|
|
288
|
+
process.exit(0);
|
|
289
|
+
}
|
|
290
|
+
if (args.includes("-v") || args.includes("--version")) {
|
|
291
|
+
printVersion();
|
|
292
|
+
process.exit(0);
|
|
293
|
+
}
|
|
294
|
+
var sourceDir = "";
|
|
295
|
+
var outputDir = "";
|
|
296
|
+
var rootLocation = "/";
|
|
297
|
+
var ignoreFiles = [];
|
|
298
|
+
var ignoreList = [];
|
|
299
|
+
for (let i = 0; i < args.length; i++) {
|
|
300
|
+
if (args[i] === "-if" || args[i] === "--ignore-file") {
|
|
301
|
+
if (i + 1 < args.length) {
|
|
302
|
+
ignoreFiles.push(args[++i]);
|
|
303
|
+
}
|
|
304
|
+
} else if (args[i] === "-root") {
|
|
305
|
+
if (i + 1 < args.length) {
|
|
306
|
+
rootLocation = args[++i];
|
|
307
|
+
}
|
|
308
|
+
} else if (!sourceDir) {
|
|
309
|
+
sourceDir = args[i];
|
|
310
|
+
} else if (!outputDir) {
|
|
311
|
+
outputDir = args[i];
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
if (!sourceDir) {
|
|
315
|
+
console.error(`${color.red}Error:${color.reset} Missing <source-dir>
|
|
316
|
+
`);
|
|
317
|
+
printHelp();
|
|
318
|
+
process.exit(1);
|
|
319
|
+
}
|
|
320
|
+
if (!outputDir) {
|
|
321
|
+
outputDir = sourceDir;
|
|
322
|
+
}
|
|
323
|
+
for (const file of ignoreFiles) {
|
|
324
|
+
const absolutePath = import_node_path2.default.resolve(process.cwd(), file);
|
|
325
|
+
if (import_node_fs2.default.existsSync(absolutePath)) {
|
|
326
|
+
const content = import_node_fs2.default.readFileSync(absolutePath, "utf8");
|
|
327
|
+
ignoreList.push(
|
|
328
|
+
...content.split(/\r?\n/).filter((line) => line.trim() !== "" && !line.startsWith("#"))
|
|
329
|
+
);
|
|
330
|
+
} else {
|
|
331
|
+
console.warn(
|
|
332
|
+
`${color.yellow}Warning:${color.reset} Ignore file not found: ${absolutePath}`
|
|
333
|
+
);
|
|
334
|
+
}
|
|
335
|
+
}
|
|
336
|
+
generateStaticListing(
|
|
337
|
+
sourceDir,
|
|
338
|
+
outputDir,
|
|
339
|
+
ignoreList.length > 0 ? ignoreList : void 0,
|
|
340
|
+
rootLocation
|
|
341
|
+
).then(({ filesListed, dirsListed }) => {
|
|
342
|
+
console.log(
|
|
343
|
+
`${color.green}Done:${color.reset} Generated ${color.cyan}${filesListed}${color.reset} files and ${color.cyan}${dirsListed}${color.reset} directories.`
|
|
344
|
+
);
|
|
345
|
+
}).catch((err) => {
|
|
346
|
+
console.error(`${color.red}Error:${color.reset} Failed to generate listings`);
|
|
347
|
+
console.error(err);
|
|
348
|
+
process.exit(1);
|
|
349
|
+
});
|
package/dist/cli.d.cts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
package/dist/cli.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
package/dist/cli.js
ADDED
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import {
|
|
3
|
+
generateStaticListing
|
|
4
|
+
} from "./chunk-ZH66Q66Y.js";
|
|
5
|
+
|
|
6
|
+
// src/cli.ts
|
|
7
|
+
import fs from "fs";
|
|
8
|
+
import path from "path";
|
|
9
|
+
var color = {
|
|
10
|
+
reset: "\x1B[0m",
|
|
11
|
+
bold: "\x1B[1m",
|
|
12
|
+
red: "\x1B[31m",
|
|
13
|
+
yellow: "\x1B[33m",
|
|
14
|
+
green: "\x1B[32m",
|
|
15
|
+
cyan: "\x1B[36m"
|
|
16
|
+
};
|
|
17
|
+
function printHelp() {
|
|
18
|
+
console.log(`
|
|
19
|
+
${color.bold}static-dir-list${color.reset}
|
|
20
|
+
|
|
21
|
+
Generate static HTML directory listings for a folder.
|
|
22
|
+
|
|
23
|
+
${color.bold}Usage:${color.reset}
|
|
24
|
+
static-dir-list <source-dir> [output-dir] [options]
|
|
25
|
+
|
|
26
|
+
${color.bold}Arguments:${color.reset}
|
|
27
|
+
<source-dir> Directory to scan (required)
|
|
28
|
+
[output-dir] Directory to write output (default: source-dir)
|
|
29
|
+
|
|
30
|
+
${color.bold}Options:${color.reset}
|
|
31
|
+
-if, --ignore-file <file> Path to ignore file (can be used multiple times)
|
|
32
|
+
-root <path> Root path for generated links (default: /)
|
|
33
|
+
-h, --help Show this help message
|
|
34
|
+
-v, --version Show CLI version
|
|
35
|
+
|
|
36
|
+
${color.bold}Examples:${color.reset}
|
|
37
|
+
static-dir-list ./public/files
|
|
38
|
+
static-dir-list ./public/files ./dist/files
|
|
39
|
+
static-dir-list ./public/files -if .gitignore
|
|
40
|
+
static-dir-list ./public/files ./dist -if .gitignore -root /files/
|
|
41
|
+
|
|
42
|
+
${color.bold}Notes:${color.reset}
|
|
43
|
+
- Ignore files use one pattern per line
|
|
44
|
+
- Empty lines and lines starting with "#" are ignored
|
|
45
|
+
- Multiple ignore files are merged
|
|
46
|
+
`);
|
|
47
|
+
}
|
|
48
|
+
function printVersion() {
|
|
49
|
+
try {
|
|
50
|
+
const pkgPath = new URL("../package.json", import.meta.url);
|
|
51
|
+
const pkg = JSON.parse(fs.readFileSync(pkgPath, "utf8"));
|
|
52
|
+
console.log(`static-dir-list v${pkg.version}`);
|
|
53
|
+
} catch {
|
|
54
|
+
console.log("static-dir-list (version unknown)");
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
var args = process.argv.slice(2);
|
|
58
|
+
if (args.length === 0 || args.includes("-h") || args.includes("--help")) {
|
|
59
|
+
printHelp();
|
|
60
|
+
process.exit(0);
|
|
61
|
+
}
|
|
62
|
+
if (args.includes("-v") || args.includes("--version")) {
|
|
63
|
+
printVersion();
|
|
64
|
+
process.exit(0);
|
|
65
|
+
}
|
|
66
|
+
var sourceDir = "";
|
|
67
|
+
var outputDir = "";
|
|
68
|
+
var rootLocation = "/";
|
|
69
|
+
var ignoreFiles = [];
|
|
70
|
+
var ignoreList = [];
|
|
71
|
+
for (let i = 0; i < args.length; i++) {
|
|
72
|
+
if (args[i] === "-if" || args[i] === "--ignore-file") {
|
|
73
|
+
if (i + 1 < args.length) {
|
|
74
|
+
ignoreFiles.push(args[++i]);
|
|
75
|
+
}
|
|
76
|
+
} else if (args[i] === "-root") {
|
|
77
|
+
if (i + 1 < args.length) {
|
|
78
|
+
rootLocation = args[++i];
|
|
79
|
+
}
|
|
80
|
+
} else if (!sourceDir) {
|
|
81
|
+
sourceDir = args[i];
|
|
82
|
+
} else if (!outputDir) {
|
|
83
|
+
outputDir = args[i];
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
if (!sourceDir) {
|
|
87
|
+
console.error(`${color.red}Error:${color.reset} Missing <source-dir>
|
|
88
|
+
`);
|
|
89
|
+
printHelp();
|
|
90
|
+
process.exit(1);
|
|
91
|
+
}
|
|
92
|
+
if (!outputDir) {
|
|
93
|
+
outputDir = sourceDir;
|
|
94
|
+
}
|
|
95
|
+
for (const file of ignoreFiles) {
|
|
96
|
+
const absolutePath = path.resolve(process.cwd(), file);
|
|
97
|
+
if (fs.existsSync(absolutePath)) {
|
|
98
|
+
const content = fs.readFileSync(absolutePath, "utf8");
|
|
99
|
+
ignoreList.push(
|
|
100
|
+
...content.split(/\r?\n/).filter((line) => line.trim() !== "" && !line.startsWith("#"))
|
|
101
|
+
);
|
|
102
|
+
} else {
|
|
103
|
+
console.warn(
|
|
104
|
+
`${color.yellow}Warning:${color.reset} Ignore file not found: ${absolutePath}`
|
|
105
|
+
);
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
generateStaticListing(
|
|
109
|
+
sourceDir,
|
|
110
|
+
outputDir,
|
|
111
|
+
ignoreList.length > 0 ? ignoreList : void 0,
|
|
112
|
+
rootLocation
|
|
113
|
+
).then(({ filesListed, dirsListed }) => {
|
|
114
|
+
console.log(
|
|
115
|
+
`${color.green}Done:${color.reset} Generated ${color.cyan}${filesListed}${color.reset} files and ${color.cyan}${dirsListed}${color.reset} directories.`
|
|
116
|
+
);
|
|
117
|
+
}).catch((err) => {
|
|
118
|
+
console.error(`${color.red}Error:${color.reset} Failed to generate listings`);
|
|
119
|
+
console.error(err);
|
|
120
|
+
process.exit(1);
|
|
121
|
+
});
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,244 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
28
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
+
|
|
30
|
+
// src/index.ts
|
|
31
|
+
var index_exports = {};
|
|
32
|
+
__export(index_exports, {
|
|
33
|
+
generateStaticListing: () => generateStaticListing
|
|
34
|
+
});
|
|
35
|
+
module.exports = __toCommonJS(index_exports);
|
|
36
|
+
var import_node_fs = __toESM(require("fs"), 1);
|
|
37
|
+
var import_node_path = __toESM(require("path"), 1);
|
|
38
|
+
var import_ignore = __toESM(require("ignore"), 1);
|
|
39
|
+
|
|
40
|
+
// src/static.ts
|
|
41
|
+
var style = `<style>
|
|
42
|
+
body { font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif; padding: 20px; color: #333; line-height: 1.5; background-color: #fff; }
|
|
43
|
+
h1 { font-size: 1.5rem; border-bottom: 1px solid #eee; padding-bottom: 0.75rem; margin-bottom: 1.5rem; }
|
|
44
|
+
table { width: 100%; border-collapse: collapse; }
|
|
45
|
+
th, td { text-align: left; padding: 0.75rem; border-bottom: 1px solid #f0f0f0; }
|
|
46
|
+
th { background: #fafafa; font-weight: 600; font-size: 0.875rem; text-transform: uppercase; letter-spacing: 0.05em; color: #666; cursor: pointer; user-select: none; position: relative; }
|
|
47
|
+
th:hover { background: #f0f0f0; }
|
|
48
|
+
th.sort-asc::after { content: ' \u2191'; position: absolute; }
|
|
49
|
+
th.sort-desc::after { content: ' \u2193'; position: absolute; }
|
|
50
|
+
.detailsColumn { text-align: right; color: #888; font-size: 0.875rem; font-variant-numeric: tabular-nums; }
|
|
51
|
+
.icon { text-decoration: none; color: #2563eb; font-weight: 500; }
|
|
52
|
+
.icon:hover { text-decoration: underline; color: #1d4ed8; }
|
|
53
|
+
.dir::before { content: "\u{1F4C1} "; margin-right: 0.5rem; }
|
|
54
|
+
.file::before { content: "\u{1F4C4} "; margin-right: 0.5rem; }
|
|
55
|
+
.up::before { content: "\u2B06\uFE0F "; margin-right: 0.5rem; }
|
|
56
|
+
#parentDirLinkBox { margin-bottom: 1rem; }
|
|
57
|
+
|
|
58
|
+
@media (prefers-color-scheme: dark) {
|
|
59
|
+
body { background-color: #121212; color: #e0e0e0; }
|
|
60
|
+
h1 { border-bottom-color: #333; }
|
|
61
|
+
th { background: #1e1e1e; color: #aaa; border-bottom-color: #333; }
|
|
62
|
+
th:hover { background: #2a2a2a; }
|
|
63
|
+
td { border-bottom-color: #222; }
|
|
64
|
+
.detailsColumn { color: #999; }
|
|
65
|
+
.icon { color: #60a5fa; }
|
|
66
|
+
.icon:hover { color: #93c5fd; }
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
@media (max-width: 640px) {
|
|
70
|
+
.detailsColumn { display: none; }
|
|
71
|
+
}
|
|
72
|
+
</style>`;
|
|
73
|
+
var script = ` <script>
|
|
74
|
+
function sortTable(n) {
|
|
75
|
+
const table = document.getElementById("tbody");
|
|
76
|
+
const headers = document.querySelectorAll("th");
|
|
77
|
+
let rows = Array.from(table.rows);
|
|
78
|
+
let dir = headers[n].getAttribute("data-dir") === "asc" ? "desc" : "asc";
|
|
79
|
+
|
|
80
|
+
// Reset headers
|
|
81
|
+
headers.forEach(h => {
|
|
82
|
+
h.classList.remove("sort-asc", "sort-desc");
|
|
83
|
+
h.removeAttribute("data-dir");
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
headers[n].classList.add(dir === "asc" ? "sort-asc" : "sort-desc");
|
|
87
|
+
headers[n].setAttribute("data-dir", dir);
|
|
88
|
+
|
|
89
|
+
rows.sort((a, b) => {
|
|
90
|
+
const aIsDir = a.getAttribute("data-is-dir") === "true";
|
|
91
|
+
const bIsDir = b.getAttribute("data-is-dir") === "true";
|
|
92
|
+
|
|
93
|
+
if (aIsDir && !bIsDir) return -1;
|
|
94
|
+
if (!aIsDir && bIsDir) return 1;
|
|
95
|
+
|
|
96
|
+
let x = a.cells[n].getAttribute("data-value");
|
|
97
|
+
let y = b.cells[n].getAttribute("data-value");
|
|
98
|
+
|
|
99
|
+
if (n > 0) { // Size or Date (numeric)
|
|
100
|
+
x = parseFloat(x);
|
|
101
|
+
y = parseFloat(y);
|
|
102
|
+
} else { // Name (string)
|
|
103
|
+
x = x.toLowerCase();
|
|
104
|
+
y = y.toLowerCase();
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
if (x < y) return dir === "asc" ? -1 : 1;
|
|
108
|
+
if (x > y) return dir === "asc" ? 1 : -1;
|
|
109
|
+
return 0;
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
rows.forEach(row => table.appendChild(row));
|
|
113
|
+
}
|
|
114
|
+
</script>`;
|
|
115
|
+
|
|
116
|
+
// src/html.ts
|
|
117
|
+
var head = (title) => `<head>
|
|
118
|
+
<meta charset="UTF-8">
|
|
119
|
+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
120
|
+
<title>${title}</title>
|
|
121
|
+
${style}
|
|
122
|
+
</head>`;
|
|
123
|
+
var body = (title, parentLink, rows) => `
|
|
124
|
+
<body>
|
|
125
|
+
<h1 id="header">${title}</h1>
|
|
126
|
+
|
|
127
|
+
${parentLink ? `
|
|
128
|
+
<div id="parentDirLinkBox">
|
|
129
|
+
<a id="parentDirLink" class="icon up" href="${parentLink}">
|
|
130
|
+
<span id="parentDirText">[parent directory]</span>
|
|
131
|
+
</a>
|
|
132
|
+
</div>` : ""}
|
|
133
|
+
|
|
134
|
+
<table>
|
|
135
|
+
<thead>
|
|
136
|
+
<tr class="header" id="theader">
|
|
137
|
+
<th onclick="sortTable(0)">Name</th>
|
|
138
|
+
<th onclick="sortTable(1)" class="detailsColumn">Size</th>
|
|
139
|
+
<th onclick="sortTable(2)" class="detailsColumn">Date Modified</th>
|
|
140
|
+
</tr>
|
|
141
|
+
</thead>
|
|
142
|
+
<tbody id="tbody">
|
|
143
|
+
${rows}
|
|
144
|
+
</tbody>
|
|
145
|
+
</table>
|
|
146
|
+
|
|
147
|
+
${script}
|
|
148
|
+
</body>`;
|
|
149
|
+
|
|
150
|
+
// src/index.ts
|
|
151
|
+
function formatSize(bytes) {
|
|
152
|
+
if (bytes === 0) return "";
|
|
153
|
+
const units = ["B", "kB", "MB", "GB", "TB"];
|
|
154
|
+
const i = Math.floor(Math.log(bytes) / Math.log(1024));
|
|
155
|
+
return `${(bytes / Math.pow(1024, i)).toFixed(2)} ${units[i]}`.replace(/\.00\s/, " ");
|
|
156
|
+
}
|
|
157
|
+
function generateRowHtml(file) {
|
|
158
|
+
const iconClass = file.isDir ? "dir" : "file";
|
|
159
|
+
const href = file.isDir ? `${file.name}/` : file.name;
|
|
160
|
+
const value = file.isDir ? `${file.name}/` : file.name;
|
|
161
|
+
return `<tr data-is-dir="${file.isDir}">
|
|
162
|
+
<td data-value="${value}"><a class="icon ${iconClass}" href="${href}">${value}</a></td>
|
|
163
|
+
<td class="detailsColumn" data-value="${file.size}">${file.sizeStr}</td>
|
|
164
|
+
<td class="detailsColumn" data-value="${file.mtime}">${file.mtimeStr}</td>
|
|
165
|
+
</tr>`;
|
|
166
|
+
}
|
|
167
|
+
function generateHtml(rootLocation, files, relativePath) {
|
|
168
|
+
const title = `Index of ${"/" + relativePath || "/"}`;
|
|
169
|
+
const parentPath = relativePath ? import_node_path.default.dirname(relativePath) : null;
|
|
170
|
+
const parentLink = parentPath === "." ? rootLocation : parentPath ? `${rootLocation}${parentPath}/` : null;
|
|
171
|
+
const rows = files.map(generateRowHtml).join("");
|
|
172
|
+
return `
|
|
173
|
+
<!DOCTYPE html>
|
|
174
|
+
<html lang="en">
|
|
175
|
+
${head(title)}
|
|
176
|
+
|
|
177
|
+
${body(title, parentLink, rows)}
|
|
178
|
+
|
|
179
|
+
</html>`;
|
|
180
|
+
}
|
|
181
|
+
async function generateStaticListing(baseDir, outputDir, ignoreList, rootLocation = "/") {
|
|
182
|
+
const absoluteBaseDir = import_node_path.default.isAbsolute(baseDir) ? baseDir : import_node_path.default.resolve(process.cwd(), baseDir);
|
|
183
|
+
const absoluteOutputDir = import_node_path.default.isAbsolute(outputDir) ? outputDir : import_node_path.default.resolve(process.cwd(), outputDir);
|
|
184
|
+
var filesListed = 0;
|
|
185
|
+
var dirsListed = 0;
|
|
186
|
+
if (!import_node_fs.default.existsSync(absoluteBaseDir)) {
|
|
187
|
+
throw new Error(`Source directory not found: ${absoluteBaseDir}`);
|
|
188
|
+
}
|
|
189
|
+
const ig = ignoreList ? (0, import_ignore.default)().add(ignoreList) : null;
|
|
190
|
+
const traverse = (currentDir, relativePath) => {
|
|
191
|
+
const items = import_node_fs.default.readdirSync(currentDir);
|
|
192
|
+
const filesInfo = [];
|
|
193
|
+
for (const item of items) {
|
|
194
|
+
const fullPath = import_node_path.default.join(currentDir, item);
|
|
195
|
+
const itemRelativePath = import_node_path.default.join(relativePath, item);
|
|
196
|
+
if (ig && ig.ignores(itemRelativePath)) {
|
|
197
|
+
continue;
|
|
198
|
+
}
|
|
199
|
+
const stats = import_node_fs.default.statSync(fullPath);
|
|
200
|
+
const isDir = stats.isDirectory();
|
|
201
|
+
filesInfo.push({
|
|
202
|
+
name: item,
|
|
203
|
+
isDir,
|
|
204
|
+
size: isDir ? 0 : stats.size,
|
|
205
|
+
sizeStr: isDir ? "" : formatSize(stats.size),
|
|
206
|
+
mtime: stats.mtimeMs,
|
|
207
|
+
mtimeStr: stats.mtime.toLocaleString()
|
|
208
|
+
});
|
|
209
|
+
if (isDir) {
|
|
210
|
+
dirsListed++;
|
|
211
|
+
traverse(fullPath, itemRelativePath);
|
|
212
|
+
} else {
|
|
213
|
+
filesListed++;
|
|
214
|
+
const targetFilePath = import_node_path.default.join(absoluteOutputDir, itemRelativePath);
|
|
215
|
+
const targetFileDir = import_node_path.default.dirname(targetFilePath);
|
|
216
|
+
if (!import_node_fs.default.existsSync(targetFileDir)) {
|
|
217
|
+
import_node_fs.default.mkdirSync(targetFileDir, { recursive: true });
|
|
218
|
+
}
|
|
219
|
+
import_node_fs.default.copyFileSync(fullPath, targetFilePath);
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
const html = generateHtml(rootLocation, filesInfo.sort((a, b) => {
|
|
223
|
+
if (a.isDir && !b.isDir) return -1;
|
|
224
|
+
if (!a.isDir && b.isDir) return 1;
|
|
225
|
+
return a.name.localeCompare(b.name);
|
|
226
|
+
}), relativePath);
|
|
227
|
+
const targetDir = import_node_path.default.join(absoluteOutputDir, relativePath);
|
|
228
|
+
if (!import_node_fs.default.existsSync(targetDir)) {
|
|
229
|
+
import_node_fs.default.mkdirSync(targetDir, { recursive: true });
|
|
230
|
+
}
|
|
231
|
+
const outputFilePath = import_node_path.default.join(targetDir, "index.html");
|
|
232
|
+
import_node_fs.default.writeFileSync(outputFilePath, html, "utf8");
|
|
233
|
+
console.log(`Generated: ${outputFilePath} (${filesInfo.length} items)`);
|
|
234
|
+
};
|
|
235
|
+
traverse(absoluteBaseDir, "");
|
|
236
|
+
return {
|
|
237
|
+
filesListed,
|
|
238
|
+
dirsListed
|
|
239
|
+
};
|
|
240
|
+
}
|
|
241
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
242
|
+
0 && (module.exports = {
|
|
243
|
+
generateStaticListing
|
|
244
|
+
});
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
interface FileInfo {
|
|
2
|
+
name: string;
|
|
3
|
+
isDir: boolean;
|
|
4
|
+
size: number;
|
|
5
|
+
sizeStr: string;
|
|
6
|
+
mtime: number;
|
|
7
|
+
mtimeStr: string;
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* Generates static HTML directory listings for a directory and its subdirectories.
|
|
11
|
+
*
|
|
12
|
+
* @param baseDir - The directory to scan for files (relative to project root or absolute).
|
|
13
|
+
* @param outputDir - The directory where the generated index.html files will be saved.
|
|
14
|
+
* @param ignoreList - An optional list of patterns to ignore (similar to .gitignore).
|
|
15
|
+
* @param rootLocation - The base URL path where the static files will be served from (defaults to "/").
|
|
16
|
+
*/
|
|
17
|
+
declare function generateStaticListing(baseDir: string, outputDir: string, ignoreList?: string[], rootLocation?: string): Promise<{
|
|
18
|
+
filesListed: number;
|
|
19
|
+
dirsListed: number;
|
|
20
|
+
}>;
|
|
21
|
+
|
|
22
|
+
export { type FileInfo, generateStaticListing };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
interface FileInfo {
|
|
2
|
+
name: string;
|
|
3
|
+
isDir: boolean;
|
|
4
|
+
size: number;
|
|
5
|
+
sizeStr: string;
|
|
6
|
+
mtime: number;
|
|
7
|
+
mtimeStr: string;
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* Generates static HTML directory listings for a directory and its subdirectories.
|
|
11
|
+
*
|
|
12
|
+
* @param baseDir - The directory to scan for files (relative to project root or absolute).
|
|
13
|
+
* @param outputDir - The directory where the generated index.html files will be saved.
|
|
14
|
+
* @param ignoreList - An optional list of patterns to ignore (similar to .gitignore).
|
|
15
|
+
* @param rootLocation - The base URL path where the static files will be served from (defaults to "/").
|
|
16
|
+
*/
|
|
17
|
+
declare function generateStaticListing(baseDir: string, outputDir: string, ignoreList?: string[], rootLocation?: string): Promise<{
|
|
18
|
+
filesListed: number;
|
|
19
|
+
dirsListed: number;
|
|
20
|
+
}>;
|
|
21
|
+
|
|
22
|
+
export { type FileInfo, generateStaticListing };
|
package/dist/index.js
ADDED
package/package.json
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "static-dir-list",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "A Node.js library and CLI tool that generates static HTML directory listings for a specified directory.",
|
|
5
|
+
"repository": {
|
|
6
|
+
"type": "git",
|
|
7
|
+
"url": "git+https://github.com/hugo-olabi/static-dir-list.git"
|
|
8
|
+
},
|
|
9
|
+
"homepage": "https://hugo-olabi.github.io/static-dir-list/",
|
|
10
|
+
"bugs": {
|
|
11
|
+
"url": "https://github.com/hugo-olabi/static-dir-list/issues"
|
|
12
|
+
},
|
|
13
|
+
"keywords": [
|
|
14
|
+
"directory listing",
|
|
15
|
+
"static html",
|
|
16
|
+
"file browser",
|
|
17
|
+
"cli",
|
|
18
|
+
"nodejs",
|
|
19
|
+
"static site",
|
|
20
|
+
"index generator"
|
|
21
|
+
],
|
|
22
|
+
"license": "MIT",
|
|
23
|
+
"type": "module",
|
|
24
|
+
"main": "./dist/index.cjs",
|
|
25
|
+
"module": "./dist/index.js",
|
|
26
|
+
"types": "./dist/index.d.ts",
|
|
27
|
+
"bin": {
|
|
28
|
+
"static-dir-list": "dist/cli.js"
|
|
29
|
+
},
|
|
30
|
+
"exports": {
|
|
31
|
+
".": {
|
|
32
|
+
"import": "./dist/index.js",
|
|
33
|
+
"require": "./dist/index.cjs",
|
|
34
|
+
"types": "./dist/index.d.ts"
|
|
35
|
+
}
|
|
36
|
+
},
|
|
37
|
+
"files": [
|
|
38
|
+
"dist",
|
|
39
|
+
"README.md"
|
|
40
|
+
],
|
|
41
|
+
"scripts": {
|
|
42
|
+
"build": "tsup src/index.ts src/cli.ts --format cjs,esm --dts --clean && chmod +x dist/cli.js",
|
|
43
|
+
"lint": "tsc --noEmit",
|
|
44
|
+
"clean": "rm -rf dist"
|
|
45
|
+
},
|
|
46
|
+
"devDependencies": {
|
|
47
|
+
"@types/node": "^22.14.0",
|
|
48
|
+
"tsup": "^8.5.1",
|
|
49
|
+
"typescript": "~5.8.2"
|
|
50
|
+
},
|
|
51
|
+
"dependencies": {
|
|
52
|
+
"ignore": "^7.0.5"
|
|
53
|
+
}
|
|
54
|
+
}
|