Detect extension in a directory using node js

栏目: IT技术 · 发布时间: 4年前

内容简介:Reading files from any package is essential before production. But since we as developers love automating things. Validating package accessibility and detect extensions among other things should be done by code.The good news is, this can be achieved easily

Reading files from any package is essential before production. But since we as developers love automating things. Validating package accessibility and detect extensions among other things should be done by code.

The good news is, this can be achieved easily in the node file system .

let's start by working on getting a file extension. We have input: filename.extension splitting the input by "." should achieve our goal easily.

`filename.extension`.split(".") 
// (2) ["filename", "extension"]

`filename.test.extension`.split(".");
// (3) ["filename", "test", "extension"]

As you notice, we need to get the last element of the result. This can be done in different ways. One of them is using pop which returns the last element of the array.

function getExtension(fileFullName) {
  return fileFullName.split(".").pop();
}

We still have a problem. We don't have the file full name. We actually have to auto-detect the extension by knowing the project root.

The first step, using readdirSync to read all files that exist in our directory.

const files = fs.readdirSync(dir);

// (4) [ 'a.js', 'b.js', 'index.js', 'package.json' ]

Then, we can use find which returns the value of the first element that satisfies function result.

const found = [10, 20, 30].find((number) => number > 10);
// 20

Now let's update our function

import fs from "fs";

function getExtension(rootDir, entry) {
  const files = fs.readdirSync(rootDir);
  // (4) [ 'a.js', 'b.ts', 'index.js', 'README.md', 'package.json' ]

  const filename = files.find((file) => {
    // return the first files that include given entry
    return file.includes(entry);
  });

  const extension = filename.split(".").pop();

  return extension;
}

// reading form current directory
const result = getExtension(".", "b");

// result: ts

We can still upgrade our function, as we usually have an index as default entry from the current directory.

function getExtension(rootDir = ".", entry = "index") {
  //
}

const result = getExtension();

// result: js

Our mission is not finished yet, but that's enough for now. Next, we will build validate together.

See you soon!

Do you like it? Please leave :star:️. I appreciate any feedback :wave::wave::wave:


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

新媒体文案创作与传播

新媒体文案创作与传播

秋叶、叶小鱼、勾俊伟 / 人民邮电出版社 / 2017-4 / 39.80元

《新媒体文案创作与传播》共分三篇。第1篇是新媒体文案基础篇,主要讲述了新媒体文案的基本概念、新媒体文案的岗位要求和职业能力素养;第二篇是新媒体文案创意实务篇,主要讲述了新媒体文案的创作思路、新媒体文案的写作技巧、爆款新媒体文案的打造、新媒体销售文案的写作、新媒体对文案传播的新要求、新媒体品-牌文案的写作,以及不同媒介的特征及发布形式;第三篇为新媒体文案相关技能补充,主要讲述的是策划能力。 《新媒体......一起来看看 《新媒体文案创作与传播》 这本书的介绍吧!

在线进制转换器
在线进制转换器

各进制数互转换器

SHA 加密
SHA 加密

SHA 加密工具

RGB HSV 转换
RGB HSV 转换

RGB HSV 互转工具