From d371ee52a3d2c111cf461aebaa3c737521e6c857 Mon Sep 17 00:00:00 2001 From: david Date: Wed, 3 May 2023 19:24:50 -0400 Subject: [PATCH] creaeted a write command --- CHANGELOG.md | 3 ++ README.md | 3 +- lib/bin/commands/write.js | 79 ++++++++++++++++++++++++++++++++++++++ lib/commands/write.html.js | 4 ++ package.json | 2 +- 5 files changed, 89 insertions(+), 2 deletions(-) create mode 100644 lib/bin/commands/write.js diff --git a/CHANGELOG.md b/CHANGELOG.md index 49169c0..47c5f5e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/README.md b/README.md index f4199c6..4ff671d 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/lib/bin/commands/write.js b/lib/bin/commands/write.js new file mode 100644 index 0000000..0a4ae63 --- /dev/null +++ b/lib/bin/commands/write.js @@ -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, +}; diff --git a/lib/commands/write.html.js b/lib/commands/write.html.js index eb0882a..ebd75e0 100644 --- a/lib/commands/write.html.js +++ b/lib/commands/write.html.js @@ -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); }); diff --git a/package.json b/package.json index 878664a..40e341e 100644 --- a/package.json +++ b/package.json @@ -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",