locize-cli 11.0.0 → 12.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (108) hide show
  1. package/CHANGELOG.md +12 -0
  2. package/LICENSE +1 -1
  3. package/README.md +1 -0
  4. package/dist/cjs/add.js +90 -0
  5. package/{bin/locize → dist/cjs/cli.js} +390 -670
  6. package/dist/cjs/combineSubkeyPreprocessor.js +155 -0
  7. package/dist/cjs/convertToDesiredFormat.js +205 -0
  8. package/dist/cjs/convertToFlatFormat.js +231 -0
  9. package/dist/cjs/copyVersion.js +60 -0
  10. package/dist/cjs/createBranch.js +59 -0
  11. package/dist/cjs/deleteBranch.js +89 -0
  12. package/dist/cjs/deleteNamespace.js +37 -0
  13. package/dist/cjs/download.js +376 -0
  14. package/dist/cjs/filterNamespaces.js +13 -0
  15. package/dist/cjs/format.js +156 -0
  16. package/dist/cjs/formats.js +33 -0
  17. package/dist/cjs/get.js +66 -0
  18. package/dist/cjs/getBranches.js +37 -0
  19. package/dist/cjs/getJob.js +37 -0
  20. package/dist/cjs/getProjectStats.js +37 -0
  21. package/dist/cjs/getRemoteLanguages.js +38 -0
  22. package/dist/cjs/getRemoteNamespace.js +125 -0
  23. package/dist/cjs/index.js +37 -0
  24. package/dist/cjs/isValidUuid.js +6 -0
  25. package/dist/cjs/lngs.js +215 -0
  26. package/dist/cjs/mapLimit.js +22 -0
  27. package/dist/cjs/mergeBranch.js +80 -0
  28. package/dist/cjs/migrate.js +239 -0
  29. package/dist/cjs/missing.js +162 -0
  30. package/dist/cjs/package.json +5 -0
  31. package/{parseLocalLanguage.js → dist/cjs/parseLocalLanguage.js} +135 -142
  32. package/dist/cjs/parseLocalLanguages.js +18 -0
  33. package/dist/cjs/parseLocalReference.js +11 -0
  34. package/dist/cjs/publishVersion.js +42 -0
  35. package/dist/cjs/removeUndefinedFromArrays.js +19 -0
  36. package/dist/cjs/removeVersion.js +42 -0
  37. package/dist/cjs/request.js +66 -0
  38. package/dist/cjs/shouldUnflatten.js +21 -0
  39. package/dist/cjs/sortFlatResources.js +13 -0
  40. package/dist/cjs/sync.js +772 -0
  41. package/dist/cjs/unflatten.js +81 -0
  42. package/dist/esm/add.js +88 -0
  43. package/dist/esm/cli.js +1020 -0
  44. package/{combineSubkeyPreprocessor.js → dist/esm/combineSubkeyPreprocessor.js} +70 -73
  45. package/dist/esm/convertToDesiredFormat.js +203 -0
  46. package/dist/esm/convertToFlatFormat.js +229 -0
  47. package/dist/esm/copyVersion.js +58 -0
  48. package/dist/esm/createBranch.js +57 -0
  49. package/dist/esm/deleteBranch.js +87 -0
  50. package/dist/esm/deleteNamespace.js +35 -0
  51. package/dist/esm/download.js +374 -0
  52. package/{filterNamespaces.js → dist/esm/filterNamespaces.js} +4 -4
  53. package/dist/esm/format.js +154 -0
  54. package/{formats.js → dist/esm/formats.js} +7 -11
  55. package/dist/esm/get.js +64 -0
  56. package/dist/esm/getBranches.js +35 -0
  57. package/dist/esm/getJob.js +35 -0
  58. package/dist/esm/getProjectStats.js +35 -0
  59. package/dist/esm/getRemoteLanguages.js +36 -0
  60. package/dist/esm/getRemoteNamespace.js +123 -0
  61. package/dist/esm/index.js +16 -0
  62. package/dist/esm/isValidUuid.js +4 -0
  63. package/dist/esm/lngs.js +213 -0
  64. package/dist/esm/mapLimit.js +20 -0
  65. package/dist/esm/mergeBranch.js +78 -0
  66. package/dist/esm/migrate.js +237 -0
  67. package/dist/esm/missing.js +160 -0
  68. package/dist/esm/parseLocalLanguage.js +194 -0
  69. package/dist/esm/parseLocalLanguages.js +16 -0
  70. package/dist/esm/parseLocalReference.js +9 -0
  71. package/dist/esm/publishVersion.js +40 -0
  72. package/{removeUndefinedFromArrays.js → dist/esm/removeUndefinedFromArrays.js} +5 -5
  73. package/dist/esm/removeVersion.js +40 -0
  74. package/dist/esm/request.js +64 -0
  75. package/{shouldUnflatten.js → dist/esm/shouldUnflatten.js} +7 -7
  76. package/dist/esm/sortFlatResources.js +11 -0
  77. package/dist/esm/sync.js +770 -0
  78. package/{unflatten.js → dist/esm/unflatten.js} +36 -34
  79. package/package.json +39 -18
  80. package/rollup.config.js +57 -0
  81. package/add.js +0 -105
  82. package/convertToDesiredFormat.js +0 -268
  83. package/convertToFlatFormat.js +0 -322
  84. package/copyVersion.js +0 -69
  85. package/createBranch.js +0 -61
  86. package/deleteBranch.js +0 -97
  87. package/deleteNamespace.js +0 -39
  88. package/download.js +0 -516
  89. package/format.js +0 -206
  90. package/get.js +0 -81
  91. package/getBranches.js +0 -40
  92. package/getJob.js +0 -40
  93. package/getProjectStats.js +0 -40
  94. package/getRemoteLanguages.js +0 -40
  95. package/getRemoteNamespace.js +0 -122
  96. package/index.js +0 -9
  97. package/isValidUuid.js +0 -2
  98. package/lngs.json +0 -211
  99. package/mergeBranch.js +0 -102
  100. package/migrate.js +0 -314
  101. package/missing.js +0 -169
  102. package/parseLocalLanguages.js +0 -22
  103. package/parseLocalReference.js +0 -10
  104. package/publishVersion.js +0 -64
  105. package/removeVersion.js +0 -64
  106. package/request.js +0 -64
  107. package/sortFlatResources.js +0 -9
  108. package/sync.js +0 -786
