-
-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(core): spread getInstallations across dir
- Loading branch information
1 parent
9a9a530
commit feb146d
Showing
13 changed files
with
166 additions
and
117 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import { DependencyType, SyncpackConfig } from '../../../constants'; | ||
import { getDependencyTypes } from '../get-dependency-types'; | ||
import { SourceWrapper } from '../get-wrappers'; | ||
import { getInstallationsOf } from './get-installations-of'; | ||
|
||
export interface Installation { | ||
/** which section the package was installed in */ | ||
type: DependencyType; | ||
/** eg 'lodash' */ | ||
name: string; | ||
/** package.json file contents */ | ||
source: SourceWrapper; | ||
/** eg '0.1.0' */ | ||
version: string; | ||
} | ||
|
||
export interface InstalledPackage { | ||
/** eg 'lodash' */ | ||
name: string; | ||
/** each location this package is installed */ | ||
installations: Installation[]; | ||
} | ||
|
||
export function* getDependencies( | ||
wrappers: SourceWrapper[], | ||
options: Pick<SyncpackConfig, 'dev' | 'peer' | 'prod'>, | ||
): Generator<InstalledPackage> { | ||
const types = getDependencyTypes(options); | ||
const visited: { [name: string]: boolean } = {}; | ||
for (const type of types) { | ||
for (const wrapper of wrappers) { | ||
if (wrapper.contents[type]) { | ||
for (const name in wrapper.contents[type]) { | ||
if (visited[name] === undefined) { | ||
visited[name] = true; | ||
yield { | ||
installations: Array.from(getInstallationsOf(name, types, wrappers)), | ||
name, | ||
}; | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { DependencyType } from '../../../constants'; | ||
import { SourceWrapper } from '../get-wrappers'; | ||
import { Installation } from './get-dependencies'; | ||
|
||
export function* getInstallationsOf( | ||
name: string, | ||
types: DependencyType[], | ||
wrappers: SourceWrapper[], | ||
): Generator<Installation> { | ||
for (const type of types) { | ||
for (const wrapper of wrappers) { | ||
const dependencies = wrapper.contents[type]; | ||
if (dependencies && dependencies[name]) { | ||
yield { | ||
name, | ||
source: wrapper, | ||
type, | ||
version: dependencies[name], | ||
}; | ||
} | ||
} | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
src/commands/lib/installations/get-mismatched-dependencies.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import 'expect-more-jest'; | ||
import { DEFAULT_CONFIG } from '../../../constants'; | ||
import { SourceWrapper } from '../get-wrappers'; | ||
import { Installation } from './get-dependencies'; | ||
import { getMismatchedDependencies } from './get-mismatched-dependencies'; | ||
|
||
const mocked = { | ||
projects: (): SourceWrapper[] => [ | ||
{ filePath: '', contents: { dependencies: { chalk: '2.3.0' } } }, | ||
{ filePath: '', contents: { devDependencies: { jest: '22.1.4' } } }, | ||
{ filePath: '', contents: { peerDependencies: { jest: '22.1.4' } } }, | ||
{ filePath: '', contents: { dependencies: { chalk: '1.0.0' } } }, | ||
{ filePath: '', contents: { dependencies: { biggy: '0.1.0' } } }, | ||
], | ||
}; | ||
|
||
type ExpectedShape = { | ||
installations: Installation[]; | ||
name: string; | ||
}; | ||
|
||
const getShape = (name: string, ...installations: Array<[string, string]>): ExpectedShape => ({ | ||
installations: installations.map(([type, version]) => expect.objectContaining({ name, type, version })), | ||
name, | ||
}); | ||
|
||
describe('getMismatchedDependencies', () => { | ||
it('lists dependencies installed with different versions', () => { | ||
const iterator = getMismatchedDependencies(mocked.projects(), DEFAULT_CONFIG); | ||
expect(Array.from(iterator)).toEqual([getShape('chalk', ['dependencies', '2.3.0'], ['dependencies', '1.0.0'])]); | ||
}); | ||
}); |
22 changes: 22 additions & 0 deletions
22
src/commands/lib/installations/get-mismatched-dependencies.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { SyncpackConfig } from '../../../constants'; | ||
import { SourceWrapper } from '../get-wrappers'; | ||
import { getDependencies, InstalledPackage } from './get-dependencies'; | ||
|
||
export function* getMismatchedDependencies( | ||
wrappers: SourceWrapper[], | ||
options: Pick<SyncpackConfig, 'dev' | 'peer' | 'prod'>, | ||
): Generator<InstalledPackage> { | ||
const iterator = getDependencies(wrappers, options); | ||
for (const installedPackage of iterator) { | ||
const { installations } = installedPackage; | ||
const len = installations.length; | ||
if (len > 1) { | ||
for (let i = 1; i < len; i++) { | ||
if (installations[i].version !== installations[i - 1].version) { | ||
yield installedPackage; | ||
break; | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import 'expect-more-jest'; | ||
import { Installation } from './get-dependencies'; | ||
import { sortByName } from './sort-by-name'; | ||
|
||
type ExpectedShape = { | ||
installations: Installation[]; | ||
name: string; | ||
}; | ||
|
||
const getShape = (name: string, ...installations: Array<[string, string]>): ExpectedShape => ({ | ||
installations: installations.map(([type, version]) => expect.objectContaining({ name, type, version })), | ||
name, | ||
}); | ||
|
||
describe('sortByName', () => { | ||
it('orders installed packages by name', () => { | ||
const toShape = (name: string): ExpectedShape => getShape(name); | ||
const unordered = ['c', 'a', 'b', 'c'].map(toShape); | ||
const ordered = ['a', 'b', 'c', 'c'].map(toShape); | ||
expect(unordered.sort(sortByName)).toEqual(ordered); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { InstalledPackage } from './get-dependencies'; | ||
|
||
export const sortByName = (a: InstalledPackage, b: InstalledPackage): 0 | 1 | -1 => { | ||
if (a.name < b.name) { | ||
return -1; | ||
} | ||
if (a.name > b.name) { | ||
return 1; | ||
} | ||
return 0; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters