1 | /*
|
2 | * Copyright 2022 The Backstage Authors
|
3 | *
|
4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | * you may not use this file except in compliance with the License.
|
6 | * You may obtain a copy of the License at
|
7 | *
|
8 | * http://www.apache.org/licenses/LICENSE-2.0
|
9 | *
|
10 | * Unless required by applicable law or agreed to in writing, software
|
11 | * distributed under the License is distributed on an "AS IS" BASIS,
|
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
13 | * See the License for the specific language governing permissions and
|
14 | * limitations under the License.
|
15 | */
|
16 |
|
17 | const yaml = require('yaml');
|
18 | const crypto = require('crypto');
|
19 |
|
20 | function createTransformer(config) {
|
21 | const process = source => {
|
22 | const json = JSON.stringify(yaml.parse(source), null, 2);
|
23 | return { code: `module.exports = ${json}`, map: null };
|
24 | };
|
25 |
|
26 | const getCacheKey = sourceText => {
|
27 | return crypto
|
28 | .createHash('md5')
|
29 | .update(sourceText)
|
30 | .update(Buffer.alloc(1))
|
31 | .update(JSON.stringify(config))
|
32 | .update(Buffer.alloc(1))
|
33 | .update('1') // increment whenever the transform logic in this file changes
|
34 | .digest('hex');
|
35 | };
|
36 |
|
37 | return { process, getCacheKey };
|
38 | }
|
39 |
|
40 | module.exports = { createTransformer };
|