Skip to content

Commit

Permalink
feat(cli): specify indentation as option
Browse files Browse the repository at this point in the history
Closes #12
  • Loading branch information
JamieMason committed Oct 28, 2018
1 parent 51054b3 commit 8b408bd
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 6 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ Options:
-p, --prod include dependencies
-d, --dev include devDependencies
-P, --peer include peerDependencies
-i, --indent [value] override indentation. defaults to " "
-h, --help output usage information
```

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

Expand Down Expand Up @@ -112,5 +114,6 @@ Options:
-p, --prod include dependencies
-d, --dev include devDependencies
-P, --peer include peerDependencies
-i, --indent [value] override indentation. defaults to " "
-h, --help output usage information
```
9 changes: 9 additions & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export const DEPENDENCY_TYPES: IManifestKey[] = [
'devDependencies',
'peerDependencies'
];

export const SORT_AZ = [
'contributors',
'dependencies',
Expand All @@ -14,6 +15,7 @@ export const SORT_AZ = [
'peerDependencies',
'scripts'
];

export const SORT_FIRST = ['name', 'description', 'version', 'author'];
export const VERSION = require('../package.json').version;
export const GREATER = 1;
Expand All @@ -40,6 +42,7 @@ export const SEMVER_ORDER = [
RANGE_ANY
];

const DEFAULT_INDENT = ' ';
const DEFAULT_SEMVER_RANGE = RANGE_EXACT;
const MONOREPO_PATTERN = './package.json';
const PACKAGES_PATTERN = './packages/*/package.json';
Expand Down Expand Up @@ -100,3 +103,9 @@ export const OPTIONS_PEER = {
description: 'include peerDependencies',
spec: '-P, --peer'
};

export const OPTION_INDENT = {
default: DEFAULT_INDENT,
description: `override indentation. defaults to "${DEFAULT_INDENT}"`,
spec: '-i, --indent [value]'
};
8 changes: 6 additions & 2 deletions src/fix-mismatches.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@ import { writeJson } from 'fs-extra';
import _ = require('lodash');
import { relative } from 'path';
import {
OPTION_INDENT,
OPTION_SOURCES,
OPTIONS_DEV,
OPTIONS_PEER,
OPTIONS_PROD
} from './constants';
import { collect } from './lib/collect';
import { getDependencyTypes } from './lib/get-dependency-types';
import { getIndent } from './lib/get-indent';
import { getPackages } from './lib/get-packages';
import { getMismatchedVersionsByName } from './lib/get-versions-by-name';
import { getNewest } from './lib/version';
Expand All @@ -21,10 +23,12 @@ export const run = async (program: CommanderApi) => {
.option(OPTIONS_PROD.spec, OPTIONS_PROD.description)
.option(OPTIONS_DEV.spec, OPTIONS_DEV.description)
.option(OPTIONS_PEER.spec, OPTIONS_PEER.description)
.option(OPTION_INDENT.spec, OPTION_INDENT.description)
.parse(process.argv);

const dependencyTypes = getDependencyTypes(program);
const pkgs = await getPackages(program);
const dependencyTypes = getDependencyTypes(program);
const indent = getIndent(program);
const mismatchedVersionsByName = getMismatchedVersionsByName(
dependencyTypes,
pkgs
Expand All @@ -49,7 +53,7 @@ export const run = async (program: CommanderApi) => {
});

await Promise.all(
pkgs.map(({ data, path }) => writeJson(path, data, { spaces: 2 }))
pkgs.map(({ data, path }) => writeJson(path, data, { spaces: indent }))
);

_.each(pkgs, (pkg) => {
Expand Down
12 changes: 10 additions & 2 deletions src/format.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,14 @@ import chalk from 'chalk';
import { writeJson } from 'fs-extra';
import _ = require('lodash');
import { relative } from 'path';
import { OPTION_SOURCES, SORT_AZ, SORT_FIRST } from './constants';
import {
OPTION_INDENT,
OPTION_SOURCES,
SORT_AZ,
SORT_FIRST
} from './constants';
import { collect } from './lib/collect';
import { getIndent } from './lib/get-indent';
import { getPackages } from './lib/get-packages';
import { CommanderApi, IManifest } from './typings';

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

program
.option(OPTION_SOURCES.spec, OPTION_SOURCES.description, collect)
.option(OPTION_INDENT.spec, OPTION_INDENT.description)
.parse(process.argv);

const pkgs = await getPackages(program);
const indent = getIndent(program);

await Promise.all(
pkgs.map(({ data, path }) => {
console.log(chalk.blue(`./${relative('.', path)}`));
const nextData = sortManifest(shortenBugs(shortenRepository(data)));
return writeJson(path, nextData, { spaces: 2 });
return writeJson(path, nextData, { spaces: indent });
})
);
};
7 changes: 7 additions & 0 deletions src/lib/get-indent.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { OPTION_INDENT } from '../constants';
import { CommanderApi } from '../typings';

export type GetIndent = (program: CommanderApi) => string;

export const getIndent: GetIndent = (program) =>
program.indent || OPTION_INDENT.default;
8 changes: 6 additions & 2 deletions src/set-semver-ranges.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import _ = require('lodash');
import { relative } from 'path';
import semver = require('semver');
import {
OPTION_INDENT,
OPTION_SEMVER_RANGE,
OPTION_SOURCES,
OPTIONS_DEV,
Expand All @@ -14,6 +15,7 @@ import {
} from './constants';
import { collect } from './lib/collect';
import { getDependencyTypes } from './lib/get-dependency-types';
import { getIndent } from './lib/get-indent';
import { getPackages } from './lib/get-packages';
import { getVersionNumber } from './lib/version';
import { CommanderApi } from './typings';
Expand All @@ -25,13 +27,15 @@ export const run = async (program: CommanderApi) => {
.option(OPTIONS_PROD.spec, OPTIONS_PROD.description)
.option(OPTIONS_DEV.spec, OPTIONS_DEV.description)
.option(OPTIONS_PEER.spec, OPTIONS_PEER.description)
.option(OPTION_INDENT.spec, OPTION_INDENT.description)
.parse(process.argv);

const semverRange: string =
program.semverRange || OPTION_SEMVER_RANGE.default;

const dependencyTypes = getDependencyTypes(program);
const pkgs = await getPackages(program);
const dependencyTypes = getDependencyTypes(program);
const indent = getIndent(program);

_(pkgs).each((pkg) =>
_(dependencyTypes)
Expand Down Expand Up @@ -61,7 +65,7 @@ export const run = async (program: CommanderApi) => {
);

await Promise.all(
pkgs.map(({ data, path }) => writeJson(path, data, { spaces: 2 }))
pkgs.map(({ data, path }) => writeJson(path, data, { spaces: indent }))
);

_.each(pkgs, (pkg) => {
Expand Down

0 comments on commit 8b408bd

Please sign in to comment.