UNPKG

1.69 kBJavaScriptView Raw
1"use strict";
2const fs = require("fs");
3const depFields_1 = require("../const/depFields");
4const addConfig_1 = require("../fns/add-cmd/addConfig");
5const cmdName_1 = require("../fns/cmdName");
6const getFiles_1 = require("../fns/getFiles");
7const sortObjectByKey_1 = require("../fns/sortObjectByKey");
8const command = cmdName_1.cmdName(__filename);
9const cmd = {
10 builder(argv) {
11 return addConfig_1.addConfig(argv, command)
12 .option('cwd', {
13 alias: 'w',
14 default: process.cwd(),
15 defaultDescription: 'current directory',
16 describe: 'Working directory. Globs will be relative to this.',
17 string: true
18 })
19 .option('indent', {
20 alias: 'i',
21 default: 2,
22 describe: 'JSON.stringify indentation',
23 number: true
24 })
25 .option('globs', {
26 alias: 'g',
27 array: true,
28 default: ['package.json'],
29 describe: 'Globs containing package.json files'
30 });
31 },
32 command,
33 describe: 'Sort package.json dependencies, peerDependencies and devDependencies alphabetically',
34 handler(c) {
35 const files = getFiles_1.flatGlob(c.globs, true, c.cwd);
36 for (const file of files) {
37 const contents = JSON.parse(fs.readFileSync(file, 'utf8'));
38 for (const obj of depFields_1.depFields) {
39 if (contents[obj]) {
40 contents[obj] = sortObjectByKey_1.sortObjectByKey(contents[obj]);
41 }
42 }
43 fs.writeFileSync(file, JSON.stringify(contents, null, c.indent) + '\n');
44 }
45 }
46};
47module.exports = cmd;