Skip to content

Commit

Permalink
feat: generate .clang-format (#163)
Browse files Browse the repository at this point in the history
`init` now generates a .clang-format file. This allows IDEs to pick up
the config and integrate well with the gts provided style.

Fixes: #120
  • Loading branch information
ofrobots committed Jun 4, 2018
1 parent 2cebae4 commit 9689211
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 6 deletions.
3 changes: 1 addition & 2 deletions src/format.ts
Expand Up @@ -18,8 +18,7 @@ import {createProgram} from './lint';

const clangFormat = require('clang-format');

const baseArgs =
['-style', '{Language: JavaScript, BasedOnStyle: Google, ColumnLimit: 80}'];
const baseArgs = ['-style=file'];

/**
* Run tslint fix and clang fix with the default configuration
Expand Down
8 changes: 7 additions & 1 deletion src/init.ts
Expand Up @@ -156,6 +156,11 @@ async function generateTsConfig(options: Options): Promise<void> {
return generateConfigFile(options, './tsconfig.json', config);
}

async function generateClangFormat(options: Options): Promise<void> {
const style = await read(path.join(__dirname, '../../.clang-format'), 'utf8');
return generateConfigFile(options, './.clang-format', style);
}

async function generateConfigFile(
options: Options, filename: string, contents: string) {
let existing;
Expand Down Expand Up @@ -183,7 +188,7 @@ async function generateConfigFile(
if (!options.dryRun) {
await write(filename, contents);
}
options.logger.dir(JSON.parse(contents));
options.logger.log(contents);
}
}

Expand Down Expand Up @@ -222,6 +227,7 @@ export async function init(options: Options): Promise<boolean> {
}
await generateTsConfig(options);
await generateTsLintConfig(options);
await generateClangFormat(options);

// Run `npm install` after initial setup so `npm run check` works right away.
if (!options.dryRun) {
Expand Down
5 changes: 2 additions & 3 deletions test/test-kitchen.ts
Expand Up @@ -86,11 +86,10 @@ test.serial('init', async t => {
execOpts);
}

// Ensure tsconfig.json got generated.
// Ensure config files got generated.
fs.accessSync(`${stagingPath}/kitchen/tsconfig.json`);

// Ensure tslint.json got generated.
fs.accessSync(`${stagingPath}/kitchen/tslint.json`);
fs.accessSync(`${stagingPath}/kitchen/.clang-format`);

// Compilation shouldn't have happened. Hence no `build` directory.
const dirContents = fs.readdirSync(`${stagingPath}/kitchen`);
Expand Down

0 comments on commit 9689211

Please sign in to comment.