Skip to content

Commit

Permalink
draft for the extract command
Browse files Browse the repository at this point in the history
  • Loading branch information
dvidsilva committed Dec 27, 2022
1 parent 5c9272a commit 1eae9bd
Show file tree
Hide file tree
Showing 6 changed files with 147 additions and 1 deletion.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ npm run gloria-local -- --version

* `collect` - `[output]` traverses the include-paths and saves information about the available files
* `copy` - `[path] [dest]` copies the static files from source paths to dest folders
* `extract` - `[dest]` creates taxonomy, optionally outputs to file
* `build` - `[path] [dest]` @TODO
* `tailwind` - `[path] [dest]` @TODO
* `version` - ` ` returns the current package version

## Development and contributing
Expand Down
2 changes: 1 addition & 1 deletion docs/commands/collect.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ description: Documentation website for the collect command GloriaJS
# Command

```
./bin/gloria collect --dest=build-logs
./bin/gloria collect --dest=logs
```

Debugging command.
Expand Down
62 changes: 62 additions & 0 deletions lib/bin/commands/extract.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
const logger = require('../../utils/logger');
const config = require('../../core/project.config');
const _collect = require('../../commands/collect');
const _extract = require('../../commands/extract');
const fs = require('fs');

/**
* work in progress, always add on top of existing ways and prevent
* backwards compatibility issues
*
* Default include-paths are pages and posts.
* @param {string} argv.dest If present, write result to destination file
* @param {string} argv.path If present overrides configuration file
*/
const extract = (argv) => {
const project = {
config: config.get(),
collected: {},
content: {},
extracted: {},
};

_collect.run(project)
.then((p) => {
const b = _extract.run(p);
console.log({ b });
return b;
})
.then((p) => {
console.log({ p });
const { config } = p;
console.log({ e: p.extracted });

if (argv.dest) {
const dest = `./${argv.dest}/extracted.${config._hash}.json`;
const ztring = JSON.stringify(p.extracted);
fs.writeFileSync(dest, ztring);
}

logger.log(p.extracted);

return p;
});
};

const options = {
dest: {
default: null,
description: `Destination folder to output a json file with results.`,
},
};

/**
* Command to read md files and other data and create taxonomy
*/
module.exports = {
command: `extract [dest] [path]`,
aliases: [],
describe: `Traverses the source files and compiles to destination.`,
builder: options,
handler: extract,
};
1 change: 1 addition & 0 deletions lib/commands/collect.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ const run = (project) => {

return $q.all(promiseArray).then(() => {
project.collected = items;
console.log({ items });
return project;
});
};
Expand Down
80 changes: 80 additions & 0 deletions lib/commands/extract.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@

const klaw = require('klaw');
const $q = require('q');
const logger = require('../utils/logger');
const fm = require('front-matter');
const path = require('path');
const fse = require('fs-extra');
const moment = require('moment');

const extract = (c, items) => {

const stat = fse.statSync(c.path);

if (stat.isDirectory()) {
items.push({
destination: null,
content: null,
stat,
isDir: true,
});
return;
}

let content = ``;
try {
content = fse.readFileSync(c.path, `utf-8`);
} catch (err) {
if (err) { throw err; }
}

content = fm(content);
const timestamp = moment.utc(content.date ? content.date : stat.birthtime).format('X');

const destination = {
folder: '',
file: path.basename(c.path),
};

const a = {
destination: destination,
content: content.body,
stat,
fm: { timestamp, ...(content.attributes) },
};

items.push(a);
return items;
};

/**
* Extracts the metadata from files and creates a taxonomy
*
* @param {*} project
* @returns
*/
const run = (project) => {
const promise = new Promise((resolve, reject) => {
if (!project.collected || !project.collected.include) {
logger.error('Missing collected property in project at extract step');
return reject();
}

const items = {
include: [],
};

project.collected.include.map((c) => {
extract(c, items.include);
});

project.extracted = items;
return resolve(project);
});

return promise;
};

module.exports = {
run,
};
File renamed without changes.

0 comments on commit 1eae9bd

Please sign in to comment.