|
6 | 6 | * found in the LICENSE file at https://angular.io/license
|
7 | 7 | */
|
8 | 8 | import { BaseException, normalize, template as templateImpl } from '@angular-devkit/core';
|
| 9 | +import { TextDecoder } from 'util'; |
9 | 10 | import { FileOperator, Rule } from '../engine/interface';
|
10 | 11 | import { FileEntry } from '../tree/interface';
|
11 | 12 | import { chain, composeFileOperators, forEach, when } from './base';
|
12 | 13 | import { rename } from './rename';
|
13 |
| -import { isBinary } from './utils/is-binary'; |
14 | 14 |
|
15 | 15 |
|
16 | 16 | export const TEMPLATE_FILENAME_RE = /\.template$/;
|
@@ -48,18 +48,26 @@ export interface PathTemplateOptions {
|
48 | 48 | pipeSeparator?: string;
|
49 | 49 | }
|
50 | 50 |
|
| 51 | +const decoder = new TextDecoder('utf-8', { fatal: true }); |
51 | 52 |
|
52 | 53 | export function applyContentTemplate<T>(options: T): FileOperator {
|
53 | 54 | return (entry: FileEntry) => {
|
54 |
| - const {path, content} = entry; |
55 |
| - if (isBinary(content)) { |
56 |
| - return entry; |
57 |
| - } |
| 55 | + const { path, content } = entry; |
| 56 | + |
| 57 | + try { |
| 58 | + const decodedContent = decoder.decode(content); |
| 59 | + |
| 60 | + return { |
| 61 | + path, |
| 62 | + content: Buffer.from(templateImpl(decodedContent, {})(options)), |
| 63 | + }; |
| 64 | + } catch (e) { |
| 65 | + if (e.code === 'ERR_ENCODING_INVALID_ENCODED_DATA') { |
| 66 | + return entry; |
| 67 | + } |
58 | 68 |
|
59 |
| - return { |
60 |
| - path: path, |
61 |
| - content: Buffer.from(templateImpl(content.toString('utf-8'), {})(options)), |
62 |
| - }; |
| 69 | + throw e; |
| 70 | + } |
63 | 71 | };
|
64 | 72 | }
|
65 | 73 |
|
|
0 commit comments