UNPKG

6.65 kBJavaScriptView Raw
1"use strict";
2const fs = require("fs");
3const path = require("path");
4const util_1 = require("./util");
5const promisifed_fs_1 = require("./promisifed-fs");
6const promise_1 = require("./promise");
7const events_1 = require("events");
8const bluebird_1 = require("bluebird");
9const awaiter_1 = require("./awaiter");
10const __awaiter = awaiter_1.tsAwaiter;
11Array.isArray(__awaiter);
12function addHandler(emitter, event, handler) {
13 emitter.on(event, handler);
14}
15class Packager {
16 constructor(options, repositoryInfo = null) {
17 this.options = options;
18 this.repositoryInfo = repositoryInfo;
19 this.isTwoPackageJsonProjectLayoutUsed = true;
20 this.eventEmitter = new events_1.EventEmitter();
21 this.projectDir = options.projectDir == null ? process.cwd() : path.resolve(options.projectDir);
22 this.appDir = this.computeAppDirectory();
23 }
24 artifactCreated(handler) {
25 addHandler(this.eventEmitter, "artifactCreated", handler);
26 return this;
27 }
28 get devPackageFile() {
29 return path.join(this.projectDir, "package.json");
30 }
31 build() {
32 return __awaiter(this, void 0, void 0, function* () {
33 const buildPackageFile = this.devPackageFile;
34 const appPackageFile = this.projectDir === this.appDir ? buildPackageFile : path.join(this.appDir, "package.json");
35 yield bluebird_1.Promise.all(Array.from(new Set([buildPackageFile, appPackageFile]), promisifed_fs_1.parseJsonFile))
36 .then((result) => {
37 this.metadata = result[result.length - 1];
38 this.devMetadata = result[0];
39 this.checkMetadata(appPackageFile);
40 this.electronVersion = util_1.getElectronVersion(this.devMetadata, buildPackageFile);
41 });
42 const cleanupTasks = [];
43 return promise_1.executeFinally(this.doBuild(cleanupTasks), error => promise_1.all(cleanupTasks.map(it => it())));
44 });
45 }
46 doBuild(cleanupTasks) {
47 return __awaiter(this, void 0, void 0, function* () {
48 const distTasks = [];
49 for (let platform of normalizePlatforms(this.options.platform)) {
50 const helper = this.createHelper(platform, cleanupTasks);
51 const archs = platform === "darwin" ? ["x64"] : (this.options.arch == null || this.options.arch === "all" ? ["ia32", "x64"] : [this.options.arch]);
52 for (let arch of archs) {
53 yield this.installAppDependencies(arch);
54 const outDir = path.join(this.projectDir, "dist", this.metadata.name + "-" + platform + "-" + arch);
55 yield helper.pack(platform, arch, outDir);
56 if (this.options.dist) {
57 distTasks.push(helper.packageInDistributableFormat(outDir, arch));
58 }
59 }
60 }
61 return yield bluebird_1.Promise.all(distTasks);
62 });
63 }
64 createHelper(platform, cleanupTasks) {
65 switch (platform) {
66 case "darwin":
67 case "osx":
68 {
69 const helperClass = require("./macPackager").default;
70 return new helperClass(this, cleanupTasks);
71 }
72 case "win32":
73 case "win":
74 case "windows":
75 {
76 const helperClass = require("./winPackager").default;
77 return new helperClass(this, cleanupTasks);
78 }
79 case "linux":
80 {
81 const helperClass = require("./linuxPackager").default;
82 return new helperClass(this);
83 }
84 default:
85 throw new Error("Unsupported platform: " + platform);
86 }
87 }
88 computeAppDirectory() {
89 let customAppPath = this.options.appDir;
90 let required = true;
91 if (customAppPath == null) {
92 customAppPath = util_1.DEFAULT_APP_DIR_NAME;
93 required = false;
94 }
95 let absoluteAppPath = path.join(this.projectDir, customAppPath);
96 try {
97 fs.accessSync(absoluteAppPath);
98 }
99 catch (e) {
100 if (required) {
101 throw new Error(customAppPath + " doesn't exists, " + e.message);
102 }
103 else {
104 this.isTwoPackageJsonProjectLayoutUsed = false;
105 return this.projectDir;
106 }
107 }
108 return absoluteAppPath;
109 }
110 checkMetadata(appPackageFile) {
111 const reportError = (missedFieldName) => {
112 throw new Error("Please specify '" + missedFieldName + "' in the application package.json ('" + appPackageFile + "')");
113 };
114 const metadata = this.metadata;
115 if (metadata.name == null) {
116 reportError("name");
117 }
118 else if (metadata.description == null) {
119 reportError("description");
120 }
121 else if (metadata.version == null) {
122 reportError("version");
123 }
124 else if (metadata.build == null) {
125 throw new Error("Please specify 'build' configuration in the application package.json ('" + appPackageFile + "'), at least\n\n" +
126 JSON.stringify({
127 build: {
128 "app-bundle-id": "your.id",
129 "app-category-type": "your.app.category.type",
130 "iconUrl": "see https://github.com/develar/electron-complete-builder#in-short",
131 }
132 }, null, " ") + "\n\n is required.\n");
133 }
134 else if (metadata.author == null) {
135 reportError("author");
136 }
137 }
138 installAppDependencies(arch) {
139 if (this.isTwoPackageJsonProjectLayoutUsed) {
140 return util_1.installDependencies(this.appDir, arch, this.electronVersion);
141 }
142 else {
143 util_1.log("Skipping app dependencies installation because dev and app dependencies are not separated");
144 return Promise.resolve(null);
145 }
146 }
147}
148exports.Packager = Packager;
149function normalizePlatforms(platforms) {
150 if (platforms == null || platforms.length === 0) {
151 return [process.platform];
152 }
153 else if (platforms[0] === "all") {
154 if (process.platform === "darwin") {
155 return ["darwin", "linux", "win32"];
156 }
157 else if (process.platform === "linux") {
158 return ["linux", "win32"];
159 }
160 else {
161 return ["win32"];
162 }
163 }
164 else {
165 return platforms;
166 }
167}
168//# sourceMappingURL=packager.js.map
\No newline at end of file