Skip to content

Commit

Permalink
creaeted a write command
Browse files Browse the repository at this point in the history
  • Loading branch information
dvidsilva committed May 3, 2023
1 parent 0f722bd commit d371ee5
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 2 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Next Version

# 2.1.7
- Write command created

# 2.1.6
- Copying files that are meant to be copied before running CSS
- Fixed sample files
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,8 @@ npm run gloria-local -- --version
- `build` - `[path] [dest]` interpolates the content into the layout
- `css:tailwind` - `[path] [dest]` runs tailwind in the html output
- `scripts` - `[path] [dest]` @TODO processes scripts
- `write` - `[dest]` @TODO writes production version to disk and cleans temp files
- `write` - `[dest]` writes production version to disk
- `cleanup` - `[dest]` @TODO: cleans up temp files after a successful build
- `version` - ` ` returns the current package version

The commands all should work on their own, and the ones that combine multiple steps do it very nicely by using promises, we pass the project around, and store and read from its properties, to create new commands or amplify existing ones, for plugins and templates, review the data structure.
Expand Down
79 changes: 79 additions & 0 deletions lib/bin/commands/write.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
const logger = require('../../utils/logger');
const config = require('../../core/project.config');
const _collect = require('../../commands/collect');
const _extract = require('../../commands/extract');
const _prebuild = require('../../commands/prebuild');
const _build = require('../../commands/build');
const _write_html = require('../../commands/write.html');
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.
*
* This method
* @param {string} argv.dest If present, write result to destination file
* @param {string} argv.folder If present writes files to folder
* @param {string} argv.write If false, skips writing files to disk
*
* @return {object} information like content and medatata from every coollected path
*/
const write = (argv) => {
const project = {
config: config.get(),
collected: {},
content: {},
extracted: {},
prebuilt: {
files: {},
styles: {},
scripts: {},
},
html: [],
};

_collect
.run(project)
.then((project) => _extract.run(project))
.then((project) => _prebuild.run(project))
.then((project) => _build.run(project))
.then((prochecto) => {
const { config } = prochecto;

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

// logger.log({ ...prochecto });
return _write_html.run(prochecto);
})
.catch((e) => {
logger.error({ e });
});
};

const options = {
dest: {
default: null,
description: `Destination folder.`,
},
};

/**
* Command to compile html files
*/
module.exports = {
command: `write [dest]`,
aliases: ['write:html'],
describe: `Compiles files, interpolates data and writes files to their correct url.
By default it will use a folder name 'build' in the root directory of the project,
Check your _config.yml to change.
It won't build to a parent folder.
The command will fail if _config.yml is invalid or not present.`,
builder: options,
handler: write,
};
4 changes: 4 additions & 0 deletions lib/commands/write.html.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,14 @@ const run = (project) => {

const parsed = upath.parse(destination);

logger.log({ "Looking for dir:": parsed.dir });
if (!fs.existsSync(parsed.dir)) {
logger.log({ "Created dir:": parsed.dir });
fs.mkdirSync(parsed.dir, { recursive: true });
}

logger.log({ "Writing to dest:": destination });

fs.writeFileSync(destination, file.content);
});

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@gloriajs/gloria",
"version": "2.1.6",
"version": "2.1.7",
"description": "Gloria is a NodeJS simple static website generator.",
"main": "cli.js",
"bin": "cli.js",
Expand Down

0 comments on commit d371ee5

Please sign in to comment.