-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathbabel-compile.js
More file actions
75 lines (60 loc) · 3.14 KB
/
babel-compile.js
File metadata and controls
75 lines (60 loc) · 3.14 KB
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
const path = require('path');
const fs = require('fs');
const { execSync } = require('child_process');
// Получаем путь к файлу из аргументов
const filePath = process.argv[2];
// Определяем путь к babel
const babelPath = path.join(__dirname, 'node_modules/.bin/babel');
// Обработка для Core файлов
if (filePath.includes('Core/sites/admin-cabinet/assets/js/src/')) {
// Находим индекс js/src в пути
const srcIndex = filePath.indexOf('js/src/');
// Получаем базовую часть пути (до js/src)
const basePath = filePath.substring(0, srcIndex);
// Получаем часть пути после js/src/
const relativePath = filePath.substring(srcIndex + 7); // 7 = длина 'js/src/'
// Создаем выходной путь в js/pbx с той же структурой подпапок
const relativeDirPath = path.dirname(relativePath);
const outputDir = path.join(basePath, 'js/pbx', relativeDirPath);
// Убедимся, что директория существует
if (!fs.existsSync(outputDir)) {
fs.mkdirSync(outputDir, { recursive: true });
console.log(`Создана директория: ${outputDir}`);
}
// Запускаем babel
try {
const cmd = `"${babelPath}" "${filePath}" --out-dir "${outputDir}" --source-maps inline --presets airbnb`;
console.log(`Выполняем команду: ${cmd}`);
execSync(cmd, { stdio: 'inherit' });
const fileNameWithoutExt = path.basename(filePath, '.js');
const outputFile = path.join(outputDir, `${fileNameWithoutExt}.js`);
console.log(`Успешно скомпилировано: ${filePath} -> ${outputFile}`);
} catch (error) {
console.error(`Ошибка компиляции: ${error.message}`);
process.exit(1);
}
}
// Обработка для Extensions
else if (filePath.includes('Extensions/') && filePath.includes('/public/assets/js/src/')) {
// Находим последний индекс /src/
const srcPos = filePath.lastIndexOf('/src/');
// Получаем директорию src
const srcDir = filePath.substring(0, srcPos + 5); // +5 чтобы включить '/src/'
// Создаем выходной путь (родительская директория src)
const outputDir = path.dirname(srcDir);
// Запускаем babel
try {
const cmd = `"${babelPath}" "${filePath}" --out-dir "${outputDir}" --source-maps inline --presets airbnb`;
console.log(`Выполняем команду для Extensions: ${cmd}`);
execSync(cmd, { stdio: 'inherit' });
const fileNameWithoutExt = path.basename(filePath, '.js');
const outputFile = path.join(outputDir, `${fileNameWithoutExt}.js`);
console.log(`Успешно скомпилировано (Extensions): ${filePath} -> ${outputFile}`);
} catch (error) {
console.error(`Ошибка компиляции (Extensions): ${error.message}`);
process.exit(1);
}
}
else {
console.log(`Файл ${filePath} не соответствует ни одному из паттернов, пропускаем`);
}