1 | import fsPath from "path";
|
2 | import Promise from "bluebird";
|
3 | import appInstall from "./app-install";
|
4 | import log from "./log";
|
5 | import { pathExists } from "./util";
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 | export default (id, localFolder, getVersion, startDownload, start, options = {}) => {
|
22 | return new Promise((resolve, reject) => {
|
23 | Promise.coroutine(function*() {
|
24 | let restart = false;
|
25 | const restartAfterUpdate = options.start === undefined ? true : options.start;
|
26 | const version = yield getVersion().catch(err => reject(err));
|
27 | const result = { id, updated: false, installed: false, version: version.remote };
|
28 |
|
29 |
|
30 | const nodeModulesPath = fsPath.join(localFolder, "node_modules");
|
31 | const nodeModulesExist = yield pathExists(nodeModulesPath).catch(err => reject(err));
|
32 |
|
33 | if (version.isUpdateRequired && !version.isDownloading) {
|
34 | log.info();
|
35 | log.info(
|
36 | version.local
|
37 | ? `Updating '${ id }' from v${ version.local } to v${ version.remote }...`
|
38 | : `First time initialization of '${ id }' at v${ version.remote }...`
|
39 | );
|
40 |
|
41 |
|
42 | yield startDownload().catch(err => reject(err));
|
43 | result.updated = true;
|
44 | restart = true;
|
45 | }
|
46 |
|
47 |
|
48 | if (!nodeModulesExist || version.isDependenciesChanged) {
|
49 | log.info(`Running [npm install] for '${ id }'...`);
|
50 | yield appInstall(localFolder);
|
51 | log.info(`...[npm install] complete for '${ id }'`);
|
52 | result.installed = true;
|
53 | restart = true;
|
54 | }
|
55 |
|
56 |
|
57 | if (restartAfterUpdate && restart) {
|
58 | yield start().catch(err => reject(err));
|
59 | }
|
60 |
|
61 |
|
62 | if (result.updated) {
|
63 | const msg = restartAfterUpdate && restart
|
64 | ? `...updated and restarted '${ id }' to v${ version.remote }.`
|
65 | : `...updated '${ id }' to v${ version.remote }.`;
|
66 | log.info("");
|
67 | log.info(msg);
|
68 | }
|
69 |
|
70 |
|
71 | resolve(result);
|
72 | })();
|
73 | });
|
74 | };
|