1 | import fs from "fs-extra";
|
2 | import fsPath from "path";
|
3 | import Promise from "bluebird";
|
4 | import { getLocalPackage } from "./app-package";
|
5 | import { pathExists } from "./util";
|
6 | import log from "./log";
|
7 |
|
8 | const fsRemove = Promise.promisify(fs.remove);
|
9 | const fsReadDir = Promise.promisify(fs.readdir);
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 | export default (id, localFolder, repo, subFolder, branch, options = {}) => {
|
32 | const force = options.force === undefined ? true : options.force;
|
33 |
|
34 | return new Promise((resolve, reject) => {
|
35 |
|
36 | const localPackage = () => getLocalPackage(id, localFolder).catch(err => reject(err));
|
37 |
|
38 | const onComplete = (wasDownloaded, result) => {
|
39 | Promise.coroutine(function*() {
|
40 | const local = yield localPackage().catch(err => reject(err));
|
41 | result.version = local.json.version;
|
42 | if (wasDownloaded) {
|
43 | log.info(`...downloaded '${ id }'.`);
|
44 | }
|
45 | resolve(result);
|
46 | })();
|
47 | };
|
48 |
|
49 |
|
50 | const download = () => {
|
51 |
|
52 | Promise.coroutine(function*() {
|
53 |
|
54 | log.info(`Downloading '${ id }'...`);
|
55 | const files = yield repo.get(subFolder, { branch }).catch(err => reject(err));
|
56 |
|
57 |
|
58 |
|
59 |
|
60 | if (yield pathExists(localFolder)) {
|
61 | const fileNames = yield fsReadDir(localFolder).catch(err => reject(err));
|
62 | for (let fileName of fileNames) {
|
63 | if (fileName !== "node_modules") {
|
64 | yield fsRemove(fsPath.join(localFolder, fileName));
|
65 | }
|
66 | }
|
67 | }
|
68 |
|
69 |
|
70 | yield files.save(localFolder).catch(err => reject(err));
|
71 |
|
72 |
|
73 | onComplete(true, { id });
|
74 | })();
|
75 | };
|
76 |
|
77 |
|
78 | Promise.coroutine(function*() {
|
79 | if (force) {
|
80 | download();
|
81 | } else {
|
82 | const local = yield localPackage();
|
83 | if (local.exists) {
|
84 | onComplete(false, { alreadyExists: true });
|
85 | } else {
|
86 | download();
|
87 | }
|
88 | }
|
89 | })();
|
90 | });
|
91 | };
|