Skip to content
Permalink
Browse files

chore: add gulpfile

  • Loading branch information
simonwong committed Dec 26, 2019
1 parent 4e4b3df commit a5370cb0c1e334d271439916648bc98586b16f05
Showing with 1,493 additions and 31 deletions.
  1. +105 −0 gulpfile.ts
  2. +11 −2 package.json
  3. +1,377 −29 yarn.lock
@@ -0,0 +1,105 @@
import { series } from 'gulp'
import path from 'path'
import fse from 'fs-extra'
import chalk from 'chalk'
import { rollup } from 'rollup'
import {
Extractor,
ExtractorConfig,
ExtractorResult,
} from '@microsoft/api-extractor'
import rollupConfig from './rollup.config'

interface TaskFunc {
(cb: Function): void
}

const log = {
progress: (text: string) => {
console.log(chalk.green(text))
},
error: (text: string) => {
console.log(chalk.red(text))
},
}

const paths = {
root: path.join(__dirname, '/'),
lib: path.join(__dirname, '/lib'),
}


// 删除 lib 文件
const clearLibFile: TaskFunc = async (cb) => {
fse.removeSync(paths.lib)
log.progress('Deleted lib file')
cb()
}

// rollup 打包
const buildByRollup: TaskFunc = async (cb) => {
const inputOptions = {
input: rollupConfig.input,
external: rollupConfig.external,
plugins: rollupConfig.plugins,
}
const outOptions = rollupConfig.output
const bundle = await rollup(inputOptions)

// 写入需要遍历输出配置
if (Array.isArray(outOptions)) {
outOptions.forEach(async (outOption) => {
await bundle.write(outOption)
})
cb()
log.progress('Rollup built successfully')
}
}

// api-extractor 整理 .d.ts 文件
const apiExtractorGenerate: TaskFunc = async (cb) => {
const apiExtractorJsonPath: string = path.join(__dirname, './api-extractor.json')
// 加载并解析 api-extractor.json 文件
const extractorConfig: ExtractorConfig = await ExtractorConfig.loadFileAndPrepare(apiExtractorJsonPath)
// 判断是否存在 index.d.ts 文件,这里必须异步先访问一边,不然后面找不到会报错
const isExist: boolean = await fse.pathExists(extractorConfig.mainEntryPointFilePath)

if (!isExist) {
log.error('API Extractor not find index.d.ts')
return
}

// 调用 API
const extractorResult: ExtractorResult = await Extractor.invoke(extractorConfig, {
localBuild: true,
// 在输出中显示信息
showVerboseMessages: true,
})

if (extractorResult.succeeded) {
// 删除多余的 .d.ts 文件
const libFiles: string[] = await fse.readdir(paths.lib)
libFiles.forEach(async file => {
if (file.endsWith('.d.ts') && !file.includes('index')) {
await fse.remove(path.join(paths.lib, file))
}
})
log.progress('API Extractor completed successfully')
cb()
} else {
log.error(`API Extractor completed with ${extractorResult.errorCount} errors`
+ ` and ${extractorResult.warningCount} warnings`)
}
}

const complete: TaskFunc = (cb) => {
log.progress('---- end ----')
cb()
}

// 构建过程
// 1. 删除 lib 文件夹
// 2. rollup 打包
// 3. api-extractor 生成统一的声明文件, 删除多余的声明文件
// 4. 完成
export const build = series(clearLibFile, buildByRollup, apiExtractorGenerate, complete)
@@ -2,7 +2,9 @@
"name": "fly-helper",
"version": "0.0.1",
"description": "工具库",
"main": "index.js",
"main": "lib/index.js",
"module": "lib/index.esm.js",
"typings": "lib/index.d.js",
"repository": {
"type": "git",
"url": "git+https://github.com/simonwong/fly-helper.git"
@@ -14,19 +16,25 @@
},
"homepage": "https://github.com/simonwong/fly-helper#readme",
"scripts": {
"test": "jest --coverage --verbose -u",
"build": "gulp build",
"api": "api-extractor run",
"lint": "eslint --ext .js,.ts ./src",
"lint:fix": "eslint --fix --ext .js,.ts ./src"
},
"devDependencies": {
"@babel/preset-env": "^7.7.7",
"@microsoft/api-extractor": "^7.7.0",
"@types/fs-extra": "^8.0.1",
"@types/gulp": "^4.0.6",
"@types/jest": "^24.0.24",
"@types/node": "^13.1.0",
"@typescript-eslint/eslint-plugin": "^2.13.0",
"@typescript-eslint/parser": "^2.13.0",
"chalk": "^3.0.0",
"eslint": "^6.8.0",
"eslint-plugin-jest": "^23.1.1",
"fs-extra": "^8.1.0",
"gulp": "^4.0.2",
"jest": "^24.9.0",
"rollup": "^1.27.14",
"rollup-plugin-babel": "^4.3.3",
@@ -35,6 +43,7 @@
"rollup-plugin-node-resolve": "^5.2.0",
"rollup-plugin-typescript2": "^0.25.3",
"ts-jest": "^24.2.0",
"ts-node": "^8.5.4",
"typescript": "^3.7.4"
},
"dependencies": {

0 comments on commit a5370cb

Please sign in to comment.
You can’t perform that action at this time.