Skip to content
This repository has been archived by the owner on Nov 10, 2022. It is now read-only.

Commit

Permalink
feat: sort-deps command
Browse files Browse the repository at this point in the history
  • Loading branch information
Alorel committed Sep 11, 2018
1 parent 69f945c commit f129b03
Show file tree
Hide file tree
Showing 5 changed files with 126 additions and 4 deletions.
4 changes: 2 additions & 2 deletions src/commands/clean-pkg-json.ts
Expand Up @@ -4,7 +4,7 @@ import {CommandModule} from 'yargs';
import {addConfig} from '../lib/addConfig';
import {cmdName} from '../lib/cmdName';
import {depFields} from '../lib/depFields';
import {flatGlob} from '../lib/getFiles';
import {flatGlobDirs} from '../lib/getFiles';
import {sortObjectByKey} from '../lib/sortObjectByKey';

interface StrObj {
Expand Down Expand Up @@ -153,7 +153,7 @@ const cmd: CommandModule = {
command,
describe: 'Clean pre-dist package.json fields. Nested object paths can be separated by dot, e.g.: "foo.bar"',
handler(c: Conf) {
const files = flatGlob(c.distDirs, '**/package.json');
const files = flatGlobDirs(c.distDirs, '**/package.json');
for (const file of files) {
const contents: any = JSON.parse(fs.readFileSync(file, 'utf8'));
if (!c.skipRemoveFields) {
Expand Down
59 changes: 59 additions & 0 deletions src/commands/sort-deps.ts
@@ -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;
9 changes: 7 additions & 2 deletions src/lib/getFiles.ts
@@ -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);
}
48 changes: 48 additions & 0 deletions test/commands/sort-deps.ts
@@ -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);
}
});
});
10 changes: 10 additions & 0 deletions test/fixtures/sort-deps/fixture.json
@@ -0,0 +1,10 @@
{
"name": "sort-deps-fixture",
"dependencies": {
"foo": "0.0.0",
"bar": "0.0.1"
},
"devDependencies": {
"qux": "0.0.2"
}
}

0 comments on commit f129b03

Please sign in to comment.