mirror of
https://github.com/balena-io/etcher.git
synced 2025-07-23 11:16:39 +00:00
Convert supported-formats.js to typescript
Change-type: patch
This commit is contained in:
parent
23b295c7c1
commit
30c2ef58cd
@ -21,6 +21,7 @@ const _ = require('lodash')
|
||||
const redux = require('redux')
|
||||
const uuidV4 = require('uuid/v4')
|
||||
const constraints = require('../../../shared/drive-constraints')
|
||||
// eslint-disable-next-line node/no-missing-require
|
||||
const supportedFormats = require('../../../shared/supported-formats')
|
||||
const errors = require('../../../shared/errors')
|
||||
// eslint-disable-next-line node/no-missing-require
|
||||
|
@ -20,6 +20,7 @@ const _ = require('lodash')
|
||||
const electron = require('electron')
|
||||
const Bluebird = require('bluebird')
|
||||
const errors = require('../../../shared/errors')
|
||||
// eslint-disable-next-line node/no-missing-require
|
||||
const supportedFormats = require('../../../shared/supported-formats')
|
||||
|
||||
/**
|
||||
|
@ -1,151 +0,0 @@
|
||||
/*
|
||||
* Copyright 2016 balena.io
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
'use strict'
|
||||
|
||||
const sdk = require('etcher-sdk')
|
||||
const _ = require('lodash')
|
||||
const mime = require('mime-types')
|
||||
const path = require('path')
|
||||
|
||||
// eslint-disable-next-line node/no-missing-require
|
||||
const fileExtensions = require('./file-extensions')
|
||||
|
||||
/**
|
||||
* @summary Get compressed extensions
|
||||
* @function
|
||||
* @public
|
||||
*
|
||||
* @returns {String[]} compressed extensions
|
||||
*
|
||||
* @example
|
||||
* _.each(supportedFormats.getCompressedExtensions(), (extension) => {
|
||||
* console.log('We support the ' + extension + ' compressed file format');
|
||||
* });
|
||||
*/
|
||||
exports.getCompressedExtensions = () => {
|
||||
const result = []
|
||||
for (const [ mimetype, cls ] of sdk.sourceDestination.SourceDestination.mimetypes.entries()) {
|
||||
if (cls.prototype instanceof sdk.sourceDestination.CompressedSource) {
|
||||
const extension = mime.extension(mimetype)
|
||||
if (extension) {
|
||||
result.push(extension)
|
||||
}
|
||||
}
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
/**
|
||||
* @summary Get non compressed extensions
|
||||
* @function
|
||||
* @public
|
||||
*
|
||||
* @returns {String[]} no compressed extensions
|
||||
*
|
||||
* @example
|
||||
* _.each(supportedFormats.getNonCompressedExtensions(), (extension) => {
|
||||
* console.log('We support the ' + extension + ' file format');
|
||||
* });
|
||||
*/
|
||||
exports.getNonCompressedExtensions = () => {
|
||||
return sdk.sourceDestination.SourceDestination.imageExtensions
|
||||
}
|
||||
|
||||
/**
|
||||
* @summary Get archive extensions
|
||||
* @function
|
||||
* @public
|
||||
*
|
||||
* @returns {String[]} archive extensions
|
||||
*
|
||||
* @example
|
||||
* _.each(supportedFormats.getArchiveExtensions(), (extension) => {
|
||||
* console.log('We support the ' + extension + ' file format');
|
||||
* });
|
||||
*/
|
||||
exports.getArchiveExtensions = () => {
|
||||
return [ 'zip', 'etch' ]
|
||||
}
|
||||
|
||||
/**
|
||||
* @summary Get all supported extensions
|
||||
* @function
|
||||
* @public
|
||||
*
|
||||
* @returns {String[]} extensions
|
||||
*
|
||||
* @example
|
||||
* _.each(supportedFormats.getAllExtensions(), (extension) => {
|
||||
* console.log('We support the ' + extension + ' format');
|
||||
* });
|
||||
*/
|
||||
exports.getAllExtensions = () => {
|
||||
return [ ...exports.getArchiveExtensions(), ...exports.getNonCompressedExtensions(), ...exports.getCompressedExtensions() ]
|
||||
}
|
||||
|
||||
/**
|
||||
* @summary Check if an image is supported
|
||||
* @function
|
||||
* @public
|
||||
*
|
||||
* @param {String} imagePath - image path
|
||||
* @returns {Boolean} whether the image is supported
|
||||
*
|
||||
* @example
|
||||
* if (supportedFormats.isSupportedImage('foo.iso.bz2')) {
|
||||
* console.log('The image is supported!');
|
||||
* }
|
||||
*/
|
||||
exports.isSupportedImage = (imagePath) => {
|
||||
const lastExtension = fileExtensions.getLastFileExtension(imagePath)
|
||||
const penultimateExtension = fileExtensions.getPenultimateFileExtension(imagePath)
|
||||
|
||||
if (_.some([
|
||||
_.includes(exports.getNonCompressedExtensions(), lastExtension),
|
||||
_.includes(exports.getArchiveExtensions(), lastExtension)
|
||||
])) {
|
||||
return true
|
||||
}
|
||||
|
||||
if (_.every([
|
||||
_.includes(exports.getCompressedExtensions(), lastExtension),
|
||||
_.includes(exports.getNonCompressedExtensions(), penultimateExtension)
|
||||
])) {
|
||||
return true
|
||||
}
|
||||
|
||||
return _.isNil(penultimateExtension) &&
|
||||
_.includes(exports.getCompressedExtensions(), lastExtension)
|
||||
}
|
||||
|
||||
/**
|
||||
* @summary Check if an image seems to be a Windows image
|
||||
* @function
|
||||
* @public
|
||||
*
|
||||
* @param {String} imagePath - image path
|
||||
* @returns {Boolean} whether the image seems to be a Windows image
|
||||
*
|
||||
* @example
|
||||
* if (supportedFormats.looksLikeWindowsImage('path/to/en_windows_7_ultimate_with_sp1_x86_dvd_u_677460.iso')) {
|
||||
* console.log('Looks like a Windows image');
|
||||
* }
|
||||
*/
|
||||
exports.looksLikeWindowsImage = (imagePath) => {
|
||||
const regex = /windows|win7|win8|win10|winxp/i
|
||||
return regex.test(path.basename(imagePath))
|
||||
}
|
91
lib/shared/supported-formats.ts
Normal file
91
lib/shared/supported-formats.ts
Normal file
@ -0,0 +1,91 @@
|
||||
/*
|
||||
* Copyright 2016 balena.io
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
import * as sdk from 'etcher-sdk';
|
||||
import * as _ from 'lodash';
|
||||
import * as mime from 'mime-types';
|
||||
import * as path from 'path';
|
||||
|
||||
import {
|
||||
getLastFileExtension,
|
||||
getPenultimateFileExtension,
|
||||
} from './file-extensions';
|
||||
|
||||
export function getCompressedExtensions(): string[] {
|
||||
const result = [];
|
||||
for (const [
|
||||
mimetype,
|
||||
cls,
|
||||
// @ts-ignore (mimetypes is private)
|
||||
] of sdk.sourceDestination.SourceDestination.mimetypes.entries()) {
|
||||
if (cls.prototype instanceof sdk.sourceDestination.CompressedSource) {
|
||||
const extension = mime.extension(mimetype);
|
||||
if (extension) {
|
||||
result.push(extension);
|
||||
}
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
export function getNonCompressedExtensions(): string[] {
|
||||
return sdk.sourceDestination.SourceDestination.imageExtensions;
|
||||
}
|
||||
|
||||
export function getArchiveExtensions(): string[] {
|
||||
return ['zip', 'etch'];
|
||||
}
|
||||
|
||||
export function getAllExtensions(): string[] {
|
||||
return [
|
||||
...getArchiveExtensions(),
|
||||
...getNonCompressedExtensions(),
|
||||
...getCompressedExtensions(),
|
||||
];
|
||||
}
|
||||
|
||||
export function isSupportedImage(imagePath: string): boolean {
|
||||
const lastExtension = getLastFileExtension(imagePath);
|
||||
const penultimateExtension = getPenultimateFileExtension(imagePath);
|
||||
|
||||
if (
|
||||
_.some([
|
||||
_.includes(getNonCompressedExtensions(), lastExtension),
|
||||
_.includes(getArchiveExtensions(), lastExtension),
|
||||
])
|
||||
) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (
|
||||
_.every([
|
||||
_.includes(getCompressedExtensions(), lastExtension),
|
||||
_.includes(getNonCompressedExtensions(), penultimateExtension),
|
||||
])
|
||||
) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return (
|
||||
_.isNil(penultimateExtension) &&
|
||||
_.includes(getCompressedExtensions(), lastExtension)
|
||||
);
|
||||
}
|
||||
|
||||
export function looksLikeWindowsImage(imagePath: string): boolean {
|
||||
const regex = /windows|win7|win8|win10|winxp/i;
|
||||
return regex.test(path.basename(imagePath));
|
||||
}
|
@ -18,6 +18,7 @@
|
||||
|
||||
const m = require('mochainon')
|
||||
const _ = require('lodash')
|
||||
// eslint-disable-next-line node/no-missing-require
|
||||
const supportedFormats = require('../../lib/shared/supported-formats')
|
||||
|
||||
describe('Shared: SupportedFormats', function () {
|
||||
|
Loading…
x
Reference in New Issue
Block a user