cli: add Lockfile.save

This commit is contained in:
Patrik Oldsberg
2020-11-22 17:42:52 +01:00
parent 06432a0e99
commit b90ad40d1c
4 changed files with 22 additions and 12 deletions
+7 -4
View File
@@ -150,8 +150,7 @@ export default async () => {
console.log();
// Finally we make sure the new lockfile doesn't have any duplicates
const lockfilePath = paths.resolveTargetRoot('yarn.lock');
const lockfile = await Lockfile.load(lockfilePath);
const lockfile = await Lockfile.load(paths.resolveTargetRoot('yarn.lock'));
const result = lockfile.analyze({
filter: name => dependencyMap.has(name),
});
@@ -160,7 +159,7 @@ export default async () => {
console.log();
console.log('Removing duplicate dependencies from yarn.lock');
lockfile.replaceVersions(result.newVersions);
await fs.writeFile(lockfilePath, lockfile.toString(), 'utf8');
await lockfile.save();
console.log(
"Running 'yarn install' to remove duplicates from node_modules",
@@ -195,5 +194,9 @@ async function workerThreads<T>(
await pop();
}
return Promise.all(Array(count).fill(0).map(pop));
return Promise.all(
Array(count)
.fill(0)
.map(pop),
);
}
+2 -5
View File
@@ -14,7 +14,6 @@
* limitations under the License.
*/
import fs from 'fs-extra';
import { Command } from 'commander';
import { Lockfile } from '../../lib/versioning';
import { paths } from '../../lib/paths';
@@ -35,8 +34,7 @@ export default async (cmd: Command) => {
let success = true;
const lockfilePath = paths.resolveTargetRoot('yarn.lock');
const lockfile = await Lockfile.load(lockfilePath);
const lockfile = await Lockfile.load(paths.resolveTargetRoot('yarn.lock'));
const result = lockfile.analyze({
filter: name => INCLUDED.some(pattern => pattern.test(name)),
});
@@ -49,8 +47,7 @@ export default async (cmd: Command) => {
if (fix) {
lockfile.replaceVersions(result.newVersions);
await fs.writeFile(lockfilePath, lockfile.toString(), 'utf8');
await lockfile.save();
} else {
const [
newVersionsForbidden,
@@ -14,6 +14,7 @@
* limitations under the License.
*/
import fs from 'fs-extra';
import mockFs from 'mock-fs';
import { Lockfile } from './Lockfile';
@@ -84,7 +85,7 @@ describe('Lockfile', () => {
expect(lockfile.toString()).toBe(mockA);
});
it('should deduplicate mockA', async () => {
it('should deduplicate and save mockA', async () => {
mockFs({
'/yarn.lock': mockA,
});
@@ -107,6 +108,10 @@ describe('Lockfile', () => {
expect(lockfile.toString()).toBe(mockA);
lockfile.replaceVersions(result.newVersions);
expect(lockfile.toString()).toBe(mockADedup);
await expect(fs.readFile('/yarn.lock', 'utf8')).resolves.toBe(mockA);
await expect(lockfile.save()).resolves.toBeUndefined();
await expect(fs.readFile('/yarn.lock', 'utf8')).resolves.toBe(mockADedup);
});
it('should deduplicate mockB', async () => {
+7 -2
View File
@@ -91,10 +91,11 @@ export class Lockfile {
queries.push({ range, version: value.version });
}
return new Lockfile(packages, data);
return new Lockfile(path, packages, data);
}
constructor(
private constructor(
private readonly path: string,
private readonly packages: Map<string, LockfileQueryEntry[]>,
private readonly data: LockfileData,
) {}
@@ -242,6 +243,10 @@ export class Lockfile {
}
}
async save() {
await fs.writeFile(this.path, this.toString(), 'utf8');
}
toString() {
return stringifyLockfile(this.data);
}