@modernpoacher/deps 2.0.16 → 2.0.18

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.
@@ -1,3 +1,38 @@
1
+ /**
2
+ * Package or configuration dependencies
3
+ * @typedef {Object.<string, string>} Dependencies
4
+ */
5
+
6
+ /**
7
+ * A dependency descriptor
8
+ * @typedef {Object} DependencyDescriptor
9
+ * @property {string} name - Dependency name in NPM.
10
+ * @property {string} version - Dependency version or "latest".
11
+ */
12
+
13
+ /**
14
+ * A package
15
+ * @typedef {Object} Package
16
+ * @property {string|{name: string, email: string}} author
17
+ * @property {Dependencies} dependencies - Production dependencies.
18
+ * @property {Dependencies} devDependencies - Development dependencies.
19
+ * @property {Dependencies} optionalDependencies - Optional dependencies.
20
+ * @property {Dependencies} bundleDependencies - Bundle dependencies.
21
+ * @property {Dependencies} peerDependencies - Peer dependencies
22
+ */
23
+
24
+ /**
25
+ * A configuration
26
+ * @typedef {Object} Configuration
27
+ * @property {string|{name: string, email: string}} author
28
+ * @property {boolean} ignore
29
+ * @property {Dependencies} dependencies - Production dependencies.
30
+ * @property {Dependencies} devDependencies - Development dependencies.
31
+ * @property {Dependencies} optionalDependencies - Optional dependencies.
32
+ * @property {Dependencies} bundleDependencies - Bundle dependencies.
33
+ * @property {Dependencies} peerDependencies - Peer dependencies
34
+ */
35
+
1
36
  import debug from 'debug'
2
37
 
3
38
  import PATH from '#where-am-i'
@@ -23,16 +58,16 @@ const log = debug('@modernpoacher/deps')
23
58
 
24
59
  log(`\`common\` (${VERSION} - ${PLATFORM}) is awake at "${PATH}"`)
25
60
 
26
- const tidy = (v) => v.replace(/\n\n/gm, String.fromCharCode(10)).trim()
61
+ const tidy = (v) => v.replace(/\n{2,}}/gm, String.fromCharCode(10)).trim()
27
62
 
28
63
  /**
29
64
  * @function getSaveProdParameter
30
65
  *
31
66
  * Get the `--save-prod` parameter
32
67
  *
33
- * @param {String} current commands string
68
+ * @param {string} current commands string
34
69
  *
35
- * @return {String}
70
+ * @return {string}
36
71
  */
37
72
  export const getSaveProdParameter = (commands) => commands.concat(' --save-prod')
38
73
 
