Skip to content

Commit

Permalink
feat: add search to page
Browse files Browse the repository at this point in the history
  • Loading branch information
daKmoR committed Apr 2, 2022
1 parent 2d9eec2 commit d220d35
Show file tree
Hide file tree
Showing 44 changed files with 707 additions and 675 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ dist-types
stats.html
*.tsbuildinfo

# Rocket Search
rocket-search-index.json

## Rocket ignore files (need to be the full relative path to the folders)
*-mdjs-generated.js
_site
Expand Down
4 changes: 2 additions & 2 deletions config/rocket.config.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { rocketLaunch } from '@rocket/launch';
import { rocketSpark } from '@rocket/spark';
// import { rocketBlog } from '@rocket/blog';
// import { rocketSearch } from '@rocket/search';
import { presetRocketSearch } from '@rocket/search';
// import { absoluteBaseUrlNetlify } from '@rocket/core/helpers';
// import { adjustPluginOptions } from 'plugins-manager';
// TODO: preset needs to be updated to use the new plugin manager
Expand Down Expand Up @@ -38,8 +38,8 @@ export default {
presets: [
rocketLaunch(),
rocketSpark(),
presetRocketSearch(),
// rocketBlog(),
// rocketSearch(),
// codeTabs({
// collections: {
// packageManagers: {
Expand Down
16 changes: 9 additions & 7 deletions packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@
"types": "./dist-types/src/index.d.ts",
"default": "./src/index.js"
},
"./test-helpers": "./test-helpers/index.js"
"./test-helpers": {
"types": "./dist-types/test-helpers/index.d.ts",
"default": "./test-helpers/index.js"
}
},
"scripts": {
"build": "npm run rocket:build",
Expand All @@ -36,20 +39,16 @@
"xtest:watch": "mocha test/**/*.test.js --parallel --watch"
},
"files": [
"*.cjs",
"*.js",
"*.mjs",
"dist",
"dist-types",
"src",
"test-helpers"
"test-helpers",
"types"
],
"keywords": [
"rocket",
"docs",
"ssg",
"demo",
"11ty",
"rollup"
],
"dependencies": {
Expand All @@ -71,6 +70,9 @@
"*": {
"*": [
"./dist-types/src/index.d.ts"
],
"test-helpers": [
"./dist-types/test-helpers/index.d.ts"
]
}
}
Expand Down
4 changes: 4 additions & 0 deletions packages/cli/src/RocketBuild.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ export class RocketBuild {
}

