From 8995f6922dd1cddbd9d5ac57a3b206ce60c19691 Mon Sep 17 00:00:00 2001 From: bys Date: Tue, 28 Jun 2022 16:10:17 +0800 Subject: [PATCH] feat: support custom fis file --- src/deploy.ts | 7 ++++++- src/http-push.ts | 8 ++++++-- src/token.ts | 9 +++++++-- src/upload.ts | 4 ++-- 4 files changed, 21 insertions(+), 7 deletions(-) diff --git a/src/deploy.ts b/src/deploy.ts index a32c9cd..a7dfce6 100644 --- a/src/deploy.ts +++ b/src/deploy.ts @@ -18,6 +18,7 @@ export function Deploy (options: IOptions | IOptions[], modified: IFile[], total const to = option.to; const cachePath = option.cachePath; const cache = option.cache; + const fisConfigFilePath = option.fisConfigFilePath; let receiver = option.receiver; let authApi = option.authApi; let validateApi = option.validateApi; @@ -40,7 +41,9 @@ export function Deploy (options: IOptions | IOptions[], modified: IFile[], total cachePath, file.getHashRelease ? file.getHashRelease() : file.relative, file.contents, - file, (error) => error ? errorHandler(file, error, () => reduce(next)) : next() + file, + (error) => error ? errorHandler(file, error, () => reduce(next)) : next(), + fisConfigFilePath ); }); @@ -79,4 +82,6 @@ export interface IOptions { retry: number; cachePath: string; cache: boolean; + /** fis 的配置文件目录 */ + fisConfigFilePath?: string; } diff --git a/src/http-push.ts b/src/http-push.ts index a709018..5a83a68 100644 --- a/src/http-push.ts +++ b/src/http-push.ts @@ -30,11 +30,14 @@ export function httpPush (options: IDeployOption[]) { cachePath, host: option.host, retry: 2, - to: option.to }, { + to: option.to, + fisConfigFilePath: option.fisConfigFilePath + }, { contents: file.contents, path: file.path, relative: '/' + file.relative, - stat: file.stat }); + stat: file.stat + }); // 在match名单中 // console.log(option.to, file.path); } @@ -56,4 +59,5 @@ interface IDeployOption { cachePath?: string; /** 是否缓存 */ cache?: boolean; + fisConfigFilePath?: string; } diff --git a/src/token.ts b/src/token.ts index 5db9ec7..8c22dfa 100644 --- a/src/token.ts +++ b/src/token.ts @@ -10,20 +10,25 @@ import { resolve } from 'path'; const HOME = homedir(); let TOKEN_FILE = `${HOME}/.fis3-tmp/deploy.json`; +let hasCustomTokenPath = false; if (!existsSync(TOKEN_FILE)) { // 优先使用FIS3全局TOKEN 因为机器唯一 使用不同TOKEN会导致频繁验证 TOKEN_FILE = `${HOME}/.gulp-deploy-http-push.json`; } -const TOKEN_PATH = resolve(TOKEN_FILE); +let TOKEN_PATH = resolve(TOKEN_FILE); let token: null|IToken = null; -export function getToken (): IToken { +export function getToken (fisConfigFilePath?: string): IToken { if (token !== null) { return token; } + if (fisConfigFilePath && !hasCustomTokenPath) { + TOKEN_PATH = resolve(HOME + '/' + fisConfigFilePath); + hasCustomTokenPath = true; + } token = existsSync(TOKEN_PATH) ? JSON.parse(readFileSync(TOKEN_PATH).toString() || '{}') as IToken : {}; diff --git a/src/upload.ts b/src/upload.ts index ff70b24..592a2da 100644 --- a/src/upload.ts +++ b/src/upload.ts @@ -9,12 +9,12 @@ import { parseUrl } from './fetch'; import { IFile } from './file'; import { getToken } from './token'; import { Text } from './util'; -export function upload (receiver, to, cache, cachePath, release, content, file: IFile, callback) { +export function upload (receiver, to, cache, cachePath, release, content, file: IFile, callback, fisConfigFilePath?: string ) { const subpath = file.subpath || file.relative; if (!subpath) { throw new Error('subpath is undefined'); } - const data = { ...getToken(), to: to + release }; + const data = { ...getToken(fisConfigFilePath), to: to + release }; fupload( // url, request options, post data, file receiver, null, data, content, subpath,