UNPKG

3.93 kBJavaScriptView Raw
1'use strict';
2/*
3 Update contents between Comment tags
4*/
5const regexUtils = require('./utils/regex')
6
7module.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 // no transform command return original block
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 // check if command exists
24 if (cmd && config.transforms && config.transforms[cmd]) {
25 let updatedContent = await config.transforms[cmd](originalContent, cmdOptions, config)
26 if (typeof updatedContent === 'function') {
27 // if plugin has no options defined, invoke it with defaults
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 // throw new Error(errMsg)
39 }
40 }
41
42 // if no transform matches
43 if (!newContent) {
44 newContent = originalContent
45 }
46
47 return `${openingTag.openTag}
48${newContent}
49${closingTag.closeTag}`
50}
51
52function parseOptions(options) {
53 if (!options) {
54 return null
55 }
56 const returnOptions = {}
57 options.split('&').map((opt, i) => { // eslint-disable-line
58 const getValues = opt.split(/=(.+)/)
59 if (getValues[0] && getValues[1]) {
60 returnOptions[getValues[0]] = getValues[1]
61 }
62 })
63 return returnOptions
64}
65
66function processTransforms(hasCommand) {
67 const hasOptions = hasCommand[1].match(/([^:]*):(.*)/)
68 const cmd = (hasOptions) ? hasOptions[1] : hasCommand[1]
69 // no options found, run command with no options
70 const cmdOptions = (hasOptions) ? hasOptions[2] : null
71 return {
72 cmd: cmd,
73 cmdOptions: parseOptions(cmdOptions)
74 }
75}
76
77function getOpeningTags(block, config) {
78 const openTagRegex = regexUtils.matchOpeningCommentTag(config.matchWord)
79 let matches
80 while ((matches = openTagRegex.exec(block)) !== null) { // eslint-disable-line
81 // This is necessary to avoid infinite loops with zero-width matches
82 if (matches.index === openTagRegex.lastIndex) {
83 openTagRegex.lastIndex++
84 }
85 /*
86 console.log('FULL Open Tag >>>>>', matches[0])
87 console.log('openTag Start', "'"+matches[1]+"'");
88 console.log('openTag End', "'"+matches[2]+"'");
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
101function getClosingTags(block, config) {
102 const closeTagRegex = regexUtils.matchClosingCommentTag(config.matchWord)
103 let matches
104 while ((matches = closeTagRegex.exec(block)) !== null) { // eslint-disable-line
105 // This is necessary to avoid infinite loops with zero-width matches
106 if (matches.index === closeTagRegex.lastIndex) {
107 closeTagRegex.lastIndex++
108 }
109 /*
110 console.log('FULL CLOSE Tag >>>>>', matches[0])
111 console.log('closeTag Start', "'"+matches[1]+"'");
112 console.log('closeTag End', "'"+matches[2]+"'");
113 /**/
114 return {
115 closeTag: matches[1] + matches[2],
116 closeTagStart: matches[1],
117 closeTagEnd: matches[2]
118 }
119 }
120}