1 | "use strict"
|
2 | const fs = require('fs-extra')
|
3 | const path = require('path')
|
4 | const merge = require('deepmerge')
|
5 | const transforms = require('./transforms')
|
6 | const regexUtils = require('./utils/regex')
|
7 | const pluginSortOrder = require('./utils/sortOrder')
|
8 | const updateContents = require('./updateContents')
|
9 | const cwd = process.cwd()
|
10 |
|
11 | module.exports = function processFile(filePath, config) {
|
12 | let content
|
13 | try {
|
14 | content = fs.readFileSync(filePath, 'utf8')
|
15 | } catch (e) {
|
16 | console.log(`FILE NOT FOUND ${filePath}`)
|
17 | throw e
|
18 | }
|
19 | |
20 |
|
21 |
|
22 | const defaultConfig = {
|
23 | |
24 |
|
25 |
|
26 |
|
27 | transforms: transforms,
|
28 | |
29 |
|
30 |
|
31 |
|
32 | outputDir: path.dirname(filePath),
|
33 | |
34 |
|
35 |
|
36 |
|
37 |
|
38 | matchWord: 'AUTO-GENERATED-CONTENT',
|
39 | |
40 |
|
41 |
|
42 |
|
43 | DEBUG: false,
|
44 | }
|
45 |
|
46 | const userConfig = (config && typeof config === 'object') ? config : {}
|
47 | const mergedConfig = merge(defaultConfig, userConfig)
|
48 |
|
49 | const registeredTransforms = Object.keys(mergedConfig.transforms)
|
50 |
|
51 | mergedConfig.originalPath = filePath
|
52 |
|
53 | mergedConfig.originalContent = content
|
54 |
|
55 | mergedConfig.outputContent = content
|
56 |
|
57 | const regex = regexUtils.matchCommentBlock(mergedConfig.matchWord)
|
58 | const match = content.match(regex)
|
59 | const transformsFound = []
|
60 |
|
61 | if (match) {
|
62 | let commentMatches
|
63 | let matchIndex = 0
|
64 | while ((commentMatches = regex.exec(content)) !== null) {
|
65 | if (commentMatches.index === regex.lastIndex) {
|
66 | regex.lastIndex++
|
67 | }
|
68 | const command = `Transform ${commentMatches[1]}`
|
69 |
|
70 | transformsFound.push({
|
71 | spaces: commentMatches[1],
|
72 | transform: commentMatches[2],
|
73 | match: match[matchIndex]
|
74 | })
|
75 |
|
76 | matchIndex++
|
77 | }
|
78 |
|
79 | const transformsToRun = pluginSortOrder(registeredTransforms, transformsFound)
|
80 | if (mergedConfig.DEBUG) {
|
81 | console.log('↓ transformsToRun')
|
82 | console.log(transformsToRun)
|
83 | }
|
84 |
|
85 |
|
86 | let transformMsg = ''
|
87 | transformsToRun.forEach((element) => {
|
88 | transformMsg += ` ⁕ ${element.transform} \n`
|
89 |
|
90 | const newContent = updateContents(element.match, mergedConfig)
|
91 |
|
92 | const firstLineIndentation = element.spaces
|
93 | const contentWithIndentation = newContent.split('\n').join(`\n` + element.spaces)
|
94 | const preserveTabs = `${firstLineIndentation}${contentWithIndentation}`
|
95 |
|
96 | content = content.replace(element.match, preserveTabs)
|
97 | mergedConfig.outputContent = content
|
98 | })
|
99 |
|
100 |
|
101 | const fileName = path.basename(filePath)
|
102 | const outputFilePath = path.join(mergedConfig.outputDir, fileName)
|
103 |
|
104 |
|
105 | fs.ensureFileSync(outputFilePath)
|
106 |
|
107 | fs.writeFileSync(outputFilePath, content)
|
108 |
|
109 | const msg = outputFilePath.replace(cwd, '')
|
110 |
|
111 | console.log(`✔ ${msg} Updated`)
|
112 | console.log(` Transforms run`)
|
113 | console.log(transformMsg)
|
114 |
|
115 |
|
116 | mergedConfig.outputFilePath = outputFilePath
|
117 | mergedConfig.outputContent = content
|
118 |
|
119 | return mergedConfig
|
120 | }
|
121 |
|
122 | if (mergedConfig.DEBUG) {
|
123 | console.log(`↓ ${filePath}`)
|
124 | console.log(`[No match] <!-- ${mergedConfig.matchWord} --> comment found`)
|
125 | }
|
126 |
|
127 |
|
128 | return mergedConfig
|
129 | }
|