Comparing version 2.0.2-beta.13 to 2.0.2-beta.14
@@ -23,3 +23,5 @@ import IFileLaneConfig from './interface/IFileLaneConfig'; | ||
constructor(config: IFileLaneConfig<O>, projectPath?: string, compilerOption?: O | undefined, events?: { | ||
onBuildSuccess?: (() => void) | undefined; | ||
onBuildSuccess?: ((data: { | ||
costTime: number; | ||
}) => void) | undefined; | ||
} | undefined); | ||
@@ -61,9 +63,17 @@ /** | ||
/** | ||
* 运行转换初始的任务 | ||
* start开始时的准备工作 | ||
*/ | ||
private runPreWorks; | ||
private complyBeforeWorks; | ||
/** | ||
* 运行转换后续的任务 | ||
* start结束后的收尾工作 | ||
*/ | ||
private runFollowWorks; | ||
private complyAfterWork; | ||
/** | ||
* 执行项目转换的前置工作 | ||
*/ | ||
private complyBeforeCompile; | ||
/** | ||
* 执行项目转换的后续工作 | ||
*/ | ||
private complyAfterCompile; | ||
private watch; | ||
@@ -81,3 +91,2 @@ /** | ||
private listenFileChange; | ||
private get outputPath(); | ||
/** | ||
@@ -84,0 +93,0 @@ * 清除输出文件夹 |
@@ -61,3 +61,3 @@ "use strict"; | ||
} | ||
yield this.cleanOutput(); | ||
yield this.complyBeforeWorks(); | ||
const fileList = this.collectFile(); | ||
@@ -125,2 +125,3 @@ if (!fileList || !fileList.length) { | ||
dispose() { | ||
this.complyAfterWork(); | ||
if (this.watcher) { | ||
@@ -137,5 +138,6 @@ this.watcher.close(); | ||
const { onBuildSuccess } = this.events || {}; | ||
const t1 = Date.now(); | ||
this.initCompilation(); | ||
try { | ||
yield this.runPreWorks(fileList); | ||
yield this.complyBeforeCompile(fileList); | ||
this.triggerPlugins(new CompilationEvent_1.default(CompilationEvent_1.default.PROJECT_START)); | ||
@@ -150,4 +152,5 @@ for (let item of fileList) { | ||
this.triggerPlugins(new CompilationEvent_1.default(CompilationEvent_1.default.FLLOW_WORK_START)); | ||
yield this.runFollowWorks(); | ||
yield this.complyAfterCompile(); | ||
this.triggerPlugins(new CompilationEvent_1.default(CompilationEvent_1.default.FLLOW_WORK_END)); | ||
onBuildSuccess === null || onBuildSuccess === void 0 ? void 0 : onBuildSuccess({ costTime: Date.now() - t1 }); | ||
shared_utils_1.ColorConsole.success({ | ||
@@ -157,3 +160,2 @@ word: 'Success: build finish!', | ||
}); | ||
onBuildSuccess === null || onBuildSuccess === void 0 ? void 0 : onBuildSuccess(); | ||
} | ||
@@ -217,3 +219,3 @@ catch (error) { | ||
} | ||
const buildPath = this.outputPath; | ||
const buildPath = FileLaneUtil_1.default.getOutputPath(this.context); | ||
return Promise.all(fileList.map((item) => { | ||
@@ -262,9 +264,35 @@ const resolvePath = path_1.default.relative(this.context.projectPath, item.path); | ||
/** | ||
* 运行转换初始的任务 | ||
* start开始时的准备工作 | ||
*/ | ||
runPreWorks(fileList) { | ||
complyBeforeWorks() { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const { preWorks } = this.config; | ||
if (preWorks) { | ||
for (let item of preWorks) { | ||
const { beforeWorks } = this.config; | ||
if (beforeWorks) { | ||
for (let item of beforeWorks) { | ||
yield item(this.context); | ||
} | ||
} | ||
}); | ||
} | ||
/** | ||
* start结束后的收尾工作 | ||
*/ | ||
complyAfterWork() { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const { afterWorks } = this.config; | ||
if (afterWorks) { | ||
for (let item of afterWorks) { | ||
yield item(this.context); | ||
} | ||
} | ||
}); | ||
} | ||
/** | ||
* 执行项目转换的前置工作 | ||
*/ | ||
complyBeforeCompile(fileList) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const { beforeCompile } = this.config; | ||
if (beforeCompile) { | ||
for (let item of beforeCompile) { | ||
yield item(this.context, fileList, this.config, this.compilerOption); | ||
@@ -276,9 +304,9 @@ } | ||
/** | ||
* 运行转换后续的任务 | ||
* 执行项目转换的后续工作 | ||
*/ | ||
runFollowWorks() { | ||
complyAfterCompile() { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const { followWorks } = this.config; | ||
if (followWorks) { | ||
for (let item of followWorks) { | ||
const { afterCompile } = this.config; | ||
if (afterCompile) { | ||
for (let item of afterCompile) { | ||
try { | ||
@@ -300,6 +328,7 @@ shared_utils_1.ColorConsole.info(`FollowWork: ${item.workerDescribe} start`); | ||
// 监听文件变化,并触发 build | ||
this.listenFileChange(() => __awaiter(this, void 0, void 0, function* () { | ||
const fileList = this.collectFile(); | ||
const onChange = (changedFileList) => __awaiter(this, void 0, void 0, function* () { | ||
const fileList = this.config.collectFile ? this.config.collectFile() : this.collectFile(); | ||
yield this.build(fileList); | ||
})); | ||
}); | ||
this.listenFileChange(onChange); | ||
}); | ||
@@ -345,3 +374,3 @@ } | ||
const { exclude, include } = this.config; | ||
// 判断是否为删除,执行onChange回调 | ||
// 执行onChange回调 | ||
if (shared_utils_1.FileUtil.include(filePath, include, exclude)) { | ||
@@ -378,6 +407,2 @@ throttledOnChange([filePath]); | ||
} | ||
get outputPath() { | ||
const { output, projectPath } = this.context; | ||
return path_1.default.join(projectPath, output); | ||
} | ||
/** | ||
@@ -388,3 +413,3 @@ * 清除输出文件夹 | ||
return __awaiter(this, void 0, void 0, function* () { | ||
yield (0, del_1.default)(this.outputPath, { force: true }); | ||
yield (0, del_1.default)(FileLaneUtil_1.default.getOutputPath(this.context), { force: true }); | ||
}); | ||
@@ -391,0 +416,0 @@ } |
@@ -25,2 +25,12 @@ import { MatchType } from '@aiot-toolkit/shared-utils'; | ||
/** | ||
* 项目文件采集器 | ||
* | ||
* @param entryFileList 输入的文件列表 | ||
* | ||
* @returns 返回项目中所有待处理的文件路径 | ||
* | ||
* @description 用于采集所有要处理的真实文件路径列表,或者输入文件列表中要处理的真实文件路径列表 | ||
*/ | ||
collectFile?: (entryFileList?: string[]) => string[]; | ||
/** | ||
* 文件收集器 | ||
@@ -72,11 +82,15 @@ * | ||
/** | ||
* 前置工作 | ||
* | ||
* @param context 上下文 | ||
* @param fileList 待转换的文件列表 | ||
* @returns | ||
* 启动阶段的准备工作 | ||
*/ | ||
preWorks?: PreWork[]; | ||
beforeWorks?: BeforeWork[]; | ||
/** | ||
* 转换结束后续的工作 | ||
* 收尾阶段的工作 | ||
*/ | ||
afterWorks?: AfterWork[]; | ||
/** | ||
* 项目转换的前置工作 | ||
*/ | ||
beforeCompile?: PreWork[]; | ||
/** | ||
* 项目转换的后续工作 | ||
* | ||
@@ -87,3 +101,3 @@ * xts--[zip] | ||
*/ | ||
followWorks?: FollowWoker<O>[]; | ||
afterCompile?: FollowWoker<O>[]; | ||
/** | ||
@@ -99,3 +113,9 @@ * 配置watch时忽略的文件或者文件夹 | ||
export type FollowWork<O = any> = (context: IFileLaneContext, config?: IFileLaneConfig, compilerOption?: O) => Promise<any>; | ||
export type PreWork<O = any> = (context: IFileLaneContext, fileList: string[], config: IFileLaneConfig, compilerOption?: O) => Promise<any>; | ||
/** | ||
* context 上下文 | ||
* fileList 待转换的文件列表 | ||
*/ | ||
export type PreWork<O = any> = (context: IFileLaneContext, fileList: string[], config: IFileLaneConfig, compilerOption: O) => Promise<any>; | ||
export type BeforeWork = (context: IFileLaneContext) => Promise<any>; | ||
export type AfterWork = (context: IFileLaneContext) => Promise<any>; | ||
export {}; |
@@ -16,3 +16,4 @@ import IFileLaneContext from '../interface/IFileLaneContext'; | ||
static checkError(message: string): void; | ||
static getOutputPath(context: IFileLaneContext): string; | ||
} | ||
export default FileLaneUtil; |
@@ -8,2 +8,3 @@ "use strict"; | ||
const fs_extra_1 = __importDefault(require("fs-extra")); | ||
const path_1 = __importDefault(require("path")); | ||
/** | ||
@@ -53,3 +54,7 @@ * FileLaneUtil | ||
} | ||
static getOutputPath(context) { | ||
const { output, projectPath } = context; | ||
return path_1.default.join(projectPath, output); | ||
} | ||
} | ||
exports.default = FileLaneUtil; |
{ | ||
"name": "file-lane", | ||
"version": "2.0.2-beta.13", | ||
"version": "2.0.2-beta.14", | ||
"description": "File conversion tool, can be one-to-one, one to N, N to one", | ||
@@ -23,4 +23,4 @@ "keywords": [ | ||
"dependencies": { | ||
"@aiot-toolkit/shared-utils": "2.0.2-beta.13", | ||
"chokidar": "^3.5.3", | ||
"@aiot-toolkit/shared-utils": "2.0.2-beta.14", | ||
"chokidar": "^3.6.0", | ||
"del": "^4.1.0", | ||
@@ -31,6 +31,5 @@ "fs-extra": "^11.2.0", | ||
"devDependencies": { | ||
"@types/archiver": "^5.3.2", | ||
"@types/fs-extra": "^11.0.4" | ||
}, | ||
"gitHead": "30bf14ae958b66937da1b79dcfcd7c436e08af50" | ||
"gitHead": "c6093733d0267d2d1359fec0af8f7a253955e3e3" | ||
} |
246
README.md
@@ -1,13 +0,247 @@ | ||
# `file-lane` | ||
# 项目介绍 | ||
用于文件转换,可1个文件转换为1个文件,也可1转多、多转1 | ||
file-lane模块用于文件转换,可实现文件1对1的转换,也可实现1对多、多对1的文件转换。 | ||
<img src="./doc/flow.jpg" style="max-width: 1000px; width: 100%"/> | ||
<img src="./doc/flow-detail.png" style="max-width: 1000px; width: 100%"/> | ||
## Usage | ||
## 安装使用 | ||
- 安装 | ||
`npm i file-lane` | ||
- 快速上手 | ||
```javascript | ||
const { FileLane } = require('file-lane') | ||
const { UxLoader } = require('aiot-toolkit/aiotpack') | ||
const Path = require('path') | ||
const projectPath = Path.join(__dirname, '../testProject') | ||
// 定义项目转换的配置参数,output和module为必要配置内容 | ||
const projectConfig = { | ||
// output指定转换后项目的存储位置 | ||
get output() { | ||
const name = Path.basename(projectPath) | ||
const result = `../.temp_${name}` | ||
return result | ||
}, | ||
// 转换过程中使用的转换模块配置 | ||
module: { | ||
rules: [ | ||
{ | ||
test: [/.+\.ux$/], | ||
exclude: [/app\.ux/], | ||
loader: [UxLoader] | ||
} | ||
] | ||
} | ||
} | ||
// 开始执行文件转换 | ||
new FileLane(projectConfig).start() | ||
// 开启watch模式 | ||
// new FileLane(projectConfig).start({ watch: true }) | ||
``` | ||
## 参数配置 | ||
- 必填参数 | ||
- [output](#output) | ||
- [module](#module) | ||
- 可选参数 | ||
- [fileCollector](#fileCollector) | ||
- [include](#include) | ||
- [exclude](#exclude) | ||
- [plugins](#plugins) | ||
- [preWorks](#preWorks) | ||
- [followWorks](#followWorks) | ||
- [watchIgnores](#watchIgnores) | ||
<a id="output">output</a> | ||
描述: 输出目录 | ||
参数类型: string | ||
示例: | ||
``` | ||
const fileLane = require('file-lane'); | ||
'temp_project' | ||
``` | ||
// TODO: DEMONSTRATE API | ||
<a id="module">module</a> | ||
描述: 文件转换规则集 | ||
参数类型: { rules: IRule[] } // IRule:转换规则 | ||
```javascript | ||
interface IRule { | ||
// 配置文件 | ||
test: MatchType | ||
// 文件的 Loader,从前向后依次执行,前一个 loader 结果做为后一个 loader 的入参 | ||
loader: ILoaderClass[] | ||
exclude?: MatchType | ||
include?: MatchType | ||
} | ||
``` | ||
示例: | ||
``` | ||
{ | ||
rules: [ | ||
{ | ||
test: [/.+\.ux$/], | ||
exclude: [/app\.ux/], | ||
loader: [UxLoader] | ||
} | ||
] | ||
} | ||
``` | ||
<a id="fileCollector">fileCollector</a> | ||
描述: 文件收集器,输入文件路径,返回待合并的文件路径,常用于多转1,默认值为直接使用源文件 | ||
参数类型: | ||
``` | ||
(file: string) => string[] | ||
``` | ||
示例: | ||
```javascript | ||
// src/a.hml -->[src/a.hml, src/a.js, src/a.css] | ||
(file: string) => { | ||
const fileList = [file] | ||
const { dir, name, ext } = path.parse(file) | ||
if (ext === '.hml') { | ||
['.js', '.css'].map((item) => { | ||
const collectFile = path.join(dir, `${name}${item}`) | ||
// 若路径真实存在,则push到文件列表中 | ||
if (fs.existsSync(collectFile)) { | ||
fileList.push(collectFile) | ||
} | ||
}) | ||
return fileList | ||
} | ||
} | ||
``` | ||
<a id="include">include</a> | ||
描述: 指定文件范围 | ||
参数类型: | ||
``` | ||
OneMatchType | OneMatchType[] | ||
``` | ||
示例: | ||
```javascript | ||
// 匹配包含有src字符串的文件 | ||
include = ['src'] | ||
``` | ||
<a id="exclude">exclude</a> | ||
描述: 需排除的文件范围 | ||
参数类型: | ||
``` | ||
OneMatchType | OneMatchType[] | ||
``` | ||
示例: | ||
```javascript | ||
// 排除路径中包含有node_modules字符串的文件 | ||
exclude = [/node_modules/] | ||
``` | ||
<a id="plugins">plugins</a> | ||
描述: 插件,在每个文件转换完成前后,所有文件转换完成前后,在打包完成前后会触发 | ||
参数类型: | ||
``` | ||
IPlugin[] | ||
``` | ||
示例: | ||
``` | ||
``` | ||
<a id="preWorks">preWorks</a> | ||
描述: 前置工作,所有文件转换前的工作 | ||
参数类型: | ||
``` | ||
PreWork[] | ||
``` | ||
示例: | ||
```javascript | ||
preWorks = [validateManifest] | ||
const validateManifest: PreWork<IJavascriptCompileOption> = async (context) => { | ||
// 校验manifest.json文件的内容 | ||
} | ||
``` | ||
<a id="followWorks">followWorks</a> | ||
描述: 后续工作,所有文件转换后的工作 | ||
参数类型: | ||
``` | ||
FollowWoker<O>[] | ||
``` | ||
示例: | ||
```javascript | ||
followWorks = [ | ||
{ | ||
worker: toRpk, | ||
workerDescribe: 'follow work' | ||
} | ||
] | ||
const toRpk: FollowWork<IJavascriptCompileOption> = async (context, config, compilerOption) => { | ||
// 生成rpk逻辑 | ||
} | ||
``` | ||
<a id="watchIgnores">watchIgnores</a> | ||
描述: 配置watch时忽略的文件或者文件夹 | ||
参数类型: | ||
``` | ||
MatchType | ||
``` | ||
示例: | ||
```javascript | ||
watchIgnores = [/node_modules/, '/build/', '/dist/'] | ||
``` |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
38603
1
977
248
+ Added@aiot-toolkit/shared-utils@2.0.2-beta.14(transitive)
+ Added@kwsites/file-exists@1.1.1(transitive)
+ Added@kwsites/promise-deferred@1.1.1(transitive)
+ Addedansi-colors@4.1.3(transitive)
+ Addedansi-regex@5.0.1(transitive)
+ Addedcamelcase@5.3.1(transitive)
+ Addedcli-progress@3.12.0(transitive)
+ Addedcliui@6.0.0(transitive)
+ Addeddecamelize@1.2.0(transitive)
+ Addeddijkstrajs@1.0.3(transitive)
+ Addedemoji-regex@8.0.0(transitive)
+ Addedfind-up@4.1.0(transitive)
+ Addedget-caller-file@2.0.5(transitive)
+ Addedis-fullwidth-code-point@3.0.0(transitive)
+ Addedlocate-path@5.0.0(transitive)
+ Addedp-limit@2.3.0(transitive)
+ Addedp-locate@4.1.0(transitive)
+ Addedp-try@2.2.0(transitive)
+ Addedpath-exists@4.0.0(transitive)
+ Addedpngjs@5.0.0(transitive)
+ Addedqrcode@1.5.4(transitive)
+ Addedrequire-directory@2.1.1(transitive)
+ Addedrequire-main-filename@2.0.0(transitive)
+ Addedset-blocking@2.0.0(transitive)
+ Addedsimple-git@3.27.0(transitive)
+ Addedstring-width@4.2.3(transitive)
+ Addedstrip-ansi@6.0.1(transitive)
+ Addedwhich-module@2.0.1(transitive)
+ Addedwrap-ansi@6.2.0(transitive)
+ Addedy18n@4.0.3(transitive)
+ Addedyargs@15.4.1(transitive)
+ Addedyargs-parser@18.1.3(transitive)
- Removed@aiot-toolkit/shared-utils@2.0.2-beta.13(transitive)
- Removedqrcode-terminal@0.12.0(transitive)
Updatedchokidar@^3.6.0