app-lib-node
v1.0.34
Published
nodejs工具类库-【app-lib-node】
Readme
二、功能描述
三、注意事项
1: mack运行demo报异常,需要mac安装powershell let isExistPort = res.stdout.toString();
六、API
Constants
Functions
NODE_INNER_MODULES
nodejs 内置所有模块
- 常用于 webpack 编译 umd 排除使用
Kind: global constant
isExistFile(path) ⇒ boolean
同步判断磁盘文件是否存在
Kind: global function
Returns: boolean - 是否存在 true 存在 false 不存在
| Param | Type | Description | | --- | --- | --- | | path | string | 相对路径 |
resolve(pathName) ⇒ string
相对路径获取的据对路径
Kind: global function
Returns: string - 据对路径地址
| Param | Type | Description | | --- | --- | --- | | pathName | string | 相对路径地址 |
isGlobalInstall() ⇒ boolean
判断当前命令是否未全局安装
- 向上两层判断是否存在 node.exe
Kind: global function
Returns: boolean - 是否为全局安装
isExistBin(relativePath, name) ⇒ boolean
是否存在 bind
Kind: global function
Returns: boolean - 是否存在 bin
| Param | Type | Description | | --- | --- | --- | | relativePath | string | 相对路径 | | name | string | 组件名称 |
getBin(compnentName) ⇒ string
获取 nodejs 的 bin 执行位置
Kind: global function
Returns: string - 组件的路径
- tips
- 开发模式直接获取当前目录的下的指定配置路径
- 局部查找
- 全局组件内部
- 全局外部
| Param | Type | Description | | --- | --- | --- | | compnentName | string | 组件名称 |
isDev() ⇒ boolean
是否为 app 定义的开发模式
Kind: global function
Returns: boolean - 是否为开发模式
- tips
- cross-env 中不支持中划线 eg:APP-MODLE
- MODE 或在 M 值为 DEV 或者 DEVELOP, develop,dev
- 主要用于该工程的开发模式
getTemplate() ⇒ string
获取模版位置
Kind: global function
Returns: string - 模版位置
readFile(path, options) ⇒ promise
读文件【异步】
Kind: global function
Returns: promise - data | 读取的文件内容
- tips
- string|Buffer|URL|integer filename or file descriptor
- https://nodejs.org/docs/latest-v13.x/api/fs.html#fs_fs_readfile_path_options_callback
| Param | Type | Description | | --- | --- | --- | | path | string | Buffer | URL | integer | 文件路径 | | options | object | ndoejs 的配置 |
readFileSync(path, options) ⇒ string
读文件【同步】
Kind: global function
Returns: string - 文件内容
- tips
- string|Buffer|URL|integer filename or file descriptor
- https://nodejs.org/docs/latest-v13.x/api/fs.html#fs_fs_readfile_path_options_callback
| Param | Type | Description | | --- | --- | --- | | path | string | Buffer | URL | integer | 文件路径 | | options | object | ndoejs 的配置 |
writeFile(path, options) ⇒ promise
写文件【异步】
Kind: global function
Returns: promise - data | 读取的文件内容
- tips
- string|Buffer|URL|integer filename or file descriptor
- https://nodejs.org/docs/latest-v13.x/api/fs.html#fs_fs_readfile_path_options_callback
| Param | Type | Description | | --- | --- | --- | | path | string | Buffer | URL | integer | 文件路径 | | options | object | ndoejs 的配置 |
writeFileSync(path, options) ⇒ string
写文件【同步】
Kind: global function
Returns: string - 文件内容
- tips
- string|Buffer|URL|integer filename or file descriptor
- https://nodejs.org/docs/latest-v13.x/api/fs.html#fs_fs_readfile_path_options_callback
| Param | Type | Description | | --- | --- | --- | | path | string | Buffer | URL | integer | 文件路径 | | options | object | ndoejs 的配置 |
writeFileSyncAndCreate(path, options) ⇒ string
写文件【同步】不存在文件夹则创建
Kind: global function
Returns: string - 文件内容
| Param | Type | Description | | --- | --- | --- | | path | string | Buffer | URL | integer | 文件路径 | | options | object | ndoejs 的配置 |
isExistDir(path) ⇒ boolean
是否为文件夹
Kind: global function
Returns: boolean - 是否为文件夹
| Param | Type | Description | | --- | --- | --- | | path | string | 文件夹路径 |
getParentDir(path, dirName, layer) ⇒ object
获取指定路径的父级目录包含的文件夹名
Kind: global function
Returns: object - 找的父级的路径
- example
{
path : null 标识不存在 否则标识存在的路径
layer: number 查到的层级
}| Param | Type | Default | Description | | --- | --- | --- | --- | | path | string | | 当前的路径 | | dirName | string | | 文件夹名称 | | layer | number | 0 | 当前目录到找到目录目录的层级 |
copyFile(src, dest, isForce, flags) ⇒ promise
复制文件
- 不存在目录则创建 但是一定是两边都包含文件 否则会报错 operation not permitted, copyfile xxx
Kind: global function
| Param | Type | Default | Description | | --- | --- | --- | --- | | src | string | | 源地址 | | dest | string | | 目标地址 | | isForce | string | | 文件存在是否强制覆盖 默认 false | | flags | number | 0 | 复制标识 |
copyFileSync(src, dest)
单文件强制复制文件
不存在目录则创建 但是一定是两边都包含文件 否则会报错 operation not permitted, copyfile xxx
Kind: global function
| Param | Type | Description | | --- | --- | --- | | src | string | 源文件路径 | | dest | string | 目标文件路径 |
mkdir(path, options) ⇒ null
创建目录【同步】
Kind: global function
Returns: null - 无返回
- tips
- https://nodejs.org/docs/latest-v13.x/api/fs.html#fs_fs_mkdir_path_options_callback
| Param | Type | Description | | --- | --- | --- | | path | string | 路径 | | options | object | 参数 |
yesOrNo(tips, [defaultValue]) ⇒ promise
录取用户输入
Kind: global function
Returns: promise - 读取结果
| Param | Type | Default | Description | | --- | --- | --- | --- | | tips | string | | 提示信息 | | [defaultValue] | string | "yes" | 默认值 |
readline(tips, isAlowEmpty) ⇒ promise
录取用户输入
Kind: global function
Returns: promise - 读取结果
| Param | Type | Default | Description | | --- | --- | --- | --- | | tips | string | | 提示信息 | | isAlowEmpty | boolean | false | 运行为空 |
updateContent(options) ⇒ string
文本中添加或者覆盖指定内容
Kind: global function
Returns: string - 新文本
| Param | Type | Description | | --- | --- | --- | | options | object | 参数 | | [options.content] | object | 文本内容 | | [options.startflag] | object | 开始标识 | | [options.endFalg] | object | 结束标识 | | [options.appContent] | object | 最佳或者替换的内容 | | [options.isRepalce] | object | 是否为替换 默认是追加 false | | [options.isStart] | object | 不存在时 追加最前面 默认是后面 | | [options.isInnerStart] | object | 追加文档时 追加在 heade 与 oldcontent 之间 默认在尾部 | | [options.space] | object | 与标识符的间隔 | | [options.newHeader] | object | 不存在时候 添加头部内容 |
getRootPath(relativePath) ⇒ string
获取安装类库的据对目录
Kind: global function
Returns: string - 主目录路径
| Param | Type | Default | Description | | --- | --- | --- | --- | | relativePath | string | "./" | 执行文件的相对目录 默认 ../../ |
getMainPath(relativePath) ⇒ string
当前安装 或者执行命令 的路径
Kind: global function
Returns: string - 新路径
- tips 默认执行的 bin 文件 在工程目录的下两层 到当前组件的跟目录层 上一侧目录都为当前组件的目录
| Param | Type | Default | Description | | --- | --- | --- | --- | | relativePath | string | "../../" | 相对路径 |
getBinPath() ⇒ string
获取当前组件 bin 的路径
Kind: global function
Returns: string - bin 路径
readdirSync(path) ⇒ array
读取目录【同步】
Kind: global function
Returns: array - 文件目录
| Param | Type | Description | | --- | --- | --- | | path | string | 目录路径 * @param {object} options 读取配置 |
delDir(path)
嵌套删除子文件夹和文件
Kind: global function
| Param | Type | Description | | --- | --- | --- | | path | string | 路径 |
delFile(path)
删除文件【同步】
Kind: global function
| Param | Type | Description | | --- | --- | --- | | path | string | 文件路径 |
isSysUsePort(port) ⇒ boolean
系统判断端口使用 // TODO 采用静态编译方式 处理静态文件 // TODO mack 上无法判断
Kind: global function
Returns: boolean - 事都被占用
| Param | Type | Description | | --- | --- | --- | | port | number | 端口号 |
getCanUsePort(port) ⇒ number
获取到能够使用的端口
- 动态自增的方式 直到可用的端口
Kind: global function
Returns: number - 可用的端口
| Param | Type | | --- | --- | | port | number |
copyProjectFile(from, to, defaultIsSucess)
复制文件【异步】
Kind: global function
| Param | Type | Default | Description | | --- | --- | --- | --- | | from | string | | 相对发布组件的目录 | | to | string | | 相对生成的目录 | | defaultIsSucess | boolean | 0 | 不存在文件的时候是否默认为成功 默认为成功 |
copyProjectFileSync(from, to, defaultIsSucess)
复制文件【同步】
Kind: global function
| Param | Type | Default | Description | | --- | --- | --- | --- | | from | string | | 相对发布组件的目录 | | to | string | | 相对生成的目录 | | defaultIsSucess | boolean | 0 | 不存在文件的时候是否默认为成功 默认为成功 |
copyFolder(source, target)
嵌套复制文件夹【同步】
Kind: global function
| Param | Type | Description | | --- | --- | --- | | source | string | 源路径 | | target | string | 目标路径 |
getLocalIP() ⇒ string
获取本机 IP
- windows 验证 其他环境还未验证
Kind: global function
Returns: string - ip 地址
- tips
- 目前值返回 IPv4 后续可以扩展 IPv6