@@ -41,9 +76,9 @@ export const getSaveProdParameter = (commands) => commands.concat(' --save-prod'
41
76
  *
42
77
  * Get the `--save-dev` parameter
43
78
  *
44
- * @param {String} current commands string
79
+ * @param {string} current commands string
45
80
  *
46
- * @return {String}
81
+ * @return {string}
47
82
  */
48
83
  export const getSaveDevParameter = (commands) => commands.concat(' --save-dev')
49
84
 
@@ -52,9 +87,9 @@ export const getSaveDevParameter = (commands) => commands.concat(' --save-dev')
52
87
  *
53
88
  * Get the "save bundle" parameter
54
89
  *
55
- * @param {String} current commands string
90
+ * @param {string} commands - Current commands string
56
91
  *
57
- * @return {String}
92
+ * @return {string}
58
93
  */
59
94
  export const getSaveBundleParameter = (commands) => commands.concat(' --save-bundle')
60
95
 
@@ -63,9 +98,9 @@ export const getSaveBundleParameter = (commands) => commands.concat(' --save-bun
63
98
  *
64
99
  * Get the `--save-optional` parameter
65
100
  *
66
- * @param {String} current commands string
101
+ * @param {string} commands - Current commands string
67
102
  *
68
- * @return {String}
103
+ * @return {string}
69
104
  */
70
105
  export const getSaveOptionalParameter = (commands) => commands.concat(' --save-optional')
71
106
 
@@ -74,10 +109,10 @@ export const getSaveOptionalParameter = (commands) => commands.concat(' --save-o
74
109
  *
75
110
  * Get the "registry" parameter and argument
76
111
  *
77
- * @param {String} registry
78
- * @param {String} current commands string
112
+ * @param {string} r - Registry
113
+ * @param {string} commands - Current commands string
79
114
  *
80
- * @return {String}
115
+ * @return {string}
81
116
  */
82
117
  export const getRegistryParameter = (r, commands) => r ? commands.concat(` --registry ${r}`) : commands
83
118
 
@@ -86,10 +121,10 @@ export const getRegistryParameter = (r, commands) => r ? commands.concat(` --reg
86
121
  *
87
122
  * Get the "force" parameter and argument
88
123
  *
89
- * @param {String} force
90
- * @param {String} current commands string
124
+ * @param {boolean} f - Force
125
+ * @param {string} commands - Current commands string
91
126
  *
92
- * @return {String}
127
+ * @return {string}
93
128
  */
94
129
  export const getForceParameter = (f, commands) => f ? commands.concat(' --force') : commands
95
130
 
@@ -98,10 +133,10 @@ export const getForceParameter = (f, commands) => f ? commands.concat(' --force'
98
133
  *
99
134
  * Get the `--no-save` parameter
100
135
  *
101
- * @param {Boolean} save
102
- * @param {String} current commands string
136
+ * @param {boolean} s - Save
137
+ * @param {string} commands - Current commands string
103
138
  *
104
- * @return {String}
139
+ * @return {string}
105
140
  */
106
141
  export const getNoSaveParameter = (s, commands) => s ? commands : commands.concat(' --no-save')
107
142
 
@@ -110,10 +145,9 @@ export const getNoSaveParameter = (s, commands) => s ? commands : commands.conca
110
145
  *
111
146
  * Get the `--save-exact` parameter
112
147
  *
113
- * @param {Boolean} exact
114
- * @param {String} current commands string
148
+ * @param {string} commands - Current commands string
115
149
  *
116
- * @return {String}
150
+ * @return {string}
117
151
  */
118
152
  export const getSaveExactParameter = (commands) => commands.concat(' --save-exact')
119
153
 
@@ -122,9 +156,9 @@ export const getSaveExactParameter = (commands) => commands.concat(' --save-exac
122
156
  *
123
157
  * Get the export PATH shell script
124
158
  *
125
- * @param {String} commands
159
+ * @param {string} commands - Current commands string
126
160
  *
127
- * @return {String}
161
+ * @return {string}
128
162
  */
129
163
  export const getExportPath = (commands) => `
130
164
  export PATH=/usr/local/bin:$PATH &> /dev/null
@@ -136,9 +170,9 @@ ${commands}
136
170
  *
137
171
  * Get the NVM shell script
138
172
  *
139
- * @param {String} commands
173
+ * @param {string} commands - Current commands string
140
174
  *
141
- * @return {String}
175
+ * @return {string}
142
176
  */
143
177
  export const getNvm = (commands) => `
144
178
  . "${NVM}" 2> /dev/null
@@ -150,30 +184,22 @@ ${commands}
150
184
  *
151
185
  * Get the installation shell script
152
186
  *
153
- * @param {String} directory
154
- * @param {String} commands
187
+ * @param {string} commands - Current commands string
155
188
  *
156
- * @return {String}
189
+ * @return {string}
157
190
  */
158
-
159
191
  export const getCommands = PLATFORM === 'win32'
160
- ? (directory = DIRECTORY, commands = 'npm i') => tidy(`
161
- echo Directory is \\"${directory}\\"
162
- ${commands}
163
- `)
164
- : (directory = DIRECTORY, commands = 'npm i') => tidy(`
165
- echo Directory is \\"${directory}\\"
166
- ${getExportPath(getNvm(commands))}
167
- `)
192
+ ? (commands = 'npm i') => tidy(commands)
193
+ : (commands = 'npm i') => tidy(getExportPath(getNvm(commands)))
168
194
 
169
195
  /**
170
196
  * @function getProdDependencies
171
197
  *
172
198
  * Get the production dependencies by destructuring the package or configuration
173
199
  *
174
- * @param {Object} package
200
+ * @param {Package|Configuration}
175
201
  *
176
- * @return {Object}
202
+ * @return {Dependencies}
177
203
  */
178
204
  export const getProdDependencies = ({ dependencies = {} } = {}) => dependencies
179
205
 
@@ -182,9 +208,9 @@ export const getProdDependencies = ({ dependencies = {} } = {}) => dependencies
182
208
  *
183
209
  * Get the development dependencies by destructuring the package or configuration
184
210
  *
185
- * @param {Object} package
211
+ * @param {Package|Configuration}
186
212
  *
187
- * @return {Object}
213
+ * @return {Dependencies}
188
214
  */
189
215
  export const getDevDependencies = ({ devDependencies = {} } = {}) => devDependencies
190
216
 
@@ -193,9 +219,9 @@ export const getDevDependencies = ({ devDependencies = {} } = {}) => devDependen
193
219
  *
194
220
  * Get the optional dependencies by destructuring the package or configuration
195
221
  *
196
- * @param {Object} package
222
+ * @param {Package|Configuration}
197
223
  *
198
- * @return {Object}
224
+ * @return {Dependencies}
199
225
  */
200
226
  export const getOptionalDependencies = ({ optionalDependencies = {} } = {}) => optionalDependencies
201
227
 
@@ -204,20 +230,20 @@ export const getOptionalDependencies = ({ optionalDependencies = {} } = {}) => o
204
230
  *
205
231
  * Get the bundle dependencies by destructuring the package or configuration
206
232
  *
207
- * @param {Object} package
233
+ * @param {Package|Configuration}
208
234
  *
209
- * @return {Object}
235
+ * @return {Dependencies}
210
236
  */
211
- export const getBundleDependencies = ({ bundleDependencies = [] } = {}) => bundleDependencies
237
+ export const getBundleDependencies = ({ bundleDependencies = {} } = {}) => bundleDependencies
212
238
 
213
239
  /**
214
240
  * @function getPeerDependencies
215
241
  *
216
242
  * Get the peer dependencies by destructuring the package or configuration
217
243
  *
218
- * @param {Object} package
244
+ * @param {Package|Configuration}
219
245
  *
220
- * @return {Object}
246
+ * @return {Dependencies}
221
247
  */
222
248
  export const getPeerDependencies = ({ peerDependencies } = {}) => peerDependencies
223
249
 
@@ -226,9 +252,9 @@ export const getPeerDependencies = ({ peerDependencies } = {}) => peerDependenci
226
252
  *
227
253
  * Get the ignore flag by destructuring the package or configuration
228
254
  *
229
- * @param {Object} package
255
+ * @param {Package|Configuration}
230
256
  *
231
- * @return {Boolean}
257
+ * @return {boolean}
232
258
  */
233
259
  export const getIgnore = ({ ignore = false } = {}) => ignore === true
234
260
 
@@ -237,20 +263,23 @@ export const getIgnore = ({ ignore = false } = {}) => ignore === true
237
263
  *
238
264
  * Get the author by destructuring the package or configuration
239
265
  *
240
- * @param {Object} package
266
+ * @param {Package|Configuration}
241
267
  *
242
- * @return {String}
268
+ * @return {string|null}
243
269
  */
244
- export const getAuthor = ({ author } = {}) => {
270
+ export const getAuthor = ({ author = '' } = {}) => {
245
271
  if ((author || false) instanceof Object) {
246
272
  const {
247
- name = '',
248
- email = ''
273
+ name: NAME = '',
274
+ email: EMAIL = ''
249
275
  } = author
250
276
 
277
+ const name = String(NAME).trim()
278
+ const email = String(EMAIL).trim()
279
+
251
280
  return (
252
281
  name && email
253
- ? `${name} <${email}>`.trim()
282
+ ? `${name} <${email}>`
254
283
  : null
255
284
  )
256
285
  }
@@ -263,9 +292,9 @@ export const getAuthor = ({ author } = {}) => {
263
292
  *
264
293
  * Determine whether the dependency is a pre-release with a Regular Expression
265
294
  *
266
- * @param {String} v
295
+ * @param {string} v
267
296
  *
268
- * @return {Boolean}
297
+ * @return {boolean}
269
298
  */
270
299
  export const isPreRelease = (v) => /-/.test(v)
271
300
 
@@ -274,9 +303,9 @@ export const isPreRelease = (v) => /-/.test(v)
274
303
  *
275
304
  * Determine whether the dependency is exact with a Regular Expression
276
305
  *
277
- * @param {String} v
306
+ * @param {string} v
278
307
  *
279
- * @return {Boolean}
308
+ * @return {boolean}
280
309
  */
281
310
  export const isExact = (v) => /^\d/.test(v)
282
311
 
@@ -285,19 +314,19 @@ export const isExact = (v) => /^\d/.test(v)
285
314
  *
286
315
  * Get an array of dependencies to be installed with the `--save-exact` parameter
287
316
  *
288
- * @param {Object} values
289
- * @param {Object} configuration
317
+ * @param {Dependencies} packageDependencies - Package dependencies
318
+ * @param {Dependencies} configurationDependencies - Configuration dependencies
290
319
  *
291
- * @return {Array}
320
+ * @return {DependencyDescriptor[]}
292
321
  */
293
- export function getDepsExact (values, configuration) {
322
+ export function getDepsExact (packageDependencies, configurationDependencies) {
294
323
  log('getDepsExact')
295
324
 
296
325
  return (
297
- Object.entries(values)
326
+ Object.entries(packageDependencies)
298
327
  .reduce((accumulator, [name, version]) => (
299
328
  isExact(version)
300
- ? accumulator.concat({ name, version: Reflect.has(configuration, name) ? Reflect.get(configuration, name) : version })
329
+ ? accumulator.concat({ name, version: Reflect.has(configurationDependencies, name) ? Reflect.get(configurationDependencies, name) : version })
301
330
  : accumulator
302
331
  ), [])
303
332
  )
@@ -308,15 +337,15 @@ export function getDepsExact (values, configuration) {
308
337
  *
309
338
  * Get an array of dependencies to be installed
310
339
  *
311
- * @param {Object} values
340
+ * @param {Dependencies} packageDependencies - Package dependencies
312
341
  *
313
- * @return {Array}
342
+ * @return {DependencyDescriptor[]}
314
343
  */
315
- export function getDeps (values) {
344
+ export function getDeps (packageDependencies) {
316
345
  log('getDeps')
317
346
 
318
347
  return (
319
- Object.entries(values)
348
+ Object.entries(packageDependencies)
320
349
  .reduce((accumulator, [name, version]) => (
321
350
  isExact(version)
322
351
  ? accumulator
@@ -332,17 +361,19 @@ export function getDeps (values) {
332
361
  *
333
362
  * Normalise the commands string
334
363
  *
335
- * @param {String} value
364
+ * @param {string} value
336
365
  *
337
- * @return {String}
366
+ * @return {string}
338
367
  */
339
- export function normalizeCommands (value) {
340
- while (/\s\s|\n/.test(value)) {
341
- value = value.replace(/\s\s/gm, String.fromCharCode(32)).replace(/\n/gm, String.fromCharCode(32))
368
+ export function normalizeCommands (commands) {
369
+ const s = String.fromCharCode(32)
370
+
371
+ while (/\s{2,}|\n+/.test(commands)) {
372
+ commands = commands.replace(/\s{2,}/gm, s).replace(/\n+/gm, s)
342
373
  }
343
374
 
344
375
  return (
345
- value.trim()
376
+ commands.trim()
346
377
  )
347
378
  }
348
379
 
@@ -351,9 +382,9 @@ export function normalizeCommands (value) {
351
382
  *
352
383
  * Transform by destructuring the value
353
384
  *
354
- * @param {Object}
385
+ * @param {DependencyDescriptor}
355
386
  *
356
- * @return {String}
387
+ * @return {string}
357
388
  */
358
389
  export const transformDependency = ({ name = '@modernpoacher/deps', version = 'latest' } = {}) => `${name}@${version}`
359
390
 
@@ -362,9 +393,9 @@ export const transformDependency = ({ name = '@modernpoacher/deps', version = 'l
362
393
  *
363
394
  * Transform the parameter to a string
364
395
  *
365
- * @param {(Array|Object)} value
396
+ * @param {DependencyDescriptor|DependencyDescriptor[]} value
366
397
  *
367
- * @return {String}
398
+ * @return {string}
368
399
  */
369
400
  export function transform (value) {
370
401
  log('transform')