@seayoo-web/finder 1.0.5 → 1.0.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/core.js +1 -1
- package/dist/plugin.vite.js +1 -1
- package/index.d.ts +81 -0
- package/index.js +7 -8
- package/package.json +1 -1
package/dist/core.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
const compressing=require("compressing"),pkg=require("../package.json"),needle=require("needle"),crypto=require("crypto"),fsp=require("fs/promises"),open=require("open"),path=require("path"),fs=require("fs"),os=require("os");require("colors");const finderServers={"finder.seayoo.io":[],"finder.seayoo.com":[],"finder.seayoo.internal":[]},paths={deploy:"/service/deploy",inspect:"/inspect/supported/projects",upload:"/service/upload"};let isDebugMode=!1;async function finderDeploy({dist:e,zip:r,archive:t,deployTo:i,user:n,key:o,debug:s,preview:a}){if(isDebugMode=!!s,!e&&!r)throw"部署参数 dist(文件目录) 或者 zip(打包文件) 需要二选一".bgBrightRed;if(r&&!/.+\.zip$/i.test(r))throw`部署参数 zip 需要指定一个zip文件 ${r}`;if(r&&!fs.existsSync(r))throw"部署参数 zip 指定的文件不存在(请确保传入完整文件路径)".bgBrightRed+" "+r;if(!(r||fs.existsSync(path.resolve(e))&&fs.lstatSync(path.resolve(e)).isDirectory()))throw"部署参数错误,dist(文件目录) 需要是一个存在的文件目录".bgBrightRed+" "+e;if(!i&&!t)throw"部署参数 archive(打包归档) 和 deployTo(部署文件) 至少需要二选一".bgBrightRed;const
|
|
1
|
+
const compressing=require("compressing"),pkg=require("../package.json"),needle=require("needle"),crypto=require("crypto"),fsp=require("fs/promises"),open=require("open"),path=require("path"),fs=require("fs"),os=require("os");require("colors");const finderServers={"finder.seayoo.io":[],"finder.seayoo.com":[],"finder.seayoo.internal":[]},paths={deploy:"/service/deploy",inspect:"/inspect/supported/projects",upload:"/service/upload"};let isDebugMode=!1;async function finderDeploy({dist:e,zip:r,archive:t,deployTo:i,user:n,key:o,debug:s,preview:a}){if(isDebugMode=!!s,!e&&!r)throw"部署参数 dist(文件目录) 或者 zip(打包文件) 需要二选一".bgBrightRed;if(r&&!/.+\.zip$/i.test(r))throw`部署参数 zip 需要指定一个zip文件 ${r}`;if(r&&!fs.existsSync(r))throw"部署参数 zip 指定的文件不存在(请确保传入完整文件路径)".bgBrightRed+" "+r;if(!(r||fs.existsSync(path.resolve(e))&&fs.lstatSync(path.resolve(e)).isDirectory()))throw"部署参数错误,dist(文件目录) 需要是一个存在的文件目录".bgBrightRed+" "+e;if(!i&&!t)throw"部署参数 archive(打包归档) 和 deployTo(部署文件) 至少需要二选一".bgBrightRed;const d=r||await async function(){const r=path.resolve(getTempDir(),getRandomZipFileName());try{await compressing.zip.compressDir(e,r,{ignoreBase:!0})}catch(e){throw"部署预处理之压缩文件目录失败".bgBrightRed+" "+e.toString()}return r}(),p=Buffer.from(fs.readFileSync(d));if(await archiveFile(d,t,r!==d),!i)return;const c=await deploy({target:i,buffer:p,user:n,key:o});return a&&c&&c.previewUrl&&open(c.previewUrl),c}async function finderUpload({file:e,deployTo:r,user:t,key:i,preview:n}){if(!e)throw"部署缺少参数 file(文件全路径)".bgBrightRed;if(e&&!fs.existsSync(e))throw"部署file参数指定的文件不存在(请确保传入完整文件路径)".bgBrightRed+" "+e;if(!r)throw"部署缺少参数 deployTo(部署目标)".bgBrightRed;const o=path.extname(e);r.endsWith(o)||(r=r.replace(/\/*$/,"")+"/"+md5(await fsp.readFile(e)));const s=await upload({target:r,buffer:Buffer.from(fs.readFileSync(e)),user:t,key:i});n&&s&&s.previewUrl&&open(s.previewUrl)}const getFinderServerFullPath=function(e){return(e.endsWith("internal")?"http://":"https://")+e};function getTempDir(){const e=path.resolve(os.tmpdir(),"webfinder");return fs.existsSync(e)||fs.mkdirSync(e,{recursive:!0}),e}function md5(e){return crypto.createHash("md5").update(e).digest("hex").toLowerCase()}function pure(e){return e.replace(/^https?:\/\//i,"")}function getRandomZipFileName(){return Date.now()+Math.random().toString(16).slice(2)+".zip"}function getDefaultArchiveFileName(){const e=new Date,r=e=>("0"+e).slice(-2);return[e.getFullYear(),r(e.getMonth()+1),r(e.getDate()),r(e.getHours()),r(e.getMinutes())].join("")+".zip"}async function archiveFile(e,r,t){if(!r)return;const i=/\.zip$/i.test(r)?path.resolve(r):path.resolve(r,getDefaultArchiveFileName());await fsp.mkdir(path.dirname(i),{recursive:!0}).catch((e=>{throw"检查归档目录失败 ".brightRed+" "+e.toString()})),await fsp.copyFile(e,i).catch((()=>{console.error("归档文件失败".brightRed)})),t&&await fsp.unlink(e).catch((()=>{console.error(`删除zip文件失败 ${e}`.brightRed)}))}async function deploy({target:e,buffer:r,user:t,key:i}){const n=await findTargetServer(e);if(!n)throw`finder不支持该域名部署,请检查 ${e}`.bgBrightRed;if(!t||!i)throw"部署缺少认证信息(user & key)".bgBrightRed;return new Promise(((o,s)=>{const a=getRandomZipFileName();needle.post(`${getFinderServerFullPath(n)}${paths.deploy}?target=${encodeURIComponent(pure(e))}`,{path:a,file:{buffer:r,filename:a,content_type:"application/octet-stream"}},{headers:{user:t,key:i},multipart:!0},(function(e,r){if(e)return s("部署接口错误".bgBrightRed+" "+JSON.stringify(e));const t=r.body;return t.err?s(t.desc):t.data&&"string"==typeof t.data?void o({previewUrl:t.data.endsWith("/")?t.data.replace(/\/*$/,"/")+"index.html?"+Math.random().toString(16).slice(2):t.data}):s("部署接口数据错误".bgBrightRed+" "+(r.body.message||JSON.stringify(r.body)))}))}))}async function upload({target:e,buffer:r,user:t,key:i}){const n=await findTargetServer(e);if(!n)throw`finder不支持该域名部署,请检查 ${e}`.bgBrightRed;if(!t||!i)throw"部署缺少认证信息(user & key)".bgBrightRed;return new Promise(((o,s)=>{const a=path.basename(e),d=path.dirname(pure(e));needle.post(`${getFinderServerFullPath(n)}${paths.upload}?target=${encodeURIComponent(d)}`,{path:a,file:{buffer:r,filename:a,content_type:"application/octet-stream"}},{headers:{user:t,key:i},multipart:!0},(function(r,t){if(r)return s("上传接口错误".bgBrightRed+" "+JSON.stringify(r));const i=t.body;return i.err?s(i.desc):i.data&&"string"==typeof i.data?void o({previewUrl:"https://"+pure(e)}):s("上传接口数据错误".bgBrightRed+" "+JSON.stringify(t.body))}))}))}async function findTargetServer(e){const r=pure(e);await updateSupportedProjects();for(const e in finderServers)if(finderServers[e].find((e=>r.startsWith(e))))return e;await updateSupportedProjects(!0);for(const e in finderServers)if(finderServers[e].find((e=>r.startsWith(e))))return e;return null}async function updateSupportedProjects(e){const r=Object.keys(finderServers);for(const t of r)finderServers[t]=await getServerSupportedProjects(t,e)||[]}async function getServerSupportedProjects(e,r){const t=path.resolve(getTempDir(),e+".json");if(fs.existsSync(t)&&!r){const e=fs.readFileSync(t).toString();try{const r=JSON.parse(e);if(Array.isArray(r))return r}catch(e){isDebugMode&&console.error("cache file form error".brightRed,e)}}return await new Promise(((r,i)=>{const n=`${getFinderServerFullPath(e)}${paths.inspect}`;needle.get(n,{user_agent:`web finder agent v${pkg.version}`},(function(i,o){if(i)return isDebugMode&&console.error(`服务器 ${n} 检查接口错误`.brightRed+" "+i.toString()),r([]);const s=o.body;if(!Array.isArray(s))return isDebugMode&&console.warn(`服务器 ${n} 接口返回内容错误`.brightRed+" "+JSON.stringify(s)),r([]);isDebugMode&&console.log(`${e} 支持服务器列表`,s);const a=s.map((e=>pure(e,!0)));fsp.writeFile(t,JSON.stringify(a)).then((()=>{r(a)}),(e=>{isDebugMode&&console.error("write cache error".brightRed,e),r(a)}))}))}))}exports.finderDeploy=finderDeploy,exports.finderUpload=finderUpload;
|
package/dist/plugin.vite.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
const{finderDeploy:finderDeploy}=require("./core"),
|
|
1
|
+
const{finderDeploy:finderDeploy}=require("./core"),path=require("path");require("colors"),module.exports=function(e){let r=null;return{name:"finerDeployAgent",generateBundle({dir:e}){r=process.cwd(),e&&(r=path.resolve(r,e))},async closeBundle(){await finderDeploy({preview:!0,archive:path.resolve(path.dirname(r),"archive"),...e,dist:r}).then((function(e){e&&console.log("部署成功".bgGreen,(e.previewUrl||"").green)}))}}};
|
package/index.d.ts
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
interface IDeployCoreOptions {
|
|
2
|
+
/** 要部署的最终目录,比如 https://xxx.com/test/path/ */
|
|
3
|
+
dist?: string;
|
|
4
|
+
/** 如果不指定目录,可以指定一个zip文件,指定zip后,dist无效 */
|
|
5
|
+
zip?: string,
|
|
6
|
+
/** 归档备份的文件名,如果不指定文件名,则按照时间戳命名zip文件 */
|
|
7
|
+
archive?: string,
|
|
8
|
+
/** 部署目标地址 */
|
|
9
|
+
deployTo: string,
|
|
10
|
+
/** 部署授权用户名 */
|
|
11
|
+
user: string,
|
|
12
|
+
/** 部署授权key */
|
|
13
|
+
key: string,
|
|
14
|
+
/** 是否部署完毕后打开目标文件 */
|
|
15
|
+
preview?: boolean
|
|
16
|
+
/** 是否调试输出更多信息 */
|
|
17
|
+
debug?: boolean
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
interface IDeployResult {
|
|
21
|
+
/** 页面预览地址 */
|
|
22
|
+
previewUrl: string
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
interface IUploadCoreOptions {
|
|
26
|
+
/** 需要上传的文件全路径 */
|
|
27
|
+
file: string,
|
|
28
|
+
/** 部署目标地址 */
|
|
29
|
+
deployTo: string,
|
|
30
|
+
/** 部署授权用户名 */
|
|
31
|
+
user: string,
|
|
32
|
+
/** 部署授权key */
|
|
33
|
+
key: string,
|
|
34
|
+
/** 是否部署完毕后打开目标文件 */
|
|
35
|
+
preview?: boolean
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
interface IVitePlugin {
|
|
39
|
+
name: string,
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/** 部署内容到 finder 中 */
|
|
43
|
+
export declare const finderDeploy: (options: IDeployCoreOptions) => Promise<IDeployResult | void>;
|
|
44
|
+
|
|
45
|
+
/** 上传本地文件到 finder */
|
|
46
|
+
export declare const finderUpload: (options: IUploadCoreOptions) => Promise<void>;
|
|
47
|
+
|
|
48
|
+
/** 部署用插件,支持 webpack 和 vite */
|
|
49
|
+
export declare const deployPlugin: {
|
|
50
|
+
/** webpack 部署插件 */
|
|
51
|
+
webpack: (options: {
|
|
52
|
+
/** 归档备份的文件名,如果不指定文件名,则按照时间戳命名zip文件 */
|
|
53
|
+
archive?: string,
|
|
54
|
+
/** 部署目标地址 */
|
|
55
|
+
deployTo: string,
|
|
56
|
+
/** 部署授权用户名 */
|
|
57
|
+
user: string,
|
|
58
|
+
/** 部署授权key */
|
|
59
|
+
key: string,
|
|
60
|
+
/** 是否部署完毕后打开目标文件 */
|
|
61
|
+
preview?: boolean
|
|
62
|
+
}) => void,
|
|
63
|
+
|
|
64
|
+
/** vite 部署插件 */
|
|
65
|
+
vite: (options: {
|
|
66
|
+
/** 如果不指定目录,可以指定一个zip文件,指定zip后,dist无效 */
|
|
67
|
+
zip?: string,
|
|
68
|
+
/** 归档备份的文件名,如果不指定文件名,则按照时间戳命名zip文件 */
|
|
69
|
+
archive?: string,
|
|
70
|
+
/** 是否调试输出更多信息 */
|
|
71
|
+
debug?: boolean,
|
|
72
|
+
/** 部署目标地址 */
|
|
73
|
+
deployTo: string,
|
|
74
|
+
/** 部署授权用户名 */
|
|
75
|
+
user: string,
|
|
76
|
+
/** 部署授权key */
|
|
77
|
+
key: string,
|
|
78
|
+
/** 是否部署完毕后打开目标文件 */
|
|
79
|
+
preview?: boolean
|
|
80
|
+
}) => IVitePlugin;
|
|
81
|
+
}
|
package/index.js
CHANGED
|
@@ -2,11 +2,10 @@ const { finderDeploy, finderUpload } = require('./dist/core');
|
|
|
2
2
|
const WebpackDeployPlugin = require('./dist/plugin.webpack');
|
|
3
3
|
const ViteDeployPlugin = require('./dist/plugin.vite');
|
|
4
4
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
}
|
|
5
|
+
exports.finderDeploy = finderDeploy;
|
|
6
|
+
exports.finderUpload = finderUpload;
|
|
7
|
+
|
|
8
|
+
exports.deployPlugin = {
|
|
9
|
+
webpack: WebpackDeployPlugin,
|
|
10
|
+
vite: ViteDeployPlugin
|
|
11
|
+
};
|