1 | 'use strict';
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 | exports.downloadFromUrl = downloadFromUrl;
|
7 | exports.unzip = unzip;
|
8 | exports.writePackageJsonSync = writePackageJsonSync;
|
9 | exports.startProcess = startProcess;
|
10 | exports.hasDependency = hasDependency;
|
11 |
|
12 | var _fs = require('fs');
|
13 |
|
14 | var _unzip = require('unzip2');
|
15 |
|
16 | var _https = require('https');
|
17 |
|
18 | var _crossSpawn = require('cross-spawn');
|
19 |
|
20 | var _crossSpawn2 = _interopRequireDefault(_crossSpawn);
|
21 |
|
22 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 | function downloadFromUrl(source, target) {
|
33 | return new Promise(function (resolve, reject) {
|
34 | var themeZipFile = (0, _fs.createWriteStream)(target);
|
35 |
|
36 | themeZipFile.on('open', function () {
|
37 | (0, _https.get)(source, function (response) {
|
38 | response.pipe(themeZipFile);
|
39 | });
|
40 | });
|
41 |
|
42 | themeZipFile.on('error', function (err) {
|
43 | (0, _fs.unlinkSync)(target);
|
44 | reject(err);
|
45 | });
|
46 |
|
47 | themeZipFile.on('close', function () {
|
48 | resolve(target);
|
49 | });
|
50 | });
|
51 | }
|
52 |
|
53 |
|
54 |
|
55 |
|
56 |
|
57 |
|
58 |
|
59 | function unzip(source, target) {
|
60 | return new Promise(function (resolve, reject) {
|
61 | var zipFile = (0, _fs.createReadStream)(source);
|
62 |
|
63 | zipFile.on('error', function (err) {
|
64 | reject(err);
|
65 | });
|
66 |
|
67 | zipFile.on('close', function () {
|
68 | (0, _fs.unlinkSync)(source);
|
69 | resolve(target);
|
70 | });
|
71 |
|
72 | zipFile.pipe((0, _unzip.Extract)({
|
73 | path: target
|
74 | }));
|
75 | });
|
76 | }
|
77 |
|
78 |
|
79 |
|
80 |
|
81 |
|
82 |
|
83 |
|
84 | function writePackageJsonSync(target) {
|
85 | var name = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'theme';
|
86 |
|
87 | var pkg = {
|
88 | name: name,
|
89 | version: '0.0.1'
|
90 | };
|
91 |
|
92 | var data = JSON.stringify(pkg, null, 2);
|
93 |
|
94 | (0, _fs.writeFileSync)(target, data);
|
95 | }
|
96 |
|
97 |
|
98 |
|
99 |
|
100 |
|
101 |
|
102 |
|
103 |
|
104 |
|
105 |
|
106 | function startProcess(command, args, options) {
|
107 | var defaultedOptions = options || {};
|
108 | defaultedOptions.stdio = defaultedOptions.stdio || 'inherit';
|
109 |
|
110 | return new Promise(function (resolve, reject) {
|
111 | var child = (0, _crossSpawn2.default)(command, args, defaultedOptions);
|
112 |
|
113 | child.on('error', function (err) {
|
114 | reject(err);
|
115 | });
|
116 |
|
117 | child.on('close', function (code) {
|
118 | resolve(code);
|
119 | });
|
120 | });
|
121 | }
|
122 |
|
123 |
|
124 |
|
125 |
|
126 |
|
127 |
|
128 |
|
129 | function hasDependency(dependencyName, pkg) {
|
130 | var depKeys = ['dependencies', 'devDependencies'];
|
131 | var hasDependencies = false;
|
132 |
|
133 | var _iteratorNormalCompletion = true;
|
134 | var _didIteratorError = false;
|
135 | var _iteratorError = undefined;
|
136 |
|
137 | try {
|
138 | for (var _iterator = depKeys[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
|
139 | var key = _step.value;
|
140 |
|
141 | if (key in pkg && dependencyName in pkg[key]) {
|
142 | hasDependencies = true;
|
143 | break;
|
144 | }
|
145 | }
|
146 | } catch (err) {
|
147 | _didIteratorError = true;
|
148 | _iteratorError = err;
|
149 | } finally {
|
150 | try {
|
151 | if (!_iteratorNormalCompletion && _iterator.return) {
|
152 | _iterator.return();
|
153 | }
|
154 | } finally {
|
155 | if (_didIteratorError) {
|
156 | throw _iteratorError;
|
157 | }
|
158 | }
|
159 | }
|
160 |
|
161 | return hasDependencies;
|
162 | } |
\ | No newline at end of file |