-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
191 lines (170 loc) · 6.04 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#!/usr/bin/env node
import inquirer from 'inquirer'
import { readFile, writeFile, unlink, mkdir, readdir } from 'fs/promises'
import { access, constants, existsSync } from 'fs'
import { pathToFileURL } from 'url'
import chalk from 'chalk'
const questions = [
{
name: 'rootPath',
type: 'input',
message: 'Project root path'
},
{
name: 'childThemePath',
type: 'input',
message: 'Child theme path'
}
]
const { rootPath, childThemePath } = await inquirer.prompt(questions)
const alpacaThemePath = `${rootPath}/vendor/snowdog/theme-frontend-alpaca`
const customVariablesFilePath = 'Snowdog_Components/components/Atoms/variables'
let customVariablesFile = await readdir(`${childThemePath}/${customVariablesFilePath}`)
customVariablesFile = customVariablesFile
.toString()
.replace('_', '')
.replace('.scss', '')
const todoComment = '// TODO: check for missing custom styles and remove unused styles if needed'
async function updateComponentStyles () {
const atomicDirectories = [
'Atoms',
'Molecules',
'Organisms',
'Templates'
]
const oldFiles = []
atomicDirectories.map((dir, index) => (oldFiles[index] = `${childThemePath}/Snowdog_Components/components/${dir}/_components.scss`))
const newDirectory = 'Snowdog_Components/components/styles'
const extendedStyles = []
const regex = /@import '([\w-_/]*)-extend'/g
const filePathIndex = ('@import \'').length
for (const [index, file] of oldFiles.entries()) {
access(
file,
constants.F_OK,
async (err) => {
if (!err) {
try {
const content = await readFile(pathToFileURL(file), 'utf-8')
const directoryExtendedStyles = content.match(regex)
for (const str of directoryExtendedStyles) {
extendedStyles.push(
[str.slice(0, filePathIndex), `../${atomicDirectories[index]}/`, str.slice(filePathIndex)]
.join('')
.replace('-extend\'', '\'')
)
}
await unlink(file)
console.log(chalk.green(`- Removed ${file}`))
} catch (err) {
console.error(chalk.red(err))
process.exit(1)
}
}
}
)
}
const newFiles = [
'_critical.scss',
'_non-critical.scss',
'_checkout.scss',
'_critical-checkout.scss'
]
newFiles.forEach((file, index) => (newFiles[index] = `${newDirectory}/${file}`))
const newDirectoryFullPath = `${childThemePath}/${newDirectory}`
if (!existsSync(newDirectoryFullPath)) {
await mkdir(newDirectoryFullPath)
};
for (const file of newFiles) {
try {
let content = await readFile(pathToFileURL(`${alpacaThemePath}/${file}`), 'utf-8')
extendedStyles.forEach(style => {
content = content.replace(style, `${style.slice(0, -1)}-extend'`)
})
if (content.includes('extend')) {
content = content.replaceAll('-extend\';', '-extend\'; // Extend')
await writeFile(`${childThemePath}/${file}`, `${todoComment}\n\n${content}`)
console.log(chalk.green(`+ Added ${childThemePath}/${file}`))
}
} catch (err) {
console.error(chalk.red(err))
console.log(chalk.blue('Make sure you updated theme alpaca to >= 2.26.0.'))
process.exit(1)
}
}
}
async function updateComponentsBuildStyles () {
const files = [
'checkout.scss',
'styles.scss'
]
const filesDirectory = 'Snowdog_Components/docs/styles'
const alpacaFiles = files.map(file => `${alpacaThemePath}/${filesDirectory}/${file}`)
const childFiles = files.map(file => `${childThemePath}/${filesDirectory}/${file}`)
for (const [index] of files.entries()) {
try {
const customVariablesImport = `@import '../../components/Atoms/variables/${customVariablesFile}';`
let content = await readFile(pathToFileURL(alpacaFiles[index]), 'utf-8')
content = content.replace('// Variables', `// Variables\n${customVariablesImport}`)
await writeFile(childFiles[index], content)
console.log(chalk.green(`+ Updated ${childFiles[index]}`))
} catch (err) {
console.error(chalk.red(err))
}
}
}
async function updateCheckoutStyles () {
const file = 'Magento_Checkout/styles/checkout.scss'
try {
const customVariablesImport = `@import '../../${customVariablesFilePath}/${customVariablesFile}';`
let content = await readFile(pathToFileURL(`${alpacaThemePath}/${file}`), 'utf-8')
content = content.replace('// Component variables', `${todoComment}\n\n// Variables\n${customVariablesImport}`)
await writeFile(`${childThemePath}/${file}`, content)
console.log(chalk.green(`+ Updated ${childThemePath}/${file}`))
} catch (err) {
console.error(chalk.red(err))
}
}
async function updateStyles () {
const oldFiles = [
'_theme.scss',
'styles.scss'
]
oldFiles.forEach((file, index) => (oldFiles[index] = `${childThemePath}/styles/${file}`))
for (const file of oldFiles) {
if (existsSync(file)) {
try {
await unlink(file)
console.log(chalk.green(`- Removed ${file}`))
} catch (err) {
console.error(chalk.red(err))
}
}
}
const newFiles = [
'critical-checkout.scss',
'critical.scss',
'styles.scss'
]
const customVariablesImport = `@import '../${customVariablesFilePath}/${customVariablesFile}';`
for (const file of newFiles) {
try {
let content = await readFile(pathToFileURL(`${alpacaThemePath}/styles/${file}`), 'utf-8')
content = content.replace('// Variables', `${todoComment}\n\n// Variables\n${customVariablesImport}`)
await writeFile(`${childThemePath}/styles/${file}`, content)
console.log(chalk.green(`+ Added ${childThemePath}/styles/${file}`))
} catch (err) {
console.error(chalk.red(err))
process.exit(1)
}
}
}
await updateComponentStyles()
await updateComponentsBuildStyles()
await updateCheckoutStyles()
await updateStyles()
console.log(chalk.magenta.bold(`
Check your theme to see the results 🤞
* Some files should include a TODO that requires your action
* Please make sure to update Magento_Theme/templates/root.phtml
`))