Permalink
Browse files

🚨 `@phenomic/core`: `content` options now supports multiples entries …

…and globs

**All your previous queries won't work with this breaking change** but don't worry, it's very easy to adjust your code.

We are sorry to add a breaking change now, but it's for the better. You will have to add "content/" in front of all your `path` in your queries. Why? It's for being more explicit and also be able to support multiples sources (multiples folders) to avoid duplicate names without a weird configuration.
The upgrade is pretty easy.

**Note**: You can keep the previous behavior very easily by using this value for `content` option: `"": {root: "content", globs: ["**/*"]}`. You can throw that in your `package.json` in a `phenomic` like this:

```json
  "phenomic": {
    "content": {
      "": {root: "content", globs: ["**/*"]}
    },
    "presets": ["@phenomic/preset-react-app"]
  }
```

Just by adding this config should fix all your queries. That said, we recommend you to replace `path: "` by `path: "content/` to avoid too much magic (think about this twice, it will be more clear for you where the data comes from).

🚨 `@phenomic/plugin-rss-feed`: now lookup in `content/posts` by default.

To get previous behavior back, just use this configuration (adjust to match yours):

```json
  "phenomic": {
    "presets": ["@phenomic/preset-react-app"],
    "plugins": [
      [
        "@phenomic/plugin-rss-feed",
        {
          "feeds": {
            "feed.xml": {
              "query": {
                "path": "content/posts"
              }
            }
          }
        }
      ]
    ]
  },
```
  • Loading branch information...
