-
-
Notifications
You must be signed in to change notification settings - Fork 74
/
Copy pathutils.js
99 lines (85 loc) · 2.41 KB
/
utils.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
const fs = require('fs-extra');
const path = require('path');
const glob = require('glob');
const crypto = require('crypto');
const flatten = require('lodash/flatten');
module.exports = {
parsePlatform(pagefile) {
const components = pagefile.split(path.sep);
return components[components.length-2];
},
parsePagename(pagefile) {
return path.basename(pagefile, '.md');
},
parseLanguage(pagefile) {
const components = pagefile.split(path.sep);
const langPathIndex = 3;
const langParts = components[components.length-langPathIndex].split('.');
if (langParts.length === 1) {
return 'en';
}
return langParts[1];
},
localeToLang(locale) {
if(locale === undefined || locale.startsWith('en')) return [];
const withDialect = ['pt', 'zh'];
let lang = locale;
if(lang.includes('.')) {
lang = lang.substring(0, lang.indexOf('.'));
}
// Check for language code & country code.
let ll = lang, cc = '';
if(lang.includes('_')) {
cc = lang.substring(lang.indexOf('_') + 1);
ll = lang.substring(0, lang.indexOf('_'));
}
// If we have dialect for this language take dialect as well.
if(withDialect.indexOf(ll) !== -1 && cc !== '') {
return [ll, ll + '_' + cc];
}
return [ll];
},
isPage(file) {
return path.extname(file) === '.md';
},
// TODO: remove this
commandSupportedOn(platform) {
return (command) => {
return command.platform.indexOf(platform) >= 0
|| command.platform.indexOf('common') >= 0;
};
},
walk: function walk(dir) {
return fs.readdir(dir)
.then((items) => {
return Promise.all(items.map((item) => {
const itemPath = path.join(dir, item);
return fs.stat(itemPath).then((stat) => {
if (stat.isDirectory()) {
return walk(itemPath);
}
return path.join(dir, item);
});
}));
})
.then((paths) => {
return flatten(paths);
});
},
glob(string,options) {
return new Promise((resolve, reject) => {
glob(string, options, (err, data) => {
if (err) {
reject(err);
} else {
resolve(data);
}
});
});
},
// eslint-disable-next-line no-magic-numbers
uniqueId(length = 32) {
const size = Math.ceil(length / 2);
return crypto.randomBytes(size).toString('hex').slice(0, length);
}
};