Better code clarity
This commit is contained in:
parent
6c3c71755a
commit
7543295e4e
|
@ -8,35 +8,29 @@ const unpkgRewrite = require("../plugins/unpkgRewrite");
|
|||
const addLeadingSlash = require("../utils/addLeadingSlash");
|
||||
const renderPage = require("../utils/renderPage");
|
||||
|
||||
function getContentTypeHeader(type) {
|
||||
return type === "application/javascript" ? type + "; charset=utf-8" : type;
|
||||
function getMatchingEntries(entry, entries) {
|
||||
const dirname = entry.name || ".";
|
||||
|
||||
return Object.keys(entries)
|
||||
.filter(name => entry.name !== name && path.dirname(name) === dirname)
|
||||
.map(name => entries[name]);
|
||||
}
|
||||
|
||||
function getMetadata(entry, entries) {
|
||||
const metadata = Object.assign(
|
||||
{
|
||||
path: addLeadingSlash(entry.name)
|
||||
},
|
||||
entry.type === "file"
|
||||
? {
|
||||
type: entry.type,
|
||||
contentType: entry.contentType,
|
||||
integrity: entry.integrity,
|
||||
lastModified: entry.lastModified,
|
||||
size: entry.size
|
||||
}
|
||||
: {
|
||||
type: entry.type
|
||||
}
|
||||
);
|
||||
const metadata = {
|
||||
path: addLeadingSlash(entry.name),
|
||||
type: entry.type
|
||||
};
|
||||
|
||||
if (entry.type === "directory") {
|
||||
metadata.files = Object.keys(entries)
|
||||
.filter(
|
||||
name =>
|
||||
name !== entry.name && path.dirname(name) === (entry.name || ".")
|
||||
)
|
||||
.map(name => getMetadata(entries[name], entries));
|
||||
if (entry.type === "file") {
|
||||
metadata.contentType = entry.contentType;
|
||||
metadata.integrity = entry.integrity;
|
||||
metadata.lastModified = entry.lastModified;
|
||||
metadata.size = entry.size;
|
||||
} else if (entry.type === "directory") {
|
||||
metadata.files = getMatchingEntries(entry, entries).map(e =>
|
||||
getMetadata(e, entries)
|
||||
);
|
||||
}
|
||||
|
||||
return metadata;
|
||||
|
@ -71,6 +65,10 @@ function rewriteBareModuleIdentifiers(code, packageConfig) {
|
|||
return babel.transform(code, options).code;
|
||||
}
|
||||
|
||||
function getContentTypeHeader(type) {
|
||||
return type === "application/javascript" ? type + "; charset=utf-8" : type;
|
||||
}
|
||||
|
||||
function serveJavaScriptModule(req, res) {
|
||||
if (req.entry.contentType !== "application/javascript") {
|
||||
return res
|
||||
|
|
|
@ -5,8 +5,16 @@ const sortBy = require("sort-by");
|
|||
const cloneElement = require("./utils/cloneElement");
|
||||
const e = require("./utils/createElement");
|
||||
|
||||
function getValues(object) {
|
||||
return Object.keys(object).map(key => object[key]);
|
||||
function getMatchingEntries(entry, entries) {
|
||||
const dirname = entry.name || ".";
|
||||
|
||||
return Object.keys(entries)
|
||||
.filter(name => entry.name !== name && path.dirname(name) === dirname)
|
||||
.map(name => entries[name]);
|
||||
}
|
||||
|
||||
function getRelativeName(base, name) {
|
||||
return base.length ? name.substr(base.length + 1) : name;
|
||||
}
|
||||
|
||||
function DirectoryListing({ filename, entry, entries }) {
|
||||
|
@ -25,18 +33,13 @@ function DirectoryListing({ filename, entry, entries }) {
|
|||
);
|
||||
}
|
||||
|
||||
const matchingEntries = getValues(entries).filter(
|
||||
({ name }) =>
|
||||
entry.name !== name && path.dirname(name) === (entry.name || ".")
|
||||
);
|
||||
const matchingEntries = getMatchingEntries(entry, entries);
|
||||
|
||||
matchingEntries
|
||||
.filter(({ type }) => type === "directory")
|
||||
.sort(sortBy("name"))
|
||||
.forEach(({ name }) => {
|
||||
const relName = name.substr(
|
||||
entry.name.length ? entry.name.length + 1 : 0
|
||||
);
|
||||
const relName = getRelativeName(entry.name, name);
|
||||
const href = relName + "/";
|
||||
|
||||
rows.push(
|
||||
|
@ -55,9 +58,7 @@ function DirectoryListing({ filename, entry, entries }) {
|
|||
.filter(({ type }) => type === "file")
|
||||
.sort(sortBy("name"))
|
||||
.forEach(({ name, size, contentType, lastModified }) => {
|
||||
const relName = name.substr(
|
||||
entry.name.length ? entry.name.length + 1 : 0
|
||||
);
|
||||
const relName = getRelativeName(entry.name, name);
|
||||
|
||||
rows.push(
|
||||
e(
|
||||
|
|
Loading…
Reference in New Issue