package/format.js DELETED
@@ -1,206 +0,0 @@
1
- const colors = require('colors')
2
- const fs = require('fs')
3
- const async = require('async')
4
- const path = require('path')
5
- const diff = require('diff')
6
- const convertToFlatFormat = require('./convertToFlatFormat')
7
- const convertToDesiredFormat = require('./convertToDesiredFormat')
8
- const sortFlatResources = require('./sortFlatResources')
9
- const formats = require('./formats')
10
- const fileExtensionsMap = formats.fileExtensionsMap
11
- const acceptedFileExtensions = formats.acceptedFileExtensions
12
- const reversedFileExtensionsMap = formats.reversedFileExtensionsMap
13
-
14
- const handleError = (err, cb) => {
15
- if (!cb && err) {
16
- console.error(colors.red(err.message))
17
- process.exit(1)
18
- }
19
- if (cb) cb(err)
20
- }
21
-
22
- const getFiles = (srcpath) => {
23
- let files = []
24
- fs.readdirSync(srcpath).forEach((file) => {
25
- if (fs.statSync(path.join(srcpath, file)).isDirectory()) {
26
- files = files.concat(getFiles(path.join(srcpath, file)))
27
- } else if (acceptedFileExtensions.indexOf(path.extname(file)) > -1) {
28
- files.push(path.join(srcpath, file))
29
- }
30
- })
31
- return files
32
- }
33
-
34
- function readLocalFile (opt, fPath, clb) {
35
- const fExt = path.extname(fPath)
36
- const namespace = path.basename(fPath, fExt)
37
- const splitted = fPath.split(path.sep)
38
- const lng = splitted[splitted.length - 2]
39
-
40
- fs.readFile(fPath, (err, data) => {
41
- if (err) return clb(err)
42
-
43
- fs.stat(fPath, (err, stat) => {
44
- if (err) return clb(err)
45
-
46
- clb(null, {
47
- namespace,
48
- path: fPath,
49
- extension: fExt,
50
- original: data.toString(),
51
- language: lng,
52
- mtime: stat.mtime
53
- })
54
- })
55
- })
56
- }
57
-
58
- function readLocalFiles (opt, filePaths, clb) {
59
- async.map(filePaths, (filePath, cb) => {
60
- readLocalFile(opt, filePath, cb)
61
- }, clb)
62
- }
63
-
64
- function convertAllFilesToFlatFormat (opt, files, clb) {
65
- async.map(files, (file, cb) => {
66
- if (fileExtensionsMap[file.extension].indexOf(opt.format) < 0) {
67
- return cb(new Error(`Format mismatch! Found ${fileExtensionsMap[file.extension][0]} but requested ${opt.format}!`))
68
- }
69
-
70
- convertToFlatFormat(opt, file.original, (err, content) => {
71
- if (err) {
72
- err.message = 'Invalid content for "' + opt.format + '" format!\n' + (err.message || '')
73
- err.message += '\n' + file.path
74
- return cb(err)
75
- }
76
-
77
- file.content = sortFlatResources(content)
78
- cb(null, file)
79
- })
80
- }, clb)
81
- }
82
-
83
- function convertAllFilesToDesiredFormat (opt, files, clb) {
84
- async.map(files, (file, cb) => {
85
- convertToDesiredFormat(opt, file.namespace, file.language, file.content, file.mtime, (err, res) => {
86
- if (err) {
87
- err.message = 'Invalid content for "' + opt.format + '" format!\n' + (err.message || '')
88
- return cb(err)
89
- }
90
- res = (opt.format !== 'xlsx' && !res.endsWith('\n')) ? (res + '\n') : res
91
- file.converted = res
92
- cb(null, file)
93
- })
94
- }, clb)
95
- }
96
-
97
- function writeLocalFile (opt, file, clb) {
98
- if (file.converted === file.original) {
99
- if (opt.noCallback) console.log(colors.grey(`${file.path} unchanged`))
100
- return clb(null)
101
- }
102
-
103
- const d = diff.diffLines(file.original, file.converted)
104
- d.forEach((part) => {
105
- // green for additions, red for deletions
106
- // grey for common parts
107
- const color = part.added ? 'green' : part.removed ? 'red' : 'grey'
108
- if (opt.noCallback) console.log(part.value[color])
109
- })
110
-
111
- if (opt.noCallback) console.log(colors.yellow(`reformatting ${file.path}...`))
112
- if (opt.dry) {
113
- if (opt.noCallback) console.log(colors.yellow(`would have reformatted ${file.path}...`))
114
- return clb(null, true)
115
- }
116
-
117
- const fileContent = (opt.format !== 'xlsx' && !file.converted.endsWith('\n')) ? (file.converted + '\n') : file.converted
118
-
119
- fs.writeFile(file.path, fileContent, (err) => clb(err, true))
120
- }
121
-
122
- function writeLocalFiles (opt, files, clb) {
123
- async.map(files, (file, cb) => {
124
- writeLocalFile(opt, file, cb)
125
- }, clb)
126
- }
127
-
128
- function processFiles (opt, filePaths, clb) {
129
- readLocalFiles(opt, filePaths, (err, orgFiles) => {
130
- if (err) return clb(err)
131
-
132
- if (!opt.format) {
133
- if (orgFiles.length === 0) {
134
- return clb(new Error('Please provide a format!'))
135
- }
136
- // guess format
137
- opt.format = fileExtensionsMap[orgFiles[0].extension][0]
138
- if (opt.noCallback) console.log(colors.bgYellow(`No format argument was passed, so guessing "${opt.format}" format.`))
139
- }
140
-
141
- convertAllFilesToFlatFormat(opt, orgFiles, (err, files) => {
142
- if (err) return clb(err)
143
-
144
- opt.getNamespace = (o, lng, ns, cb) => {
145
- const foundOrgFile = orgFiles.find((f) => f.namespace === ns && f.language === lng)
146
- if (!foundOrgFile) {
147
- return cb(new Error(`No file found for language "${lng}" and namespace "${ns}" locally!`))
148
- }
149
- cb(null, foundOrgFile.content, foundOrgFile.mtime)
150
- }
151
-
152
- // just the value
153
- files.forEach((f) => {
154
- if (f.content) {
155
- Object.keys(f.content).forEach((k) => {
156
- if (f.content[k] && typeof f.content[k] === 'object' && f.content[k].value !== undefined) {
157
- f.content[k] = f.content[k].value
158
- }
159
- })
160
- }
161
- })
162
-
163
- convertAllFilesToDesiredFormat(opt, files, (err, convertedFiles) => {
164
- if (err) return clb(err)
165
-
166
- writeLocalFiles(opt, convertedFiles, clb)
167
- })
168
- })
169
- })
170
- }
171
-
172
- const format = (opt, cb) => {
173
- if (opt.format && !reversedFileExtensionsMap[opt.format]) {
174
- return handleError(new Error(`${opt.format} is not a valid format!`), cb)
175
- }
176
-
177
- opt.noCallback = !cb
178
-
179
- fs.lstat(opt.fileOrDirectory, (err, stat) => {
180
- if (err) return handleError(err, cb)
181
-
182
- const isDirectory = stat.isDirectory()
183
-
184
- let filePaths = []
185
- if (isDirectory) {
186
- try {
187
- filePaths = getFiles(opt.fileOrDirectory)
188
- } catch (err) {}
189
- } else {
190
- filePaths = [opt.fileOrDirectory]
191
- }
192
-
193
- processFiles(opt, filePaths, (err, writeResults) => {
194
- if (err) return handleError(err, cb)
195
- if (!cb) {
196
- console.log(colors.green('FINISHED'))
197
- if (opt.dry && writeResults.find((wr) => !!wr)) {
198
- process.exit(1)
199
- }
200
- }
201
- if (cb) cb(null)
202
- })
203
- })
204
- }
205
-
206
- module.exports = format
package/get.js DELETED
@@ -1,81 +0,0 @@
1
- const colors = require('colors')
2
- const request = require('./request')
3
- const flatten = require('flat')
4
-
5
- const get = (opt, cb) => {
6
- const url = `${opt.apiEndpoint}/{{projectId}}/{{version}}/{{lng}}/{{ns}}`
7
- .replace('{{projectId}}', opt.projectId)
8
- .replace('{{ver}}', opt.version)
9
- .replace('{{version}}', opt.version)
10
- .replace('{{language}}', opt.language)
11
- .replace('{{lng}}', opt.language)
12
- .replace('{{ns}}', opt.namespace)
13
- .replace('{{namespace}}', opt.namespace)
14
-
15
- // if (!cb) console.log(colors.yellow(`getting ${opt.key} from ${opt.version}/${opt.language}/${opt.namespace}...`));
16
-
17
- if (opt.key && opt.key.indexOf(',') > 0 && opt.key.indexOf(' ') < 0) {
18
- opt.keys = opt.key.split(',')
19
- delete opt.key
20
- }
21
-
22
- request(`${url}${opt.cdnType === 'standard' ? '?cache=no' : ''}`, {
23
- method: 'get'
24
- }, (err, res, obj) => {
25
- if (err) {
26
- if (!cb) console.log(colors.red(`get failed for ${opt.key || opt.keys.join(', ')} from ${opt.version}/${opt.language}/${opt.namespace}...`))
27
- if (err) {
28
- if (!cb) { console.error(colors.red(err.message)); process.exit(1) }
29
- if (cb) cb(err)
30
- return
31
- }
32
- }
33
- const ignore404 = res.status === 404 && opt.cdnType === 'standard'
34
- if (res.status >= 300 && !ignore404) {
35
- if (!cb) { console.error(colors.red(res.statusText + ' (' + res.status + ')')); process.exit(1) }
36
- if (cb) cb(new Error(res.statusText + ' (' + res.status + ')'))
37
- return
38
- }
39
- // if (!cb) console.log(colors.green(`got ${opt.opt.key || opt.keys.join(', ')} from ${opt.version}/${opt.language}/${opt.namespace}...`));
40
-
41
- if (ignore404) obj = {}
42
-
43
- const flat = flatten(obj)
44
- if (opt.key) {
45
- if (!flat[opt.key]) {
46
- if (!cb) { console.error(colors.red(`${opt.key} not found in ${opt.version}/${opt.language}/${opt.namespace} => ${JSON.stringify(obj, null, 2)}`)); process.exit(1) }
47
- if (cb) cb(new Error(`${opt.key} not found in ${opt.version}/${opt.language}/${opt.namespace} => ${JSON.stringify(obj, null, 2)}`))
48
- return
49
- }
50
- if (!cb) console.log(flat[opt.key])
51
- }
52
- if (opt.keys) {
53
- const ret = {}
54
- const retWitAllKeys = {}
55
- opt.keys.forEach((k) => {
56
- if (flat[k] !== undefined) {
57
- ret[k] = flat[k]
58
- }
59
- retWitAllKeys[k] = flat[k]
60
- })
61
- const retKeys = Object.keys(ret)
62
- if (retKeys.length === 0) {
63
- if (!cb) { console.error(colors.red(`${opt.keys.join(', ')} not found in ${opt.version}/${opt.language}/${opt.namespace} => ${JSON.stringify(obj, null, 2)}`)); process.exit(1) }
64
- if (cb) cb(new Error(`${opt.keys.join(', ')} not found in ${opt.version}/${opt.language}/${opt.namespace} => ${JSON.stringify(obj, null, 2)}`))
65
- return
66
- }
67
- if (!cb) {
68
- if (console.table) {
69
- console.table(retWitAllKeys)
70
- } else {
71
- opt.keys.forEach((k) => {
72
- console.log(`${k}\t=>\t${ret[k] || ''}`)
73
- })
74
- }
75
- }
76
- }
77
- if (cb) cb(null)
78
- })
79
- }
80
-
81
- module.exports = get
package/getBranches.js DELETED
@@ -1,40 +0,0 @@
1
- const colors = require('colors')
2
- const request = require('./request')
3
-
4
- const getBranches = (opt, cb) => {
5
- request(opt.apiEndpoint + '/branches/' + opt.projectId, {
6
- method: 'get',
7
- headers: {
8
- Authorization: opt.apiKey
9
- }
10
- }, (err, res, obj) => {
11
- if (err || (obj && (obj.errorMessage || obj.message))) {
12
- if (!cb) console.log(colors.red('getting branches failed...'))
13
-
14
- if (err) {
15
- if (!cb) { console.error(colors.red(err.message)); process.exit(1) }
16
- if (cb) cb(err)
17
- return
18
- }
19
- if (obj && (obj.errorMessage || obj.message)) {
20
- if (!cb) { console.error(colors.red((obj.errorMessage || obj.message))); process.exit(1) }
21
- if (cb) cb(new Error((obj.errorMessage || obj.message)))
22
- return
23
- }
24
- }
25
- if (res.status === 404) {
26
- if (!cb) { console.error(colors.yellow(res.statusText + ' (' + res.status + ')')); process.exit(1) }
27
- if (cb) cb(null, null)
28
- return
29
- }
30
- if (res.status >= 300) {
31
- if (!cb) { console.error(colors.red(res.statusText + ' (' + res.status + ')')); process.exit(1) }
32
- if (cb) cb(new Error(res.statusText + ' (' + res.status + ')'))
33
- return
34
- }
35
- if (!cb) console.log(colors.green('getting branches successful'))
36
- if (cb) cb(null, obj)
37
- })
38
- }
39
-
40
- module.exports = getBranches
package/getJob.js DELETED
@@ -1,40 +0,0 @@
1
- const colors = require('colors')
2
- const request = require('./request')
3
-
4
- const getJob = (opt, jobId, cb) => {
5
- request(opt.apiEndpoint + '/jobs/' + opt.projectId + '/' + jobId, {
6
- method: 'get',
7
- headers: {
8
- Authorization: opt.apiKey
9
- }
10
- }, (err, res, obj) => {
11
- if (err || (obj && (obj.errorMessage || obj.message))) {
12
- if (!cb) console.log(colors.red('getting job failed...'))
13
-
14
- if (err) {
15
- if (!cb) { console.error(colors.red(err.message)); process.exit(1) }
16
- if (cb) cb(err)
17
- return
18
- }
19
- if (obj && (obj.errorMessage || obj.message)) {
20
- if (!cb) { console.error(colors.red((obj.errorMessage || obj.message))); process.exit(1) }
21
- if (cb) cb(new Error((obj.errorMessage || obj.message)))
22
- return
23
- }
24
- }
25
- if (res.status === 404) {
26
- if (!cb) { console.error(colors.yellow(res.statusText + ' (' + res.status + ')')); process.exit(1) }
27
- if (cb) cb(null, null)
28
- return
29
- }
30
- if (res.status >= 300) {
31
- if (!cb) { console.error(colors.red(res.statusText + ' (' + res.status + ')')); process.exit(1) }
32
- if (cb) cb(new Error(res.statusText + ' (' + res.status + ')'))
33
- return
34
- }
35
- if (!cb) console.log(colors.green('getting job successful'))
36
- if (cb) cb(null, obj)
37
- })
38
- }
39
-
40
- module.exports = getJob
@@ -1,40 +0,0 @@
1
- const colors = require('colors')
2
- const request = require('./request')
3
-
4
- const getProjectStats = (opt, cb) => {
5
- request(opt.apiEndpoint + '/stats/project/' + opt.projectId, {
6
- method: 'get',
7
- headers: {
8
- Authorization: opt.apiKey
9
- }
10
- }, (err, res, obj) => {
11
- if (err || (obj && (obj.errorMessage || obj.message))) {
12
- if (!cb) console.log(colors.red('getting job failed...'))
13
-
14
- if (err) {
15
- if (!cb) { console.error(colors.red(err.message)); process.exit(1) }
16
- if (cb) cb(err)
17
- return
18
- }
19
- if (obj && (obj.errorMessage || obj.message)) {
20
- if (!cb) { console.error(colors.red((obj.errorMessage || obj.message))); process.exit(1) }
21
- if (cb) cb(new Error((obj.errorMessage || obj.message)))
22
- return
23
- }
24
- }
25
- if (res.status === 404) {
26
- if (!cb) { console.error(colors.yellow(res.statusText + ' (' + res.status + ')')); process.exit(1) }
27
- if (cb) cb(null, null)
28
- return
29
- }
30
- if (res.status >= 300) {
31
- if (!cb) { console.error(colors.red(res.statusText + ' (' + res.status + ')')); process.exit(1) }
32
- if (cb) cb(new Error(res.statusText + ' (' + res.status + ')'))
33
- return
34
- }
35
- if (!cb) console.log(colors.green('getting project stats successful'))
36
- if (cb) cb(null, obj)
37
- })
38
- }
39
-
40
- module.exports = getProjectStats
@@ -1,40 +0,0 @@
1
- const request = require('./request')
2
-
3
- const getRemoteLanguages = (opt, cb) => {
4
- request(opt.apiEndpoint + '/languages/' + opt.projectId + '?ts=' + Date.now() + (opt.cdnType === 'standard' ? '&cache=no' : ''), {
5
- method: 'get'
6
- }, (err, res, obj) => {
7
- if (err || (obj && (obj.errorMessage || obj.message))) {
8
- if (err) return cb(err)
9
- if (obj && (obj.errorMessage || obj.message)) {
10
- if (res && res.statusText && res.status) {
11
- return cb(new Error(res.statusText + ' (' + res.status + ') | ' + (obj.errorMessage || obj.message)))
12
- }
13
- return cb(new Error((obj.errorMessage || obj.message)))
14
- }
15
- }
16
- if (res.status >= 300) return cb(new Error(res.statusText + ' (' + res.status + ')'))
17
-
18
- if (Object.keys(obj).length === 0) {
19
- return cb(new Error('Project with id "' + opt.projectId + '" not found!'))
20
- }
21
-
22
- const lngs = Object.keys(obj)
23
- let foundRefLng = null
24
- lngs.forEach((l) => {
25
- if (obj[l].isReferenceLanguage) foundRefLng = l
26
- })
27
- if (!foundRefLng) {
28
- return cb(new Error('Reference language for project with id "' + opt.projectId + '" not found!'))
29
- }
30
- opt.referenceLanguage = foundRefLng
31
-
32
- // reflng first
33
- lngs.splice(lngs.indexOf(opt.referenceLanguage), 1)
34
- lngs.unshift(opt.referenceLanguage)
35
-
36
- cb(null, lngs)
37
- })
38
- }
39
-
40
- module.exports = getRemoteLanguages
@@ -1,122 +0,0 @@
1
- const request = require('./request')
2
- const flatten = require('flat')
3
- const sortFlatResources = require('./sortFlatResources')
4
-
5
- const getRandomDelay = (delayFrom, delayTo) => Math.floor(Math.random() * delayTo) + delayFrom
6
-
7
- function onlyKeysFlat (resources, prefix, ret) {
8
- ret = ret || {}
9
- if (!resources) return ret
10
- Object.keys(resources).forEach((k) => {
11
- if (typeof resources[k] === 'string' || !resources[k] || typeof resources[k].value === 'string') {
12
- if (prefix) {
13
- ret[prefix + '.' + k] = resources[k]
14
- } else {
15
- ret[k] = resources[k]
16
- }
17
- } else {
18
- onlyKeysFlat(resources[k], prefix ? prefix + '.' + k : k, ret)
19
- }
20
- })
21
- return ret
22
- }
23
-
24
- const pullNamespacePaged = (opt, lng, ns, cb, next, retry) => {
25
- next = next || ''
26
- request(opt.apiEndpoint + '/pull/' + opt.projectId + '/' + opt.version + '/' + lng + '/' + ns + '?' + 'next=' + next + ((opt.raw || opt.overriddenOnly) ? '&raw=true' : '') + '&ts=' + Date.now(), {
27
- method: 'get',
28
- headers: {
29
- Authorization: opt.apiKey
30
- }
31
- }, (err, res, obj) => {
32
- if (err) return cb(err)
33
- if (res.status >= 300) {
34
- retry = retry || 0
35
- if (retry < 3 && res.status !== 401) {
36
- setTimeout(() => {
37
- pullNamespacePaged(opt, lng, ns, cb, next, retry + 1)
38
- }, getRandomDelay(3000, 10000))
39
- return
40
- }
41
- if (obj && (obj.errorMessage || obj.message)) {
42
- if (res.statusText && res.status) {
43
- return cb(new Error(res.statusText + ' (' + res.status + ') | ' + (obj.errorMessage || obj.message)))
44
- }
45
- return cb(new Error((obj.errorMessage || obj.message)))
46
- }
47
- return cb(new Error(res.statusText + ' (' + res.status + ')'))
48
- }
49
-
50
- if (opt.overriddenOnly && obj) {
51
- const newObj = {}
52
- Object.keys(obj).forEach((k) => {
53
- if (obj[k].overrides !== undefined) {
54
- if (opt.raw) {
55
- newObj[k] = obj[k]
56
- } else {
57
- newObj[k] = obj[k].value
58
- }
59
- }
60
- })
61
- obj = newObj
62
- }
63
-
64
- cb(null, {
65
- result: opt.raw ? sortFlatResources(onlyKeysFlat(obj)) : sortFlatResources(flatten(obj)),
66
- next: res.headers.get('x-next-page'),
67
- lastModified: res.headers.get('last-modified') ? new Date(res.headers.get('last-modified')) : undefined
68
- })
69
- })
70
- }
71
-
72
- const pullNamespace = (opt, lng, ns, cb) => {
73
- const ret = {}
74
- let lastModified = new Date(2000, 0, 1);
75
- (function nextPage (next) {
76
- pullNamespacePaged(opt, lng, ns, (err, info) => {
77
- if (err) return cb(err)
78
-
79
- Object.keys(info.result).forEach((k) => {
80
- ret[k] = info.result[k]
81
- })
82
-
83
- if (info.lastModified && info.lastModified.getTime() > (lastModified ? lastModified.getTime() : 0)) {
84
- lastModified = info.lastModified
85
- }
86
-
87
- if (info.next) {
88
- return nextPage(info.next)
89
- }
90
- cb(null, ret, lastModified)
91
- }, next)
92
- })()
93
- }
94
-
95
- const getRemoteNamespace = (opt, lng, ns, cb) => {
96
- if (opt.unpublished) return pullNamespace(opt, lng, ns, cb)
97
-
98
- request(opt.apiEndpoint + (opt.isPrivate ? '/private' : '') + '/' + opt.projectId + '/' + opt.version + '/' + lng + '/' + ns + '?ts=' + Date.now() + (opt.cdnType === 'standard' ? '&cache=no' : ''), {
99
- method: 'get',
100
- headers: opt.isPrivate
101
- ? {
102
- Authorization: opt.apiKey
103
- }
104
- : undefined
105
- }, (err, res, obj) => {
106
- if (err) return cb(err)
107
- const ignore404 = res.status === 404 && opt.cdnType === 'standard'
108
- if (ignore404) return cb(null, {}, undefined)
109
- if (res.status >= 300) {
110
- if (obj && (obj.errorMessage || obj.message)) {
111
- if (res.statusText && res.status) {
112
- return cb(new Error(res.statusText + ' (' + res.status + ') | ' + (obj.errorMessage || obj.message)))
113
- }
114
- return cb(new Error((obj.errorMessage || obj.message)))
115
- }
116
- return cb(new Error(res.statusText + ' (' + res.status + ')'))
117
- }
118
- cb(null, sortFlatResources(flatten(obj)), res.headers.get('last-modified') ? new Date(res.headers.get('last-modified')) : undefined)
119
- })
120
- }
121
-
122
- module.exports = getRemoteNamespace
package/index.js DELETED
@@ -1,9 +0,0 @@
1
- module.exports = {
2
- add: require('./add'),
3
- get: require('./get'),
4
- download: require('./download'),
5
- migrate: require('./migrate'),
6
- sync: require('./sync'),
7
- publishVersion: require('./publishVersion'),
8
- copyVersion: require('./copyVersion')
9
- }
package/isValidUuid.js DELETED
@@ -1,2 +0,0 @@
1
- const uuidRegex = /^[0-9A-F]{8}-[0-9A-F]{4}-4[0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}$/i
2
- module.exports = (id) => uuidRegex.test(id)