feat(build): add getFileExtension util

This commit is contained in:
Sam Tolmay 2020-10-20 12:01:46 +02:00
parent 127eb39713
commit 46586f05f2
6 changed files with 151 additions and 98 deletions

View File

@ -4,7 +4,7 @@
"@babel/preset-env",
{
"targets": {
"node": "14"
"node": "12"
}
}
]

View File

@ -13,6 +13,7 @@
See the License for the specific language governing permissions and
limitations under the License.
*/
import fs from 'fs';
import path from 'path';
import cleanDirectory from './cleanDirectory';

View File

@ -18,14 +18,7 @@ import JSON5 from 'json5';
import YAML from 'js-yaml';
import { type } from '@lowdefy/helpers';
import readFile from './readFile';
function getExt(filePath) {
const arr = filePath.split('.');
if (arr.length === 1) {
return null;
}
return arr[arr.length - 1];
}
import getFileExtension from './getFileExtension';
async function getJsonFile(filePath) {
const file = await readFile(filePath);
@ -52,7 +45,7 @@ async function getTextFile(filePath) {
}
async function handleFileType(filePath) {
const ext = getExt(filePath);
const ext = getFileExtension(filePath);
switch (ext) {
case 'yaml':
case 'yml':
@ -73,6 +66,4 @@ async function getFile(filePath) {
);
}
export { getExt };
export default getFile;

View File

@ -1,6 +1,3 @@
/* eslint-disable no-unused-vars */
/* eslint-disable no-undef */
/*
Copyright 2020 Lowdefy, Inc
@ -18,101 +15,85 @@
*/
import path from 'path';
import getFile, { getExt } from './getFile';
import getFile from './getFile';
describe('getExt', () => {
test('getExt a.b', () => {
expect(getExt('a.b')).toBe('b');
});
test('getExt a.b.c', () => {
expect(getExt('a.b.c')).toBe('c');
});
test('getExt a', () => {
expect(getExt('a')).toBe(null);
test('getFile jsonFile.json', async () => {
const filePath = path.resolve(process.cwd(), 'src/test/jsonFile.json');
const file = await getFile(filePath);
expect(file).toEqual({
filePath,
content: {
fileName: 'jsonFile.json',
test: true,
},
});
});
describe('getFile', () => {
test('getFile jsonFile.json', async () => {
const filePath = path.resolve(process.cwd(), 'src/test/jsonFile.json');
const file = await getFile(filePath);
expect(file).toEqual({
filePath,
content: {
fileName: 'jsonFile.json',
test: true,
},
});
test('getFile yamlFile.yaml', async () => {
const filePath = path.resolve(process.cwd(), 'src/test/yamlFile.yaml');
const file = await getFile(filePath);
expect(file).toEqual({
filePath,
content: {
fileName: 'yamlFile.yaml',
test: true,
},
});
});
test('getFile yamlFile.yaml', async () => {
const filePath = path.resolve(process.cwd(), 'src/test/yamlFile.yaml');
const file = await getFile(filePath);
expect(file).toEqual({
filePath,
content: {
fileName: 'yamlFile.yaml',
test: true,
},
});
test('getFile ymlFile.yml', async () => {
const filePath = path.resolve(process.cwd(), 'src/test/ymlFile.yml');
const file = await getFile(filePath);
expect(file).toEqual({
filePath,
content: {
fileName: 'ymlFile.yml',
test: true,
},
});
});
test('getFile ymlFile.yml', async () => {
const filePath = path.resolve(process.cwd(), 'src/test/ymlFile.yml');
const file = await getFile(filePath);
expect(file).toEqual({
filePath,
content: {
fileName: 'ymlFile.yml',
test: true,
},
});
});
test('getFile doesNotExist.txt', async () => {
const filePath = path.resolve(process.cwd(), 'src/test/doesNotExist.txt');
await expect(getFile(filePath)).rejects.toThrow('ENOENT: no such file or directory');
});
test('getFile doesNotExist.txt', async () => {
const filePath = path.resolve(process.cwd(), 'src/test/doesNotExist.txt');
await expect(getFile(filePath)).rejects.toThrow('ENOENT: no such file or directory');
});
test('getFile null', async () => {
await expect(getFile(null)).rejects.toThrow(
'Tried to get file with file path null, but file path should be a string'
);
});
test('getFile null', async () => {
await expect(getFile(null)).rejects.toThrow(
'Tried to get file with file path null, but file path should be a string'
);
});
test('getFile markdown.md', async () => {
const filePath = path.resolve(process.cwd(), 'src/test/markdown.md');
const file = await getFile(filePath);
expect(file).toEqual({
filePath,
content: `### Title
test('getFile markdown.md', async () => {
const filePath = path.resolve(process.cwd(), 'src/test/markdown.md');
const file = await getFile(filePath);
expect(file).toEqual({
filePath,
content: `### Title
Hello there
`,
});
});
test('getFile html.html', async () => {
const filePath = path.resolve(process.cwd(), 'src/test/html.html');
const file = await getFile(filePath);
expect(file).toEqual({
filePath,
content: `<h1>Heading</h1>
<p>Hello there</p>
`,
});
});
test('getFile text.txt', async () => {
const filePath = path.resolve(process.cwd(), 'src/test/text.txt');
const file = await getFile(filePath);
expect(file).toEqual({
filePath,
content: `Hello
This is a txt file.`,
});
});
});
test('getFile html.html', async () => {
const filePath = path.resolve(process.cwd(), 'src/test/html.html');
const file = await getFile(filePath);
expect(file).toEqual({
filePath,
content: `<h1>Heading</h1>
<p>Hello there</p>
`,
});
});
test('getFile text.txt', async () => {
const filePath = path.resolve(process.cwd(), 'src/test/text.txt');
const file = await getFile(filePath);
expect(file).toEqual({
filePath,
content: `Hello
This is a txt file.`,
});
});

View File

@ -0,0 +1,35 @@
/*
Copyright 2020 Lowdefy, Inc
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.
*/
function getFileExtension(filePath) {
const arr = filePath.split('.');
if (arr.length === 1) {
return null;
}
return arr[arr.length - 1];
}
function getFileSubExtension(path) {
const arr = path.split('.');
if (arr.length < 3) {
return null;
}
return arr[arr.length - 2];
}
export { getFileSubExtension };
export default getFileExtension;

View File

@ -0,0 +1,45 @@
/*
Copyright 2020 Lowdefy, Inc
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 getFileExtension, { getFileSubExtension } from './getFileExtension';
test('getFileExtension a', () => {
expect(getFileExtension('a')).toBe(null);
});
test('getFileExtension a.b', () => {
expect(getFileExtension('a.b')).toBe('b');
});
test('getFileExtension a.b.c', () => {
expect(getFileExtension('a.b.c')).toBe('c');
});
test('getFileSubExtension a', () => {
expect(getFileSubExtension('a')).toBe(null);
});
test('getFileSubExtension a.b', () => {
expect(getFileSubExtension('a.b')).toBe(null);
});
test('getFileSubExtension a.b.c', () => {
expect(getFileSubExtension('a.b.c')).toBe('b');
});
test('getFileSubExtension a.b.c.d', () => {
expect(getFileSubExtension('a.b.c.d')).toBe('c');
});