async build() {
await this.cli.events.dispatchEventDone('build-start');

this.engine = new Engine();
this.engine.setOptions({
docsDir: this.cli.options.inputDir,
Expand Down Expand Up @@ -118,6 +120,8 @@ export class RocketBuild {
);
await writeFile(notFoundHtmlFilePath, notFoundHtml);
}

await this.cli.events.dispatchEventDone('build-end');
}

async buildOpenGraphImages() {
Expand Down
3 changes: 3 additions & 0 deletions packages/cli/src/RocketCli.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import path from 'path';
import { rm } from 'fs/promises';
import { mergeDeep } from './helpers/mergeDeep.js';
import { existsSync } from 'fs';
import { AsyncEventEmitter } from './helpers/AsyncEventEmitter.js';

/** @typedef {import('../types/main.js').RocketCliPlugin} RocketCliPlugin */
/** @typedef {import('../types/main.js').FullRocketCliOptions} FullRocketCliOptions */
Expand Down Expand Up @@ -60,6 +61,8 @@ export class RocketCli {
// },
};

events = new AsyncEventEmitter();

/** @type {RocketCliPlugin | undefined} */
activePlugin = undefined;

Expand Down
21 changes: 21 additions & 0 deletions packages/cli/src/helpers/AsyncEventEmitter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { EventEmitter } from 'events';

/**
* This class emits events asynchronously.
* It can be used for time measurements during a build.
*/
export class AsyncEventEmitter extends EventEmitter {
/**
* @param {string} type - The event name to emit.
* @param {*[]} args - Additional arguments that get passed to listeners.
* @returns {Promise<*[]>} - Promise resolves once all listeners were invoked
*/
async dispatchEventDone(type, ...args) {
let listeners = this.listeners(type);
if (listeners.length === 0) {
return [];
}

return Promise.all(listeners.map(listener => listener.apply(this, args)));
}
}
187 changes: 6 additions & 181 deletions packages/cli/test-helpers/index.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,14 @@
import chai from 'chai';
import { RocketCli } from '../src/RocketCli.js';
import path from 'path';
import globby from 'globby';
import fs, { move, remove } from 'fs-extra';
import prettier from 'prettier';
import { fileURLToPath } from 'url';
import { existsSync } from 'fs';

const { expect } = chai;

let fixtureDir = '';
import path from 'path';
import { setupTestCli } from './test-helpers.js';

export function setFixtureDir(importMetaUrl) {
fixtureDir = path.dirname(fileURLToPath(importMetaUrl));
export function prepareTestCli(importMetaUrl) {
const dir = path.dirname(fileURLToPath(importMetaUrl));
return (cwd, cliOptions = ['build'], options = {}) => setupTestCli(cwd, cliOptions, options, dir);
}

/**
* @typedef {object} readOutputOptions
* @property {boolean} stripToBody
* @property {boolean} stripStartEndWhitespace
* @property {boolean} stripScripts
* @property {boolean} formatHtml
* @property {boolean} replaceImageHashes
* @property {start|build} type
*/
const { expect } = chai;

/**
* @param {function} method
Expand All @@ -44,163 +29,3 @@ export async function expectThrowsAsync(method, { errorMatch, errorMessage } = {
expect(error.message).to.equal(errorMessage);
}
}

export async function readOutput(
cli,
fileName,
{
stripToBody = false,
stripStartEndWhitespace = true,
stripScripts = false,
formatHtml = false,
type = 'build',
replaceImageHashes = false,
} = {},
) {
if (!cli || !cli.config) {
throw new Error(`No valid cli provided to readOutput - you passed a ${typeof cli}: ${cli}`);
}

const outputDir =
type === 'bootstrap'
? path.join(cli.config.outputDir, '..')
: type === 'build'
? cli.config.outputDir
: cli.config.outputDevDir;

let text = await fs.promises.readFile(path.join(outputDir, fileName));
text = text.toString();
if (stripToBody) {
const bodyOpenTagEnd = text.indexOf('>', text.indexOf('<body') + 1) + 1;
const bodyCloseTagStart = text.indexOf('</body>');
text = text.substring(bodyOpenTagEnd, bodyCloseTagStart);
}
if (stripScripts) {
const scriptOpenTagEnd = text.indexOf('<script>');
const scriptCloseTagStart = text.indexOf('</script>', scriptOpenTagEnd) + 9;
text = text.substring(0, scriptOpenTagEnd) + text.substring(scriptCloseTagStart);
}
if (replaceImageHashes) {
text = text.replace(/\/images\/([a-z0-9]+)-/g, '/images/__HASH__-');
}
if (formatHtml) {
text = prettier.format(text, { parser: 'html', printWidth: 100 });
}
if (stripStartEndWhitespace) {
text = text.trim();
}
return text;
}

export async function getfixtureExpectedFiles(pathToDir) {
const cwd = path.join(fixtureDir, pathToDir);
const paths = await globby('**/*', { cwd, absolute: true, dot: true });
return paths;
}

export async function execute(pathToConfig, { type = 'start', captureLog = false } = {}) {
let log = [];
const origLog = console.log;
if (captureLog) {
console.log = (...args) => {
log = [...log, ...args];
};
}

const configFile = path.join(fixtureDir, pathToConfig.split('/').join(path.sep));
const configFileDir = path.dirname(configFile);

const cli = new RocketCli({
argv: [type, '--config-file', configFile],
});

await cli.setup();
cli.config.outputDevDir = path.join(configFileDir, '__output-dev');
cli.config.devServer.open = false;
cli.config.devServer.port = 8080;
cli.config.watch = false;
cli.config.outputDir = path.join(configFileDir, '__output');

await fs.emptyDir(cli.config.outputDevDir);
await fs.emptyDir(cli.config.outputDir);

await cli.run();

/**
* @param {*} cli
* @param {string} fileName
* @param {readOutputOptions} options
*/
async function readOutput2(fileName, options = {}) {
options.type = type;
return readOutput(cli, fileName, options);
}

function outputExists(fileName) {
const outputDir = type === 'build' ? cli.config.outputDir : cli.config.outputDevDir;
const filePath = path.join(outputDir, fileName);

return fs.existsSync(filePath);
}

if (captureLog) {
console.log = origLog;
}
return { log, readOutput: readOutput2, cli, outputExists };
}

export async function executeBootstrap(pathToDir) {
const configFileDir = path.join(fixtureDir, pathToDir.split('/').join(path.sep));
const cli = new RocketCli({ argv: ['bootstrap'] });

await cli.setup();
cli.config.outputDevDir = path.join(configFileDir, '__output-dev');
cli.config.devServer.open = false;
cli.config.devServer.port = 8080;
cli.config.watch = false;
cli.config.outputDir = path.join(configFileDir, '__output');

await fs.emptyDir(configFileDir);
await cli.run();

return { cli };
}

export async function executeUpgrade(pathToConfig) {
const configFile = path.join(fixtureDir, pathToConfig.split('/').join(path.sep));
const cli = new RocketCli({
argv: ['upgrade', '--config-file', configFile],
});
await cli.setup();

// restore from backup if available - in cases the test did stop in the middle
if (cli.config._inputDirCwdRelative) {
const backupDir = path.join(cli.config._inputDirCwdRelative, '..', 'docs_backup');
if (existsSync(backupDir)) {
await remove(cli.config._inputDirCwdRelative);
await move(backupDir, cli.config._inputDirCwdRelative);
}
}
await cli.run();
return {
cli,
fileExists: fileName => {
const outputDir = cli.config._inputDirCwdRelative;
return fs.existsSync(path.join(outputDir, fileName));
},
readFile: async fileName => {
// TODO: use readOutput once it's changed to read full file paths
const filePath = path.join(cli.config._inputDirCwdRelative, fileName);
const text = await fs.promises.readFile(filePath);
return text.toString();
},
};
}

export function trimWhiteSpace(inString) {
return inString
.split('\n')
.map(line => line.trim())
.filter(line => line)
.join('\n');
}

0 comments on commit d220d35

Please sign in to comment.