Prettify everything
This commit is contained in:
@ -1,35 +1,35 @@
|
||||
const getFileContentType = require("../getFileContentType")
|
||||
const getFileContentType = require("../getFileContentType");
|
||||
|
||||
it("gets a content type of text/plain for LICENSE|README|CHANGES|AUTHORS|Makefile", () => {
|
||||
expect(getFileContentType("AUTHORS")).toBe("text/plain")
|
||||
expect(getFileContentType("CHANGES")).toBe("text/plain")
|
||||
expect(getFileContentType("LICENSE")).toBe("text/plain")
|
||||
expect(getFileContentType("Makefile")).toBe("text/plain")
|
||||
expect(getFileContentType("PATENTS")).toBe("text/plain")
|
||||
expect(getFileContentType("README")).toBe("text/plain")
|
||||
})
|
||||
expect(getFileContentType("AUTHORS")).toBe("text/plain");
|
||||
expect(getFileContentType("CHANGES")).toBe("text/plain");
|
||||
expect(getFileContentType("LICENSE")).toBe("text/plain");
|
||||
expect(getFileContentType("Makefile")).toBe("text/plain");
|
||||
expect(getFileContentType("PATENTS")).toBe("text/plain");
|
||||
expect(getFileContentType("README")).toBe("text/plain");
|
||||
});
|
||||
|
||||
it("gets a content type of text/plain for .*rc files", () => {
|
||||
expect(getFileContentType(".eslintrc")).toBe("text/plain")
|
||||
expect(getFileContentType(".babelrc")).toBe("text/plain")
|
||||
expect(getFileContentType(".anythingrc")).toBe("text/plain")
|
||||
})
|
||||
expect(getFileContentType(".eslintrc")).toBe("text/plain");
|
||||
expect(getFileContentType(".babelrc")).toBe("text/plain");
|
||||
expect(getFileContentType(".anythingrc")).toBe("text/plain");
|
||||
});
|
||||
|
||||
it("gets a content type of text/plain for .git* files", () => {
|
||||
expect(getFileContentType(".gitignore")).toBe("text/plain")
|
||||
expect(getFileContentType(".gitanything")).toBe("text/plain")
|
||||
})
|
||||
expect(getFileContentType(".gitignore")).toBe("text/plain");
|
||||
expect(getFileContentType(".gitanything")).toBe("text/plain");
|
||||
});
|
||||
|
||||
it("gets a content type of text/plain for .*ignore files", () => {
|
||||
expect(getFileContentType(".eslintignore")).toBe("text/plain")
|
||||
expect(getFileContentType(".anythingignore")).toBe("text/plain")
|
||||
})
|
||||
expect(getFileContentType(".eslintignore")).toBe("text/plain");
|
||||
expect(getFileContentType(".anythingignore")).toBe("text/plain");
|
||||
});
|
||||
|
||||
it("gets a content type of text/plain for .ts files", () => {
|
||||
expect(getFileContentType("app.ts")).toBe("text/plain")
|
||||
expect(getFileContentType("app.d.ts")).toBe("text/plain")
|
||||
})
|
||||
expect(getFileContentType("app.ts")).toBe("text/plain");
|
||||
expect(getFileContentType("app.d.ts")).toBe("text/plain");
|
||||
});
|
||||
|
||||
it("gets a content type of text/plain for .flow files", () => {
|
||||
expect(getFileContentType("app.js.flow")).toBe("text/plain")
|
||||
})
|
||||
expect(getFileContentType("app.js.flow")).toBe("text/plain");
|
||||
});
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
const parsePackageURL = require("../parsePackageURL")
|
||||
const parsePackageURL = require("../parsePackageURL");
|
||||
|
||||
describe("parsePackageURL", () => {
|
||||
it("parses plain packages", () => {
|
||||
@ -9,8 +9,8 @@ describe("parsePackageURL", () => {
|
||||
packageName: "history",
|
||||
packageVersion: "1.0.0",
|
||||
filename: "/umd/history.min.js"
|
||||
})
|
||||
})
|
||||
});
|
||||
});
|
||||
|
||||
it("parses plain packages with a hyphen in the name", () => {
|
||||
expect(parsePackageURL("/query-string@5.0.0/index.js")).toEqual({
|
||||
@ -20,8 +20,8 @@ describe("parsePackageURL", () => {
|
||||
packageName: "query-string",
|
||||
packageVersion: "5.0.0",
|
||||
filename: "/index.js"
|
||||
})
|
||||
})
|
||||
});
|
||||
});
|
||||
|
||||
it("parses plain packages with no version specified", () => {
|
||||
expect(parsePackageURL("/query-string/index.js")).toEqual({
|
||||
@ -31,8 +31,8 @@ describe("parsePackageURL", () => {
|
||||
packageName: "query-string",
|
||||
packageVersion: "latest",
|
||||
filename: "/index.js"
|
||||
})
|
||||
})
|
||||
});
|
||||
});
|
||||
|
||||
it("parses plain packages with version spec", () => {
|
||||
expect(parsePackageURL("/query-string@>=4.0.0/index.js")).toEqual({
|
||||
@ -42,8 +42,8 @@ describe("parsePackageURL", () => {
|
||||
packageName: "query-string",
|
||||
packageVersion: ">=4.0.0",
|
||||
filename: "/index.js"
|
||||
})
|
||||
})
|
||||
});
|
||||
});
|
||||
|
||||
it("parses scoped packages", () => {
|
||||
expect(parsePackageURL("/@angular/router@4.3.3/src/index.d.ts")).toEqual({
|
||||
@ -53,8 +53,8 @@ describe("parsePackageURL", () => {
|
||||
packageName: "@angular/router",
|
||||
packageVersion: "4.3.3",
|
||||
filename: "/src/index.d.ts"
|
||||
})
|
||||
})
|
||||
});
|
||||
});
|
||||
|
||||
it("parses package names with a period in them", () => {
|
||||
expect(parsePackageURL("/index.js")).toEqual({
|
||||
@ -64,8 +64,8 @@ describe("parsePackageURL", () => {
|
||||
packageName: "index.js",
|
||||
packageVersion: "latest",
|
||||
filename: ""
|
||||
})
|
||||
})
|
||||
});
|
||||
});
|
||||
|
||||
it("parses valid query parameters", () => {
|
||||
expect(parsePackageURL("/history?main=browser")).toEqual({
|
||||
@ -75,11 +75,11 @@ describe("parsePackageURL", () => {
|
||||
packageName: "history",
|
||||
packageVersion: "latest",
|
||||
filename: ""
|
||||
})
|
||||
})
|
||||
});
|
||||
});
|
||||
|
||||
it("returns null for invalid pathnames", () => {
|
||||
expect(parsePackageURL("history")).toBe(null)
|
||||
expect(parsePackageURL("/.invalid")).toBe(null)
|
||||
})
|
||||
})
|
||||
expect(parsePackageURL("history")).toBe(null);
|
||||
expect(parsePackageURL("/.invalid")).toBe(null);
|
||||
});
|
||||
});
|
||||
|
||||
@ -1,5 +1,5 @@
|
||||
const babel = require("babel-core")
|
||||
const unpkgRewrite = require("../unpkgRewriteBabelPlugin")
|
||||
const babel = require("babel-core");
|
||||
const unpkgRewrite = require("../unpkgRewriteBabelPlugin");
|
||||
|
||||
const testCases = [
|
||||
{
|
||||
@ -8,7 +8,8 @@ const testCases = [
|
||||
},
|
||||
{
|
||||
before: "import router from '@angular/router';",
|
||||
after: "import router from 'https://unpkg.com/@angular/router@4.3.5?module';"
|
||||
after:
|
||||
"import router from 'https://unpkg.com/@angular/router@4.3.5?module';"
|
||||
},
|
||||
{
|
||||
before: "import map from 'lodash.map';",
|
||||
@ -54,23 +55,23 @@ const testCases = [
|
||||
before: "export var message = 'hello';",
|
||||
after: "export var message = 'hello';"
|
||||
}
|
||||
]
|
||||
];
|
||||
|
||||
const dependencies = {
|
||||
react: "15.6.1",
|
||||
"@angular/router": "4.3.5",
|
||||
"lodash.map": "4.6.0",
|
||||
pn: "1.0.0"
|
||||
}
|
||||
};
|
||||
|
||||
describe("Rewriting imports/exports", () => {
|
||||
testCases.forEach(testCase => {
|
||||
it(`successfully rewrites "${testCase.before}"`, () => {
|
||||
const result = babel.transform(testCase.before, {
|
||||
plugins: [unpkgRewrite(dependencies)]
|
||||
})
|
||||
});
|
||||
|
||||
expect(result.code).toEqual(testCase.after)
|
||||
})
|
||||
})
|
||||
})
|
||||
expect(result.code).toEqual(testCase.after);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@ -1,9 +1,9 @@
|
||||
function createPackageURL(packageName, version, pathname, search) {
|
||||
let url = `/${packageName}`
|
||||
if (version != null) url += `@${version}`
|
||||
if (pathname) url += pathname
|
||||
if (search) url += search
|
||||
return url
|
||||
let url = `/${packageName}`;
|
||||
if (version != null) url += `@${version}`;
|
||||
if (pathname) url += pathname;
|
||||
if (search) url += search;
|
||||
return url;
|
||||
}
|
||||
|
||||
module.exports = createPackageURL
|
||||
module.exports = createPackageURL;
|
||||
|
||||
@ -1,13 +1,22 @@
|
||||
const mime = require("mime")
|
||||
const mime = require("mime");
|
||||
|
||||
mime.define({
|
||||
"text/plain": ["authors", "changes", "license", "makefile", "patents", "readme", "ts", "flow"]
|
||||
})
|
||||
"text/plain": [
|
||||
"authors",
|
||||
"changes",
|
||||
"license",
|
||||
"makefile",
|
||||
"patents",
|
||||
"readme",
|
||||
"ts",
|
||||
"flow"
|
||||
]
|
||||
});
|
||||
|
||||
const TextFiles = /\/?(\.[a-z]*rc|\.git[a-z]*|\.[a-z]*ignore)$/i
|
||||
const TextFiles = /\/?(\.[a-z]*rc|\.git[a-z]*|\.[a-z]*ignore)$/i;
|
||||
|
||||
function getFileContentType(file) {
|
||||
return TextFiles.test(file) ? "text/plain" : mime.lookup(file)
|
||||
return TextFiles.test(file) ? "text/plain" : mime.lookup(file);
|
||||
}
|
||||
|
||||
module.exports = getFileContentType
|
||||
module.exports = getFileContentType;
|
||||
|
||||
@ -1,15 +1,15 @@
|
||||
const fs = require("fs")
|
||||
const fs = require("fs");
|
||||
|
||||
function getFileStats(file) {
|
||||
return new Promise((resolve, reject) => {
|
||||
fs.lstat(file, (error, stats) => {
|
||||
if (error) {
|
||||
reject(error)
|
||||
reject(error);
|
||||
} else {
|
||||
resolve(stats)
|
||||
resolve(stats);
|
||||
}
|
||||
})
|
||||
})
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
module.exports = getFileStats
|
||||
module.exports = getFileStats;
|
||||
|
||||
@ -1,44 +1,51 @@
|
||||
const fs = require("fs")
|
||||
const path = require("path")
|
||||
const SRIToolbox = require("sri-toolbox")
|
||||
const getFileContentType = require("./getFileContentType")
|
||||
const getFileStats = require("./getFileStats")
|
||||
const getFileType = require("./getFileType")
|
||||
const fs = require("fs");
|
||||
const path = require("path");
|
||||
const SRIToolbox = require("sri-toolbox");
|
||||
const getFileContentType = require("./getFileContentType");
|
||||
const getFileStats = require("./getFileStats");
|
||||
const getFileType = require("./getFileType");
|
||||
|
||||
function getEntries(dir, file, maximumDepth) {
|
||||
return new Promise((resolve, reject) => {
|
||||
fs.readdir(path.join(dir, file), function(error, files) {
|
||||
if (error) {
|
||||
reject(error)
|
||||
reject(error);
|
||||
} else {
|
||||
resolve(
|
||||
Promise.all(files.map(f => getFileStats(path.join(dir, file, f)))).then(statsArray => {
|
||||
Promise.all(
|
||||
files.map(f => getFileStats(path.join(dir, file, f)))
|
||||
).then(statsArray => {
|
||||
return Promise.all(
|
||||
statsArray.map((stats, index) =>
|
||||
getMetadataRecursive(dir, path.join(file, files[index]), stats, maximumDepth - 1)
|
||||
getMetadataRecursive(
|
||||
dir,
|
||||
path.join(file, files[index]),
|
||||
stats,
|
||||
maximumDepth - 1
|
||||
)
|
||||
)
|
||||
)
|
||||
);
|
||||
})
|
||||
)
|
||||
);
|
||||
}
|
||||
})
|
||||
})
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
function formatTime(time) {
|
||||
return new Date(time).toISOString()
|
||||
return new Date(time).toISOString();
|
||||
}
|
||||
|
||||
function getIntegrity(file) {
|
||||
return new Promise((resolve, reject) => {
|
||||
fs.readFile(file, function(error, data) {
|
||||
if (error) {
|
||||
reject(error)
|
||||
reject(error);
|
||||
} else {
|
||||
resolve(SRIToolbox.generate({ algorithms: ["sha384"] }, data))
|
||||
resolve(SRIToolbox.generate({ algorithms: ["sha384"] }, data));
|
||||
}
|
||||
})
|
||||
})
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
function getMetadataRecursive(dir, file, stats, maximumDepth) {
|
||||
@ -48,27 +55,31 @@ function getMetadataRecursive(dir, file, stats, maximumDepth) {
|
||||
path: file,
|
||||
size: stats.size,
|
||||
type: getFileType(stats)
|
||||
}
|
||||
};
|
||||
|
||||
if (stats.isFile()) {
|
||||
return getIntegrity(path.join(dir, file)).then(integrity => {
|
||||
metadata.integrity = integrity
|
||||
return metadata
|
||||
})
|
||||
metadata.integrity = integrity;
|
||||
return metadata;
|
||||
});
|
||||
}
|
||||
|
||||
if (!stats.isDirectory() || maximumDepth === 0) return Promise.resolve(metadata)
|
||||
if (!stats.isDirectory() || maximumDepth === 0)
|
||||
return Promise.resolve(metadata);
|
||||
|
||||
return getEntries(dir, file, maximumDepth).then(files => {
|
||||
metadata.files = files
|
||||
return metadata
|
||||
})
|
||||
metadata.files = files;
|
||||
return metadata;
|
||||
});
|
||||
}
|
||||
|
||||
function getMetadata(baseDir, path, stats, maximumDepth, callback) {
|
||||
getMetadataRecursive(baseDir, path, stats, maximumDepth).then(function(metadata) {
|
||||
callback(null, metadata)
|
||||
}, callback)
|
||||
getMetadataRecursive(baseDir, path, stats, maximumDepth).then(function(
|
||||
metadata
|
||||
) {
|
||||
callback(null, metadata);
|
||||
},
|
||||
callback);
|
||||
}
|
||||
|
||||
module.exports = getMetadata
|
||||
module.exports = getMetadata;
|
||||
|
||||
@ -1,35 +1,35 @@
|
||||
const url = require("url")
|
||||
const validatePackageName = require("./validatePackageName")
|
||||
const url = require("url");
|
||||
const validatePackageName = require("./validatePackageName");
|
||||
|
||||
const URLFormat = /^\/((?:@[^/@]+\/)?[^/@]+)(?:@([^/]+))?(\/.*)?$/
|
||||
const URLFormat = /^\/((?:@[^/@]+\/)?[^/@]+)(?:@([^/]+))?(\/.*)?$/;
|
||||
|
||||
function decodeParam(param) {
|
||||
if (param) {
|
||||
try {
|
||||
return decodeURIComponent(param)
|
||||
return decodeURIComponent(param);
|
||||
} catch (error) {
|
||||
// Ignore invalid params.
|
||||
}
|
||||
}
|
||||
|
||||
return ""
|
||||
return "";
|
||||
}
|
||||
|
||||
function parsePackageURL(packageURL) {
|
||||
const { pathname, search, query } = url.parse(packageURL, true)
|
||||
const { pathname, search, query } = url.parse(packageURL, true);
|
||||
|
||||
const match = URLFormat.exec(pathname)
|
||||
const match = URLFormat.exec(pathname);
|
||||
|
||||
// Disallow invalid URL formats.
|
||||
if (match == null) return null
|
||||
if (match == null) return null;
|
||||
|
||||
const packageName = match[1]
|
||||
const packageName = match[1];
|
||||
|
||||
// Disallow invalid npm package names.
|
||||
if (!validatePackageName(packageName)) return null
|
||||
if (!validatePackageName(packageName)) return null;
|
||||
|
||||
const packageVersion = decodeParam(match[2]) || "latest"
|
||||
const filename = decodeParam(match[3])
|
||||
const packageVersion = decodeParam(match[2]) || "latest";
|
||||
const filename = decodeParam(match[3]);
|
||||
|
||||
return {
|
||||
// If the URL is /@scope/name@version/file.js?main=browser:
|
||||
@ -39,7 +39,7 @@ function parsePackageURL(packageURL) {
|
||||
packageName, // @scope/name
|
||||
packageVersion, // version
|
||||
filename // /file.js
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
module.exports = parsePackageURL
|
||||
module.exports = parsePackageURL;
|
||||
|
||||
@ -1,9 +1,9 @@
|
||||
const fs = require("fs")
|
||||
const path = require("path")
|
||||
const csso = require("csso")
|
||||
const fs = require("fs");
|
||||
const path = require("path");
|
||||
const csso = require("csso");
|
||||
|
||||
function readCSS(...args) {
|
||||
return csso.minify(fs.readFileSync(path.resolve(...args), "utf8")).css
|
||||
return csso.minify(fs.readFileSync(path.resolve(...args), "utf8")).css;
|
||||
}
|
||||
|
||||
module.exports = readCSS
|
||||
module.exports = readCSS;
|
||||
|
||||
@ -1,11 +1,13 @@
|
||||
const React = require("react")
|
||||
const ReactDOMServer = require("react-dom/server")
|
||||
const React = require("react");
|
||||
const ReactDOMServer = require("react-dom/server");
|
||||
|
||||
const doctype = "<!DOCTYPE html>"
|
||||
const doctype = "<!DOCTYPE html>";
|
||||
|
||||
function renderPage(page, props) {
|
||||
const html = ReactDOMServer.renderToStaticMarkup(React.createElement(page, props))
|
||||
return doctype + html
|
||||
const html = ReactDOMServer.renderToStaticMarkup(
|
||||
React.createElement(page, props)
|
||||
);
|
||||
return doctype + html;
|
||||
}
|
||||
|
||||
module.exports = renderPage
|
||||
module.exports = renderPage;
|
||||
|
||||
@ -1,7 +1,7 @@
|
||||
const URL = require("whatwg-url")
|
||||
const warning = require("warning")
|
||||
const URL = require("whatwg-url");
|
||||
const warning = require("warning");
|
||||
|
||||
const BareIdentifierFormat = /^((?:@[^\/]+\/)?[^\/]+)(\/.*)?$/
|
||||
const BareIdentifierFormat = /^((?:@[^\/]+\/)?[^\/]+)(\/.*)?$/;
|
||||
|
||||
function unpkgRewriteBabelPlugin(dependencies = {}) {
|
||||
return {
|
||||
@ -9,36 +9,36 @@ function unpkgRewriteBabelPlugin(dependencies = {}) {
|
||||
|
||||
visitor: {
|
||||
"ImportDeclaration|ExportNamedDeclaration|ExportAllDeclaration"(path) {
|
||||
if (!path.node.source) return // probably a variable declaration
|
||||
if (!path.node.source) return; // probably a variable declaration
|
||||
|
||||
if (
|
||||
URL.parseURL(path.node.source.value) != null ||
|
||||
path.node.source.value.substr(0, 2) === "//"
|
||||
)
|
||||
return // valid URL or URL w/o protocol, leave it alone
|
||||
return; // valid URL or URL w/o protocol, leave it alone
|
||||
|
||||
if ([".", "/"].indexOf(path.node.source.value.charAt(0)) >= 0) {
|
||||
// local path
|
||||
path.node.source.value = `${path.node.source.value}?module`
|
||||
path.node.source.value = `${path.node.source.value}?module`;
|
||||
} else {
|
||||
// "bare" identifier
|
||||
const match = BareIdentifierFormat.exec(path.node.source.value)
|
||||
const packageName = match[1]
|
||||
const file = match[2] || ""
|
||||
const match = BareIdentifierFormat.exec(path.node.source.value);
|
||||
const packageName = match[1];
|
||||
const file = match[2] || "";
|
||||
|
||||
warning(
|
||||
dependencies[packageName],
|
||||
'Missing version info for package "%s" in dependencies; falling back to "latest"',
|
||||
packageName
|
||||
)
|
||||
);
|
||||
|
||||
const version = dependencies[packageName] || "latest"
|
||||
const version = dependencies[packageName] || "latest";
|
||||
|
||||
path.node.source.value = `https://unpkg.com/${packageName}@${version}${file}?module`
|
||||
path.node.source.value = `https://unpkg.com/${packageName}@${version}${file}?module`;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
module.exports = unpkgRewriteBabelPlugin
|
||||
module.exports = unpkgRewriteBabelPlugin;
|
||||
|
||||
@ -1,7 +1,7 @@
|
||||
const validateNpmPackageName = require("validate-npm-package-name")
|
||||
const validateNpmPackageName = require("validate-npm-package-name");
|
||||
|
||||
function validatePackageName(packageName) {
|
||||
return validateNpmPackageName(packageName).errors == null
|
||||
return validateNpmPackageName(packageName).errors == null;
|
||||
}
|
||||
|
||||
module.exports = validatePackageName
|
||||
module.exports = validatePackageName;
|
||||
|
||||
Reference in New Issue
Block a user