Skip to content

Instantly share code, notes, and snippets.

@marionebl
Created January 11, 2019 10:10
Show Gist options
  • Save marionebl/0f51e279235d2b825005b36c64345db8 to your computer and use it in GitHub Desktop.
Save marionebl/0f51e279235d2b825005b36c64345db8 to your computer and use it in GitHub Desktop.
patternplate search cli prototpe
import * as Path from "path";
import { loadConfig } from "@patternplate/load-config";
import { loadDocs } from "@patternplate/load-docs";
import { loadMeta } from "@patternplate/load-meta";
import { validate } from "@patternplate/validate-config";
import { createSearch } from "@patternplate/search";
import * as Types from "@patternplate/types";
const yargsParser = require("yargs-parser");
interface Flags {
[flag: string]: unknown;
}
export async function main(cli: Flags) {
const cwd =
typeof cli.cwd === "string" ? Path.resolve(cli.cwd) : process.cwd();
if (typeof cli.query !== "string" && !cli.query) {
console.error(`--query must be a non-empty string`);
process.exit(1);
return;
}
const loaded = await loadConfig({ cwd });
const [error, valid] = validate({
target: loaded.config,
name: loaded.filepath
});
if (!valid && error) {
console.error(error);
process.exit(1);
return;
}
const validated = loaded.config as Types.PatternplateConfig;
const configCwd = loaded.filepath ? Path.dirname(loaded.filepath) : cwd;
const [docs, { patterns }] = await Promise.all([
loadDocs({
cwd: configCwd,
docs: validated.docs || []
}),
loadMeta({
cwd: configCwd,
entry: validated.entry || []
})
]);
const pool = [...docs, ...patterns];
const search = createSearch(pool);
search(cli.query)
.map(id => pool.find(p => p.id === id))
.forEach(item => console.log(JSON.stringify(item)));
}
main(yargsParser(process.argv.slice(2))).catch(err => {
throw err;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment