-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
22 changed files
with
168 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,26 +2,18 @@ | |
|
||
> 实用程序库 | ||
[Web端使用文档](https://aiden-fe.github.io/compass-helpers/browser/) | ||
[Web端使用文档](https://aiden-fe.github.io/compass-helpers/web/) | ||
|
||
[Node端使用文档](https://aiden-fe.github.io/compass-helpers/node/) | ||
|
||
## Getting Started | ||
|
||
### 安装 | ||
### web项目使用 | ||
|
||
npm方式安装: | ||
|
||
`npm install @compass-aiden/helpers` | ||
|
||
浏览器script标签安装: | ||
|
||
通过jsdelivr或者unpkg直接script标签导入`dist/compass-helpers.umd.js`文件后,window.CompassHelpers即可访问到 | ||
|
||
### 导入使用 | ||
|
||
#### web项目 | ||
|
||
```typescript | ||
// 自动识别导入esm文件 | ||
import { formatDate } from '@compass-aiden/helpers'; | ||
|
@@ -33,15 +25,29 @@ import { formatDate } from '@compass-aiden/helpers/esm'; | |
import '@compass-aiden/helpers/umd'; | ||
``` | ||
|
||
#### node项目 | ||
浏览器script标签安装: | ||
|
||
```html | ||
<!-- 请根据个人需求采用unpkg或者jsdelivr链接 --> | ||
<script src="https://unpkg.com/@compass-aiden/[email protected]/dist/compass-helpers.umd.js"></script> | ||
<script> | ||
console.log(window.CompassHelpers.formatDate()); | ||
</script> | ||
``` | ||
|
||
### node项目使用 | ||
|
||
npm方式安装: | ||
|
||
`npm install @compass-aiden/helpers` | ||
|
||
```typescript | ||
// 自动识别导入cjs文件, 如果无法识别tsconfig 设置 { "module": "NodeNext", "moduleResolution": "NodeNext" } | ||
// 自动识别导入cjs文件, 如果无法识别为cjs导入, 可在tsconfig 设置 { "module": "NodeNext", "moduleResolution": "NodeNext" }或通过其他方式导入 | ||
import { createFile } from '@compass-aiden/helpers'; | ||
// 全量导入 | ||
import * as allHelpers from '@compass-aiden/helpers'; | ||
// 通过别名路径导入cjs文件 | ||
import { createFile } from '@compass-aiden/helpers/node'; | ||
import { createFile } from '@compass-aiden/helpers/cjs'; | ||
// 自动导入cjs文件 | ||
const { createFile } = require('@compass-aiden/helpers'); | ||
``` | ||
|
@@ -61,6 +67,27 @@ const { createFile } = require('@compass-aiden/helpers'); | |
- `pnpm test` 执行单元测试 | ||
- `pnpm build:doc` 构建文档 | ||
|
||
### 添加一个新的工具函数 | ||
|
||
1. 请先确定该函数适用的平台 Web/Node/Common | ||
2. 通用函数请放入 `src/common-modules` | ||
3. Web平台函数放入 `src/web-modules` | ||
4. Node平台函数放入 `src/node-modules` | ||
5. 为函数添加一定的文档描述,如下示例 | ||
|
||
```typescript | ||
// src/web-modules/example.ts | ||
|
||
/** | ||
* @category Tools | ||
*/ | ||
export default function example() { | ||
console.log('可指定的category在 src/web.ts 或 src/node.ts 文件顶部声明'); | ||
} | ||
``` | ||
|
||
更多文档注解参考 [TypeDoc](https://typedoc.org/guides/overview/) | ||
|
||
### Publish library | ||
|
||
1. 变更package.json内的version字段 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,8 @@ | ||
import createFolder from './create-folder'; | ||
import createFile from './create-file'; | ||
import isCommandExists from './is-command-exists'; | ||
import isFileOrFolderExists from './is-file-or-folder-exists'; | ||
import requireModule from './require-module'; | ||
import scanDependencyManager from './scan-dependency-manager'; | ||
|
||
export { createFolder, createFile }; | ||
export { createFolder, createFile, isCommandExists, isFileOrFolderExists, requireModule, scanDependencyManager }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { execSync } from 'child_process'; | ||
|
||
/** | ||
* 命令是否存在 | ||
* @param command | ||
* @category Tools | ||
*/ | ||
export default function isCommandExists(command: string) { | ||
try { | ||
// 使用execSync执行命令,并捕获命令执行结果 | ||
execSync(`command -v ${command}`, { stdio: 'ignore' }); | ||
// 如果执行成功,返回true | ||
return true; | ||
} catch (error) { | ||
// 如果执行失败,则捕获到错误,说明命令不存在,返回false | ||
return false; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { accessSync } from 'fs'; | ||
|
||
/** | ||
* @description 检查路径下文件或文件夹是否存在 | ||
* @param {string} p 文件或文件夹路径 | ||
* @returns {boolean} | ||
* @category Files | ||
*/ | ||
export default function isFileOrFolderExists(p: string): boolean { | ||
try { | ||
accessSync(p); | ||
return true; | ||
} catch (err) { | ||
return false; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { createRequire } from 'module'; | ||
|
||
/** | ||
* @description 读取模块文件,类似require()函数 | ||
* @param filePath | ||
* @category Files | ||
*/ | ||
export default function requireModule(filePath: string) { | ||
const requireFunc = createRequire(import.meta.url); | ||
return requireFunc(filePath); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { join } from 'path'; | ||
import isFileOrFolderExists from './is-file-or-folder-exists'; | ||
|
||
/** | ||
* @description 扫描依赖管理器 | ||
* @category Tools | ||
*/ | ||
export default function scanDependencyManager(opt?: { cwd?: string }): 'npm' | 'pnpm' | 'yarn' { | ||
const { cwd } = { | ||
cwd: opt?.cwd || process.cwd(), | ||
}; | ||
let isPnpm = false; | ||
isPnpm = isFileOrFolderExists(join(cwd, 'pnpm-lock.yaml')); | ||
if (isPnpm) { | ||
return 'pnpm'; | ||
} | ||
let isYarn = false; | ||
isYarn = isFileOrFolderExists(join(cwd, 'yarn.lock')); | ||
if (isYarn) { | ||
return 'yarn'; | ||
} | ||
return 'npm'; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,17 @@ | ||
/** | ||
* @categoryDescription Date | ||
* 日期相关操作函数 | ||
* @categoryDescription Factories | ||
* 工厂类辅助工具 | ||
* @categoryDescription Tools | ||
* 辅助性的工具函数 | ||
* @categoryDescription Files | ||
* 文件系统相关辅助函数 | ||
* @categoryDescription Types | ||
* TS类型相关辅助 | ||
* @module | ||
*/ | ||
|
||
export * from './common-modules/index'; | ||
export * from './interfaces/index'; | ||
export * from './node-modules/index'; |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
/** | ||
* @categoryDescription Date | ||
* 日期相关操作函数 | ||
* @categoryDescription Factories | ||
* 工厂类辅助工具 | ||
* @categoryDescription Tools | ||
* 辅助性的工具函数 | ||
* @categoryDescription Types | ||
* TS类型相关辅助 | ||
* @module | ||
*/ | ||
|
||
export * from './common-modules/index'; | ||
export * from './interfaces/index'; | ||
export * from './web-modules/index'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters