UNPKG

4.94 kBJavaScriptView Raw
1import config from "../config";
2import rimraf from "rimraf";
3import path from "path";
4import fs from "fs";
5import { copyFiles, readJson, mkdirs, runCommands } from "node-pearls";
6import console from "cli-console";
7import archiver from "archiver";
8
9// 将js文件先打tar包再打zip包
10function tarzip(file, outputFile, zipname){
11 return new Promise(function(resolve, reject){
12 var dir = path.dirname(file);
13
14 var tarfile = path.join(dir, zipname + ".tar");
15
16 var input = fs.createReadStream(file);
17 var tarwrite = fs.createWriteStream(tarfile);
18 // 打tar包
19 let tar = archiver('tar');
20 tar.on('error', function(err){
21 reject(err);
22 });
23 tar.append(input, {
24 name: "framework.js"
25 });
26 tar.pipe(tarwrite);
27 tar.finalize();
28 tarwrite.on("close", function(){
29 var tarread = fs.createReadStream(tarfile);
30 var output = fs.createWriteStream(outputFile);
31 // 打zip包
32 let zip = archiver('zip');
33 zip.on('error', function(err){
34 reject(err);
35 });
36
37 zip.append(tarread, {
38 name: zipname + ".tar"
39 });
40 zip.pipe(output);
41
42 zip.finalize();
43
44 output.on("close", function(){
45 fs.unlink(tarfile);
46 resolve();
47 });
48 });
49 });
50}
51
52export default async function(options){
53 // 要打包的平台
54 const platforms = options.platforms ? options.platforms.split(",").map(platform => platform.trim()) : [];
55 const zipname = options.zipname || "framework";
56 const iosZipname = options.iosZipname || zipname + "_package";
57 const androidZipname = options.androidZipname || zipname + "_package";
58 const pushBranch = options.pushBranch || "master";
59 const versionRule = options.version;
60 const currentDir = process.cwd();
61 const tmpInstallDir = path.join(config["tmp-dir"], "framework-tmp-dir");
62
63 mkdirs.sync(tmpInstallDir);
64 fs.writeFileSync(path.join(tmpInstallDir, "package.json"), JSON.stringify({
65 name: "framework-tmp-dir",
66 version: "0.0.1"
67 }));
68 await Promise.all(platforms.map(platform => (async () => {
69 let modName = `enjoy-bundle-framework-${platform}`;
70 await runCommands(`npm i ${modName}` + (versionRule ? `@${versionRule}` : ""), {
71 cwd: tmpInstallDir
72 });
73 let version = readJson.sync(path.join(tmpInstallDir, "node_modules", modName, "package.json")).version;
74
75 if(platform === "ios"){
76 let gitname = `elongreactnativepackages@${pushBranch}`;
77 let gitdir = path.join(config["tmp-dir"], gitname);
78
79 console.title("拉取项目");
80 if(!fs.existsSync(gitdir)){
81 await runCommands(`
82 git clone git@git.17usoft.com:ios/elongreactnativepackages.git ${gitdir} -b ${pushBranch} --depth 1
83 `);
84 }else{
85 await runCommands(`
86 git pull
87 `, {
88 cwd: gitdir
89 });
90 }
91
92 console.title("压缩拷贝文件");
93 await tarzip(
94 path.join(path.join(tmpInstallDir, "node_modules", modName, options.release ? "index.js" : "index.dev.js")),
95 path.join(gitdir, iosZipname + ".zip"),
96 iosZipname
97 );
98
99 console.title("提交git");
100 await runCommands(`
101 git add .
102 git commit -m 0:framework@${version}:true
103 git push
104 `, {
105 cwd: gitdir
106 });
107 }else if(platform === "android"){
108 let gitname = `as_elongrnplugin_44_jsbundle@${pushBranch}`;
109 let gitdir = path.join(config["tmp-dir"], gitname);
110
111 console.title("拉取项目");
112 if(!fs.existsSync(gitdir)){
113 await runCommands(`
114 git clone git@git.17usoft.com:elongtravel_android/as_elongrnplugin_44_jsbundle.git ${gitdir} -b ${pushBranch} --depth 1
115 `);
116 }else{
117 await runCommands(`
118 git pull
119 `, {
120 cwd: gitdir
121 });
122 }
123
124 console.title("压缩拷贝文件");
125 await tarzip(
126 path.join(path.join(tmpInstallDir, "node_modules", modName, options.release ? "index.js" : "index.dev.js")),
127 path.join(gitdir, androidZipname + ".zip"),
128 androidZipname
129 );
130
131 console.title("提交git");
132 await runCommands(`
133 git add .
134 git commit -m 0:framework@${version}:true
135 git push
136 `, {
137 cwd: gitdir
138 });
139 }else{
140
141 }
142 })()));
143}
\No newline at end of file