-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcopyEditorScss.js
68 lines (56 loc) · 1.79 KB
/
copyEditorScss.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
import fs from 'fs';
import path from 'path';
import { FILES_PATHS } from '../CONFIG.js';
import isInThemeOrPluginRoot from './isInThemeOrPluginRoot.js';
const projectRootDir = process.cwd();
function copyEditorScss(oldBlockName, newBlockName) {
const projType = isInThemeOrPluginRoot();
const _new_editor_scss_path = path.join(
projectRootDir,
FILES_PATHS.blocksDirPath[projType],
newBlockName,
'_editor.scss'
);
const _editor_scss_exists = fs.existsSync(_new_editor_scss_path);
if (!_editor_scss_exists) {
console.log(
chalk.yellow(
`\nIt appears that the _editor.scss file is missing. If you need it, you should create and import it manually.\n`
)
);
} else {
// Continue here
updateEditorScss(_new_editor_scss_path, oldBlockName, newBlockName);
}
}
function updateEditorScss(editorScssPath, oldBlockName, newBlockName) {
if (oldBlockName === 'new-block-blueprint') {
oldBlockName = 'BLOCK_NAME_HERE';
}
let _editor_scss_content = fs
.readFileSync(editorScssPath)
.toString()
.split('\n');
// If file has content
if (_editor_scss_content.length) {
// TODO: Replace sidebar selectors as well
_editor_scss_content = _editor_scss_content.map((line) => {
if (line.includes(`.tr-block-wrap--${oldBlockName}`)) {
line = line.replace(oldBlockName, newBlockName);
}
return line;
});
}
// Convert to string again
_editor_scss_content = _editor_scss_content.join('\n');
// Prepare for writing
_editor_scss_content = new Uint8Array(Buffer.from(_editor_scss_content));
try {
fs.writeFileSync(editorScssPath, _editor_scss_content, 'utf8');
} catch (err) {
console.log(chalk.red(err));
console.log('\n');
}
}
export default copyEditorScss;
export { updateEditorScss };