1 | 'use strict';
|
2 |
|
3 |
|
4 |
|
5 | const regexUtils = require('./utils/regex')
|
6 |
|
7 | module.exports = async function updateContents(block, config) {
|
8 | let newContent
|
9 | const openingTag = getOpeningTags(block, config)
|
10 | const closingTag = getClosingTags(block, config)
|
11 |
|
12 | if (!openingTag.transform) {
|
13 |
|
14 | return block
|
15 | }
|
16 | const contentStart = openingTag.openTag.length
|
17 | const endStart = block.indexOf(closingTag.closeTag, openingTag.openTag.length)
|
18 | const originalContent = block.slice(contentStart, endStart).replace(/^\s+|\s+$/g, '')
|
19 |
|
20 | if (openingTag.transform) {
|
21 | const cmd = openingTag.transform.cmd
|
22 | const cmdOptions = openingTag.transform.cmdOptions || {}
|
23 |
|
24 | if (cmd && config.transforms && config.transforms[cmd]) {
|
25 | let updatedContent = await config.transforms[cmd](originalContent, cmdOptions, config)
|
26 | if (typeof updatedContent === 'function') {
|
27 |
|
28 | updatedContent = await updatedContent(originalContent, cmdOptions, config)
|
29 | }
|
30 | newContent = updatedContent
|
31 | if (!newContent) {
|
32 | console.log(`COMMAND '${cmd}' is returning undefined value. using original content instead. Make sure you return a value from your transform`)
|
33 | }
|
34 | }
|
35 | if (!config.transforms[cmd]) {
|
36 | console.warn(`Error '${cmd}' transform function not found in \`config.transforms\``)
|
37 | console.warn(`Comment block skipped: <!-- ${config.matchWord}:START (${cmd}) -->`)
|
38 |
|
39 | }
|
40 | }
|
41 |
|
42 |
|
43 | if (!newContent) {
|
44 | newContent = originalContent
|
45 | }
|
46 |
|
47 | return `${openingTag.openTag}
|
48 | ${newContent}
|
49 | ${closingTag.closeTag}`
|
50 | }
|
51 |
|
52 | function parseOptions(options) {
|
53 | if (!options) {
|
54 | return null
|
55 | }
|
56 | const returnOptions = {}
|
57 | options.split('&').map((opt, i) => {
|
58 | const getValues = opt.split(/=(.+)/)
|
59 | if (getValues[0] && getValues[1]) {
|
60 | returnOptions[getValues[0]] = getValues[1]
|
61 | }
|
62 | })
|
63 | return returnOptions
|
64 | }
|
65 |
|
66 | function processTransforms(hasCommand) {
|
67 | const hasOptions = hasCommand[1].match(/([^:]*):(.*)/)
|
68 | const cmd = (hasOptions) ? hasOptions[1] : hasCommand[1]
|
69 |
|
70 | const cmdOptions = (hasOptions) ? hasOptions[2] : null
|
71 | return {
|
72 | cmd: cmd,
|
73 | cmdOptions: parseOptions(cmdOptions)
|
74 | }
|
75 | }
|
76 |
|
77 | function getOpeningTags(block, config) {
|
78 | const openTagRegex = regexUtils.matchOpeningCommentTag(config.matchWord)
|
79 | let matches
|
80 | while ((matches = openTagRegex.exec(block)) !== null) {
|
81 |
|
82 | if (matches.index === openTagRegex.lastIndex) {
|
83 | openTagRegex.lastIndex++
|
84 | }
|
85 | |
86 |
|
87 |
|
88 |
|
89 |
|
90 | const hasCommand = matches[0].match(/\((.*)\)/)
|
91 | const cmd = (hasCommand) ? processTransforms(hasCommand) : false
|
92 | return {
|
93 | openTag: matches[0],
|
94 | openTagStart: matches[1],
|
95 | openTagEnd: matches[2],
|
96 | transform: cmd
|
97 | }
|
98 | }
|
99 | }
|
100 |
|
101 | function getClosingTags(block, config) {
|
102 | const closeTagRegex = regexUtils.matchClosingCommentTag(config.matchWord)
|
103 | let matches
|
104 | while ((matches = closeTagRegex.exec(block)) !== null) {
|
105 |
|
106 | if (matches.index === closeTagRegex.lastIndex) {
|
107 | closeTagRegex.lastIndex++
|
108 | }
|
109 | |
110 |
|
111 |
|
112 |
|
113 |
|
114 | return {
|
115 | closeTag: matches[1] + matches[2],
|
116 | closeTagStart: matches[1],
|
117 | closeTagEnd: matches[2]
|
118 | }
|
119 | }
|
120 | }
|