Hello everyone,
I am trying to host an Eleventy and Netlify CMS app. In my .eleventy.js file I have some fs reading and writing files with the help of the eleventy-plugin-injector plugin that helps you inject arbitrary code into the site build process.
eleventyConfig.addPlugin(pluginInjector, {
watch: './_site/categories/**.md',
inject: (eleventyInstance, options, file) => {
const categoryName = path.basename(file).split('.')[0];
const filetext = fs.readFileSync(file, 'utf-8');
if (!filetext.includes('pagination')) {
let paginationText = `\npagination:\n`
+ ` data: collections.${categoryName}\n`
+ ` size: 5\n`
+ `permalink: /category/{{name}}/{{pagination.pageNumber+1}}/index.html\n`;
let content = `\n{% include "paginateproducts.njk" %}`;
let newtext = filetext.replace('\n', paginationText);
newtext += content;
try {
fs.writeFileSync(`./_site/categories/${categoryName}.md`, newtext);
} catch(e) {
console.log('error writing', e.message)
}
}
}
});
This piece of code reads the category files in categories/**.md
and it adds pagination objects to them in their front matter. If you are wondering why I am doing that instead of doing them manually is because the Admin of website can add a new category at anytime so I have to add the pagination object based on the name of the new category (for more info about the problem I had you can read my issue here: https://github.com/11ty/eleventy/issues/1338).
I figured out this workaround and the eleventy app works fine in my local server, but when I pushed it to github I was already afraid it won’t work as the files comes from the repo etc and not from some folder, and indeed it didn’t work. The build shell gives me this error:
$ npx eleventy
12:58:16 PM: > ENOENT: no such file or directory, open './_site/categories/**.md'
12:58:16 PM: `Error` was thrown:
12:58:16 PM: Error: ENOENT: no such file or directory, open './_site/categories/**.md'
12:58:16 PM: at Object.openSync (fs.js:458:3)
12:58:16 PM: at Object.readFileSync (fs.js:360:35)
12:58:16 PM: at Object.inject (/opt/build/repo/.eleventy.js:13:27)
12:58:16 PM: at Eleventy.write (/opt/build/repo/node_modules/@infinity-interactive/eleventy-plugin-injector/index.js:121:23)
12:58:16 PM: at Eleventy.wrapper (/opt/build/repo/node_modules/@infinity-interactive/eleventy-plugin-injector/index.js:28:41)
12:58:16 PM: at /opt/build/repo/node_modules/@11ty/eleventy/cmd.js:77:14
Should I just give up on this solution, or there is a workaround for this? I hope my problem is understandable and thank you in advance.