【NPM库】- 0x05 - 文件、路径操作
目录
1. importLocal
1.1. webpack-dev-server 的一个特性
1.2. __filename、__dirname
1.3. path.dirname、path.join、path.resolve、path.relative
1.4. process.cwd()
1.5. find-up
1.6. pkg-dir
1.7. import-local
2. rimraf
3. glob
3.1. glob 模式
3.2. glob 起源
3.3. glob 示例
4. globby
1. importLocal
1.1. webpack-dev-server 的一个特性
webpack-dev-server 官网描述了其 CLI 的一个特性:
webpack-dev-server will always use a local installation over a global one. https://github.com/webpack/webpack-dev-server
看看效果:
看看 webpack-dev-server 实现:
看看 import-local 实现:
分析一波原理:...
1.2. __filename、__dirname
- __filename:当前模块的文件名。这是当前的模块文件的绝对路径。
- __dirname:当前模块的目录名。
示例:
1.3. path.dirname、path.join、path.resolve、path.relative
- path.dirname(path)
- path.dirname() 方法会返回 path 的目录名。
- path.join([...paths])
- The path.join() method joins all given path segments together using the platform-specific separator as a delimiter, then normalizes the resulting path.
- Zero-length path segments are ignored. If the joined path string is a zero-length string then '.' will be returned, representing the current working directory.
- path.resolve([...paths])
- The path.resolve() method resolves a sequence of paths or path segments into an absolute path.
- path.relative(from, to)
- The path.relative() method returns the relative path from from to to based on the current working directory. If from and to each resolve to the same path (after calling path.resolve() on each), a zero-length string is returned.
示例:
const path = require("path");
console.log(path.dirname(__filename));
const p1 = path.join("/foo", "bar", "baz/asdf", "quuz", "..");
console.log(p1);
const p2 = path.join("./a", "./b");
console.log(p2);
const p3 = path.resolve("/foo/bar", "/tmp/file/");
console.log(p3);
const p4 = path.resolve("wwwroot", "staticfiles/png/", "../gif/image.gif");
console.log(p4);
const p5= path.relative('/data/orandea/test/aaa', '/data/orandea/impl/bbb');
console.log(p5);
1.4. process.cwd()
- The process.cwd() method returns the current working directory of the Node.js process.
示例:
1.5. find-up
- Find a file or directory by walking up parent directories.
示例:
const findUp = require('find-up');
(async () => {
console.log(await findUp('unicorn.png'));
console.log(await findUp(['rainbow.png', 'unicorn.png']));
console.log(await findUp("unicorn.png", {cwd: __dirname}))
})();
1.6. pkg-dir
- Find the root directory of a Node.js project or npm package
源码:
示例:
const pkgDir = require('pkg-dir');
(async () => {
const dir = await pkgDir(__dirname);
console.log(dir);
})();
1.7. import-local
- Let a globally installed package use a locally installed version of itself if available
示例:
const importLocal = require('import-local');
if (importLocal(__filename)) {
console.log('Using local version of this package');
} else {
// Code for both global and local version here…
}
原理:
2. rimraf
rimraf 是 Linux 命令 rm -rf 的 node 版本。就是用来删除目录、删除文件的。
示例:
3. glob
3.1. glob 模式
In computer programming, glob patterns specify sets of filenames with wildcard characters.
mv *.txt textfiles/ moves
moves (mv) all files with names ending in .txt from the current directory to the directory textfiles.
—— 来源于 Wiki
3.2. glob 起源
The command interpreters of the early versions of Unix relied on a separate program to expand wildcard characters in unquoted arguments to a command: /etc/glob. That program performed the expansion and supplied the expanded list of file paths to the command for execution. Its name is an abbreviation for "global command". Later, this functionality was provided as a library function, glob(), used by programs such as the shell.
—— 来源于 Wiki
3.3. glob 示例
4. globby
globby 是 glob 的增强版本
示例:
参考:
import-local: https://github.com/sindresorhus/import-local process.cwd(): https://nodejs.org/api/process.html#process_process_cwd find-up: https://github.com/sindresorhus/find-up resolve-cwd: https://github.com/sindresorhus/resolve-cwd pkg-dir: https://github.com/sindresorhus/pkg-dir
- Keras中神经网络模型的5阶段生命周期
- java的断言(assert)
- Android studio中Rendering Problems不能可视化操作的解决办法
- 使用 Referer Meta 标签控制 referer—详解 referrer-policy
- 网站抓取引子 - 获得网页中的表格
- Android Firebase 服务简介
- CVE-2015-0393:Oracle发布严重安全漏洞预警
- 研究人员发现一种利用Siri窃取苹果iPhone/iPad数据的方法
- 关于Python中的__main__和编程模板
- 世界大战尽在掌控:盘点全球网络攻击实时追踪系统
- Activity数据传递
- apache反向代理一、泛解析域名二、APACHE配置
- Python学习 - 可视化变量赋值、循环、程序运行过程
- jdk源码分析红黑树——插入篇1.插入root2.父黑3.父红4.父红,叔红5.1父红,叔黑,外侧子孙5.2父红,叔黑,内侧子孙
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- redis源码之set结构
- redis源码之zset结构的实现
- Elasticsearch: range 数据类型及基于range的聚合 (7.4发行版新功能)
- 漫画:如何在数组中找到和为 “特定值” 的三个数?
- 除了MySQL,大牛DBA还会啥?
- 用 Docker swarm 快速部署分布式图数据库 Nebula Graph 集群
- Labelhub 基于腾讯云 Serverless 技术为人工智能企业提供数据与模型解决方案
- 手把手教你使用 Prometheus 监控 JVM
- 基于云开发 CloudBase 搭建在线视频会议应用
- 手搓一个分布式大气监测系统(六)云端能力更新、说明及源码放出
- maybe incorrect parameters such as bit_rate, rate, width or height
- vue 怎么将Checkbox 多选框选中的值提交
- vue-element怎么给select下拉框赋值?
- 小程序生成二维码海报的组件-wxa-plugin-canvas
- kbone 是什么?这可能是最好的小程序开源框架