diff --git a/VERSION b/VERSION index 1111c9c..9beca35 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -0.0.14 \ No newline at end of file +0.0.15 \ No newline at end of file diff --git a/dist/package.json b/dist/package.json index a5335e8..e189553 100644 --- a/dist/package.json +++ b/dist/package.json @@ -1,6 +1,6 @@ { "name": "pack.gl", - "version": "0.0.14", + "version": "0.0.15", "description": "Package Builder.", "keywords": [ "pack.gl", diff --git a/dist/ts/class/TemplateWriter.ts b/dist/ts/class/TemplateWriter.ts new file mode 100644 index 0000000..2ddef1f --- /dev/null +++ b/dist/ts/class/TemplateWriter.ts @@ -0,0 +1,99 @@ +// class/TemplateWriter.ts + +// Copyright 2023 Scape Agency BV + +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at + +// http://www.apache.org/licenses/LICENSE-2.0 + +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + + +// ============================================================================ +// Import +// ============================================================================ + +import fs from 'fs/promises'; +import path from 'path'; +import nunjucks from 'nunjucks'; + + +// ============================================================================ +// Classes +// ============================================================================ + +class TemplateWriter { + + /** + * Constructs a TemplateWriter instance. + * @param templatesDir - Directory for Nunjucks templates. + * @param enableCache - Enable or disable caching for Nunjucks. + */ + constructor( + templatesDir: string, + enableCache: boolean = false + ) { + nunjucks.configure(templatesDir, { + autoescape: true, + noCache: !enableCache + }); + } + + /** + * 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. + */ + async generateTemplate(template: string, context: {}): Promise<string> { + try { + // const formattedColors = this.formatColorsForTemplate(); + // return nunjucks.render(template, { colors: formattedColors }); + return nunjucks.render( + template, + context, + ); + } catch (error) { + console.error(`Error generating template: ${error}`); + // throw error; + throw new Error('Template generation failed'); + + } + } + + /** + * 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. + */ + async generateToFile(template: string, outputFile: string, context: {}): Promise<void> { + try { + const content = await this.generateTemplate(template, context); + const dir = path.dirname(outputFile); + + // Ensure the directory exists + await fs.mkdir(dir, { recursive: true }); + + // Write the file + await fs.writeFile(outputFile, content, 'utf-8'); + } catch (error) { + console.error(`Error writing to file: ${error}`); + throw new Error('File writing failed'); + } + } + +} + + +// ============================================================================ +// Export +// ============================================================================ + +export default TemplateWriter; diff --git a/dist/ts/config/nunjucks.config.ts b/dist/ts/config/nunjucks.config.ts new file mode 100644 index 0000000..9445a4f --- /dev/null +++ b/dist/ts/config/nunjucks.config.ts @@ -0,0 +1,44 @@ +// config/nunjucks.config.ts + +// Copyright 2023 Scape Agency BV + +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at + +// http://www.apache.org/licenses/LICENSE-2.0 + +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + + +// ============================================================================ +// Import +// ============================================================================ + +import path from 'node:path' + + +// ============================================================================ +// 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: !enableCache + +}; + + +// ============================================================================ +// Export +// ============================================================================ + +export default nunjucksConfig; diff --git a/package-lock.json b/package-lock.json index d71935f..7632bfd 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "pack.gl", - "version": "0.0.14", + "version": "0.0.15", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "pack.gl", - "version": "0.0.14", + "version": "0.0.15", "funding": [ { "type": "github", @@ -59,7 +59,7 @@ "icon.gl": "^0.0.1", "loop.gl": "^0.0.1", "mini-css-extract-plugin": "^2.7.6", - "pack.gl": "^0.0.13", + "pack.gl": "^0.0.15", "page.gl": "^0.0.1", "postcss-loader": "^7.3.3", "postcss-preset-env": "^9.1.2", @@ -70,7 +70,7 @@ "ts-loader": "^9.4.3", "ts-node": "^10.9.1", "typescript": "^5.3.3", - "unit.gl": "^0.0.24", + "unit.gl": "^0.0.25", "webpack": "^5.89.0", "webpack-cli": "^5.1.4", "webpack-dev-server": "^4.15.1", @@ -10566,9 +10566,9 @@ } }, "node_modules/pack.gl": { - "version": "0.0.13", - "resolved": "https://registry.npmjs.org/pack.gl/-/pack.gl-0.0.13.tgz", - "integrity": "sha512-Trfrv0r8w4uNjdYql3nZRRuVTjxBbsxh3NMf4E2BlzKnUqmlh4HG6U8kdtSAqgUmsCDwlYpch/8GntAQOI745g==", + "version": "0.0.15", + "resolved": "https://registry.npmjs.org/pack.gl/-/pack.gl-0.0.15.tgz", + "integrity": "sha512-RCJsEXrg4fOYipuCJgjWuXri28IFRo5xf7ju9gEbuTquw2TWKqoQlYQiTLXcGB/RB4Ptyx+1SekCgyOrWGXIYw==", "dev": true }, "node_modules/page.gl": { @@ -14487,9 +14487,9 @@ } }, "node_modules/unit.gl": { - "version": "0.0.24", - "resolved": "https://registry.npmjs.org/unit.gl/-/unit.gl-0.0.24.tgz", - "integrity": "sha512-xqa13Wgetjv+FXFJTnHfVWAcGxix2phVJOm8uROoct8kPeeroDg836mSv6K0LTHh+MRIqZ495lGSmyusUKxz+w==", + "version": "0.0.25", + "resolved": "https://registry.npmjs.org/unit.gl/-/unit.gl-0.0.25.tgz", + "integrity": "sha512-BHXb7kw8h82HH7xVCiRecZ/JRfLMyscs/1kxJkbvscpN1KQRl6F17Bnffet+BSEpSgwZL+5QYH70VYxHPBp9vA==", "dev": true }, "node_modules/universalify": { diff --git a/package.json b/package.json index f35c362..af54b45 100644 --- a/package.json +++ b/package.json @@ -92,7 +92,7 @@ "icon.gl": "^0.0.1", "loop.gl": "^0.0.1", "mini-css-extract-plugin": "^2.7.6", - "pack.gl": "^0.0.13", + "pack.gl": "^0.0.15", "page.gl": "^0.0.1", "postcss-loader": "^7.3.3", "postcss-preset-env": "^9.1.2", @@ -103,7 +103,7 @@ "ts-loader": "^9.4.3", "ts-node": "^10.9.1", "typescript": "^5.3.3", - "unit.gl": "^0.0.24", + "unit.gl": "^0.0.25", "webpack": "^5.89.0", "webpack-cli": "^5.1.4", "webpack-dev-server": "^4.15.1", diff --git a/src/ts/class/TemplateWriter.ts b/src/ts/class/TemplateWriter.ts new file mode 100644 index 0000000..2ddef1f --- /dev/null +++ b/src/ts/class/TemplateWriter.ts @@ -0,0 +1,99 @@ +// class/TemplateWriter.ts + +// Copyright 2023 Scape Agency BV + +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at + +// http://www.apache.org/licenses/LICENSE-2.0 + +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + + +// ============================================================================ +// Import +// ============================================================================ + +import fs from 'fs/promises'; +import path from 'path'; +import nunjucks from 'nunjucks'; + + +// ============================================================================ +// Classes +// ============================================================================ + +class TemplateWriter { + + /** + * Constructs a TemplateWriter instance. + * @param templatesDir - Directory for Nunjucks templates. + * @param enableCache - Enable or disable caching for Nunjucks. + */ + constructor( + templatesDir: string, + enableCache: boolean = false + ) { + nunjucks.configure(templatesDir, { + autoescape: true, + noCache: !enableCache + }); + } + + /** + * 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. + */ + async generateTemplate(template: string, context: {}): Promise<string> { + try { + // const formattedColors = this.formatColorsForTemplate(); + // return nunjucks.render(template, { colors: formattedColors }); + return nunjucks.render( + template, + context, + ); + } catch (error) { + console.error(`Error generating template: ${error}`); + // throw error; + throw new Error('Template generation failed'); + + } + } + + /** + * 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. + */ + async generateToFile(template: string, outputFile: string, context: {}): Promise<void> { + try { + const content = await this.generateTemplate(template, context); + const dir = path.dirname(outputFile); + + // Ensure the directory exists + await fs.mkdir(dir, { recursive: true }); + + // Write the file + await fs.writeFile(outputFile, content, 'utf-8'); + } catch (error) { + console.error(`Error writing to file: ${error}`); + throw new Error('File writing failed'); + } + } + +} + + +// ============================================================================ +// Export +// ============================================================================ + +export default TemplateWriter; diff --git a/src/ts/config/nunjucks.config.ts b/src/ts/config/nunjucks.config.ts new file mode 100644 index 0000000..9445a4f --- /dev/null +++ b/src/ts/config/nunjucks.config.ts @@ -0,0 +1,44 @@ +// config/nunjucks.config.ts + +// Copyright 2023 Scape Agency BV + +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at + +// http://www.apache.org/licenses/LICENSE-2.0 + +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + + +// ============================================================================ +// Import +// ============================================================================ + +import path from 'node:path' + + +// ============================================================================ +// 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: !enableCache + +}; + + +// ============================================================================ +// Export +// ============================================================================ + +export default nunjucksConfig;