Skip to content

Commit d220d35

Browse files
committed
feat: add search to page
1 parent 2d9eec2 commit d220d35

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+707
-675
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ dist-types
2626
stats.html
2727
*.tsbuildinfo
2828

29+
# Rocket Search
30+
rocket-search-index.json
31+
2932
## Rocket ignore files (need to be the full relative path to the folders)
3033
*-mdjs-generated.js
3134
_site

config/rocket.config.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { rocketLaunch } from '@rocket/launch';
22
import { rocketSpark } from '@rocket/spark';
33
// import { rocketBlog } from '@rocket/blog';
4-
// import { rocketSearch } from '@rocket/search';
4+
import { presetRocketSearch } from '@rocket/search';
55
// import { absoluteBaseUrlNetlify } from '@rocket/core/helpers';
66
// import { adjustPluginOptions } from 'plugins-manager';
77
// TODO: preset needs to be updated to use the new plugin manager
@@ -38,8 +38,8 @@ export default {
3838
presets: [
3939
rocketLaunch(),
4040
rocketSpark(),
41+
presetRocketSearch(),
4142
// rocketBlog(),
42-
// rocketSearch(),
4343
// codeTabs({
4444
// collections: {
4545
// packageManagers: {

packages/cli/package.json

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,10 @@
2323
"types": "./dist-types/src/index.d.ts",
2424
"default": "./src/index.js"
2525
},
26-
"./test-helpers": "./test-helpers/index.js"
26+
"./test-helpers": {
27+
"types": "./dist-types/test-helpers/index.d.ts",
28+
"default": "./test-helpers/index.js"
29+
}
2730
},
2831
"scripts": {
2932
"build": "npm run rocket:build",
@@ -36,20 +39,16 @@
3639
"xtest:watch": "mocha test/**/*.test.js --parallel --watch"
3740
},
3841
"files": [
39-
"*.cjs",
40-
"*.js",
41-
"*.mjs",
42-
"dist",
4342
"dist-types",
4443
"src",
45-
"test-helpers"
44+
"test-helpers",
45+
"types"
4646
],
4747
"keywords": [
4848
"rocket",
4949
"docs",
5050
"ssg",
5151
"demo",
52-
"11ty",
5352
"rollup"
5453
],
5554
"dependencies": {
@@ -71,6 +70,9 @@
7170
"*": {
7271
"*": [
7372
"./dist-types/src/index.d.ts"
73+
],
74+
"test-helpers": [
75+
"./dist-types/test-helpers/index.d.ts"
7476
]
7577
}
7678
}

packages/cli/src/RocketBuild.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,8 @@ export class RocketBuild {
8787
}
8888

