@gregorlohaus/tdir 0.1.6 → 0.1.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/cli.js CHANGED
@@ -199,6 +199,16 @@ function writeSkippedTemplate(templateRoot, skipped) {
199
199
  writeFileSync(templatePath, content);
200
200
  return 1;
201
201
  }
202
+ function replaceFlatTokens(content, manifest) {
203
+ const entries = Object.entries(manifest.tokens).filter(([, tokens]) => tokens.length > 0).sort(([a], [b]) => b.length - a.length);
204
+ let result = content;
205
+ for (const [rendered, tokens] of entries) {
206
+ if (rendered === "")
207
+ continue;
208
+ result = result.split(rendered).join(tokens[0]);
209
+ }
210
+ return result;
211
+ }
202
212
  function dirnamePath(path) {
203
213
  const normalized = normalizePath(path);
204
214
  const index = normalized.lastIndexOf("/");
@@ -271,13 +281,18 @@ function walkFiles(root, excludedRoots = []) {
271
281
  }
272
282
  return files;
273
283
  }
274
- function copyIncludedRenderedFiles(renderedRoot, templateRoot, includedOutputPaths, directoryMap) {
284
+ function copyIncludedRenderedFiles(renderedRoot, templateRoot, includedOutputPaths, directoryMap, manifest) {
275
285
  let filesWritten = 0;
276
286
  for (const outputPath of includedOutputPaths) {
277
287
  const renderedPath = resolveInside(renderedRoot, outputPath);
278
288
  const templatePath = resolveInside(templateRoot, inferTemplatePath(outputPath, directoryMap));
279
289
  mkdirSync(dirname(templatePath), { recursive: true });
280
- copyFileSync(renderedPath, templatePath);
290
+ const content = readFileSync(renderedPath);
291
+ if (isUtf8Text(content)) {
292
+ writeFileSync(templatePath, replaceFlatTokens(content.toString("utf-8"), manifest));
293
+ } else {
294
+ copyFileSync(renderedPath, templatePath);
295
+ }
281
296
  filesWritten += 1;
282
297
  }
283
298
  return filesWritten;
@@ -333,7 +348,7 @@ function reverseDir(renderedDir, templateDir, options = {}) {
333
348
  for (const skipped of manifest.skipped ?? []) {
334
349
  filesWritten += writeSkippedTemplate(templateRoot, skipped);
335
350
  }
336
- filesWritten += copyIncludedRenderedFiles(renderedRoot, templateRoot, includedOutputPaths, directoryMap);
351
+ filesWritten += copyIncludedRenderedFiles(renderedRoot, templateRoot, includedOutputPaths, directoryMap, manifest);
337
352
  return { filesWritten, warnings };
338
353
  }
339
354
 
package/dist/index.js CHANGED
@@ -186,6 +186,14 @@ function addReverseMapToken(state, file, token) {
186
186
  tokens.push(token.token);
187
187
  state.manifest.tokens[token.result] = tokens;
188
188
  }
189
+ function addFlatToken(state, result, token) {
190
+ if (!state?.manifest)
191
+ return;
192
+ const tokens = state.manifest.tokens[result] ?? [];
193
+ if (!tokens.includes(token))
194
+ tokens.push(token);
195
+ state.manifest.tokens[result] = tokens;
196
+ }
189
197
  function getReverseMapPath(destRoot, reverseMap) {
190
198
  if (reverseMap === true)
191
199
  return resolveOutputPath(destRoot, ".tdir-map.json");
@@ -341,6 +349,11 @@ function processIfBlocksWithMap(content, context) {
341
349
  };
342
350
  }
343
351
  function renderContentWithMap(content, context, state, file) {
352
+ for (const match of content.matchAll(VAR_RE2)) {
353
+ const token = match[0];
354
+ const path = match[1];
355
+ addFlatToken(state, String(resolveContext(context, path) ?? ""), token);
356
+ }
344
357
  const processedResult = processIfBlocksWithMap(content, context);
345
358
  const processed = processedResult.content;
346
359
  for (const token of processedResult.conditionalTokens) {
@@ -685,6 +698,16 @@ function writeSkippedTemplate(templateRoot, skipped) {
685
698
  writeFileSync2(templatePath, content);
686
699
  return 1;
687
700
  }
701
+ function replaceFlatTokens(content, manifest) {
702
+ const entries = Object.entries(manifest.tokens).filter(([, tokens]) => tokens.length > 0).sort(([a], [b]) => b.length - a.length);
703
+ let result = content;
704
+ for (const [rendered, tokens] of entries) {
705
+ if (rendered === "")
706
+ continue;
707
+ result = result.split(rendered).join(tokens[0]);
708
+ }
709
+ return result;
710
+ }
688
711
  function dirnamePath(path) {
689
712
  const normalized = normalizePath(path);
690
713
  const index = normalized.lastIndexOf("/");
@@ -757,13 +780,18 @@ function walkFiles(root, excludedRoots = []) {
757
780
  }
758
781
  return files;
759
782
  }
760
- function copyIncludedRenderedFiles(renderedRoot, templateRoot, includedOutputPaths, directoryMap) {
783
+ function copyIncludedRenderedFiles(renderedRoot, templateRoot, includedOutputPaths, directoryMap, manifest) {
761
784
  let filesWritten = 0;
762
785
  for (const outputPath of includedOutputPaths) {
763
786
  const renderedPath = resolveInside(renderedRoot, outputPath);
764
787
  const templatePath = resolveInside(templateRoot, inferTemplatePath(outputPath, directoryMap));
765
788
  mkdirSync2(dirname2(templatePath), { recursive: true });
766
- copyFileSync2(renderedPath, templatePath);
789
+ const content = readFileSync3(renderedPath);
790
+ if (isUtf8Text3(content)) {
791
+ writeFileSync2(templatePath, replaceFlatTokens(content.toString("utf-8"), manifest));
792
+ } else {
793
+ copyFileSync2(renderedPath, templatePath);
794
+ }
767
795
  filesWritten += 1;
768
796
  }
769
797
  return filesWritten;
@@ -819,7 +847,7 @@ function reverseDir(renderedDir, templateDir, options = {}) {
819
847
  for (const skipped of manifest.skipped ?? []) {
820
848
  filesWritten += writeSkippedTemplate(templateRoot, skipped);
821
849
  }
822
- filesWritten += copyIncludedRenderedFiles(renderedRoot, templateRoot, includedOutputPaths, directoryMap);
850
+ filesWritten += copyIncludedRenderedFiles(renderedRoot, templateRoot, includedOutputPaths, directoryMap, manifest);
823
851
  return { filesWritten, warnings };
824
852
  }
825
853
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gregorlohaus/tdir",
3
- "version": "0.1.6",
3
+ "version": "0.1.7",
4
4
  "license": "MIT",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",