Permalink
Browse files

🐛`phenomic/plugin-collector-files`: correctly clean extensions from f…

…ilename for id (not only md|json but all extensions by the plugin loaded at runtime)
  • Loading branch information...
MoOx committed May 2, 2018
1 parent c1d37aa commit b924d0bca097ba31e9f38c0c0a35c68fa98865ae
View
@@ -176,7 +176,8 @@ declare type PhenomicPlugin = {|
collectFile?: ({|
db: PhenomicDB,
fileName: string,
parsed: Object
parsed: Object,
transformer: PhenomicPlugin
|}) => $ReadOnlyArray<mixed> | Promise<$ReadOnlyArray<mixed>>,
// bunder
buildForPrerendering?: () => Promise<PhenomicAppType>,
@@ -44,17 +44,18 @@ async function processFile({
}
}
const transformPlugin = transformers.find(
(plugin: PhenomicPlugin) =>
Array.isArray(plugin.supportedFileTypes) &&
plugin.supportedFileTypes.indexOf(path.extname(file.name).slice(1)) !== -1
);
const plugin = transformPlugin || defaultTransformPlugin;
if (typeof plugin.transform !== "function") {
const transformPlugin =
transformers.find(
(plugin: PhenomicPlugin) =>
Array.isArray(plugin.supportedFileTypes) &&
plugin.supportedFileTypes.indexOf(path.extname(file.name).slice(1)) !==
-1
) || defaultTransformPlugin;
if (typeof transformPlugin.transform !== "function") {
throw new Error("transform plugin must implement a transform() method");
}
if (contents) {
const parsed: PhenomicTransformResult = await plugin.transform({
const parsed: PhenomicTransformResult = await transformPlugin.transform({
file,
contents
});
@@ -69,7 +70,12 @@ async function processFile({
return collectors.forEach((plugin: PhenomicPlugin) => {
const fileName = path.join(fileKey, file.name);
if (typeof plugin.collectFile === "function") {
plugin.collectFile({ db, fileName, parsed });
plugin.collectFile({
db,
fileName,
parsed,
transformer: transformPlugin
});
}
});
}
@@ -12,7 +12,26 @@ db.destroy();
const p = collector(config, {});
Object.keys(fixtures).map(path => {
if (p.collectFile)
p.collectFile({ db, fileName: path, parsed: fixtures[path] });
p.collectFile({
db,
fileName: path,
parsed: fixtures[path],
transformer: {
name: "@phenomic/plugin-default-transform",
// for testing, according to db
supportedFileTypes: ["md", "json"],
transform({ contents }) {
return {
partial: {},
data: {
body: contents
}
};
}
}
});
});
it("should collect everything", async () => {
@@ -2,23 +2,27 @@
import { getId, getFieldValue, injectData, parsePath } from "..";
const exts = ["md"];
it("should be able to generate keys", () => {
expect(getId("test", { partial: {}, data: {} })).toEqual("test");
expect(getId("test", { partial: {}, data: {} }, exts)).toEqual("test");
expect(getId("test.md", { partial: {}, data: {} })).toEqual("test");
expect(getId("test.md", { partial: {}, data: {} }, exts)).toEqual("test");
// @todo handle this case
expect(getId("test/index.md", { partial: {}, data: {} })).toEqual("test");
expect(getId("test/test/index.md", { partial: {}, data: {} })).toEqual(
"test/test"
expect(getId("test/index.md", { partial: {}, data: {} }, exts)).toEqual(
"test"
);
expect(getId("test\\test\\index.md", { partial: {}, data: {} })).toEqual(
expect(getId("test/test/index.md", { partial: {}, data: {} }, exts)).toEqual(
"test/test"
);
expect(
getId("test\\test\\index.md", { partial: {}, data: {} }, exts)
).toEqual("test/test");
expect(getId("test.md", { partial: {}, data: { path: "yep" } })).toEqual(
"yep"
);
expect(
getId("test.md", { partial: {}, data: { path: "yep" } }, exts)
).toEqual("yep");
});
it("should be able to generate fields lists from arrays", () => {
@@ -7,7 +7,11 @@ function normalizeWindowsPath(value: string): string {
return value.replace(/(\/|\\)+/g, sep);
}
export function getId(name: string, json: PhenomicTransformResult): string {
export function getId(
name: string,
json: PhenomicTransformResult,
extensions?: $ReadOnlyArray<string> = []
): string {
if (json.data.path) {
debug(`id for '${name}' is '${json.data.path}' (from json)`);
return json.data.path;
@@ -16,10 +20,11 @@ export function getId(name: string, json: PhenomicTransformResult): string {
name = normalizeWindowsPath(name);
// remove (index).md,json etc, for id
const id = name
// @todo remove from supportedFileTypes!
// remove extension for prettier keys
.replace(/.(md|json)$/, "")
// remove index too
.replace(/\/index$/, "");
.replace(new RegExp(`\\.(${extensions.join("|")})$`), "")
// remove index too (and consider README as index)
.replace(/\/(index|README)$/, "");
debug(`id for '${name}' is '${id}' (automatically computed)`);
return id;
}
@@ -88,29 +93,36 @@ export function parsePath(
const collectorFiles: PhenomicPluginModule<{}> = () => {
return {
name: "@phenomic/plugin-collector-files",
collectFile({ db, fileName: name, parsed: json }) {
collectFile({ db, fileName: name, parsed: json, transformer }) {
name = normalizeWindowsPath(name);
const id = getId(name, json);
const id = getId(name, json, transformer.supportedFileTypes);
const { filename, allPaths } = parsePath(name);
const adjustedJSON = injectData(filename, json);
debug(`collecting '${filename}'`, adjustedJSON);
// debug(`collecting '${filename}'`, adjustedJSON);
debug(`collecting '${filename}'`);
// full resource, not sorted
db.put(null, id, adjustedJSON);
return allPaths.map(pathName => {
let relativeKey = id.replace(pathName + sep, "");
if (relativeKey === pathName) {
relativeKey = "";
}
const sortedKey = relativeKey;
debug(`collecting '${relativeKey}' for path '${pathName}'`);
// @todo optimize this and avoid inserting adjustedJSON several times
// we should be able to inject a ref to get it back from __null__ when
// reading
db.put([pathName], relativeKey, adjustedJSON);
db.put([pathName, "default"], sortedKey);
db.put([pathName, "default"], relativeKey);
Object.keys(json.data).map(type => {
return getFieldValue(json, type).map(value => {
db.update([pathName, type, value], sortedKey);
// db.update([type], value);
db.update([type, "default"], value);
db.update([type, "path", pathName], value);
return getFieldValue(json, type).map(fieldValue => {
debug(
`collecting '${relativeKey}' for path '${pathName}': ${type}/${
fieldValue
}`
);
db.update([pathName, type, fieldValue], relativeKey);
db.update([type, "default"], fieldValue);
db.update([type, "path", pathName], fieldValue);
});
});
});

0 comments on commit b924d0b

Please sign in to comment.