Skip to content

Commit 21b9f7d

Browse files
authored
refactor: replace node.js module imports with own implementations (#302)
1 parent 3c489b4 commit 21b9f7d

File tree

7 files changed

+45
-13
lines changed

7 files changed

+45
-13
lines changed

lib/file/getFileBlobAsync.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
import { extname } from 'path';
2-
31
import { ENCODING, FILE_EXTENSION } from './constants';
42

53
import { wrapBlob } from './common';
4+
import getFileExt from './getFileExt';
65

76
const ENCODING_TO_BLOB_GETTER = {
87
[ENCODING.ASCII]: fileContent => Cypress.Promise.resolve(fileContent),
@@ -25,7 +24,7 @@ export default function getFileBlobAsync({ fileName, fileContent, mimeType, enco
2524
let blobContent = blob;
2625

2726
// https://github.com/abramenal/cypress-file-upload/issues/175
28-
if (extname(fileName).slice(1) === FILE_EXTENSION.JSON) {
27+
if (getFileExt(fileName) === FILE_EXTENSION.JSON) {
2928
blobContent = JSON.stringify(fileContent, null, 2);
3029
}
3130

lib/file/getFileEncoding.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
import { extname } from 'path';
2-
31
import { ENCODING, FILE_EXTENSION } from './constants';
2+
import getFileExt from './getFileExt';
43

54
/*
65
* Copied from https://github.com/cypress-io/cypress/blob/develop/packages/server/lib/fixture.coffee#L104
@@ -36,7 +35,7 @@ const EXTENSION_TO_ENCODING = {
3635
const DEFAULT_ENCODING = ENCODING.UTF8;
3736

3837
export default function getFileEncoding(filePath) {
39-
const extension = extname(filePath).slice(1);
38+
const extension = getFileExt(filePath);
4039
const encoding = EXTENSION_TO_ENCODING[extension];
4140

4241
return encoding || DEFAULT_ENCODING;

lib/file/getFileExt.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
export default function getFileExt(filePath) {
2+
if (!filePath) {
3+
return '';
4+
}
5+
6+
const pos = filePath.lastIndexOf('.');
7+
8+
if (pos === -1) {
9+
return '';
10+
}
11+
12+
return filePath.slice(pos + 1);
13+
}

lib/file/getFileMimeType.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import { extname } from 'path';
2-
31
// eslint-disable-next-line import/no-extraneous-dependencies
42
import { getType } from 'mime';
53

4+
import getFileExt from './getFileExt';
5+
66
export default function getFileMimeType(filePath) {
7-
const extension = extname(filePath).slice(1);
7+
const extension = getFileExt(filePath);
88
const mimeType = getType(extension);
99

1010
return mimeType;

lib/file/getFileName.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
const UNIX_SEP = '/';
2+
const WIN_SEP = '\\';
3+
4+
export default function getFileName(filePath) {
5+
if (!filePath) {
6+
return '';
7+
}
8+
9+
let indexSep = filePath.lastIndexOf(UNIX_SEP);
10+
if (indexSep === -1) {
11+
indexSep = filePath.lastIndexOf(WIN_SEP);
12+
}
13+
14+
if (indexSep === -1) {
15+
return filePath;
16+
}
17+
18+
return filePath.slice(indexSep + 1);
19+
}

lib/file/index.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
export { default as getFileBlobAsync } from './getFileBlobAsync';
2+
export { default as getFileContent } from './getFileContent';
23
export { default as getFileEncoding } from './getFileEncoding';
4+
export { default as getFileExt } from './getFileExt';
35
export { default as getFileMimeType } from './getFileMimeType';
4-
export { default as getFileContent } from './getFileContent';
6+
export { default as getFileName } from './getFileName';
57
export { default as resolveFile } from './resolveFile';

src/helpers/getFixtureInfo.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
1-
import path from 'path';
1+
import { getFileName } from '../../lib/file';
22

33
export default function getFixtureInfo(fixtureInput) {
44
if (typeof fixtureInput === 'string') {
55
return {
66
filePath: fixtureInput,
77
encoding: '',
88
mimeType: '',
9-
fileName: path.basename(fixtureInput),
9+
fileName: getFileName(fixtureInput),
1010
};
1111
}
1212

1313
return {
1414
filePath: fixtureInput.filePath,
1515
encoding: fixtureInput.encoding || '',
1616
mimeType: fixtureInput.mimeType || '',
17-
fileName: fixtureInput.fileName || path.basename(fixtureInput.filePath),
17+
fileName: fixtureInput.fileName || getFileName(fixtureInput.filePath),
1818
fileContent: fixtureInput.fileContent,
1919
lastModified: fixtureInput.lastModified,
2020
};

0 commit comments

Comments
 (0)