8989
async build() {
90+
await this.cli.events.dispatchEventDone('build-start');
91+
9092
this.engine = new Engine();
9193
this.engine.setOptions({
9294
docsDir: this.cli.options.inputDir,
@@ -118,6 +120,8 @@ export class RocketBuild {
118120
);
119121
await writeFile(notFoundHtmlFilePath, notFoundHtml);
120122
}
123+
124+
await this.cli.events.dispatchEventDone('build-end');
121125
}
122126

123127
async buildOpenGraphImages() {

packages/cli/src/RocketCli.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import path from 'path';
1010
import { rm } from 'fs/promises';
1111
import { mergeDeep } from './helpers/mergeDeep.js';
1212
import { existsSync } from 'fs';
13+
import { AsyncEventEmitter } from './helpers/AsyncEventEmitter.js';
1314

1415
/** @typedef {import('../types/main.js').RocketCliPlugin} RocketCliPlugin */
1516
/** @typedef {import('../types/main.js').FullRocketCliOptions} FullRocketCliOptions */
@@ -60,6 +61,8 @@ export class RocketCli {
6061
// },
6162
};
6263

64+
events = new AsyncEventEmitter();
65+
6366
/** @type {RocketCliPlugin | undefined} */
6467
activePlugin = undefined;
6568

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { EventEmitter } from 'events';
2+
3+
/**
4+
* This class emits events asynchronously.
5+
* It can be used for time measurements during a build.
6+
*/
7+
export class AsyncEventEmitter extends EventEmitter {
8+
/**
9+
* @param {string} type - The event name to emit.
10+
* @param {*[]} args - Additional arguments that get passed to listeners.
11+
* @returns {Promise<*[]>} - Promise resolves once all listeners were invoked
12+
*/
13+
async dispatchEventDone(type, ...args) {
14+
let listeners = this.listeners(type);
15+
if (listeners.length === 0) {
16+
return [];
17+
}
18+
19+
return Promise.all(listeners.map(listener => listener.apply(this, args)));
20+
}
21+
}

packages/cli/test-helpers/index.js

Lines changed: 6 additions & 181 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,14 @@
11
import chai from 'chai';
2-
import { RocketCli } from '../src/RocketCli.js';
3-
import path from 'path';
4-
import globby from 'globby';
5-
import fs, { move, remove } from 'fs-extra';
6-
import prettier from 'prettier';
72
import { fileURLToPath } from 'url';
8-
import { existsSync } from 'fs';
9-
10-
const { expect } = chai;
11-
12-
let fixtureDir = '';
3+
import path from 'path';
4+
import { setupTestCli } from './test-helpers.js';
135

14-
export function setFixtureDir(importMetaUrl) {
15-
fixtureDir = path.dirname(fileURLToPath(importMetaUrl));
6+
export function prepareTestCli(importMetaUrl) {
7+
const dir = path.dirname(fileURLToPath(importMetaUrl));
8+
return (cwd, cliOptions = ['build'], options = {}) => setupTestCli(cwd, cliOptions, options, dir);
169
}
1710

18-
/**
19-
* @typedef {object} readOutputOptions
20-
* @property {boolean} stripToBody
21-
* @property {boolean} stripStartEndWhitespace
22-
* @property {boolean} stripScripts
23-
* @property {boolean} formatHtml
24-
* @property {boolean} replaceImageHashes
25-
* @property {start|build} type
26-
*/
11+
const { expect } = chai;
2712

2813
/**
2914
* @param {function} method
@@ -44,163 +29,3 @@ export async function expectThrowsAsync(method, { errorMatch, errorMessage } = {
4429
expect(error.message).to.equal(errorMessage);
4530
}
4631
}
47-
48-
export async function readOutput(
49-
cli,
50-
fileName,
51-
{
52-
stripToBody = false,
53-
stripStartEndWhitespace = true,
54-
stripScripts = false,
55-
formatHtml = false,
56-
type = 'build',
57-
replaceImageHashes = false,
58-
} = {},
59-
) {
60-
if (!cli || !cli.config) {
61-
throw new Error(`No valid cli provided to readOutput - you passed a ${typeof cli}: ${cli}`);
62-
}
63-
64-
const outputDir =
65-
type === 'bootstrap'
66-
? path.join(cli.config.outputDir, '..')
67-
: type === 'build'
68-
? cli.config.outputDir
69-
: cli.config.outputDevDir;
70-
71-
let text = await fs.promises.readFile(path.join(outputDir, fileName));
72-
text = text.toString();
73-
if (stripToBody) {
74-
const bodyOpenTagEnd = text.indexOf('>', text.indexOf('<body') + 1) + 1;
75-
const bodyCloseTagStart = text.indexOf('</body>');
76-
text = text.substring(bodyOpenTagEnd, bodyCloseTagStart);
77-
}
78-
if (stripScripts) {
79-
const scriptOpenTagEnd = text.indexOf('<script>');
80-
const scriptCloseTagStart = text.indexOf('</script>', scriptOpenTagEnd) + 9;
81-
text = text.substring(0, scriptOpenTagEnd) + text.substring(scriptCloseTagStart);
82-
}
83-
if (replaceImageHashes) {
84-
text = text.replace(/\/images\/([a-z0-9]+)-/g, '/images/__HASH__-');
85-
}
86-
if (formatHtml) {
87-
text = prettier.format(text, { parser: 'html', printWidth: 100 });
88-
}
89-
if (stripStartEndWhitespace) {
90-
text = text.trim();
91-
}
92-
return text;
93-
}
94-
95-
export async function getfixtureExpectedFiles(pathToDir) {
96-
const cwd = path.join(fixtureDir, pathToDir);
97-
const paths = await globby('**/*', { cwd, absolute: true, dot: true });
98-
return paths;
99-
}
100-
101-
export async function execute(pathToConfig, { type = 'start', captureLog = false } = {}) {
102-
let log = [];
103-
const origLog = console.log;
104-
if (captureLog) {
105-
console.log = (...args) => {
106-
log = [...log, ...args];
107-
};
108-
}
109-
110-
const configFile = path.join(fixtureDir, pathToConfig.split('/').join(path.sep));
111-
const configFileDir = path.dirname(configFile);
112-
113-
const cli = new RocketCli({
114-
argv: [type, '--config-file', configFile],
115-
});
116-
117-
await cli.setup();
118-
cli.config.outputDevDir = path.join(configFileDir, '__output-dev');
119-
cli.config.devServer.open = false;
120-
cli.config.devServer.port = 8080;
121-
cli.config.watch = false;
122-
cli.config.outputDir = path.join(configFileDir, '__output');
123-
124-
await fs.emptyDir(cli.config.outputDevDir);
125-
await fs.emptyDir(cli.config.outputDir);
126-
127-
await cli.run();
128-
129-
/**
130-
* @param {*} cli
131-
* @param {string} fileName
132-
* @param {readOutputOptions} options
133-
*/
134-
async function readOutput2(fileName, options = {}) {
135-
options.type = type;
136-
return readOutput(cli, fileName, options);
137-
}
138-
139-
function outputExists(fileName) {
140-
const outputDir = type === 'build' ? cli.config.outputDir : cli.config.outputDevDir;
141-
const filePath = path.join(outputDir, fileName);
142-
143-
return fs.existsSync(filePath);
144-
}
145-
146-
if (captureLog) {
147-
console.log = origLog;
148-
}
149-
return { log, readOutput: readOutput2, cli, outputExists };
150-
}
151-
152-
export async function executeBootstrap(pathToDir) {
153-
const configFileDir = path.join(fixtureDir, pathToDir.split('/').join(path.sep));
154-
const cli = new RocketCli({ argv: ['bootstrap'] });
155-
156-
await cli.setup();
157-
cli.config.outputDevDir = path.join(configFileDir, '__output-dev');
158-
cli.config.devServer.open = false;
159-
cli.config.devServer.port = 8080;
160-
cli.config.watch = false;
161-
cli.config.outputDir = path.join(configFileDir, '__output');
162-
163-
await fs.emptyDir(configFileDir);
164-
await cli.run();
165-
166-
return { cli };
167-
}
168-
169-
export async function executeUpgrade(pathToConfig) {
170-
const configFile = path.join(fixtureDir, pathToConfig.split('/').join(path.sep));
171-
const cli = new RocketCli({
172-
argv: ['upgrade', '--config-file', configFile],
173-
});
174-
await cli.setup();
175-
176-
// restore from backup if available - in cases the test did stop in the middle
177-
if (cli.config._inputDirCwdRelative) {
178-
const backupDir = path.join(cli.config._inputDirCwdRelative, '..', 'docs_backup');
179-
if (existsSync(backupDir)) {
180-
await remove(cli.config._inputDirCwdRelative);
181-
await move(backupDir, cli.config._inputDirCwdRelative);
182-
}
183-
}
184-
await cli.run();
185-
return {
186-
cli,
187-
fileExists: fileName => {
188-
const outputDir = cli.config._inputDirCwdRelative;
189-
return fs.existsSync(path.join(outputDir, fileName));
190-
},
191-
readFile: async fileName => {
192-
// TODO: use readOutput once it's changed to read full file paths
193-
const filePath = path.join(cli.config._inputDirCwdRelative, fileName);
194-
const text = await fs.promises.readFile(filePath);
195-
return text.toString();
196-
},
197-
};
198-
}
199-
200-
export function trimWhiteSpace(inString) {
201-
return inString
202-
.split('\n')
203-
.map(line => line.trim())
204-
.filter(line => line)
205-
.join('\n');
206-
}

0 commit comments

Comments
 (0)