mirror of
https://github.com/balena-io/etcher.git
synced 2025-07-24 19:56:37 +00:00
Convert file-extensions.js to typescript
Change-type: patch
This commit is contained in:
parent
db24ee4d37
commit
23b295c7c1
@ -23,6 +23,7 @@ const uuidV4 = require('uuid/v4')
|
|||||||
const constraints = require('../../../shared/drive-constraints')
|
const constraints = require('../../../shared/drive-constraints')
|
||||||
const supportedFormats = require('../../../shared/supported-formats')
|
const supportedFormats = require('../../../shared/supported-formats')
|
||||||
const errors = require('../../../shared/errors')
|
const errors = require('../../../shared/errors')
|
||||||
|
// eslint-disable-next-line node/no-missing-require
|
||||||
const fileExtensions = require('../../../shared/file-extensions')
|
const fileExtensions = require('../../../shared/file-extensions')
|
||||||
const utils = require('../../../shared/utils')
|
const utils = require('../../../shared/utils')
|
||||||
const settings = require('./settings')
|
const settings = require('./settings')
|
||||||
|
@ -14,63 +14,50 @@
|
|||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
'use strict'
|
import * as _ from 'lodash';
|
||||||
|
import { lookup } from 'mime-types';
|
||||||
const mime = require('mime-types')
|
|
||||||
const _ = require('lodash')
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @summary Get the extensions of a file
|
* @summary Get the extensions of a file
|
||||||
* @function
|
|
||||||
* @public
|
|
||||||
*
|
|
||||||
* @param {String} filePath - file path
|
|
||||||
* @returns {String[]} extensions
|
|
||||||
*
|
*
|
||||||
* @example
|
* @example
|
||||||
* const extensions = fileExtensions.getFileExtensions('path/to/foo.img.gz');
|
* const extensions = fileExtensions.getFileExtensions('path/to/foo.img.gz');
|
||||||
* console.log(extensions);
|
* console.log(extensions);
|
||||||
* > [ 'img', 'gz' ]
|
* > [ 'img', 'gz' ]
|
||||||
*/
|
*/
|
||||||
exports.getFileExtensions = _.memoize((filePath) => {
|
export function getFileExtensions(filePath: string): string[] {
|
||||||
return _.chain(filePath)
|
return _.chain(filePath)
|
||||||
.split('.')
|
.split('.')
|
||||||
.tail()
|
.tail()
|
||||||
.map(_.toLower)
|
.map(_.toLower)
|
||||||
.value()
|
.value();
|
||||||
})
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @summary Get the last file extension
|
* @summary Get the last file extension
|
||||||
* @function
|
|
||||||
* @public
|
|
||||||
*
|
|
||||||
* @param {String} filePath - file path
|
|
||||||
* @returns {(String|Null)} last extension
|
|
||||||
*
|
*
|
||||||
* @example
|
* @example
|
||||||
* const extension = fileExtensions.getLastFileExtension('path/to/foo.img.gz');
|
* const extension = fileExtensions.getLastFileExtension('path/to/foo.img.gz');
|
||||||
* console.log(extension);
|
* console.log(extension);
|
||||||
* > 'gz'
|
* > 'gz'
|
||||||
*/
|
*/
|
||||||
exports.getLastFileExtension = (filePath) => {
|
export function getLastFileExtension(filePath: string): string | null {
|
||||||
return _.last(exports.getFileExtensions(filePath)) || null
|
return _.last(getFileExtensions(filePath)) || null;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @summary Get the penultimate file extension
|
* @summary Get the penultimate file extension
|
||||||
* @function
|
|
||||||
* @public
|
|
||||||
*
|
|
||||||
* @param {String} filePath - file path
|
|
||||||
* @returns {(String|Null)} penultimate extension
|
|
||||||
*
|
*
|
||||||
* @example
|
* @example
|
||||||
* const extension = fileExtensions.getPenultimateFileExtension('path/to/foo.img.gz');
|
* const extension = fileExtensions.getPenultimateFileExtension('path/to/foo.img.gz');
|
||||||
* console.log(extension);
|
* console.log(extension);
|
||||||
* > 'img'
|
* > 'img'
|
||||||
*/
|
*/
|
||||||
exports.getPenultimateFileExtension = (filePath) => {
|
export function getPenultimateFileExtension(filePath: string): string | null {
|
||||||
const ext = _.last(_.initial(exports.getFileExtensions(filePath)))
|
const extensions = getFileExtensions(filePath);
|
||||||
return !_.isNil(ext) && mime.lookup(ext) ? ext : null
|
if (extensions.length >= 2) {
|
||||||
|
const ext = extensions[extensions.length - 2];
|
||||||
|
return lookup(ext) ? ext : null;
|
||||||
|
}
|
||||||
|
return null;
|
||||||
}
|
}
|
@ -21,6 +21,7 @@ const _ = require('lodash')
|
|||||||
const mime = require('mime-types')
|
const mime = require('mime-types')
|
||||||
const path = require('path')
|
const path = require('path')
|
||||||
|
|
||||||
|
// eslint-disable-next-line node/no-missing-require
|
||||||
const fileExtensions = require('./file-extensions')
|
const fileExtensions = require('./file-extensions')
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
6
npm-shrinkwrap.json
generated
6
npm-shrinkwrap.json
generated
@ -1186,6 +1186,12 @@
|
|||||||
"resolved": "https://registry.npmjs.org/@types/marked/-/marked-0.3.0.tgz",
|
"resolved": "https://registry.npmjs.org/@types/marked/-/marked-0.3.0.tgz",
|
||||||
"integrity": "sha512-CSf9YWJdX1DkTNu9zcNtdCcn6hkRtB5ILjbhRId4ZOQqx30fXmdecuaXhugQL6eyrhuXtaHJ7PHI+Vm7k9ZJjg=="
|
"integrity": "sha512-CSf9YWJdX1DkTNu9zcNtdCcn6hkRtB5ILjbhRId4ZOQqx30fXmdecuaXhugQL6eyrhuXtaHJ7PHI+Vm7k9ZJjg=="
|
||||||
},
|
},
|
||||||
|
"@types/mime-types": {
|
||||||
|
"version": "2.1.0",
|
||||||
|
"resolved": "https://registry.npmjs.org/@types/mime-types/-/mime-types-2.1.0.tgz",
|
||||||
|
"integrity": "sha1-nKUs2jY/aZxpRmwqbM2q2RPqenM=",
|
||||||
|
"dev": true
|
||||||
|
},
|
||||||
"@types/minimatch": {
|
"@types/minimatch": {
|
||||||
"version": "3.0.3",
|
"version": "3.0.3",
|
||||||
"resolved": "https://registry.npmjs.org/@types/minimatch/-/minimatch-3.0.3.tgz",
|
"resolved": "https://registry.npmjs.org/@types/minimatch/-/minimatch-3.0.3.tgz",
|
||||||
|
@ -94,6 +94,7 @@
|
|||||||
"@babel/plugin-proposal-function-bind": "^7.2.0",
|
"@babel/plugin-proposal-function-bind": "^7.2.0",
|
||||||
"@babel/preset-env": "^7.6.0",
|
"@babel/preset-env": "^7.6.0",
|
||||||
"@babel/preset-react": "^7.0.0",
|
"@babel/preset-react": "^7.0.0",
|
||||||
|
"@types/mime-types": "^2.1.0",
|
||||||
"@types/node": "^12.12.24",
|
"@types/node": "^12.12.24",
|
||||||
"@types/react-dom": "^16.8.4",
|
"@types/react-dom": "^16.8.4",
|
||||||
"babel-loader": "^8.0.4",
|
"babel-loader": "^8.0.4",
|
||||||
|
@ -18,6 +18,7 @@
|
|||||||
|
|
||||||
const m = require('mochainon')
|
const m = require('mochainon')
|
||||||
const _ = require('lodash')
|
const _ = require('lodash')
|
||||||
|
// eslint-disable-next-line node/no-missing-require
|
||||||
const fileExtensions = require('../../lib/shared/file-extensions')
|
const fileExtensions = require('../../lib/shared/file-extensions')
|
||||||
|
|
||||||
describe('Shared: fileExtensions', function () {
|
describe('Shared: fileExtensions', function () {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user