MoOx committed Apr 17, 2018
1 parent 97a0ee8 commit 05ef940603981bb9971b02234a31e601d29067f7
@@ -25,20 +25,20 @@ Array [
exports[`should build example correctly 2`] = `
Array [
"phenomic/pages/item/.json",
"phenomic/pages/item/about.json",
"phenomic/posts/by-default/1/desc/limit-4.json",
"phenomic/posts/by-default/1/desc/limit-6.json",
"phenomic/posts/by-default/1/desc/limit-6/after-c2Vjb25kLXBvc3Q=.json",
"phenomic/posts/by-default/1/desc/limit-6/after-dGhpcmQtcG9zdA==.json",
"phenomic/posts/by-default/1/desc/limit-6/after-Zm91cnRoLXBvc3Q=.json",
"phenomic/posts/by-default/1/desc/limit-6/after-ZmlmdGgtcG9zdA==.json",
"phenomic/posts/by-default/1/desc/limit-6/after-Zmlyc3QtcG9zdA==.json",
"phenomic/posts/item/fifth-post.json",
"phenomic/posts/item/first-post.json",
"phenomic/posts/item/fourth-post.json",
"phenomic/posts/item/second-post.json",
"phenomic/posts/item/third-post.json",
"phenomic/content/pages/item/.json",
"phenomic/content/pages/item/about.json",
"phenomic/content/posts/by-default/1/desc/limit-4.json",
"phenomic/content/posts/by-default/1/desc/limit-6.json",
"phenomic/content/posts/by-default/1/desc/limit-6/after-c2Vjb25kLXBvc3Q=.json",
"phenomic/content/posts/by-default/1/desc/limit-6/after-dGhpcmQtcG9zdA==.json",
"phenomic/content/posts/by-default/1/desc/limit-6/after-Zm91cnRoLXBvc3Q=.json",
"phenomic/content/posts/by-default/1/desc/limit-6/after-ZmlmdGgtcG9zdA==.json",
"phenomic/content/posts/by-default/1/desc/limit-6/after-Zmlyc3QtcG9zdA==.json",
"phenomic/content/posts/item/fifth-post.json",
"phenomic/content/posts/item/first-post.json",
"phenomic/content/posts/item/fourth-post.json",
"phenomic/content/posts/item/second-post.json",
"phenomic/content/posts/item/third-post.json",
]
`;
@@ -54,11 +54,11 @@ const Page = ({ hasError, isLoading, page, posts }) =>
export default withPhenomicApi(Page, props => ({
page: query({
path: "pages",
path: "content/pages",
id: props.params.splat || ""
}),
posts: query({
path: "posts",
path: "content/posts",
limit: 4,
after: props.params.after
})
@@ -40,7 +40,7 @@ const PageBlog = ({ hasError, isLoading, posts }) =>
export default withPhenomicApi(PageBlog, props => ({
posts: query({
path: "posts",
path: "content/posts",
limit: 6,
after: props.params.after
})
@@ -23,5 +23,5 @@ const PageBlogPost = ({ hasError, isLoading, post }) => {
};
export default withPhenomicApi(PageBlogPost, props => ({
post: query({ path: "posts", id: props.params.splat })
post: query({ path: "content/posts", id: props.params.splat })
}));
@@ -55,7 +55,7 @@ const Home = ({ isLoading, posts }) => (
const HomeContainer = withPhenomicApi(Home, props => ({
posts: query({
path: "posts",
path: "content/posts",
limit: 2,
after: props.params.after
})
@@ -115,7 +115,7 @@ const BlogPost = ({ hasError, isLoading, page }) => {
};
const BlogPostContainer = withPhenomicApi(BlogPost, props => ({
page: query({ path: "posts", id: props.params.splat })
page: query({ path: "content/posts", id: props.params.splat })
}));
const PageError = ({ error }) => {
@@ -18,17 +18,17 @@ Array [
exports[`should build example correctly 2`] = `
Array [
"phenomic/posts/by-default/1/desc/limit-2.json",
"phenomic/posts/by-default/1/desc/limit-2/after-c2Vjb25kLXBvc3Q=.json",
"phenomic/posts/by-default/1/desc/limit-2/after-dGhpcmQtcG9zdA==.json",
"phenomic/posts/by-default/1/desc/limit-2/after-Zm91cnRoLXBvc3Q=.json",
"phenomic/posts/by-default/1/desc/limit-2/after-ZmlmdGgtcG9zdA==.json",
"phenomic/posts/by-default/1/desc/limit-2/after-Zmlyc3QtcG9zdA==.json",
"phenomic/posts/item/fifth-post.json",
"phenomic/posts/item/first-post.json",
"phenomic/posts/item/fourth-post.json",
"phenomic/posts/item/second-post.json",
"phenomic/posts/item/third-post.json",
"phenomic/content/posts/by-default/1/desc/limit-2.json",
"phenomic/content/posts/by-default/1/desc/limit-2/after-c2Vjb25kLXBvc3Q=.json",
"phenomic/content/posts/by-default/1/desc/limit-2/after-dGhpcmQtcG9zdA==.json",
"phenomic/content/posts/by-default/1/desc/limit-2/after-Zm91cnRoLXBvc3Q=.json",
"phenomic/content/posts/by-default/1/desc/limit-2/after-ZmlmdGgtcG9zdA==.json",
"phenomic/content/posts/by-default/1/desc/limit-2/after-Zmlyc3QtcG9zdA==.json",
"phenomic/content/posts/item/fifth-post.json",
"phenomic/content/posts/item/first-post.json",
"phenomic/content/posts/item/fourth-post.json",
"phenomic/content/posts/item/second-post.json",
"phenomic/content/posts/item/third-post.json",
]
`;
@@ -76,7 +76,7 @@ const Content = ({ hasError, isLoading, page }) => {
};
const ContentContainer = withPhenomicApi(Content, () => ({
page: query({ id: "index" })
page: query({ id: "content" })
}));
export default createApp(() => (
@@ -19,17 +19,17 @@ Array [
exports[`should build example correctly 2`] = `
Array [
"phenomic/posts/by-default/1/desc/limit-2.json",
"phenomic/posts/by-default/1/desc/limit-2/after-c2Vjb25kLXBvc3Q=.json",
"phenomic/posts/by-default/1/desc/limit-2/after-dGhpcmQtcG9zdA==.json",
"phenomic/posts/by-default/1/desc/limit-2/after-Zm91cnRoLXBvc3Q=.json",
"phenomic/posts/by-default/1/desc/limit-2/after-ZmlmdGgtcG9zdA==.json",
"phenomic/posts/by-default/1/desc/limit-2/after-Zmlyc3QtcG9zdA==.json",
"phenomic/posts/item/fifth-post.json",
"phenomic/posts/item/first-post.json",
"phenomic/posts/item/fourth-post.json",
"phenomic/posts/item/second-post.json",
"phenomic/posts/item/third-post.json",
"phenomic/content/posts/by-default/1/desc/limit-2.json",
"phenomic/content/posts/by-default/1/desc/limit-2/after-c2Vjb25kLXBvc3Q=.json",
"phenomic/content/posts/by-default/1/desc/limit-2/after-dGhpcmQtcG9zdA==.json",
"phenomic/content/posts/by-default/1/desc/limit-2/after-Zm91cnRoLXBvc3Q=.json",
"phenomic/content/posts/by-default/1/desc/limit-2/after-ZmlmdGgtcG9zdA==.json",
"phenomic/content/posts/by-default/1/desc/limit-2/after-Zmlyc3QtcG9zdA==.json",
"phenomic/content/posts/item/fifth-post.json",
"phenomic/content/posts/item/first-post.json",
"phenomic/content/posts/item/fourth-post.json",
"phenomic/content/posts/item/second-post.json",
"phenomic/content/posts/item/third-post.json",
]
`;
@@ -73,7 +73,7 @@ let queries = props => {
let posts =
PhenomicPresetReactApp.query(
PaginatedList({
path: "posts",
path: "content/posts",
by: Some("default"),
value: None,
order: None,
@@ -30,7 +30,7 @@ let jsComponent =
let queries = props => {
let post =
PhenomicPresetReactApp.query(
Item({path: "posts", id: props##params##splat})
Item({path: "content/posts", id: props##params##splat})
);
{"post": post};
};
@@ -107,10 +107,12 @@ declare type PhenomicInputPlugins = {|
>
|};
type globs = $ReadOnlyArray<string>;
declare type PhenomicInputConfig = {|
baseUrl?: string,
path?: string,
content?: string,
content?: { [key: string]: globs | {| root: string, globs: globs |} },
outdir?: string,
port?: number,
bundleName?: string,
@@ -206,7 +208,7 @@ declare type PhenomicPlugins = $ReadOnlyArray<PhenomicPlugin>;
declare type PhenomicConfig = {|
baseUrl: Url,
path: string,
content: string,
content: { [key: string]: globs | {| root: string, globs: globs |} },
outdir: string,
port: number,
bundleName: string,
@@ -228,7 +230,7 @@ declare type PhenomicRoute = {|
params?: { [key: string]: any },
component: {
getInitialProps?: ({ params: { [key: string]: any } }) => Object,
getAllPossibleUrls?: ({ path: string }) => Array<string>,
getAllPossibleUrls?: ({ path: string }) => $ReadOnlyArray<string>,
getQueries?: ({ params: { [key: string]: any } }) => {
[key: string]: PhenomicQueryConfig
}
@@ -17,9 +17,6 @@ import getPath from "../utils/getPath";
const debug = require("debug")("phenomic:core:commands:build");
const getContentPath = (config: PhenomicConfig) =>
getPath(path.join(config.path, config.content));
let lastStamp = Date.now();
async function getContent(db, config: PhenomicConfig) {
debug("getting content");
@@ -36,34 +33,63 @@ async function getContent(db, config: PhenomicConfig) {
throw Error("Phenomic expects at least a collector plugin");
}
let contentPath;
try {
contentPath = await getContentPath(config);
} catch (e) {
log.warn(
`no '${
config.content
}' folder found. Please create and put files in this folder if you want the content to be accessible (eg: markdown or JSON files). `
);
}
await Promise.all(
Object.keys(config.content).map(async contentKey => {
let contentPath;
let globs;
try {
let folder;
if (contentPath) {
const files = oneShot({
path: contentPath,
plugins: config.plugins
});
await db.destroy();
await Promise.all(
files.map(file =>
processFile({
db,
file,
transformers,
collectors
})
)
);
}
// "key(and folder)": ["glob/*"]
if (Array.isArray(config.content[contentKey])) {
folder = path.join(config.path, contentKey);
// $FlowFixMe stfu
globs = config.content[contentKey];
} else if (
config.content[contentKey].root &&
config.content[contentKey].globs
) {
// "key": {root: folder, globs: ["glob/*"] }
folder = path.join(config.path, config.content[contentKey].root);
// $FlowFixMe stfu
globs = config.content[contentKey].globs;
} else {
throw new Error(
"Unexpected config for 'content' option: " +
config.content[contentKey].toString()
);
}
contentPath = await getPath(folder);
} catch (e) {
log.warn(
`no '${
contentKey
}' folder found or unable to read files. Please create and put files in this folder (or double check it) if you want the content to be accessible (eg: markdown or JSON files). `
);
}
if (contentPath) {
const files = oneShot({
path: contentPath,
// $FlowFixMe stfu
patterns: globs
});
await db.destroy();
await Promise.all(
files.map(file =>
processFile({
db,
fileKey: contentKey,
file,
transformers,
collectors
})
)
);
}
})
);
}
async function build(config: PhenomicConfig) {
Oops, something went wrong.

0 comments on commit 05ef940

Please sign in to comment.