1 | import { constants, elapsed, filter as filterUtil, listr, loadSettings, log, moment, updatePackageRef, util, } from '../common';
|
2 | export const name = 'sync-versions';
|
3 | export const alias = ['v', 'sv'];
|
4 | export const description = 'Updates version reference in package.json files.';
|
5 | export const args = {
|
6 | '-i': 'Include ignored modules.',
|
7 | };
|
8 | export async function cmd(args) {
|
9 | const options = (args && args.options) || {};
|
10 | const includeIgnored = options.i || false;
|
11 | const config = { includeIgnored };
|
12 | await syncVersions(config);
|
13 | }
|
14 | export async function syncVersions(options = {}) {
|
15 | const { includeIgnored = false } = options;
|
16 | const write = (msg) => util.write(msg, options.silent);
|
17 | const settings = await loadSettings();
|
18 | if (!settings) {
|
19 | write(log.yellow(constants.CONFIG_NOT_FOUND_ERROR));
|
20 | return;
|
21 | }
|
22 | const modules = settings.modules
|
23 | .filter((pkg) => filterUtil.localDeps(pkg).length > 0)
|
24 | .filter((pkg) => filterUtil.includeIgnored(pkg, includeIgnored));
|
25 | await syncModules(modules, options);
|
26 | return {
|
27 | settings: settings,
|
28 | modules,
|
29 | };
|
30 | }
|
31 | async function syncModules(modules, options = {}) {
|
32 | const startedAt = new Date();
|
33 | const { includeIgnored = false, silent = false } = options;
|
34 | const write = (msg) => util.write(msg, options.silent);
|
35 | const sync = async (sources, target) => {
|
36 | for (const source of sources) {
|
37 | if (source.package) {
|
38 | await updatePackageRef(target, source.package.name, source.package.version, { save: true });
|
39 | }
|
40 | }
|
41 | };
|
42 | const tasks = modules.map((target) => {
|
43 | const sources = filterUtil
|
44 | .localDeps(target)
|
45 | .filter((dep) => filterUtil.includeIgnored(dep.package, includeIgnored));
|
46 | const title = log.magenta(target.name);
|
47 | return {
|
48 | title,
|
49 | task: () => sync(sources, target),
|
50 | };
|
51 | });
|
52 | try {
|
53 | if (silent) {
|
54 | for (const item of tasks) {
|
55 | await item.task();
|
56 | }
|
57 | }
|
58 | else {
|
59 | const taskList = listr(tasks, { concurrent: false });
|
60 | await taskList.run();
|
61 | write(log.gray(` ${elapsed(startedAt)}, ${moment().format('h:mm:ssa')}`));
|
62 | write('');
|
63 | }
|
64 | }
|
65 | catch (error) {
|
66 | write(log.yellow(`\nFailed while syncing module '${error.message}'.`));
|
67 | }
|
68 | return modules;
|
69 | }
|