+11
-5
@@ -0,1 +1,2 @@ | ||
| #!/usr/bin/env node | ||
| 'use strict'; | ||
@@ -5,14 +6,19 @@ | ||
| const {version} = require('./package.json'); | ||
| const {performance} = require('perf_hooks'); | ||
| console.log(`tinyjam v${version}`); | ||
| if (process.argv.length < 4) { | ||
| console.log(`tinyjam v${version}`); | ||
| console.log('Usage: tinyjam <source_dir> <output_dir>'); | ||
| console.log('usage: tinyjam <source_dir> <output_dir>'); | ||
| } else { | ||
| console.log(''); | ||
| const start = performance.now(); | ||
| const src = process.argv[2]; | ||
| const out = process.argv[3]; | ||
| tinyjam(src, out); | ||
| console.time('tinyjam'); | ||
| tinyjam(src, out); | ||
| console.timeEnd('tinyjam'); | ||
| const elapsed = performance.now() - start; | ||
| console.log(`\nDone in ${elapsed.toLocaleString()}ms.`); | ||
| } |
+25
-26
@@ -14,5 +14,5 @@ 'use strict'; | ||
| function tinyjam(src, dest, options = {}) { | ||
| fs.mkdirSync(dest, {recursive: true}); | ||
| fs.mkdirSync(dest, {recursive: true}); // make sure destination exists | ||
| const markedOptions = { | ||
| const markedOptions = { // Markdown renderer options | ||
| breaks: options.breaks, | ||
@@ -23,9 +23,10 @@ smartypants: options.smartypants | ||
| const proto = {}; | ||
| const root = createCtx('.'); | ||
| proto.root = root; | ||
| const root = createCtx('.'); // root data object | ||
| proto.root = root; // add data root access to all leaf nodes | ||
| const templates = []; | ||
| walk(src, root); | ||
| walk(src, root); // process files, collect data and templates to render | ||
| // render templates; we do it later to make sure all data is collected first | ||
| for (const {template, data, dir, name, ext, isCollection} of templates) { | ||
@@ -43,8 +44,10 @@ if (isCollection) { | ||
| const path = join(dir, name) + ext; | ||
| console.log(`rendering \t${path}`); | ||
| const out = template(data); | ||
| fs.writeFileSync(join(dest, path), out); | ||
| console.log(`render ${path}`); | ||
| fs.writeFileSync(join(dest, path), template(data)); | ||
| } | ||
| // create an object to be used as evalulation data in a template | ||
| function createCtx(rootPath, properties) { | ||
| // prototype magic to make sure all data objects have access to root/rootPath | ||
| // in templates and includes, and without them being returned in Object.keys | ||
| const ctxProto = Object.create(proto); | ||
@@ -57,2 +60,3 @@ ctxProto.rootPath = rootPath; | ||
| // recursively walk through and process files inside the source directory | ||
| function walk(dir, data) { | ||
@@ -71,3 +75,3 @@ const files = fs.readdirSync(dir); | ||
| if (file[0] === '.' || file === 'node_modules' || ext === '.lock' || name.endsWith('-lock')) { | ||
| console.log(`skipping \t${shortPath}`); | ||
| console.log(`skip ${shortPath}`); | ||
| continue; | ||
@@ -86,3 +90,3 @@ } | ||
| if (ext === '.md') { | ||
| console.log(`reading \t${shortPath} (markdown)`); | ||
| console.log(`read ${shortPath}`); | ||
| const {attributes, body} = fm(fs.readFileSync(path, 'utf8')); | ||
@@ -96,27 +100,22 @@ | ||
| } else if (ext === '.yml' || ext === '.yaml') { | ||
| console.log(`reading \t${shortPath} (yaml)`); | ||
| console.log(`read ${shortPath}`); | ||
| data[name] = createCtx(rootPath, yaml.safeLoad(fs.readFileSync(path, 'utf8'))); | ||
| } else if (ext === '.ejs') { | ||
| if (name[0] === '_') { | ||
| console.log(`skipping \t${shortPath} (include)`); | ||
| continue; // skip includes | ||
| if (name[0] === '_') { // skip includes | ||
| console.log(`skip ${shortPath}`); | ||
| continue; | ||
| } | ||
| console.log(`compiling \t${shortPath} (template)`); | ||
| const outExt = extname(name) ? '' : '.html'; | ||
| const src = fs.readFileSync(path, 'utf8'); | ||
| const template = ejs.compile(src, {filename: path}); | ||
| const isCollection = name === 'item'; | ||
| console.log(`compile ${shortPath}`); | ||
| templates.push({ | ||
| template, | ||
| data, | ||
| name, | ||
| template: ejs.compile(fs.readFileSync(path, 'utf8'), {filename: path}), | ||
| isCollection: name === 'item', | ||
| dir: dirname(shortPath), | ||
| name, | ||
| ext: outExt, | ||
| data, | ||
| isCollection | ||
| ext: extname(name) ? '' : '.html' | ||
| }); | ||
| } else { | ||
| console.log(`copying \t${shortPath} (static)`); | ||
| console.log(`copy ${shortPath}`); | ||
| fs.copyFileSync(path, join(dest, shortPath)); | ||
@@ -123,0 +122,0 @@ } |
+1
-1
@@ -8,3 +8,3 @@ { | ||
| }, | ||
| "version": "0.0.1", | ||
| "version": "0.0.2", | ||
| "author": "Vladimir Agafonkin", | ||
@@ -11,0 +11,0 @@ "license": "ISC", |
Debug access
Supply chain riskUses debug, reflection and dynamic code execution features.
Found 1 instance in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
11395
4.68%112
3.7%3
50%