1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.syncWatch = exports.chmod = exports.syncModules = exports.sync = exports.cmd = exports.args = exports.description = exports.alias = exports.name = void 0;
|
4 | var tslib_1 = require("tslib");
|
5 | var operators_1 = require("rxjs/operators");
|
6 | var common_1 = require("../common");
|
7 | var listCommand = require("./ls.cmd");
|
8 | exports.name = 'sync';
|
9 | exports.alias = ['s', 'sl'];
|
10 | exports.description = "Syncs each module's dependency tree within the workspace.";
|
11 | exports.args = {
|
12 | '-i': 'Include ignored modules.',
|
13 | '-w': 'Sync on changes to files.',
|
14 | '-v': 'Update version reference in package.json files.',
|
15 | };
|
16 | function cmd(args) {
|
17 | return tslib_1.__awaiter(this, void 0, void 0, function () {
|
18 | var options, watch, includeIgnored, updateVersions, config;
|
19 | return tslib_1.__generator(this, function (_a) {
|
20 | switch (_a.label) {
|
21 | case 0:
|
22 | options = (args && args.options) || {};
|
23 | watch = options.w || false;
|
24 | includeIgnored = options.i || false;
|
25 | updateVersions = options.v || false;
|
26 | config = { includeIgnored: includeIgnored, updateVersions: updateVersions };
|
27 | if (!watch) return [3, 2];
|
28 | return [4, syncWatch(config)];
|
29 | case 1:
|
30 | _a.sent();
|
31 | return [3, 4];
|
32 | case 2: return [4, sync(config)];
|
33 | case 3:
|
34 | _a.sent();
|
35 | _a.label = 4;
|
36 | case 4: return [2];
|
37 | }
|
38 | });
|
39 | });
|
40 | }
|
41 | exports.cmd = cmd;
|
42 | function sync(options) {
|
43 | if (options === void 0) { options = {}; }
|
44 | return tslib_1.__awaiter(this, void 0, void 0, function () {
|
45 | var _a, includeIgnored, write, settings, modules;
|
46 | return tslib_1.__generator(this, function (_b) {
|
47 | switch (_b.label) {
|
48 | case 0:
|
49 | _a = options.includeIgnored, includeIgnored = _a === void 0 ? false : _a;
|
50 | write = function (msg) { return common_1.util.write(msg, options.silent); };
|
51 | return [4, common_1.loadSettings()];
|
52 | case 1:
|
53 | settings = _b.sent();
|
54 | if (!settings) {
|
55 | write(common_1.log.yellow(common_1.constants.CONFIG_NOT_FOUND_ERROR));
|
56 | return [2];
|
57 | }
|
58 | modules = settings.modules
|
59 | .filter(function (pkg) { return common_1.filter.localDeps(pkg).length > 0; })
|
60 | .filter(function (pkg) { return common_1.filter.includeIgnored(pkg, includeIgnored); });
|
61 | return [4, syncModules(modules, options)];
|
62 | case 2:
|
63 | _b.sent();
|
64 | return [2, {
|
65 | settings: settings,
|
66 | modules: modules,
|
67 | }];
|
68 | }
|
69 | });
|
70 | });
|
71 | }
|
72 | exports.sync = sync;
|
73 | function syncModules(modules, options) {
|
74 | if (options === void 0) { options = {}; }
|
75 | return tslib_1.__awaiter(this, void 0, void 0, function () {
|
76 | var startedAt, _a, includeIgnored, _b, updateVersions, _c, silent, write, sync, tasks, _i, tasks_1, item, taskList, error_1;
|
77 | var _this = this;
|
78 | return tslib_1.__generator(this, function (_d) {
|
79 | switch (_d.label) {
|
80 | case 0:
|
81 | startedAt = new Date();
|
82 | _a = options.includeIgnored, includeIgnored = _a === void 0 ? false : _a, _b = options.updateVersions, updateVersions = _b === void 0 ? false : _b, _c = options.silent, silent = _c === void 0 ? false : _c;
|
83 | write = function (msg) { return common_1.util.write(msg, options.silent); };
|
84 | sync = function (sources, target) { return tslib_1.__awaiter(_this, void 0, void 0, function () {
|
85 | var _i, sources_1, source;
|
86 | return tslib_1.__generator(this, function (_a) {
|
87 | switch (_a.label) {
|
88 | case 0:
|
89 | _i = 0, sources_1 = sources;
|
90 | _a.label = 1;
|
91 | case 1:
|
92 | if (!(_i < sources_1.length)) return [3, 7];
|
93 | source = sources_1[_i];
|
94 | if (!source.package) return [3, 6];
|
95 | return [4, common_1.copy.module(source.package, target)];
|
96 | case 2:
|
97 | _a.sent();
|
98 | return [4, common_1.copy.logUpdate(target)];
|
99 | case 3:
|
100 | _a.sent();
|
101 | return [4, chmod(target)];
|
102 | case 4:
|
103 | _a.sent();
|
104 | if (!updateVersions) return [3, 6];
|
105 | return [4, common_1.updatePackageRef(target, source.package.name, source.package.version, {
|
106 | save: true,
|
107 | })];
|
108 | case 5:
|
109 | _a.sent();
|
110 | _a.label = 6;
|
111 | case 6:
|
112 | _i++;
|
113 | return [3, 1];
|
114 | case 7: return [2];
|
115 | }
|
116 | });
|
117 | }); };
|
118 | tasks = modules.map(function (target) {
|
119 | var sources = common_1.filter
|
120 | .localDeps(target)
|
121 | .filter(function (dep) { return common_1.filter.includeIgnored(dep.package, includeIgnored); });
|
122 | var sourceNames = sources.map(function (dep) { return " " + common_1.log.cyan(dep.name); });
|
123 | var title = common_1.log.magenta(target.name) + " " + common_1.log.cyan(sourceNames.length > 0 ? '⬅' : '') + sourceNames;
|
124 | return {
|
125 | title: title,
|
126 | task: function () { return sync(sources, target); },
|
127 | };
|
128 | });
|
129 | _d.label = 1;
|
130 | case 1:
|
131 | _d.trys.push([1, 9, , 10]);
|
132 | if (!silent) return [3, 6];
|
133 | _i = 0, tasks_1 = tasks;
|
134 | _d.label = 2;
|
135 | case 2:
|
136 | if (!(_i < tasks_1.length)) return [3, 5];
|
137 | item = tasks_1[_i];
|
138 | return [4, item.task()];
|
139 | case 3:
|
140 | _d.sent();
|
141 | _d.label = 4;
|
142 | case 4:
|
143 | _i++;
|
144 | return [3, 2];
|
145 | case 5: return [3, 8];
|
146 | case 6:
|
147 | taskList = common_1.listr(tasks, { concurrent: false });
|
148 | return [4, taskList.run()];
|
149 | case 7:
|
150 | _d.sent();
|
151 | write(common_1.log.gray(" " + common_1.elapsed(startedAt) + ", " + common_1.moment().format('h:mm:ssa')));
|
152 | write('');
|
153 | _d.label = 8;
|
154 | case 8: return [3, 10];
|
155 | case 9:
|
156 | error_1 = _d.sent();
|
157 | write(common_1.log.yellow("\nFailed while syncing module '" + error_1.message + "'."));
|
158 | return [3, 10];
|
159 | case 10: return [2, modules];
|
160 | }
|
161 | });
|
162 | });
|
163 | }
|
164 | exports.syncModules = syncModules;
|
165 | function chmod(module) {
|
166 | return tslib_1.__awaiter(this, void 0, void 0, function () {
|
167 | var dir, cmd, files, wait;
|
168 | return tslib_1.__generator(this, function (_a) {
|
169 | switch (_a.label) {
|
170 | case 0:
|
171 | dir = common_1.fs.join(module.dir, 'node_modules/.bin');
|
172 | return [4, common_1.fs.pathExistsSync(dir)];
|
173 | case 1:
|
174 | if (!(_a.sent())) {
|
175 | return [2, []];
|
176 | }
|
177 | cmd = common_1.exec.command("chmod 777");
|
178 | return [4, common_1.fs.readdir(dir)];
|
179 | case 2:
|
180 | files = (_a.sent()).map(function (name) { return common_1.fs.join(dir, name); });
|
181 | wait = files.map(function (path) {
|
182 | return cmd.clone().add(path).run({ silent: true });
|
183 | });
|
184 | return [4, Promise.all(wait)];
|
185 | case 3:
|
186 | _a.sent();
|
187 | return [2, files];
|
188 | }
|
189 | });
|
190 | });
|
191 | }
|
192 | exports.chmod = chmod;
|
193 | function syncWatch(options) {
|
194 | if (options === void 0) { options = {}; }
|
195 | return tslib_1.__awaiter(this, void 0, void 0, function () {
|
196 | var _a, includeIgnored, _b, silent, write, result, modules, settings;
|
197 | return tslib_1.__generator(this, function (_c) {
|
198 | switch (_c.label) {
|
199 | case 0:
|
200 | _a = options.includeIgnored, includeIgnored = _a === void 0 ? false : _a, _b = options.silent, silent = _b === void 0 ? false : _b;
|
201 | write = function (msg) { return common_1.util.write(msg, options.silent); };
|
202 | write(common_1.log.magenta('\nSync watching:'));
|
203 | return [4, listCommand.ls({
|
204 | dependencies: 'local',
|
205 | includeIgnored: includeIgnored,
|
206 | })];
|
207 | case 1:
|
208 | result = _c.sent();
|
209 | if (!result) {
|
210 | return [2];
|
211 | }
|
212 | modules = result.modules, settings = result.settings;
|
213 | modules.forEach(function (pkg) { return watch(pkg, modules, settings.watchPattern, includeIgnored, silent); });
|
214 | return [2];
|
215 | }
|
216 | });
|
217 | });
|
218 | }
|
219 | exports.syncWatch = syncWatch;
|
220 | function watch(pkg, modules, watchPattern, includeIgnored, silent) {
|
221 | var sync = function () {
|
222 | var dependants = common_1.dependsOn(pkg, modules);
|
223 | if (dependants.length > 0) {
|
224 | common_1.util.write(common_1.log.green(pkg.name + " changed: "), silent);
|
225 | syncModules(dependants, { includeIgnored: includeIgnored });
|
226 | }
|
227 | };
|
228 | common_1.file
|
229 | .watch(common_1.fs.join(pkg.dir, watchPattern))
|
230 | .pipe(operators_1.filter(function (path) { return !path.includes('node_modules/'); }), operators_1.debounceTime(1000))
|
231 | .subscribe(function () { return sync(); });
|
232 | }
|