Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 21 additions & 35 deletions lib/compile.js
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove async and promises and just get the paths synchronously like it did before.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Previous version was not synchronous. Walk emitted events and the main compile export function wouldn't really wait for it.
What's more, even if I make my changes use readdirSync + the main compile func statSync it's still not synchronous due to twig() constructor not calling async: false.

I've made it full sync now by combining the above.

PS. It looks to me like the recursive flags from the options (defaults to false) was and still is a no-op. We could technically now make it work as expected with readdirSync params.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good points, I didn't notice that before. Thank you for getting it to work synchronously all the same. It makes it easier to work with in its current state. The package is a weird mix of async and sync that needs to get converted to all async, but until then this is a big help.

Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
const FS = require('fs');
const minimatch = require('minimatch');
const WALK = require('walk');
const {matchesGlob} = require('node:path');
const Twig = require('..');
const PATHS = require('./paths');

const {twig} = Twig;

exports.defaults = {
compress: false,
pattern: '*\\.twig',
recursive: false
pattern: '*.twig',
};

exports.compile = function (options, files) {
Expand All @@ -19,48 +17,35 @@ exports.compile = function (options, files) {
}

files.forEach(file => {
FS.stat(file, (err, stats) => {
if (err) {
console.error('ERROR ' + file + ': Unable to stat file');
return;
}
const stats = FS.statSync(file);

if (stats.isDirectory()) {
parseTemplateFolder(file, options.pattern);
} else if (stats.isFile()) {
parseTemplateFile(file);
} else {
console.log('ERROR ' + file + ': Unknown file information');
}
});
if (stats.isDirectory()) {
parseTemplateFolder(file, options.pattern);
} else if (stats.isFile()) {
parseTemplateFile(file);
} else {
console.log('ERROR ' + file + ': Unknown file information');
}
});

function parseTemplateFolder(directory, pattern) {
directory = PATHS.stripSlash(directory);

// Get the files in the directory
// Walker options
const walker = WALK.walk(directory, {followLinks: false});
const files = [];
const entries = FS.readdirSync(directory, {recursive: true, withFileTypes: true});

walker.on('file', (root, stat, next) => {
// Normalize (remove / from end if present)
root = PATHS.stripSlash(root);
for (const entry of entries) {
if (!entry.isFile()) {
continue;
}

// Match against file pattern
const {name} = stat;
const file = root + '/' + name;
if (minimatch(name, pattern)) {
const {name} = entry;
if (matchesGlob(name, pattern)) {
const root = PATHS.stripSlash(entry.parentPath || entry.path);
const file = root + '/' + name;
parseTemplateFile(file, directory);
files.push(file);
}

next();
});

walker.on('end', () => {
// Console.log(files);
});
}
}

function parseTemplateFile(file, base) {
Expand Down Expand Up @@ -93,6 +78,7 @@ exports.compile = function (options, files) {
twig({
id: outputId,
path: file,
async: false,
load(template) {
// Compile!
const output = template.compile(options);
Expand Down
22 changes: 4 additions & 18 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 1 addition & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,7 @@
},
"dependencies": {
"@babel/runtime": "^7.8.4",
"locutus": "^3.0.9",
"minimatch": "^10",
"walk": "2.3.x"
"locutus": "^3.0.9"
},
"devDependencies": {
"@babel/core": "^7.8.4",
Expand Down
Loading