UNPKG

@shencom/oss-upload

Version:
283 lines (276 loc) 8.97 kB
import * as ora from 'ora'; import ora__default from 'ora'; import * as minimist from 'minimist'; import minimist__default from 'minimist'; import AliOSS, { PutObjectOptions, ListObjectResult } from 'ali-oss'; interface FileData { url: string; md5: string; } interface OssOptions { /** 上传路径 如: plugins/test/scloud/xxx */ ossPath: string; /** bucket名 */ bucket?: string; /** 过滤上传删除文件,同 glob.ignore */ ignore?: string[]; /** 过滤上传oss文件,合并 ignore */ uploadIgnore?: string[]; /** 过滤删除oss文件,合并 ignore */ deleteIgnore?: string[]; /** 超时时间 */ timeout?: number; /** 开启debug,开启后不进行上传和删除操作 */ debug?: boolean; /** oss 上传配置 */ uploadOptions?: ((filePath: string, defaultOptions: Partial<PutObjectOptions>) => Partial<PutObjectOptions>) | Partial<PutObjectOptions>; } interface UploadOptionsBase extends Partial<Pick<OssOptions, 'ossPath'>> { /** 目录路径 */ dirPath: string; /** * 是否开启本地文件和oss文件进行对比 * * ```text * 对比规则: * 本地存在,oss不存在: 上传 * 本地存在,oss存在: 覆盖 * 本地不存在,oss存在: 删除 * ``` */ diff?: boolean; /** 开启debug,开启后不进行上传和删除操作 */ debug?: boolean; /** 是否关闭确认提示,默认为: false */ isCloseConfirm?: boolean; /** 并发上传数量,默认: 5 */ concurrency?: number; /** 处理获取本地文件列表钩子 */ localFileHook?: (p: string[]) => string[]; /** 处理oss路径列表钩子 */ ossPathHook?: (paths: string[]) => string[]; /** 处理获取oss文件列表钩子 */ ossFileHook?: (p: AliOSS.ObjectMeta[]) => AliOSS.ObjectMeta[]; /** 上传前钩子 */ beforeUploadHook?: (p: string[]) => string[]; /** 删除前钩子 */ beforeDeleteHook?: (p: string[]) => string[]; } interface CloseVersion { /** 是否开启清除版本号目录,默认: false */ isClearVersion?: false; /** 当前版本号 */ version?: string; } interface OpenVersion { /** 是否开启清除版本号目录,默认: false */ isClearVersion: true; /** 当前版本号 */ version: string; } type UploadOptionsVersion = (OpenVersion | CloseVersion) & { /** 保留版本号目录个数,默认: 10 */ versionLimit?: number; }; type OssPutOptions = Pick<UploadOptionsBase, 'isCloseConfirm' | 'concurrency'>; type UploadOptions = UploadOptionsBase & UploadOptionsVersion; interface OssUploadItem { ossPath: string; filePath: string; } interface OssDownloadItem { ossPath: string; localPath: string; } declare class OSSBase { protected config: OssOptions; protected CLIENT?: AliOSS; protected uploadIgnore: string[]; protected deleteIgnore: string[]; constructor(options: OssOptions); private setIgnore; protected _list(prefix?: string, options?: AliOSS.ListV2ObjectsQuery): Promise<ListObjectResult[]>; /** * 获取文件对象 * * @protected * @param {string} [refix=this.config.ossPath] * @return {Promise<AliOSS.ObjectMeta[]>} * @memberof OSSBase */ protected _listObjects(refix?: string): Promise<AliOSS.ObjectMeta[]>; /** * 获取目录 * * @protected * @param {string} [refix=this.config.ossPath] * @return {Promise<string[]>} * @memberof OSSBase */ protected _listPrefixes(refix?: string): Promise<string[]>; protected _head(path: string): Promise<string>; protected _put(options: OssUploadItem): Promise<void> | Promise<AliOSS.PutObjectResult>; protected _get(paths: OssDownloadItem, options?: AliOSS.GetObjectOptions): Promise<AliOSS.GetObjectResult>; protected _delete(paths: string[]): Promise<{ successList: string[]; failList: string[]; }>; } declare class OSS extends OSSBase { constructor(ops: OssOptions); /** * 获取oss文件列表 * * @param {string[]} ossPaths oss路径 * @param {string} returnType 返回类型 * @return {Promise<(AliOSS.ObjectMeta|string)[]>} * @memberof OSS */ list(ossPaths: string[] | string, returnType?: 'file'): Promise<AliOSS.ObjectMeta[]>; list(ossPaths: string[] | string, returnType?: 'dir'): Promise<string[]>; list(ossPaths: string[] | string, returnType?: 'all', options?: AliOSS.ListV2ObjectsQuery): Promise<Pick<AliOSS.ListObjectResult, 'objects' | 'prefixes'>[]>; /** * 批量获取oss文件的md5 * * @param {string[]} ossFilePaths oss文件路径 * @returns {Promise<FileData[]>} * @memberof OSS */ head(ossFilePaths: string[]): Promise<FileData[]>; /** * 删除oss文件 * * @param {string[]} ossPaths oss文件路径 * @returns * @memberof OSS */ delete(ossPaths: string[]): Promise<void>; /** * 上传文件 * * @param {OssUploadItem[]} paths 上传路径对象 * @returns * @memberof OSS */ put(paths: OssUploadItem[], options?: OssPutOptions): Promise<void>; /** * 下载文件 * * @param {OssDownloadItem[]} paths 下载路径对象 * @param {AliOSS.GetObjectOptions} options 下载参数 * @returns * @memberof OSS */ download(paths: OssDownloadItem[], options?: AliOSS.GetObjectOptions): Promise<unknown>; /** * 代码上传 * * @param {UploadOptions} ops 上传配置 * @memberof OSS */ upload(ops: UploadOptions): Promise<void>; /** * 清除版本号文件夹 * * @param {string[]} paths 文件路径 * @param {number} [limit] 保留版本号目录个数 * @return * @memberof OSS */ clearVersionDir(paths: string[], opt: UploadOptions): Promise<void>; private setOssBasePath; private _handleClearVersionDir; private _handleOssPath; private _handleLocalPath; private _handlePutFile; private _handleDeleteFile; } declare const _argv: minimist__default.ParsedArgs; /** 构建模式 */ declare const Mode: "build" | "serve"; /** 构建环境 */ declare const Env: "tst" | "production" | "uat"; declare const isTst: boolean; declare const isUat: boolean; declare const isPro: boolean; declare const isBuild: boolean; declare const isServe: boolean; /** 跳过打包,直接上传 */ declare const isUpload: boolean; declare const isDebug: boolean; /** 环境名 */ declare const envName: "\u6D4B\u8BD5\u670D" | "UAT" | "\u6B63\u5F0F\u670D"; declare const spinner: ora__default.Ora; declare const branch: string; /** * md5加密 * * @export * @param {string | Buffer | NodeJS.TypedArray | DataView} val * @returns */ declare function md5(val: string | Buffer | NodeJS.TypedArray | DataView): string; /** * 获取命令行参数 * * @export * @returns */ declare function getCliParam(): string[]; declare function confirm(message: string): Promise<boolean>; /** * 验证打包 git 分支 是否对应 * * `master => 正式` * * `uat => UAT` * * @export * @returns */ declare function verifyBuild(): boolean; /** * 文件是否存在 * * @export * @param {string} filePath 文件路径 * @returns */ declare function isFile(filePath: string): boolean; /** 检测路径是否存在 */ declare function CheckEnvPath(envPath: string): string; declare function throwErr(prefix: string, message: string): void; declare const versionReg: RegExp; declare const versionExec: (v: string) => RegExpExecArray | null; declare const versionSort: (v: string[]) => string[]; declare const versionValid: (v: string) => string | null; declare const version: string; declare const _default: { md5(val: string | Buffer | NodeJS.TypedArray | DataView): string; getCliParam(): string[]; confirm(message: string): Promise<boolean>; verifyBuild(): boolean; isFile(filePath: string): boolean; CheckEnvPath(envPath: string): string; throwErr(prefix: string, message: string): void; _argv: minimist.ParsedArgs; Mode: "build" | "serve"; Env: "tst" | "production" | "uat"; isTst: boolean; isUat: boolean; isPro: boolean; isBuild: boolean; isServe: boolean; isUpload: boolean; isDebug: boolean; envName: "\u6D4B\u8BD5\u670D" | "UAT" | "\u6B63\u5F0F\u670D"; spinner: ora.Ora; branch: string; versionReg: RegExp; versionExec: (v: string) => RegExpExecArray | null; versionSort: (v: string[]) => string[]; versionValid: (v: string) => string | null; OSS: typeof OSS; version: string; }; export { CheckEnvPath, Env, Mode, OSS, _argv, branch, confirm, _default as default, envName, getCliParam, isBuild, isDebug, isFile, isPro, isServe, isTst, isUat, isUpload, md5, spinner, throwErr, verifyBuild, version, versionExec, versionReg, versionSort, versionValid };