UNPKG

4.08 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.downloadFromUrl = downloadFromUrl;
7exports.unzip = unzip;
8exports.writePackageJsonSync = writePackageJsonSync;
9exports.startProcess = startProcess;
10exports.hasDependency = hasDependency;
11
12var _fs = require('fs');
13
14var _unzip = require('unzip2');
15
16var _https = require('https');
17
18var _crossSpawn = require('cross-spawn');
19
20var _crossSpawn2 = _interopRequireDefault(_crossSpawn);
21
22function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
23
24/**
25 * Download file from url and write to target.
26 *
27 * @param {string} source - The url to the file to download.
28 * @param {string} target - The path to the file destination.
29 *
30 * @return {string} - The path to the file destination.
31 */
32function 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 * Extract zip file to target and unlink zip file.
55 *
56 * @param {string} source - The path to the zip file.
57 * @param {string} target - The path to the unzip destination.
58 */
59function 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 * Write minimal package.json to destination.
80 *
81 * @param {string} target - The path to the target package.json.
82 * @param {string} name - The name of the theme.
83 */
84function 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 * Start a child process and stream output.
99 *
100 * @param {string} command - The command to run.
101 * @param {string} args - List of string arguments.
102 * @param {string} args - Options object
103 *
104 * See: https://nodejs.org/api/child_process.html#child_process_child_process_spawn_command_args_options
105 */
106function 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 * Check for dependency name on package.json
125 *
126 * @param {string} dependencyName - The name of the dependency.
127 * @param {object} pkg - The package.json object.
128 */
129function 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