-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(publish): 优化publish,从方法参数取值,而不是node命令行参数
- Loading branch information
Showing
6 changed files
with
162 additions
and
125 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
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,20 @@ | ||
export const ENV_MAP = { | ||
DEV_HOST_NAME: 'DEV_HOST_NAME', | ||
DEV_HOST_PWD: 'DEV_HOST_PWD', | ||
|
||
VUE_APP_DIR: 'VUE_APP_DIR', | ||
VUE_APP_AUTHOR: 'VUE_APP_AUTHOR', | ||
VUE_APP_PATH_PROD: 'VUE_APP_PATH_PROD', | ||
VUE_APP_PATH_TEST: 'VUE_APP_PATH_TEST', | ||
} as const; | ||
|
||
export const PUBLISH_ENV_MAP = { | ||
PROD: 'prod', | ||
TEST: 'test', | ||
DEV_CLOUD: 'devcloud', | ||
} as const; | ||
|
||
export const PUBLISH_HOST_ENV = { | ||
PROD: 'web-static', | ||
TEST: 'web-test', | ||
} as const; |
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,109 +1,22 @@ | ||
import * as http from 'http'; | ||
import * as path from 'path'; | ||
import * as fs from 'fs'; | ||
import type { IPublishOptions } from './types'; | ||
import { readEnvVariable } from '../env-variable/env-variable'; | ||
|
||
|
||
export function postFile(fileDataInfo, fileKeyValue, options?: IPublishOptions) { | ||
return new Promise(((resolve, reject) => { | ||
if (!options) { | ||
console.log('[publish] failed. 需要 options'); | ||
return; | ||
} | ||
|
||
const req = http.request(options, (res) => { | ||
// res.setEncoding("utf8"); | ||
res.on('data', (chunk) => { | ||
const result = JSON.parse(chunk); | ||
console.log('[postFile] result: ', result); | ||
resolve(result); | ||
}); | ||
}); | ||
|
||
req.on('error', (e) => { | ||
console.log(`[postFile] problem with request: ${e.message}`); | ||
reject(e); | ||
}); | ||
|
||
const boundaryKey = Math.random().toString(16); | ||
const endData = `\r\n----${boundaryKey}--`; | ||
|
||
let dataLength = 0; | ||
const dataArr: Array<Record<string, any>> = []; | ||
for (const item of fileDataInfo) { | ||
const dataInfo = `\r\n----${boundaryKey}\r\n` + `Content-Disposition: form-data; name="${item.urlKey}"\r\n\r\n${item.urlValue}`; | ||
const dataBinary = Buffer.from(dataInfo, 'utf-8'); | ||
dataLength += dataBinary.length; | ||
dataArr.push({ | ||
dataInfo, | ||
}); | ||
} | ||
|
||
const files: Array<Record<string, any>> = []; | ||
for (const item of fileKeyValue) { | ||
const content = `\r\n----${boundaryKey}\r\n` + 'Content-Type: application/octet-stream\r\n' + `Content-Disposition: form-data; name="${item.urlKey}"; filename="${path.basename(item.urlValue)}"\r\n` + 'Content-Transfer-Encoding: binary\r\n\r\n'; | ||
// 当编码为ascii时,中文会乱码。 | ||
const contentBinary = Buffer.from(content, 'utf-8'); | ||
files.push({ | ||
contentBinary, | ||
filePath: item.urlValue, | ||
}); | ||
} | ||
|
||
let contentLength = 0; | ||
for (const item of files) { | ||
const { filePath } = item; | ||
if (fs.existsSync(filePath)) { | ||
const stat = fs.statSync(filePath); | ||
contentLength += stat.size; | ||
} else { | ||
contentLength += Buffer.from('\r\n', 'utf-8').length; | ||
} | ||
contentLength += item.contentBinary.length; | ||
} | ||
export function getRootDir() { | ||
const rootDir = process.cwd(); | ||
return rootDir; | ||
} | ||
|
||
req.setHeader('Content-Type', `multipart/form-data; boundary=--${boundaryKey}`); | ||
req.setHeader('Content-Length', dataLength + contentLength + Buffer.byteLength(endData)); | ||
|
||
// 将参数发出 | ||
for (const item of dataArr) { | ||
req.write(item.dataInfo); | ||
} | ||
export function getEnvValue(key: string) { | ||
const rootDir = getRootDir(); | ||
const localEnvPath = path.join(rootDir, '.env.local'); | ||
const envPath = path.join(rootDir, '.env'); | ||
|
||
let fileIndex = 0; | ||
const doOneFile = function () { | ||
req.write(files[fileIndex].contentBinary); | ||
const currentFilePath = files[fileIndex].filePath; | ||
if (fs.existsSync(currentFilePath)) { | ||
// @ts-ignore | ||
const fileStream = fs.createReadStream(currentFilePath, { bufferSize: 4 * 1024 }); | ||
fileStream.pipe(req, { end: false }); | ||
|
||
fileStream.on('end', () => { | ||
fileIndex += 1; | ||
if (fileIndex === files.length) { | ||
req.end(endData); | ||
} else { | ||
doOneFile(); | ||
} | ||
}); | ||
} else { | ||
req.write('\r\n'); | ||
fileIndex += 1; | ||
const envValue = readEnvVariable(key, envPath); | ||
const localEnvValue = readEnvVariable(key, localEnvPath); | ||
|
||
if (fileIndex === files.length) { | ||
req.end(endData); | ||
} else { | ||
doOneFile(); | ||
} | ||
} | ||
}; | ||
|
||
if (fileIndex === files.length) { | ||
req.end(endData); | ||
} else { | ||
doOneFile(); | ||
} | ||
})); | ||
return localEnvValue || envValue || ''; | ||
} | ||
|
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,109 @@ | ||
import * as http from 'http'; | ||
import * as path from 'path'; | ||
import * as fs from 'fs'; | ||
import type { IPublishOptions } from './types'; | ||
|
||
|
||
export function postFile(fileDataInfo, fileKeyValue, options?: IPublishOptions) { | ||
return new Promise(((resolve, reject) => { | ||
if (!options) { | ||
console.log('[publish] failed. 需要 options'); | ||
return; | ||
} | ||
|
||
const req = http.request(options, (res) => { | ||
// res.setEncoding("utf8"); | ||
res.on('data', (chunk) => { | ||
const result = JSON.parse(chunk); | ||
console.log('[postFile] result: ', result); | ||
resolve(result); | ||
}); | ||
}); | ||
|
||
req.on('error', (e) => { | ||
console.log(`[postFile] problem with request: ${e.message}`); | ||
reject(e); | ||
}); | ||
|
||
const boundaryKey = Math.random().toString(16); | ||
const endData = `\r\n----${boundaryKey}--`; | ||
|
||
let dataLength = 0; | ||
const dataArr: Array<Record<string, any>> = []; | ||
for (const item of fileDataInfo) { | ||
const dataInfo = `\r\n----${boundaryKey}\r\n` + `Content-Disposition: form-data; name="${item.urlKey}"\r\n\r\n${item.urlValue}`; | ||
const dataBinary = Buffer.from(dataInfo, 'utf-8'); | ||
dataLength += dataBinary.length; | ||
dataArr.push({ | ||
dataInfo, | ||
}); | ||
} | ||
|
||
const files: Array<Record<string, any>> = []; | ||
for (const item of fileKeyValue) { | ||
const content = `\r\n----${boundaryKey}\r\n` + 'Content-Type: application/octet-stream\r\n' + `Content-Disposition: form-data; name="${item.urlKey}"; filename="${path.basename(item.urlValue)}"\r\n` + 'Content-Transfer-Encoding: binary\r\n\r\n'; | ||
// 当编码为ascii时,中文会乱码。 | ||
const contentBinary = Buffer.from(content, 'utf-8'); | ||
files.push({ | ||
contentBinary, | ||
filePath: item.urlValue, | ||
}); | ||
} | ||
|
||
let contentLength = 0; | ||
for (const item of files) { | ||
const { filePath } = item; | ||
if (fs.existsSync(filePath)) { | ||
const stat = fs.statSync(filePath); | ||
contentLength += stat.size; | ||
} else { | ||
contentLength += Buffer.from('\r\n', 'utf-8').length; | ||
} | ||
contentLength += item.contentBinary.length; | ||
} | ||
|
||
req.setHeader('Content-Type', `multipart/form-data; boundary=--${boundaryKey}`); | ||
req.setHeader('Content-Length', dataLength + contentLength + Buffer.byteLength(endData)); | ||
|
||
// 将参数发出 | ||
for (const item of dataArr) { | ||
req.write(item.dataInfo); | ||
} | ||
|
||
let fileIndex = 0; | ||
const doOneFile = function () { | ||
req.write(files[fileIndex].contentBinary); | ||
const currentFilePath = files[fileIndex].filePath; | ||
if (fs.existsSync(currentFilePath)) { | ||
// @ts-ignore | ||
const fileStream = fs.createReadStream(currentFilePath, { bufferSize: 4 * 1024 }); | ||
fileStream.pipe(req, { end: false }); | ||
|
||
fileStream.on('end', () => { | ||
fileIndex += 1; | ||
if (fileIndex === files.length) { | ||
req.end(endData); | ||
} else { | ||
doOneFile(); | ||
} | ||
}); | ||
} else { | ||
req.write('\r\n'); | ||
fileIndex += 1; | ||
|
||
if (fileIndex === files.length) { | ||
req.end(endData); | ||
} else { | ||
doOneFile(); | ||
} | ||
} | ||
}; | ||
|
||
if (fileIndex === files.length) { | ||
req.end(endData); | ||
} else { | ||
doOneFile(); | ||
} | ||
})); | ||
} | ||
|
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 |
---|---|---|
|
@@ -3,4 +3,5 @@ export type IPublishOptions = { | |
port: string; | ||
path: string; | ||
method?: string; | ||
publishEnv?: string; | ||
}; |