-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathindex.js
123 lines (114 loc) · 4.09 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
const path = require('path');
const fs = require('fs');
const globby = require('globby');
const { promisify } = require('util');
const chalk = require('chalk');
const makeDir = require('make-dir');
const pathExists = require('path-exists');
const cpy = require('copy-template-dir');
const copy = promisify(cpy);
const { zipFunctions } = require('@netlify/zip-it-and-ship-it'); // eslint-disable-line
const { parse } = require('./parser')
const readFile = promisify(fs.readFile);
const writeFile = promisify(fs.writeFile);
module.exports = {
async onPostBuild(opts) {
const {
inputs: {
exclude = [],
generatedFunctionName = 'search',
publishDirJSONFileName = 'searchIndex',
debugMode,
},
constants: { PUBLISH_DIR, FUNCTIONS_SRC, FUNCTIONS_DIST },
utils: { build }
} = opts;
if (generatedFunctionName === null && publishDirJSONFileName === null) {
build.failPlugin(
'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 searchIndex = {}
const newManifest = await walk(PUBLISH_DIR, exclude)
// https://www.npmjs.com/package/html-to-text#user-content-options
await Promise.all(
newManifest.map(async (htmlFilePath) => {
const indexPath = path.relative(PUBLISH_DIR, htmlFilePath);
const htmlFileContent = await readFile(htmlFilePath, 'utf8');
searchIndex[`/${indexPath}`] = await parse(htmlFileContent, htmlFilePath, { PUBLISH_DIR })
})
);
let stringifiedIndex = JSON.stringify(searchIndex);
/**
*
* clientside JSON
*
*/
if (publishDirJSONFileName) {
let searchIndexPath = path.join(
PUBLISH_DIR,
publishDirJSONFileName + '.json'
);
if (await pathExists(searchIndexPath)) {
console.warn(
`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?
}
await makeDir(`${searchIndexPath}/..`); // make a dir out of the parent
await writeFile(searchIndexPath, stringifiedIndex);
console.log(
`Search Index JSON generated at ${chalk.cyan(
`/${publishDirJSONFileName}.json`
)}!`
);
}
/**
*
* serverless function + json
*
*/
if (generatedFunctionName) {
if (typeof FUNCTIONS_SRC === 'undefined') {
build.failPlugin('FUNCTIONS_SRC is undefined - did you forget to declare a functions folder in netlify.toml? https://github.com/sw-yx/netlify-plugin-search-index#usage')
}
const searchIndexFunctionPath = path.join(
FUNCTIONS_SRC,
generatedFunctionName
);
const vars = {
searchIndex: generatedFunctionName
};
await copy(
__dirname + '/functionTemplate',
searchIndexFunctionPath,
vars
);
// now we have copied it out to intermediate dir
// we may want to do some processing/templating
await writeFile(
path.join(searchIndexFunctionPath, 'searchIndex.json'),
stringifiedIndex
);
// and then..
// we still need to zip this to dist because netlify build doesnt recognize generated functions
await zipFunctions(FUNCTIONS_SRC, FUNCTIONS_DIST);
console.log(
`Netlify Function generated at ${chalk.cyan(
`/.netlify/functions/${generatedFunctionName}`
)}!`
);
// done with generating functions
}
}
}
async function walk(dir, exclude = []) {
return (await globby(path.join(dir, '**/*.html')))
.filter(p =>
exclude
.find(r => p.replace(dir, '').match(r)) === undefined
)
}