From a80aeb2f7f53cee94744fad7ee6b287c1720abec Mon Sep 17 00:00:00 2001 From: "Dylan R. E. Moonfire" Date: Thu, 27 Aug 2020 20:54:28 -0500 Subject: [PATCH] feat(extract): added additional options to extract - `--no-list` to not write the output as a list of objects - `--yaml` to write the output as YAML - `--content` to extract the content of a property of the given name - Updated read me to include usage of extract --- README.md | 14 ++++++++ package-lock.json | 9 +++++ package.json | 1 + src/tools/extract.ts | 79 +++++++++++++++++++++++++++++++++++++++----- 4 files changed, 94 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 25a8899..c8a40c1 100644 --- a/README.md +++ b/README.md @@ -40,6 +40,20 @@ Options: * `-t`, `--total`: Add a total line to the bottom that combines. * `-s`, `--separator`: Comma-separates the numbers in the total. +### extract + +> Extract the metadata (front matter) from one or more files. + +```sh +markdowny extract *.markdown [options] +``` + +Options: + +* `--no-list`: Don't write out the extract data as a list/sequence of items (default). +* `-y`, `--yaml`: Extract the data as YAML instead of JSON (default). +* `--content PROPERTY`: Extract the content as a property of the given name instead of just removing it. Defaults to not extracting the content. + ### sections > Displays each file as a heading 1 section with the title and contents provided. diff --git a/package-lock.json b/package-lock.json index a799d84..fe6de47 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1699,6 +1699,15 @@ "integrity": "sha512-wWKOClTTiizcZhXnPY4wikVAwmdYHp8q6DmC+EJUzAMsycb7HB32Kh9RN4+0gExjmPmZSAQjgURXIGATPegAvA==", "dev": true }, + "@types/yaml": { + "version": "1.9.7", + "resolved": "https://registry.npmjs.org/@types/yaml/-/yaml-1.9.7.tgz", + "integrity": "sha512-8WMXRDD1D+wCohjfslHDgICd2JtMATZU8CkhH8LVJqcJs6dyYj5TGptzP8wApbmEullGBSsCEzzap73DQ1HJaA==", + "dev": true, + "requires": { + "yaml": "*" + } + }, "@types/yargs": { "version": "11.1.6", "resolved": "https://registry.npmjs.org/@types/yargs/-/yargs-11.1.6.tgz", diff --git a/package.json b/package.json index 0928acf..d937428 100644 --- a/package.json +++ b/package.json @@ -54,6 +54,7 @@ "@semantic-release/npm": "^5.0.1", "@types/lodash": "^4.14.116", "@types/read-pkg-up": "^3.0.1", + "@types/yaml": "^1.9.7", "@types/yargs": "^11.1.1", "barrelsby": "^1.0.2", "commitizen": "^2.10.1", diff --git a/src/tools/extract.ts b/src/tools/extract.ts index 473e473..9aa4e78 100644 --- a/src/tools/extract.ts +++ b/src/tools/extract.ts @@ -1,6 +1,8 @@ import * as _ from "lodash"; -import * as yargs from "yargs"; +import * as fs from "fs"; import * as scanner from "../scanner"; +import * as yaml from "js-yaml"; +import * as yargs from "yargs"; export var command = "extract"; export var describe = "Extracts a YAML from a list of files"; @@ -10,20 +12,79 @@ export function builder(yargs: yargs.Arguments) return yargs .help("help") - .alias("o", "output") - .default("output", "-") + .option( + "list", + { + type: "boolean", + default: true, + describe: "Extract the data as a list of files, otherwise as a single file", + } + ) + .option( + "output", + { + alias: "o", + default: "-", + describe: "Write output to a file or `-` for standard out", + } + ) + .option( + "yaml", + { + alias: "y", + type: "boolean", + default: false, + describe: "Extract the output as YAML instead of JSON", + } + ) + .option( + "content", + { + type: "string", + describe: "Include the content as a property in the extracted data", + } + ) .demand(1); } export function handler(argv: any) { + // Parse through the files and retrieve the metadata. var files = argv._.splice(1); - var data = scanner.scanFiles(argv, files); - render(argv, data); -} + var data: any = scanner.scanFiles(argv, files); -export function render(argv, data) -{ - console.log(JSON.stringify(data)); + // Figure out how to handle the content property. + if (!argv.content) + { + // Remove the content from the parsed file. + data.forEach(x => delete x.__content); + } else if (argv.content !== "__content") + { + // Rename the content property. + data.forEach(x => { + x[argv.content] = x.__content; + delete x.__content; + }) + } + + // Determine if we are displaying a list of items. If we want a list, then + // we wrap the data in another list (list of a list) so it writes out only + // one item. Otherwise, we write out each one. + const items = argv.list + ? [data] + : data; + + // Figure the output of the results (JSON or YAML). + const output = argv.yaml + ? items.map((x: any) => yaml.safeDump(x)).join("\n") + : items.map((x: any) => JSON.stringify(x)).join("\n"); + + // Figure out where to write. + if (argv.output === "-") + { + console.log(output); + } else { + fs.writeFileSync(argv.output, Buffer.from(output, "utf-8")); + } }