-
-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2a3ce5d
commit bae1133
Showing
12 changed files
with
281 additions
and
8 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,7 @@ | |
"author": "Jamie Mason <[email protected]> (https://github.com/JamieMason)", | ||
"bin": { | ||
"syncpack": "dist/bin.js", | ||
"syncpack-format": "dist/bin-format.js", | ||
"syncpack-fix-mismatches": "dist/bin-fix-mismatches.js", | ||
"syncpack-list-mismatches": "dist/bin-list-mismatches.js", | ||
"syncpack-list": "dist/bin-list.js" | ||
|
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,18 @@ | ||
#!/usr/bin/env node | ||
|
||
import chalk from 'chalk'; | ||
import * as program from 'commander'; | ||
import * as _ from 'lodash'; | ||
import { relative } from 'path'; | ||
import { DEFAULT_PATTERN, OPTION_PACKAGES } from './constants'; | ||
import { format } from './manifests'; | ||
|
||
program.option(OPTION_PACKAGES.spec, OPTION_PACKAGES.description).parse(process.argv); | ||
|
||
const { packages = DEFAULT_PATTERN } = program; | ||
|
||
format(packages).then((descriptors) => { | ||
_.each(descriptors, (descriptor) => { | ||
console.log(chalk.blue(`./${relative('.', descriptor.path)}`)); | ||
}); | ||
}); |
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
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,91 @@ | ||
import { getUntidyManifest } from '../../../test/fixtures'; | ||
import { shuffleObject } from '../../../test/helpers'; | ||
import { SORT_FIRST } from '../../constants'; | ||
import { IManifest } from '../../typings'; | ||
import { manifestData } from './index'; | ||
|
||
describe('format', () => { | ||
let results: IManifest[]; | ||
beforeAll(() => { | ||
results = manifestData.format([shuffleObject(getUntidyManifest()) as IManifest]); | ||
}); | ||
|
||
it('sorts specified keys to the top of package.json', () => { | ||
results.forEach((result) => { | ||
expect(Object.keys(result).slice(0, SORT_FIRST.length)).toEqual(SORT_FIRST); | ||
}); | ||
}); | ||
|
||
it('sorts remaining keys alphabetically', () => { | ||
results.forEach((result) => { | ||
expect(Object.keys(result).slice(SORT_FIRST.length - 1)).toEqual([ | ||
'author', | ||
'bin', | ||
'bugs', | ||
'dependencies', | ||
'devDependencies', | ||
'files', | ||
'homepage', | ||
'keywords', | ||
'license', | ||
'main', | ||
'peerDependencies', | ||
'repository', | ||
'scripts' | ||
]); | ||
}); | ||
}); | ||
|
||
it('sorts "dependencies" alphabetically', () => { | ||
results.forEach((result) => { | ||
expect(result.dependencies).toEqual({ | ||
arnold: '5.0.0', | ||
dog: '2.13.0', | ||
guybrush: '7.1.1', | ||
mango: '2.3.0' | ||
}); | ||
}); | ||
}); | ||
|
||
it('sorts "devDependencies" alphabetically', () => { | ||
results.forEach((result) => { | ||
expect(result.devDependencies).toEqual({ stroopwafel: '4.4.2', waldorf: '22.1.4' }); | ||
}); | ||
}); | ||
|
||
it('sorts "files" alphabetically', () => { | ||
results.forEach((result) => { | ||
expect(result.files).toEqual(['assets', 'dist']); | ||
}); | ||
}); | ||
|
||
it('sorts "keywords" alphabetically', () => { | ||
results.forEach((result) => { | ||
expect(result.keywords).toEqual(['thing', 'those', 'whatsits']); | ||
}); | ||
}); | ||
|
||
it('sorts "peerDependencies" alphabetically', () => { | ||
results.forEach((result) => { | ||
expect(result.peerDependencies).toEqual({ giftwrap: '0.1.2', jambalaya: '6.1.4', zoolander: '1.4.25' }); | ||
}); | ||
}); | ||
|
||
it('sorts "scripts" alphabetically', () => { | ||
results.forEach((result) => { | ||
expect(result.scripts).toEqual({ build: 'tsc', format: 'prettier', lint: 'tslint', test: 'jest' }); | ||
}); | ||
}); | ||
|
||
it('uses shorthand "bugs"', () => { | ||
results.forEach((result) => { | ||
expect(result.bugs).toEqual('https://github.com/JaneDoe/do-it/issues'); | ||
}); | ||
}); | ||
|
||
it('uses shorthand "repository"', () => { | ||
results.forEach((result) => { | ||
expect(result.repository).toEqual('JaneDoe/do-it'); | ||
}); | ||
}); | ||
}); |
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,56 @@ | ||
import * as _ from 'lodash'; | ||
import { SORT_AZ, SORT_FIRST } from '../../constants'; | ||
import { IDictionary, IManifest } from '../../typings'; | ||
|
||
export type Format = (manifests: IManifest[]) => IManifest[]; | ||
export type ManifestMapper = (manifest: IManifest) => IManifest; | ||
|
||
const shortenBugs: ManifestMapper = (manifest: IManifest) => { | ||
if (manifest.bugs && typeof manifest.bugs === 'object' && manifest.bugs.url) { | ||
return { | ||
...manifest, | ||
bugs: manifest.bugs.url | ||
}; | ||
} | ||
return manifest; | ||
}; | ||
|
||
const shortenRepository: ManifestMapper = (manifest) => { | ||
if ( | ||
manifest.repository && | ||
typeof manifest.repository === 'object' && | ||
manifest.repository.url && | ||
manifest.repository.url.indexOf('github.com') !== -1 | ||
) { | ||
return { | ||
...manifest, | ||
repository: manifest.repository.url.split('github.com/')[1] | ||
}; | ||
} | ||
return manifest; | ||
}; | ||
|
||
const sortObject = (obj: IManifest) => | ||
_(obj) | ||
.entries() | ||
.sortBy('0') | ||
.reduce((next, [key, value]) => ({ ...next, [key]: value }), {}); | ||
|
||
const sortValue = (value: any) => | ||
_.isArray(value) ? value.slice(0).sort() : _.isObject(value) ? sortObject(value) : value; | ||
|
||
const sortManifest: ManifestMapper = (manifest) => { | ||
const [first, rest] = _(manifest) | ||
.entries() | ||
.sortBy('0') | ||
.partition(([key, value]) => SORT_FIRST.indexOf(key) !== -1) | ||
.value(); | ||
const firstSorted = [...first].sort(([keyA], [keyB]) => SORT_FIRST.indexOf(keyA) - SORT_FIRST.indexOf(keyB)); | ||
const restSorted = _(rest) | ||
.map(([key, value]) => [key, SORT_AZ.indexOf(key) !== -1 ? sortValue(value) : value]) | ||
.value(); | ||
return _([...firstSorted, ...restSorted]).reduce((obj, [key, value]) => ({ ...obj, [key]: value }), {} as IManifest); | ||
}; | ||
|
||
export const format: Format = (manifests) => | ||
_.map(manifests, (manifest) => sortManifest(shortenBugs(shortenRepository(manifest)))); |
6 changes: 3 additions & 3 deletions
6
src/manifests/manifest-data.spec.ts → src/manifests/manifest-data/index.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
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,55 @@ | ||
import { IManifest } from '../src/typings'; | ||
|
||
export const getUntidyManifest = (): IManifest => | ||
JSON.parse(`{ | ||
"files": [ | ||
"assets", | ||
"dist" | ||
], | ||
"bugs": { | ||
"url": "https://github.com/JaneDoe/do-it/issues" | ||
}, | ||
"author": "Jane Doe <[email protected]>", | ||
"devDependencies": { | ||
"waldorf": "22.1.4", | ||
"stroopwafel": "4.4.2" | ||
}, | ||
"scripts": { | ||
"test": "jest", | ||
"build": "tsc", | ||
"lint": "tslint", | ||
"format": "prettier" | ||
}, | ||
"version": "1.0.2", | ||
"main": "do-it", | ||
"license": "MIT", | ||
"description": "Does the thing", | ||
"homepage": "https://github.com/JaneDoe/do-it#readme", | ||
"dependencies": { | ||
"guybrush": "7.1.1", | ||
"arnold": "5.0.0", | ||
"dog": "2.13.0", | ||
"mango": "2.3.0" | ||
}, | ||
"name": "do-it", | ||
"repository": { | ||
"url": "git://github.com/JaneDoe/do-it", | ||
"type": "git" | ||
}, | ||
"keywords": [ | ||
"those", | ||
"whatsits", | ||
"thing" | ||
], | ||
"bin": { | ||
"zoo": "dist/zoo.js", | ||
"moose": "dist/moose.js", | ||
"apple": "dist/apple.js" | ||
}, | ||
"peerDependencies": { | ||
"jambalaya": "6.1.4", | ||
"giftwrap": "0.1.2", | ||
"zoolander": "1.4.25" | ||
} | ||
} | ||
`); |
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