Skip to content

Commit 13f1d2a

Browse files
authored
feat(tools): process HBS files in nested directories (#2067)
Now the the tools will process HBS files within nested directories and produce the output "lit" files into the "dist", maintaining the file structures. FIXES #2065
1 parent edfe8ca commit 13f1d2a

File tree

3 files changed

+29
-5
lines changed

3 files changed

+29
-5
lines changed

packages/tools/lib/hbs2ui5/index.js

+21-5
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ const getopts = require('getopts');
33
const hbs2lit = require('../hbs2lit');
44
const path = require('path');
55
const litRenderer = require('./RenderTemplates/LitRenderer');
6+
const recursiveReadDir = require("recursive-readdir");
7+
const mkdirp = require('mkdirp');
68

79
const args = getopts(process.argv.slice(2), {
810
alias: {
@@ -23,34 +25,48 @@ const onError = (place) => {
2325
const isHandlebars = (fileName) => fileName.indexOf('.hbs') !== -1;
2426

2527
const processFile = (file, outputDir) => {
26-
2728
const litCode = hbs2lit(file);
28-
29+
const absoluteOutputDir = composeAbsoluteOutputDir(file, outputDir);
2930
const componentNameMatcher = /(\w+)(\.hbs)/gim;
3031
const componentName = componentNameMatcher.exec(file)[1];
31-
writeRenderers(outputDir, componentName, litRenderer.generateTemplate(componentName, litCode));
32+
33+
writeRenderers(absoluteOutputDir, componentName, litRenderer.generateTemplate(componentName, litCode));
34+
};
35+
36+
const composeAbsoluteOutputDir = (file, outputDir) => {
37+
// (1) Extract the dir structure from the source file path - "src/lvl1/lvl2/MyCompBadge.hbs"
38+
// - remove the filename - "src/lvl1/lvl2"
39+
// - remove the leading dir - "lvl1/lvl2"
40+
const fileDir = file.split(path.sep).slice(1, -1).join(path.sep);
41+
42+
// (2) Compose full output dir - "dist/generated/templates/lvl1/lvl2"
43+
return `${outputDir}${path.sep}${fileDir}`;
3244
};
3345

3446
const wrapDirectory = (directory, outputDir) => {
3547
directory = path.normalize(directory);
3648
outputDir = path.normalize(outputDir);
3749

38-
fs.readdir(directory, (err, files) => {
50+
recursiveReadDir(directory, (err, files) => {
3951

4052
if (err) {
4153
onError('directory');
4254
}
4355

4456
files.forEach(fileName => {
4557
if (isHandlebars(fileName)) {
46-
processFile(path.join(directory, fileName), outputDir);
58+
processFile(fileName, outputDir);
4759
}
4860
});
4961
})
5062
};
5163

5264
const writeRenderers = (outputDir, controlName, fileContent) => {
5365
try {
66+
if (!fs.existsSync(outputDir)) {
67+
mkdirp.sync(outputDir);
68+
}
69+
5470
const compiledFilePath = `${outputDir}${path.sep}${controlName}Template.lit.js`;
5571

5672
// strip DOS line endings because the break the source maps

packages/tools/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
"postcss-import": "^12.0.1",
6060
"properties-reader": "^0.3.1",
6161
"puppeteer": "^1.11.0",
62+
"recursive-readdir": "^2.2.2",
6263
"rimraf": "^2.6.2",
6364
"rollup": "^1.1.2",
6465
"rollup-plugin-babel": "^4.0.3",

yarn.lock

+7
Original file line numberDiff line numberDiff line change
@@ -7370,6 +7370,13 @@ readline-sync@^1.4.7:
73707370
resolved "https://registry.yarnpkg.com/readline-sync/-/readline-sync-1.4.10.tgz#41df7fbb4b6312d673011594145705bf56d8873b"
73717371
integrity sha512-gNva8/6UAe8QYepIQH/jQ2qn91Qj0B9sYjMBBs3QOB8F2CXcKgLxQaJRP76sWVRQt+QU+8fAkCbCvjjMFu7Ycw==
73727372

7373+
recursive-readdir@^2.2.2:
7374+
version "2.2.2"
7375+
resolved "https://registry.npmjs.org/recursive-readdir/-/recursive-readdir-2.2.2.tgz#9946fb3274e1628de6e36b2f6714953b4845094f"
7376+
integrity sha512-nRCcW9Sj7NuZwa2XvH9co8NPeXUBhZP7CRKJtU+cS6PW9FpCIFoI5ib0NT1ZrbNuPoRy0ylyCaUL8Gih4LSyFg==
7377+
dependencies:
7378+
minimatch "3.0.4"
7379+
73737380
redent@^1.0.0:
73747381
version "1.0.0"
73757382
resolved "https://registry.yarnpkg.com/redent/-/redent-1.0.0.tgz#cf916ab1fd5f1f16dfb20822dd6ec7f730c2afde"

0 commit comments

Comments
 (0)