This repository has been archived by the owner on Nov 10, 2022. It is now read-only.
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
Showing
5 changed files
with
126 additions
and
4 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 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,59 @@ | ||
import * as fs from 'fs'; | ||
import {CommandModule} from 'yargs'; | ||
import {addConfig} from '../lib/addConfig'; | ||
import {cmdName} from '../lib/cmdName'; | ||
import {flatGlob} from '../lib/getFiles'; | ||
import {sortObjectByKey} from '../lib/sortObjectByKey'; | ||
|
||
const command = cmdName(__filename); | ||
|
||
interface Conf { | ||
cwd: string; | ||
|
||
globs: string[]; | ||
|
||
indent: number; | ||
} | ||
|
||
const cmd: CommandModule = { | ||
builder(argv) { | ||
return addConfig(argv, command) | ||
.option('cwd', { | ||
alias: 'w', | ||
default: process.cwd(), | ||
defaultDescription: 'current directory', | ||
describe: 'Working directory. Globs will be relative to this.', | ||
string: true | ||
}) | ||
.option('indent', { | ||
alias: 'i', | ||
default: 2, | ||
describe: 'JSON.stringify indentation', | ||
number: true | ||
}) | ||
.option('globs', { | ||
alias: 'g', | ||
array: true, | ||
default: ['package.json'], | ||
describe: 'Globs containing package.json files' | ||
}); | ||
}, | ||
command, | ||
describe: 'Sort package.json dependencies, peerDependencies and devDependencies alphabetically', | ||
handler(c: Conf) { | ||
const files = flatGlob(c.globs, true, c.cwd); | ||
|
||
for (const file of files) { | ||
const contents = JSON.parse(fs.readFileSync(file, 'utf8')); | ||
for (const obj of ['dependencies', 'devDependencies', 'peerDependencies']) { | ||
if (contents[obj]) { | ||
contents[obj] = sortObjectByKey(contents[obj]); | ||
} | ||
} | ||
|
||
fs.writeFileSync(file, JSON.stringify(contents, null, c.indent)); | ||
} | ||
} | ||
}; | ||
|
||
export = cmd; |
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 |
---|---|---|
@@ -1,11 +1,16 @@ | ||
import {sync as glob$} from 'glob'; | ||
import {flattenGlob} from './flattenGlob'; | ||
|
||
export function flatGlob(dirs: string[], glob: string, absolute = true): string[] { | ||
export function flatGlob(globs: string[], absolute = true, cwd = process.cwd()): string[] { | ||
return globs.map<string[]>(glob => glob$(glob, {cwd, absolute})) | ||
.reduce<string[]>(flattenGlob, []); | ||
} | ||
|
||
export function flatGlobDirs(dirs: string[], glob: string, absolute = true): string[] { | ||
return dirs.map<string[]>(cwd => glob$(glob, {cwd, absolute})) | ||
.reduce<string[]>(flattenGlob, []); | ||
} | ||
|
||
export function getFiles(dirs: string[], ext: string, absolute = true): string[] { | ||
return flatGlob(dirs, `**/*.${ext}`, absolute); | ||
return flatGlobDirs(dirs, `**/*.${ext}`, absolute); | ||
} |
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,48 @@ | ||
import {expect} from 'chai'; | ||
import {alo} from '../../src/alo'; | ||
import {Fixture} from '../util/Fixture'; | ||
|
||
//tslint:disable:object-literal-sort-keys | ||
|
||
describe('sort-deps', () => { | ||
let fixture: Fixture; | ||
let initialCwd: string; | ||
|
||
before('init fixture', () => { | ||
fixture = new Fixture('sort-deps'); | ||
|
||
return fixture.write(); | ||
}); | ||
|
||
before('modify cwd', () => { | ||
initialCwd = process.cwd(); | ||
process.chdir(fixture.outDir()); | ||
}); | ||
|
||
before('run', () => alo(['sort-deps', '-i', '3', '-g', 'fixture.json'])); | ||
|
||
it('Test contents', async () => { | ||
const expected = JSON.stringify( | ||
{ | ||
name: 'sort-deps-fixture', | ||
dependencies: { | ||
bar: '0.0.1', | ||
foo: '0.0.0' | ||
}, | ||
devDependencies: { | ||
qux: '0.0.2' | ||
} | ||
}, | ||
null, | ||
3 //tslint:disable-line:no-magic-numbers | ||
); | ||
|
||
expect(await fixture.readOut('fixture.json')).to.eq(expected); | ||
}); | ||
|
||
after('restore cwd', () => { | ||
if (initialCwd) { | ||
process.chdir(initialCwd); | ||
} | ||
}); | ||
}); |
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,10 @@ | ||
{ | ||
"name": "sort-deps-fixture", | ||
"dependencies": { | ||
"foo": "0.0.0", | ||
"bar": "0.0.1" | ||
}, | ||
"devDependencies": { | ||
"qux": "0.0.2" | ||
} | ||
} |