Skip to content

Commit

Permalink
#3 - Add cli option to output the resolved configuration.
Browse files Browse the repository at this point in the history
  • Loading branch information
dsherret committed Jul 7, 2019
1 parent ecad73d commit 2bba159
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 7 deletions.
1 change: 1 addition & 0 deletions src/cli/CommandLineOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@ export interface CommandLineOptions {
showVersion: boolean;
config: string | undefined;
outputFilePaths: boolean;
outputResolvedConfig: boolean;
filePatterns: string[];
}
9 changes: 5 additions & 4 deletions src/cli/getHelpText.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ Syntax: dprint [options] [...file patterns]
Examples: dprint
dprint "src/**/*.ts"
Options:
-h, --help Output this message.
-v, --version Output the version.
-c, --config Configuration file to use (default: dprint.config)
--outputFilePaths Logs the list of file paths.
-h, --help Output this message.
-v, --version Output the version.
-c, --config Configuration file to use (default: dprint.config)
--outputFilePaths Outputs the list of file paths.
--outputResolvedConfig Outputs the resolved configuration from the dprint.config file.
`;
}
3 changes: 2 additions & 1 deletion src/cli/parseCommandLineArgs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@ import { CommandLineOptions } from "./CommandLineOptions";
export function parseCommandLineArgs(args: string[]): CommandLineOptions {
const argv = minimist(args, {
string: ["config"],
boolean: ["help", "version", "outputFilePaths"]
boolean: ["help", "version", "outputFilePaths", "outputResolvedConfig"]
});

return {
config: getConfigFilePath(),
showHelp: argv["h"] || argv["help"],
showVersion: argv["v"] || argv["version"],
outputFilePaths: argv["outputFilePaths"],
outputResolvedConfig: argv["outputResolvedConfig"],
filePatterns: argv._
};

Expand Down
14 changes: 12 additions & 2 deletions src/cli/runCli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,14 @@ export async function runCli(args: string[], environment: Environment) {
}

export async function runCliWithOptions(options: CommandLineOptions, environment: Environment) {
if (options.showHelp)
if (options.showHelp) {
environment.log(getHelpText());
else if (options.showVersion)
return;
}
else if (options.showVersion) {
environment.log(getPackageVersion());
return;
}

const unresolvedConfiguration = await resolveConfigFile(options.config, environment);
const configResult = resolveConfiguration(unresolvedConfiguration);
Expand All @@ -30,6 +34,12 @@ export async function runCliWithOptions(options: CommandLineOptions, environment
if (options.outputFilePaths) {
for (const filePath of filePaths)
environment.log(filePath);
return;
}
else if (options.outputResolvedConfig) {
// todo: print this out formatted
environment.log(JSON.stringify(configResult.config));
return;
}

const promises: Promise<void>[] = [];
Expand Down
1 change: 1 addition & 0 deletions src/tests/cli/getDefaultCommandLineOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export function getDefaultCommandLineOptions(): CommandLineOptions {
showHelp: false,
showVersion: false,
outputFilePaths: false,
outputResolvedConfig: false,
filePatterns: []
};
}
4 changes: 4 additions & 0 deletions src/tests/cli/parseCommandLineArgsTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,8 @@ describe(nameof(parseCommandLineArgs), () => {
it("should parse file globs specified with a leading outputFilePaths", () => {
doTest(["--outputFilePaths", "file.ts", "file2.ts"], { outputFilePaths: true, filePatterns: ["file.ts", "file2.ts"] });
});

it("should parse file globs specified with a leading outputResolvedConfig", () => {
doTest(["--outputResolvedConfig", "file.ts", "file2.ts"], { outputResolvedConfig: true, filePatterns: ["file.ts", "file2.ts"] });
});
});
6 changes: 6 additions & 0 deletions src/tests/cli/runCliTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,10 @@ describe(nameof(runCliWithOptions), () => {
expect(await environment.readFile("/file1.ts")).to.equal("5 + 1;\n");
expect(await environment.readFile("/file2.ts")).to.equal("console.log(5);\n");
});

it("should output the resolved config when specifying to", async () => {
const logs = await getLogs({ outputResolvedConfig: true });
expect(logs.length).to.equal(1);
expect(logs[0].startsWith("{")).to.be.true;
});
});

0 comments on commit 2bba159

Please sign in to comment.