-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathi18n.js
More file actions
204 lines (170 loc) · 5.46 KB
/
i18n.js
File metadata and controls
204 lines (170 loc) · 5.46 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
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
192
193
194
195
196
197
198
199
200
201
202
203
204
import { writeFile, readText, getPackageJson, select, execute } from './utils';
const fs = require('fs');
const fetch = require('node-fetch');
const program = require('commander');
if (!fs.existsSync('./package.json')) {
console.error('Not found package.json');
process.exit(1);
}
const parseCSV = csv => {
const rows = csv.replace(/\r/g, '').split('\n');
const column = rows.splice(0, 1)[0]?.split(',');
let files = {};
for (let i = 1; i < column.length; i++) {
files[column[i]] = {};
}
rows.forEach(row => {
const data = row.split(',');
for (let i = 1; i < column.length; i++) {
files[column[i]][data[0]] = data[i] || '$$' + data[0];
}
});
return files;
};
const parseGJson = (json, flag) => {
const registNames = { FLAG: -1, CODE: -1 };
let rows = [];
let columns = [];
json?.feed?.entry?.forEach(v => {
const cell = v['gs$cell'];
if (parseInt(cell.row) === 1) {
columns[parseInt(cell.col) - 1] = cell.inputValue;
} else {
if (!rows[parseInt(cell.row) - 2]) rows[parseInt(cell.row) - 2] = [];
rows[parseInt(cell.row) - 2][parseInt(cell.col) - 1] = cell.inputValue;
}
});
let files = {};
columns
.filter((v, index) => {
const isLanguage = Object.keys(registNames).indexOf(v.toUpperCase()) < 0;
registNames[v.toUpperCase()] = index;
return isLanguage;
})
.forEach(column => {
files[column] = {};
});
rows.forEach(data => {
for (const key in files) {
const i = registNames[key.toUpperCase()];
const code = data[registNames['CODE']];
if (registNames['FLAG'] >= 0 && flag) {
const _flag = data[registNames['FLAG']];
if (_flag && flag !== _flag) continue;
}
if (!code) continue;
files[key][code] = data[i] || '$$' + code;
}
});
return files;
};
const App = async (pkgs, forceReset) => {
let _config = {};
const file = await getPackageJson();
if (file?.i18n) _config = file?.i18n;
if (forceReset) _config = {};
if (!_config?.output) {
const output = await readText(
'Enter the folder where create the language json : ',
);
if (!output) {
console.log('Folder is required');
process.exit(1);
}
_config.output = output;
}
if (_config?.preset === undefined) {
const value = await select('What kind of file do you want?', [
'Google Docs',
'Spreadsheet File (xlsx)',
]);
_config.preset = value?.id;
}
if (_config?.flag === undefined) {
const flag = await readText('원하는 FLAG값을 입력해주세요. : ');
_config.flag = flag;
}
if (!_config?.path) {
const path = await readText('Path : ');
if (!path) {
console.log('Path is required');
process.exit(1);
}
_config.path = path;
}
if (_config.preset === 1) {
// File Parse..
if (!fs.existsSync(_config.path)) {
console.log(' ');
console.error('파일을 찾을수가 없어요.');
process.exit(1);
}
} else if (_config.preset === 0) {
// Google Docs..
if (_config.path.indexOf('https://docs.google.com/spreadsheets/d/') !== 0) {
console.log(' ');
console.error('구글 경로가 아닌가봅니다.');
process.exit(1);
}
if (_config.path.indexOf('pub?output=csv') >= 0) {
const response = await fetch(_config.path);
const data = await response.text();
const files = parseCSV(data);
const fnames = Object.keys(files);
if (!fs.existsSync(`${_config.output}`)) {
fs.mkdirSync(`${_config.output}`);
}
for (let i = 0; i < fnames?.length; i++) {
const text = JSON.stringify(files[fnames[i]], null, ' ');
const savePath = `${_config.output}/${fnames[i]}.json`.replace(
/\/\//g,
'/',
);
await writeFile(savePath, text);
console.log(` ==> ${savePath} 저장 완료!`);
}
} else if (_config.path.indexOf('/edit#gid=') >= 0) {
let key = _config.path.substring(39);
key = key.substring(0, key.indexOf('/'));
const uri = `https://spreadsheets.google.com/feeds/cells/${key}/1/public/full?alt=json`;
const response = await fetch(uri);
let data = await response.text();
let json = null;
try {
json = JSON.parse(data);
} catch (e) {
console.error(
'Publish 모드가 아닌 것 같습니다. 확인후 다시 시도해주세요.\n참고 URL: https://www.freecodecamp.org/news/cjn-google-sheets-as-json-endpoint/',
);
process.exit(1);
}
const files = parseGJson(json, _config.flag);
const fnames = Object.keys(files);
if (!fs.existsSync(`${_config.output}`)) {
fs.mkdirSync(`${_config.output}`);
}
for (let i = 0; i < fnames?.length; i++) {
const text = JSON.stringify(files[fnames[i]], null, ' ');
const savePath = `${_config.output}/${fnames[i]}.json`.replace(
/\/\//g,
'/',
);
await writeFile(savePath, text);
console.log(savePath + ' 저장 완료!');
}
} else {
console.error('구글 경로가 아닌가봅니다.');
process.exit(1);
}
file.i18n = _config;
const text = JSON.stringify(file, null, 2);
await writeFile('./actbase.json', text);
await execute('git add ' + _config.output);
}
};
program.option('-r, --reset', 'reset latest file.').parse(process.argv);
var pkgs = program.args;
App(
pkgs,
process.argv.indexOf('-r') > 0 || process.argv.indexOf('--reset') >= 0,
);