【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
- 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 数组属性和方法
- 分享给您一个快速离职的 SHELL 脚本
- ASP.NET WebAPI 中的参数绑定
- Qt音视频开发7-ffmpeg音频播放
- 本地 Docker Registry 的安装与使用
- 安装 Docker 环境
- 为生产环境编译 Angular 2 应用
- 剑指offer 03:二维数组中的查找
- 《剑指offer》04:替换空格
- 《剑指offer》05: 从尾到头打印链表
- 《剑指offer》第六天:重建二叉树
- Android | 《看完不忘系列》之Glide
- Android | 资源冲突覆盖的一些思考
- 如何获取流式应用程序中checkpoint的最新offset
- Spark之离线统计热点城市信息
- 使用OpenCV实现图像增强