-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmakeTransByConentDir.js
172 lines (160 loc) · 6.04 KB
/
makeTransByConentDir.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
// Converts a multilang content folder given in the form of "Translation by Filename"
// into the form of "Translation by content directory"
// see https://gohugo.io/content-management/multilingual/#translate-your-content
const fs = require('fs');
const path = require('path');
function getLanguages(directoryPath) {
const langs = {};
try {
const files = fs.readdirSync(directoryPath);
files.forEach((file) => {
const filePath = path.join(directoryPath, file);
const regex = /^(.*?)\.([^.]+)\.([^.]+)$/;
const match = file.match(regex);
if (match) {
const fileName = match[1];
const fileLang = match[2];
const fileExtension = match[3];
langs[fileLang] = true;
} else {
try {
const stats = fs.statSync(filePath);
if (stats.isDirectory()) {
Object.assign(langs, getLanguages(filePath));
}
} catch (err) {
console.error(`Error reading file/directory ${filePath}: ${err}`);
}
}
});
} catch (err) {
console.error(`Error reading directory ${directoryPath}: ${err}`);
}
return langs;
}
function processDirectory(directoryPath, oldDirectory, newDirectory, langs) {
try {
const files = fs.readdirSync(directoryPath);
files.forEach((file) => {
const relSubDirectory = directoryPath.replace(oldDirectory, '');
const filePath = path.join(directoryPath, file);
try {
const stats = fs.statSync(filePath);
const langRegex = /^(.*?)\.([^.]+)\.([^.]+)$/;
const langMatch = file.match(langRegex);
if (langMatch) {
if (stats.isDirectory() || stats.isFile()) {
// language files are be copied over as is
// language directories are completly copied over as is, no need to scan its content as it already identified for a language
const fileName = langMatch[1];
const fileLang = langMatch[2];
const fileExtension = langMatch[3];
const newFileDirectory = path.join(newDirectory, fileLang, relSubDirectory);
const newFilePath = path.join(newFileDirectory, fileName + '.' + fileExtension);
try {
fs.mkdirSync(newFileDirectory, { recursive: true });
fs.cpSync(filePath, newFilePath, { recursive: true });
} catch (err) {
console.error(`Error copying file/directory ${filePath} to ${newFilePath}: ${err}`);
return false;
}
}
} else {
if (stats.isDirectory()) {
// non-language directories by itself are irrelevant, but let's check for their content
if (!processDirectory(filePath, oldDirectory, newDirectory, langs)) {
return false;
}
} else if (stats.isFile()){
// non-language files are a different beast: copy the file into all languages that don't have a language file
const nonLangRegex = /^(.*?)(\.([^.]+))?$/;
const nonLangMatch = file.match(nonLangRegex);
const fileName = nonLangMatch[1];
const fileExtension = nonLangMatch.length > 3 ? nonLangMatch[3] : "";
Object.keys(langs).forEach((fileLang) => {
const langFilePath = path.join(directoryPath, fileName + '.' + fileLang + '.' + fileExtension);
const langExists = fs.existsSync(langFilePath);
if (!langExists) {
const newFileDirectory = path.join(newDirectory, fileLang, relSubDirectory);
const newFilePath = path.join(newFileDirectory, fileName + '.' + fileExtension);
try {
fs.mkdirSync(newFileDirectory, { recursive: true });
fs.cpSync(filePath, newFilePath, { recursive: true });
} catch (err) {
console.error(`Error copying file ${filePath} to ${newFilePath}: ${err}`);
return false;
}
}
});
}
}
} catch (err) {
console.error(`Error reading file/directory ${filePath}: ${err}`);
return false;
}
});
} catch (err) {
console.error(`Error reading directory ${directoryPath}: ${err}`);
return false;
}
return true;
}
function modifyConfig(configDirectory) {
}
function runThatShit(contentDirectory) {
const sourceDirectory = contentDirectory;
const backupDirectory = contentDirectory + ".backup";
const targetDirectory = contentDirectory + ".temp";
// check directories
try {
fs.accessSync(sourceDirectory, fs.constants.W_OK | fs.constants.R_OK);
} catch (err) {
console.error(`No read/write permisson for directory ${sourceDirectory}: ${err}`);
return false;
}
/*
try {
fs.statSync(backupDirectory);
console.error(`Backup directory from last failed conversion still present ${backupDirectory}`);
return false;
} catch (err) {}
*/
// Make space for the conversion
try {
fs.rmSync(targetDirectory, { recursive: true });
} catch (err) {
try {
fs.statSync(targetDirectory);
console.error(`Error removing directory ${targetDirectory}: ${err}`);
return false;
} catch (err) {}
}
try {
fs.rmSync(backupDirectory, { recursive: true });
} catch (err) {
try {
fs.statSync(backupDirectory);
console.error(`Error removing directory ${backupDirectory}: ${err}`);
return false;
} catch (err) {}
}
// convert that shit
const langs = getLanguages(sourceDirectory);
if( !processDirectory(sourceDirectory, sourceDirectory, targetDirectory, langs) ){
return false;
}
// move final result around
try {
fs.renameSync(sourceDirectory, backupDirectory);
fs.renameSync(targetDirectory, sourceDirectory);
fs.rmSync(backupDirectory, { recursive: true });
} catch (err) {
console.error(`Error deleting/renaming directories: ${err}`);
return false;
}
// edit the config file accordingly
modifyConfig(path.join(sourceDirectory, ".."));
return true;
}
const contentDirectory = path.join(__dirname, 'content');
runThatShit(contentDirectory);