From fe0b2aeca2db6259dda6a494d0f23e427bdfc2f8 Mon Sep 17 00:00:00 2001 From: Lars van Vianen Date: Wed, 3 Jan 2024 18:02:36 +0100 Subject: [PATCH] v0.0.18 --- VERSION | 2 +- dist/js/class/TemplateWriter.d.ts | 15 +++++++-- dist/js/class/TemplateWriter.js | 27 ++++++++++------ dist/js/config/nunjucks.config.d.ts | 8 +++++ dist/js/config/nunjucks.config.js | 17 ++++++++++ dist/js/function/clean_directory.d.ts | 6 ++++ dist/js/function/clean_directory.js | 31 ++++++++++++++++++ dist/js/index.js | 3 +- dist/package.json | 2 +- dist/ts/class/TemplateWriter.ts | 45 +++++++++++++++++++++------ dist/ts/config/nunjucks.config.ts | 2 +- dist/ts/function/clean_directory.ts | 33 ++++++++++++++++++++ dist/ts/index.ts | 8 ++--- package-lock.json | 4 +-- package.json | 2 +- src/ts/index.ts | 8 ++--- 16 files changed, 177 insertions(+), 36 deletions(-) create mode 100644 dist/js/config/nunjucks.config.d.ts create mode 100644 dist/js/config/nunjucks.config.js diff --git a/VERSION b/VERSION index e484aaf..a618622 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -0.0.16 \ No newline at end of file +0.0.18 \ No newline at end of file diff --git a/dist/js/class/TemplateWriter.d.ts b/dist/js/class/TemplateWriter.d.ts index 0453aad..745cb39 100644 --- a/dist/js/class/TemplateWriter.d.ts +++ b/dist/js/class/TemplateWriter.d.ts @@ -1,23 +1,32 @@ declare class TemplateWriter { + context: {}; + /** + * Configuration for the Nunjucks writer compiler. + */ + private config; + /** + * Default configuration for the TypeScript compiler. + */ + private static defaultConfig; /** * Constructs a TemplateWriter instance. * @param templatesDir - Directory for Nunjucks templates. * @param enableCache - Enable or disable caching for Nunjucks. */ - constructor(templatesDir: string, enableCache?: boolean); + constructor(templatesDir: string, context: {}, customConfig?: any); /** * Generates a template using the provided template file and context. * @param template - The template file name. * @param context - Context data to render the template with. * @returns The rendered template as a string. */ - generateTemplate(template: string, context: {}): Promise; + generateTemplate(template: string): Promise; /** * Writes the rendered template content to a file. * @param template - The template file name. * @param outputFile - The output file path. * @param context - Context data to render the template with. */ - generateToFile(template: string, outputFile: string, context: {}): Promise; + generateToFile(template: string, outputFile: string): Promise; } export default TemplateWriter; diff --git a/dist/js/class/TemplateWriter.js b/dist/js/class/TemplateWriter.js index d6e4203..116d9f7 100644 --- a/dist/js/class/TemplateWriter.js +++ b/dist/js/class/TemplateWriter.js @@ -20,20 +20,29 @@ Object.defineProperty(exports, "__esModule", { value: true }); var promises_1 = __importDefault(require("fs/promises")); var path_1 = __importDefault(require("path")); var nunjucks_1 = __importDefault(require("nunjucks")); +var nunjucks_config_js_1 = __importDefault(require("../config/nunjucks.config.js")); // ============================================================================ // Classes // ============================================================================ class TemplateWriter { + /** + * Default configuration for the TypeScript compiler. + */ + static { this.defaultConfig = nunjucks_config_js_1.default; } /** * Constructs a TemplateWriter instance. * @param templatesDir - Directory for Nunjucks templates. * @param enableCache - Enable or disable caching for Nunjucks. */ - constructor(templatesDir, enableCache = false) { - nunjucks_1.default.configure(templatesDir, { - autoescape: true, - noCache: !enableCache - }); + constructor(templatesDir, context, + // enableCache: boolean = false, + customConfig = {}) { + this.context = context; + this.config = { + ...TemplateWriter.defaultConfig, + ...customConfig + }; + nunjucks_1.default.configure(templatesDir, this.config); } /** * Generates a template using the provided template file and context. @@ -41,11 +50,11 @@ class TemplateWriter { * @param context - Context data to render the template with. * @returns The rendered template as a string. */ - async generateTemplate(template, context) { + async generateTemplate(template) { try { // const formattedColors = this.formatColorsForTemplate(); // return nunjucks.render(template, { colors: formattedColors }); - return nunjucks_1.default.render(template, context); + return nunjucks_1.default.render(template, this.context); } catch (error) { console.error(`Error generating template: ${error}`); @@ -59,9 +68,9 @@ class TemplateWriter { * @param outputFile - The output file path. * @param context - Context data to render the template with. */ - async generateToFile(template, outputFile, context) { + async generateToFile(template, outputFile) { try { - const content = await this.generateTemplate(template, context); + const content = await this.generateTemplate(template); const dir = path_1.default.dirname(outputFile); // Ensure the directory exists await promises_1.default.mkdir(dir, { recursive: true }); diff --git a/dist/js/config/nunjucks.config.d.ts b/dist/js/config/nunjucks.config.d.ts new file mode 100644 index 0000000..f98f816 --- /dev/null +++ b/dist/js/config/nunjucks.config.d.ts @@ -0,0 +1,8 @@ +declare const nunjucksConfig: { + autoescape: boolean; + throwOnUndefined: boolean; + trimBlocks: boolean; + lstripBlocks: boolean; + noCache: boolean; +}; +export default nunjucksConfig; diff --git a/dist/js/config/nunjucks.config.js b/dist/js/config/nunjucks.config.js new file mode 100644 index 0000000..2b84ea8 --- /dev/null +++ b/dist/js/config/nunjucks.config.js @@ -0,0 +1,17 @@ +"use strict"; +// config/nunjucks.config.ts +Object.defineProperty(exports, "__esModule", { value: true }); +// ============================================================================ +// Constants +// ============================================================================ +const nunjucksConfig = { + autoescape: true, // Controls if output with dangerous characters are escaped automatically + throwOnUndefined: false, // Throw errors when outputting a null/undefined value + trimBlocks: true, // Automatically remove trailing newlines from a block/tag + lstripBlocks: true, // Automatically remove leading whitespace from a block/tag + noCache: true +}; +// ============================================================================ +// Export +// ============================================================================ +exports.default = nunjucksConfig; diff --git a/dist/js/function/clean_directory.d.ts b/dist/js/function/clean_directory.d.ts index e69de29..6abaa1c 100644 --- a/dist/js/function/clean_directory.d.ts +++ b/dist/js/function/clean_directory.d.ts @@ -0,0 +1,6 @@ +/** + * Cleans a specified directory and logs the process. + * @param directoryPath - The path of the directory to clean. + */ +declare function cleanDirectory(directoryPath: string): Promise; +export default cleanDirectory; diff --git a/dist/js/function/clean_directory.js b/dist/js/function/clean_directory.js index 3918c74..1cf744d 100644 --- a/dist/js/function/clean_directory.js +++ b/dist/js/function/clean_directory.js @@ -1 +1,32 @@ "use strict"; +var __importDefault = (this && this.__importDefault) || function (mod) { + return (mod && mod.__esModule) ? mod : { "default": mod }; +}; +Object.defineProperty(exports, "__esModule", { value: true }); +var DirectoryCleaner_js_1 = __importDefault(require("../class/DirectoryCleaner.js")); +var StylizedLogger_js_1 = __importDefault(require("../class/StylizedLogger.js")); +const directoryCleaner = new DirectoryCleaner_js_1.default(); +const logger = new StylizedLogger_js_1.default(); +/** + * Cleans a specified directory and logs the process. + * @param directoryPath - The path of the directory to clean. + */ +async function cleanDirectory(directoryPath) { + try { + logger.header('Clean Directories'); + await directoryCleaner.cleanDirectory(directoryPath); + logger.body(`Directory cleaned: ${directoryPath}`); + } + catch (error) { + logger.error(`Error cleaning directory: ${error}`); + throw error; // Rethrow the error for further handling if necessary + } +} +// ============================================================================ +// Export +// ============================================================================ +exports.default = cleanDirectory; +// Usage example +// cleanAndLogDirectory(CONFIG.path.dist) +// .then(() => console.log('Directory cleaning completed.')) +// .catch(error => console.error(error)); diff --git a/dist/js/index.js b/dist/js/index.js index ad695fc..42fc0a7 100644 --- a/dist/js/index.js +++ b/dist/js/index.js @@ -1,5 +1,5 @@ "use strict"; -// script/index.ts +// index.ts var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; @@ -52,6 +52,7 @@ var StylizedLogger_js_1 = __importDefault(require("./class/StylizedLogger.js")); exports.StylizedLogger = StylizedLogger_js_1.default; var TemplateWriter_js_1 = __importDefault(require("./class/TemplateWriter.js")); exports.TemplateWriter = TemplateWriter_js_1.default; +// Import | Internal Functions var gl_installer_1 = __importDefault(require("./function/gl_installer")); exports.gl_installer = gl_installer_1.default; var clean_directory_1 = __importDefault(require("./function/clean_directory")); diff --git a/dist/package.json b/dist/package.json index 2f0c396..b148057 100644 --- a/dist/package.json +++ b/dist/package.json @@ -1,6 +1,6 @@ { "name": "pack.gl", - "version": "0.0.16", + "version": "0.0.18", "description": "Package Builder.", "keywords": [ "pack.gl", diff --git a/dist/ts/class/TemplateWriter.ts b/dist/ts/class/TemplateWriter.ts index 2ddef1f..c2016d9 100644 --- a/dist/ts/class/TemplateWriter.ts +++ b/dist/ts/class/TemplateWriter.ts @@ -22,6 +22,7 @@ import fs from 'fs/promises'; import path from 'path'; import nunjucks from 'nunjucks'; +import nunjucksConfig from "../config/nunjucks.config.js" // ============================================================================ @@ -30,6 +31,19 @@ import nunjucks from 'nunjucks'; class TemplateWriter { + context: {}; + + + /** + * Configuration for the Nunjucks writer compiler. + */ + private config: {}; + + /** + * Default configuration for the TypeScript compiler. + */ + private static defaultConfig: any = nunjucksConfig; + /** * Constructs a TemplateWriter instance. * @param templatesDir - Directory for Nunjucks templates. @@ -37,12 +51,25 @@ class TemplateWriter { */ constructor( templatesDir: string, - enableCache: boolean = false + context: {}, + + // enableCache: boolean = false, + customConfig: any = {}, + ) { - nunjucks.configure(templatesDir, { - autoescape: true, - noCache: !enableCache - }); + this.context = context; + this.config = { + ...TemplateWriter.defaultConfig, + ...customConfig + }; + nunjucks.configure( + templatesDir, + this.config, + // { + // autoescape: true, + // // noCache: !enableCache + // } + ); } /** @@ -51,13 +78,13 @@ class TemplateWriter { * @param context - Context data to render the template with. * @returns The rendered template as a string. */ - async generateTemplate(template: string, context: {}): Promise { + async generateTemplate(template: string): Promise { try { // const formattedColors = this.formatColorsForTemplate(); // return nunjucks.render(template, { colors: formattedColors }); return nunjucks.render( template, - context, + this.context, ); } catch (error) { console.error(`Error generating template: ${error}`); @@ -73,9 +100,9 @@ class TemplateWriter { * @param outputFile - The output file path. * @param context - Context data to render the template with. */ - async generateToFile(template: string, outputFile: string, context: {}): Promise { + async generateToFile(template: string, outputFile: string): Promise { try { - const content = await this.generateTemplate(template, context); + const content = await this.generateTemplate(template); const dir = path.dirname(outputFile); // Ensure the directory exists diff --git a/dist/ts/config/nunjucks.config.ts b/dist/ts/config/nunjucks.config.ts index 9445a4f..d55dc88 100644 --- a/dist/ts/config/nunjucks.config.ts +++ b/dist/ts/config/nunjucks.config.ts @@ -32,7 +32,7 @@ const nunjucksConfig = { throwOnUndefined: false, // Throw errors when outputting a null/undefined value trimBlocks: true, // Automatically remove trailing newlines from a block/tag lstripBlocks: true, // Automatically remove leading whitespace from a block/tag - // noCache: !enableCache + noCache: true, }; diff --git a/dist/ts/function/clean_directory.ts b/dist/ts/function/clean_directory.ts index e69de29..6c3cfd5 100644 --- a/dist/ts/function/clean_directory.ts +++ b/dist/ts/function/clean_directory.ts @@ -0,0 +1,33 @@ +import DirectoryCleaner from '../class/DirectoryCleaner.js'; +import StylizedLogger from '../class/StylizedLogger.js'; +import CONFIG from '../path/to/config.js'; // Assuming CONFIG is imported from a config file + +const directoryCleaner = new DirectoryCleaner(); +const logger = new StylizedLogger(); + +/** + * Cleans a specified directory and logs the process. + * @param directoryPath - The path of the directory to clean. + */ +async function cleanDirectory(directoryPath: string): Promise { + try { + logger.header('Clean Directories'); + await directoryCleaner.cleanDirectory(directoryPath); + logger.body(`Directory cleaned: ${directoryPath}`); + } catch (error) { + logger.error(`Error cleaning directory: ${error}`); + throw error; // Rethrow the error for further handling if necessary + } +} + +// ============================================================================ +// Export +// ============================================================================ + +export default cleanDirectory; + + +// Usage example +// cleanAndLogDirectory(CONFIG.path.dist) +// .then(() => console.log('Directory cleaning completed.')) +// .catch(error => console.error(error)); diff --git a/dist/ts/index.ts b/dist/ts/index.ts index 232e10a..23ba71a 100644 --- a/dist/ts/index.ts +++ b/dist/ts/index.ts @@ -1,4 +1,4 @@ -// script/index.ts +// index.ts // Copyright 2023 Scape Agency BV @@ -19,7 +19,6 @@ // Import // ============================================================================ - // Import | Utility Classes import DirectoryCleaner from './class/DirectoryCleaner'; import DirectoryCopier from './class/DirectoryCopier'; @@ -40,12 +39,11 @@ import NpmCommandRunner from './class/NpmCommandRunner.js'; import StylizedLogger from './class/StylizedLogger.js'; import TemplateWriter from './class/TemplateWriter.js'; - +// Import | Internal Functions import gl_installer from './function/gl_installer'; import cleanDirectory from './function/clean_directory'; - // ============================================================================ // Export // ============================================================================ @@ -72,6 +70,8 @@ export { StylizedLogger, TemplateWriter, + // Export | Internal Functions gl_installer, cleanDirectory, + }; diff --git a/package-lock.json b/package-lock.json index a973140..adebbe4 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "pack.gl", - "version": "0.0.17", + "version": "0.0.18", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "pack.gl", - "version": "0.0.17", + "version": "0.0.18", "funding": [ { "type": "github", diff --git a/package.json b/package.json index 3773d45..63af2ae 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "pack.gl", "description": "Package Builder.", - "version": "0.0.17", + "version": "0.0.18", "config": { "version_short": "0.0" }, diff --git a/src/ts/index.ts b/src/ts/index.ts index 232e10a..23ba71a 100644 --- a/src/ts/index.ts +++ b/src/ts/index.ts @@ -1,4 +1,4 @@ -// script/index.ts +// index.ts // Copyright 2023 Scape Agency BV @@ -19,7 +19,6 @@ // Import // ============================================================================ - // Import | Utility Classes import DirectoryCleaner from './class/DirectoryCleaner'; import DirectoryCopier from './class/DirectoryCopier'; @@ -40,12 +39,11 @@ import NpmCommandRunner from './class/NpmCommandRunner.js'; import StylizedLogger from './class/StylizedLogger.js'; import TemplateWriter from './class/TemplateWriter.js'; - +// Import | Internal Functions import gl_installer from './function/gl_installer'; import cleanDirectory from './function/clean_directory'; - // ============================================================================ // Export // ============================================================================ @@ -72,6 +70,8 @@ export { StylizedLogger, TemplateWriter, + // Export | Internal Functions gl_installer, cleanDirectory, + };