UNPKG

8.5 kBJavaScriptView Raw
1"use strict";
2var __assign = (this && this.__assign) || function () {
3 __assign = Object.assign || function(t) {
4 for (var s, i = 1, n = arguments.length; i < n; i++) {
5 s = arguments[i];
6 for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
7 t[p] = s[p];
8 }
9 return t;
10 };
11 return __assign.apply(this, arguments);
12};
13Object.defineProperty(exports, "__esModule", { value: true });
14var Fs = require("fs-extra");
15var git_1 = require("./git");
16var local_storage_1 = require("./local-storage");
17var manual_1 = require("./manual");
18var paths_1 = require("./paths");
19var step_1 = require("./step");
20var utils_1 = require("./utils");
21/**
22 This module contains package.json related methods
23 */
24// Updates all the dependencies in the package.json file based on a provided manifest.
25// If no manifest was provided an editor will be provided where we will be able to create
26// the manifest on the run. Rebase conflicts will be resolved automatically
27function updateDependencies(updatedDeps) {
28 if (updatedDeps) {
29 if (!(updatedDeps instanceof Object)) {
30 throw TypeError('New dependencies must be described using an object');
31 }
32 }
33 else {
34 var pack = Fs.readJsonSync(paths_1.Paths.npm.package);
35 var deps_1 = __assign({}, pack.dependencies, pack.devDependencies, pack.peerDependencies);
36 var versionColumn_1 = Object.keys(deps_1).reduce(function (depLength, dep) {
37 return depLength < dep.length ? dep.length : depLength;
38 }, 0);
39 var initialContent = '# Please pick the new versions of the project\'s dependencies\n\n';
40 initialContent += Object.keys(deps_1).sort().map(function (dep) {
41 return utils_1.Utils.padRight(dep, versionColumn_1) + " " + deps_1[dep];
42 }).join('\n');
43 var editedContent = git_1.Git.edit(initialContent);
44 if (initialContent === editedContent) {
45 return;
46 }
47 updatedDeps = editedContent
48 .trim()
49 .replace(/# .+/g, '')
50 .split('\n')
51 .map(function (line) { return line.trim(); })
52 .filter(Boolean)
53 .map(function (line) { return line.split(/\s+/); })
54 .reduce(function (prev, _a) {
55 var dep = _a[0], version = _a[1];
56 prev[dep] = version;
57 return prev;
58 }, {});
59 }
60 var packSteps = git_1.Git([
61 'log',
62 '--format=%s',
63 '--grep=^Step [0-9]\\+',
64 '--',
65 paths_1.Paths.npm.package,
66 ]).split('\n')
67 .filter(Boolean)
68 .map(function (line) { return step_1.Step.descriptor(line).number; });
69 var minPackSuperStep = Math.min.apply(Math, packSteps).toFixed();
70 var missingSuperSteps = git_1.Git([
71 'log',
72 '--format=%s',
73 '--grep=^Step [0-9]\\+:',
74 ]).split('\n')
75 .filter(Boolean)
76 .map(function (line) { return step_1.Step.descriptor(line).number.toString(); })
77 .filter(function (step) { return !packSteps.includes(step); })
78 .filter(function (step) { return step >= minPackSuperStep; });
79 var steps = []
80 .concat(packSteps)
81 .concat(missingSuperSteps);
82 // Checking if the root commit has affected the package.json, since it has been
83 // filtered in the last operation
84 var shouldEditRoot = git_1.Git([
85 'diff-tree', '--no-commit-id', '--name-only', '-r', git_1.Git.rootHash(),
86 ]).includes('package.json');
87 if (shouldEditRoot) {
88 steps.push('root');
89 }
90 step_1.Step.edit(steps);
91 var rebaseHooksDisabled;
92 try {
93 rebaseHooksDisabled = local_storage_1.localStorage.getItem('REBASE_HOOKS_DISABLED');
94 }
95 catch (e) {
96 rebaseHooksDisabled = '';
97 }
98 try {
99 // We completely trust Tortilla to do its thing right now and we don't need any
100 // modifications to the commit messages
101 local_storage_1.localStorage.setItem('REBASE_HOOKS_DISABLED', 1);
102 var _loop_1 = function () {
103 // Reading package.json content and ensuring it's formatted correctly
104 var packContent = Fs.readFileSync(paths_1.Paths.npm.package).toString();
105 // Plucking indention
106 var indent = packContent.match(/\{\n([^"]+)/) || [];
107 // Default indention
108 indent = indent[1] || '\s\s';
109 var currPackContent = void 0;
110 var headPackContent = currPackContent = packContent;
111 // Keep replacing conflict notations until we get both unresolved versions
112 for (var newHeadPackContent = void 0, newCurrPackContent = void 0; newHeadPackContent !== headPackContent &&
113 newCurrPackContent !== currPackContent; newHeadPackContent = headPackContent.replace(git_1.Git.conflict, '$1'),
114 newCurrPackContent = currPackContent.replace(git_1.Git.conflict, '$2')) {
115 // Force initialization
116 if (!newHeadPackContent || !newCurrPackContent) {
117 continue;
118 }
119 headPackContent = newHeadPackContent;
120 currPackContent = newCurrPackContent;
121 }
122 var headPack = JSON.parse(headPackContent);
123 var currPack = JSON.parse(currPackContent);
124 var depsTypes = ['dependencies', 'devDependencies', 'peerDependencies'];
125 // We have some conflicts to resolve
126 if (headPackContent !== currPackContent) {
127 // Picking the updated dependencies versions
128 depsTypes.forEach(function (depsType) {
129 if (!currPack[depsType] || !headPack[depsType]) {
130 return;
131 }
132 Object.keys(headPack[depsType]).forEach(function (dep) {
133 if (currPack[depsType][dep]) {
134 currPack[depsType][dep] = headPack[depsType][dep];
135 }
136 });
137 });
138 }
139 // Running a second update based on the provided manifest
140 Object.keys(updatedDeps).forEach(function (dep) {
141 // Picking the updated dependencies versions
142 depsTypes.forEach(function (depsType) {
143 if (!currPack[depsType]) {
144 return;
145 }
146 Object.keys(updatedDeps).forEach(function (dependencyName) {
147 if (currPack[depsType][dependencyName]) {
148 currPack[depsType][dependencyName] = updatedDeps[dependencyName];
149 }
150 });
151 });
152 });
153 Fs.writeFileSync(paths_1.Paths.npm.package, JSON.stringify(currPack, null, indent));
154 git_1.Git.print(['add', paths_1.Paths.npm.package]);
155 // If this is root commit or a super-step, re-render the correlated manual
156 var commitMsg = git_1.Git.recentCommit(['--format=%s']);
157 var isSuperStep = !!step_1.Step.superDescriptor(commitMsg);
158 // If there are staged files, it's probably a conflict in the following step in
159 // we're not really in a super step
160 if (isSuperStep && !git_1.Git.stagedFiles().length) {
161 manual_1.Manual.render();
162 }
163 try {
164 git_1.Git.print(['rebase', '--continue'], { env: { GIT_EDITOR: true } });
165 }
166 catch (e) {
167 var modifiedFiles = git_1.Git(['diff', '--name-only'])
168 .split('\n')
169 .filter(Boolean);
170 // Checking if only package.json is both modified
171 var expectedConflict = modifiedFiles.length === 2 && modifiedFiles.every(function (file) {
172 return file === 'package.json';
173 });
174 if (!expectedConflict) {
175 throw e;
176 }
177 }
178 };
179 while (git_1.Git.rebasing()) {
180 _loop_1();
181 }
182 }
183 finally {
184 // Resume editing commit messages
185 if (rebaseHooksDisabled) {
186 local_storage_1.localStorage.setItem('REBASE_HOOKS_DISABLED', rebaseHooksDisabled);
187 }
188 else {
189 local_storage_1.localStorage.removeItem('REBASE_HOOKS_DISABLED');
190 }
191 }
192}
193exports.Package = {
194 updateDependencies: updateDependencies,
195};
196//# sourceMappingURL=package.js.map
\No newline at end of file