Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
swyxio committed Mar 16, 2020
1 parent ac3cede commit 42cd0f3
Show file tree
Hide file tree
Showing 10 changed files with 63 additions and 30 deletions.
22 changes: 20 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ However, you may wish to have a way to generate this index based ONLY on crawlin

Specify the plugin in your `netlify.yml`. No config is required but we show the default options here.

**Generating both**:
**Generating both serverless function and clientside JSON**:

```yml
build:
Expand Down Expand Up @@ -47,8 +47,16 @@ plugins:
debugMode: false # (for development) turn true for extra diagnostic logging
```
Without config, this would generate a function at `https://yoursite.netlify.com/.netlify/functions/search` and a clientside JSON blob at `https://yoursite.netlify.com/searchIndex.json`

<details>

<summary>

**Generating serverless function only**:

</summary>

To use this plugin only for the generated serveless function, supply `null` to the `publishDirJSONFileName`:

```yml
Expand All @@ -61,8 +69,16 @@ plugins:

This would generate a Netlify function at `https://yoursite.netlify.com/.netlify/functions/mySearchFunction` which you can query with `https://yoursite.netlify.com/.netlify/functions/mySearchFunction?search=foo`.

</details>

<details>

<summary>

**Generating clientside JSON only**:

</summary>

To use this plugin only for the clientside JSON file, supply `null` to the `generatedFunctionName`:

```yml
Expand All @@ -75,7 +91,9 @@ plugins:

This would generate a clientside JSON at `https://yoursite.netlify.com/mySearchIndex.json`.

Supplying `null` to both would be meaningless.
</details>

Supplying `null` to both `generatedFunctionName` and `publishDirJSONFileName` would be meaningless (because there would be nothing to generate) and cause an error.

## What It Does

Expand Down
Empty file removed fixtures/functions/.gitkeep
Empty file.
2 changes: 1 addition & 1 deletion fixtures/publishDir/blog/blog1.html
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ <h1>
<div class="postDate">1 January, 2020</div>
<h1>This is our First Post</h1>
<div>
some words for our first post
Lorem ipsum, dolor sit amet consectetur adipisicing elit. Quis ullam cumque ducimus perspiciatis excepturi optio velit dolores quam veniam voluptas quas
</div>
</div>
</div>
Expand Down
2 changes: 1 addition & 1 deletion fixtures/publishDir/blog/subdir/blog2.html
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ <h1>
<div class="postDate">1 February, 2020</div>
<h1>This is our Second Post</h1>
<div>
some words for our second post
Lorem ipsum dolor sit amet consectetur adipisicing elit. Accusamus atque quidem, quod reiciendis nam eveniet in ex vero officiis corporis sequi illo?
</div>
</div>
</div>
Expand Down
2 changes: 1 addition & 1 deletion fixtures/publishDir/blog/subdir/blog3.html
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ <h1>
<div class="postDate">1 March, 2020</div>
<h1>This is our Third Post</h1>
<div>
some words for our 3rd post
Animi deleniti asperiores odit minus architecto iure, veritatis recusandae?
</div>
</div>
</div>
Expand Down
File renamed without changes.
18 changes: 11 additions & 7 deletions fixtures/this.test.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
/**
* @jest-environment node
*/

const fs = require('fs');
const path = require('path');

// actual test
const netlifyPlugin = require('../index.js');
test('plugin fixture works', async () => {
expect(async () => {
const initPlugin = netlifyPlugin();
console.log(`running ${initPlugin.name}`);
await initPlugin.onPostBuild({
test('plugin fixture works', () => {
const initPlugin = netlifyPlugin();
console.log(`running ${initPlugin.name}`);
return initPlugin
.onPostBuild({
// from netlify.yml
pluginConfig: {
debugMode: false,
Expand All @@ -19,6 +23,6 @@ test('plugin fixture works', async () => {
FUNCTIONS_SRC: 'fixtures/functions',
FUNCTIONS_DIST: 'fixtures/functions-dist'
}
});
}).not.toThrow();
})
.then(() => expect(true).toBe(true));
});
33 changes: 26 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const { promisify } = require('util');
const chalk = require('chalk');
const makeDir = require('make-dir');
const pathExists = require('path-exists');
const readdirp = require('readdirp');
const readDir = promisify(fs.readdir);
const cpy = require('copy-template-dir');
const copy = promisify(cpy);
const { zipFunctions } = require('@netlify/zip-it-and-ship-it'); // eslint-disable-line
Expand All @@ -21,6 +21,7 @@ function netlifyPluginSearchIndex(conf) {
pluginConfig: {
generatedFunctionName = 'search',
publishDirJSONFileName = 'searchIndex',
debugMode,
...htmlToTextOptions // https://www.npmjs.com/package/html-to-text#user-content-options
},
constants: { BUILD_DIR, FUNCTIONS_SRC, FUNCTIONS_DIST }
Expand All @@ -31,12 +32,12 @@ function netlifyPluginSearchIndex(conf) {
'generatedFunctionName and publishDirJSONFileName cannot both be null, this plugin wouldnt be generating anything!'
);
}
if (debugMode) {
console.warn('debugMode is not implemented yet for this plugin');
}

let newManifest = [];
newManifest = await readdirp
.promise(BUILD_DIR, { directoryFilter: ['node_modules'] })
.then((x) => x.map((y) => y.fullPath));
newManifest = newManifest.filter((x) => x.endsWith('.html'));
newManifest = await walk(BUILD_DIR);
let searchIndex = {};
// https://www.npmjs.com/package/html-to-text#user-content-options
await Promise.all(
Expand Down Expand Up @@ -65,10 +66,11 @@ function netlifyPluginSearchIndex(conf) {
);
if (await pathExists(searchIndexPath)) {
console.warn(
`searchIndex detected at ${searchIndexPath}, will overwrite for this build but this may indicate an accidental conflict`
`Existing file at ${searchIndexPath}, plugin will overwrite it but this may indicate an accidental conflict. Delete this file from your repo to avoid confusion - the plugin should be the sole manager of your search index`
);
// to do: let people turn off this warning?
}
makeDir(`${searchIndexPath}/..`); // make a dir out of the parent
await makeDir(`${searchIndexPath}/..`); // make a dir out of the parent
await writeFile(searchIndexPath, stringifiedIndex);
console.log(
`Search Index JSON generated at ${chalk.blue(
Expand Down Expand Up @@ -114,3 +116,20 @@ function netlifyPluginSearchIndex(conf) {
};
}
module.exports = netlifyPluginSearchIndex;

// https://gist.github.com/kethinov/6658166
async function walk(dir, filelist) {
var files = await readDir(dir);
filelist = filelist || [];
await Promise.all(
files.map(async function(file) {
const dirfile = path.join(dir, file);
if (fs.statSync(dirfile).isDirectory()) {
filelist = await walk(dirfile + '/', filelist);
} else {
if (dirfile.endsWith('.html')) filelist.push(dirfile);
}
})
);
return filelist;
}
11 changes: 2 additions & 9 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@
"html-to-text": "^5.1.1",
"make-dir": "^3.0.0",
"patch-package": "^6.2.1",
"path-exists": "^4.0.0",
"readdirp": "^3.2.0"
"path-exists": "^4.0.0"
},
"repository": {
"type": "git",
Expand Down

0 comments on commit 42cd0f3

Please sign in to comment.