Skip to content

Commit 8b408bd

Browse files
committed
feat(cli): specify indentation as option
Closes #12
1 parent 51054b3 commit 8b408bd

6 files changed

Lines changed: 41 additions & 6 deletions

File tree

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ Options:
5050
-p, --prod include dependencies
5151
-d, --dev include devDependencies
5252
-P, --peer include peerDependencies
53+
-i, --indent [value] override indentation. defaults to " "
5354
-h, --help output usage information
5455
```
5556

@@ -63,6 +64,7 @@ Usage: syncpack format [options]
6364
Options:
6465
6566
-s, --source [pattern] glob pattern for package.json files to read from
67+
-i, --indent [value] override indentation. defaults to " "
6668
-h, --help output usage information
6769
```
6870

@@ -112,5 +114,6 @@ Options:
112114
-p, --prod include dependencies
113115
-d, --dev include devDependencies
114116
-P, --peer include peerDependencies
117+
-i, --indent [value] override indentation. defaults to " "
115118
-h, --help output usage information
116119
```

src/constants.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ export const DEPENDENCY_TYPES: IManifestKey[] = [
55
'devDependencies',
66
'peerDependencies'
77
];
8+
89
export const SORT_AZ = [
910
'contributors',
1011
'dependencies',
@@ -14,6 +15,7 @@ export const SORT_AZ = [
1415
'peerDependencies',
1516
'scripts'
1617
];
18+
1719
export const SORT_FIRST = ['name', 'description', 'version', 'author'];
1820
export const VERSION = require('../package.json').version;
1921
export const GREATER = 1;
@@ -40,6 +42,7 @@ export const SEMVER_ORDER = [
4042
RANGE_ANY
4143
];
4244

45+
const DEFAULT_INDENT = ' ';
4346
const DEFAULT_SEMVER_RANGE = RANGE_EXACT;
4447
const MONOREPO_PATTERN = './package.json';
4548
const PACKAGES_PATTERN = './packages/*/package.json';
@@ -100,3 +103,9 @@ export const OPTIONS_PEER = {
100103
description: 'include peerDependencies',
101104
spec: '-P, --peer'
102105
};
106+
107+
export const OPTION_INDENT = {
108+
default: DEFAULT_INDENT,
109+
description: `override indentation. defaults to "${DEFAULT_INDENT}"`,
110+
spec: '-i, --indent [value]'
111+
};

src/fix-mismatches.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,15 @@ import { writeJson } from 'fs-extra';
33
import _ = require('lodash');
44
import { relative } from 'path';
55
import {
6+
OPTION_INDENT,
67
OPTION_SOURCES,
78
OPTIONS_DEV,
89
OPTIONS_PEER,
910
OPTIONS_PROD
1011
} from './constants';
1112
import { collect } from './lib/collect';
1213
import { getDependencyTypes } from './lib/get-dependency-types';
14+
import { getIndent } from './lib/get-indent';
1315
import { getPackages } from './lib/get-packages';
1416
import { getMismatchedVersionsByName } from './lib/get-versions-by-name';
1517
import { getNewest } from './lib/version';
@@ -21,10 +23,12 @@ export const run = async (program: CommanderApi) => {
2123
.option(OPTIONS_PROD.spec, OPTIONS_PROD.description)
2224
.option(OPTIONS_DEV.spec, OPTIONS_DEV.description)
2325
.option(OPTIONS_PEER.spec, OPTIONS_PEER.description)
26+
.option(OPTION_INDENT.spec, OPTION_INDENT.description)
2427
.parse(process.argv);
2528

26-
const dependencyTypes = getDependencyTypes(program);
2729
const pkgs = await getPackages(program);
30+
const dependencyTypes = getDependencyTypes(program);
31+
const indent = getIndent(program);
2832
const mismatchedVersionsByName = getMismatchedVersionsByName(
2933
dependencyTypes,
3034
pkgs
@@ -49,7 +53,7 @@ export const run = async (program: CommanderApi) => {
4953
});
5054

5155
await Promise.all(
52-
pkgs.map(({ data, path }) => writeJson(path, data, { spaces: 2 }))
56+
pkgs.map(({ data, path }) => writeJson(path, data, { spaces: indent }))
5357
);
5458

5559
_.each(pkgs, (pkg) => {

src/format.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,14 @@ import chalk from 'chalk';
22
import { writeJson } from 'fs-extra';
33
import _ = require('lodash');
44
import { relative } from 'path';
5-
import { OPTION_SOURCES, SORT_AZ, SORT_FIRST } from './constants';
5+
import {
6+
OPTION_INDENT,
7+
OPTION_SOURCES,
8+
SORT_AZ,
9+
SORT_FIRST
10+
} from './constants';
611
import { collect } from './lib/collect';
12+
import { getIndent } from './lib/get-indent';
713
import { getPackages } from './lib/get-packages';
814
import { CommanderApi, IManifest } from './typings';
915

@@ -59,15 +65,17 @@ export const run = async (program: CommanderApi) => {
5965

6066
program
6167
.option(OPTION_SOURCES.spec, OPTION_SOURCES.description, collect)
68+
.option(OPTION_INDENT.spec, OPTION_INDENT.description)
6269
.parse(process.argv);
6370

6471
const pkgs = await getPackages(program);
72+
const indent = getIndent(program);
6573

6674
await Promise.all(
6775
pkgs.map(({ data, path }) => {
6876
console.log(chalk.blue(`./${relative('.', path)}`));
6977
const nextData = sortManifest(shortenBugs(shortenRepository(data)));
70-
return writeJson(path, nextData, { spaces: 2 });
78+
return writeJson(path, nextData, { spaces: indent });
7179
})
7280
);
7381
};

src/lib/get-indent.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { OPTION_INDENT } from '../constants';
2+
import { CommanderApi } from '../typings';
3+
4+
export type GetIndent = (program: CommanderApi) => string;
5+
6+
export const getIndent: GetIndent = (program) =>
7+
program.indent || OPTION_INDENT.default;

src/set-semver-ranges.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import _ = require('lodash');
44
import { relative } from 'path';
55
import semver = require('semver');
66
import {
7+
OPTION_INDENT,
78
OPTION_SEMVER_RANGE,
89
OPTION_SOURCES,
910
OPTIONS_DEV,
@@ -14,6 +15,7 @@ import {
1415
} from './constants';
1516
import { collect } from './lib/collect';
1617
import { getDependencyTypes } from './lib/get-dependency-types';
18+
import { getIndent } from './lib/get-indent';
1719
import { getPackages } from './lib/get-packages';
1820
import { getVersionNumber } from './lib/version';
1921
import { CommanderApi } from './typings';
@@ -25,13 +27,15 @@ export const run = async (program: CommanderApi) => {
2527
.option(OPTIONS_PROD.spec, OPTIONS_PROD.description)
2628
.option(OPTIONS_DEV.spec, OPTIONS_DEV.description)
2729
.option(OPTIONS_PEER.spec, OPTIONS_PEER.description)
30+
.option(OPTION_INDENT.spec, OPTION_INDENT.description)
2831
.parse(process.argv);
2932

3033
const semverRange: string =
3134
program.semverRange || OPTION_SEMVER_RANGE.default;
3235

33-
const dependencyTypes = getDependencyTypes(program);
3436
const pkgs = await getPackages(program);
37+
const dependencyTypes = getDependencyTypes(program);
38+
const indent = getIndent(program);
3539

3640
_(pkgs).each((pkg) =>
3741
_(dependencyTypes)
@@ -61,7 +65,7 @@ export const run = async (program: CommanderApi) => {
6165
);
6266

6367
await Promise.all(
64-
pkgs.map(({ data, path }) => writeJson(path, data, { spaces: 2 }))
68+
pkgs.map(({ data, path }) => writeJson(path, data, { spaces: indent }))
6569
);
6670

6771
_.each(pkgs, (pkg) => {

0 commit comments

Comments
 (0)