From 246ed5dab845366f0013c431194b3060ff8c82ec Mon Sep 17 00:00:00 2001 From: Ali Ijaz Sheikh Date: Fri, 1 Jun 2018 16:11:41 +0200 Subject: [PATCH] feat: generate tslint.json `init` now generates a tslint.json file which inherits from the default configuration. This empowers the users to customize if necessary instead of being overly opinionated. Fixes: https://github.com/google/ts-style/issues/127 Fixes: https://github.com/google/ts-style/issues/119 --- src/init.ts | 45 ++++++++++++++++++++++++++------------------ test/test-kitchen.ts | 7 +++++++ 2 files changed, 34 insertions(+), 18 deletions(-) diff --git a/src/init.ts b/src/init.ts index 20460fae..1b57c87c 100644 --- a/src/init.ts +++ b/src/init.ts @@ -142,40 +142,48 @@ async function writePackageJson( options.logger.dir(preview); } +async function generateTsLintConfig(options: Options): Promise { + const config = formatJson({extends: 'gts/tslint.json'}); + return generateConfigFile(options, './tslint.json', config); +} + async function generateTsConfig(options: Options): Promise { + const config = formatJson({ + extends: './node_modules/gts/tsconfig-google.json', + compilerOptions: {rootDir: '.', outDir: 'build'}, + include: ['src/*.ts', 'src/**/*.ts', 'test/*.ts', 'test/**/*.ts'] + }); + return generateConfigFile(options, './tsconfig.json', config); +} + +async function generateConfigFile( + options: Options, filename: string, contents: string) { let existing; try { - existing = await read('./tsconfig.json', 'utf8'); + existing = await read(filename, 'utf8'); } catch (err) { if (err.code === 'ENOENT') { /* not found, create it. */ } else { - throw new Error(`Unknown error reading tsconfig.json: ${err.message}`); + throw new Error(`Unknown error reading ${filename}: ${err.message}`); } } - const tsconfig = formatJson({ - extends: './node_modules/gts/tsconfig-google.json', - compilerOptions: {rootDir: '.', outDir: 'build'}, - include: ['src/*.ts', 'src/**/*.ts', 'test/*.ts', 'test/**/*.ts'] - }); - - let writeTsConfig = true; - if (existing && existing === tsconfig) { - options.logger.log('No edits needed in tsconfig.json.'); + let writeFile = true; + if (existing && existing === contents) { + options.logger.log(`No edits needed in ${filename}`); return; } else if (existing) { - writeTsConfig = await query( - `${chalk.bold('tsconfig.json')} already exists`, 'Overwrite', false, - options); + writeFile = await query( + `${chalk.bold(filename)} already exists`, 'Overwrite', false, options); } - if (writeTsConfig) { - options.logger.log('Writing tsconfig.json...'); + if (writeFile) { + options.logger.log(`Writing ${filename}...`); if (!options.dryRun) { - await write('./tsconfig.json', tsconfig); + await write(filename, contents); } - options.logger.dir(JSON.parse(tsconfig)); + options.logger.dir(JSON.parse(contents)); } } @@ -213,6 +221,7 @@ export async function init(options: Options): Promise { options.logger.log('No edits needed in package.json.'); } await generateTsConfig(options); + await generateTsLintConfig(options); // Run `npm install` after initial setup so `npm run check` works right away. if (!options.dryRun) { diff --git a/test/test-kitchen.ts b/test/test-kitchen.ts index 3ae61f0e..17b169e7 100644 --- a/test/test-kitchen.ts +++ b/test/test-kitchen.ts @@ -85,8 +85,13 @@ test.serial('init', async t => { `npx -p ${stagingPath}/gts.tgz --ignore-existing gts init -n`, execOpts); } + + // Ensure tsconfig.json got generated. fs.accessSync(`${stagingPath}/kitchen/tsconfig.json`); + // Ensure tslint.json got generated. + fs.accessSync(`${stagingPath}/kitchen/tslint.json`); + // Compilation shouldn't have happened. Hence no `build` directory. const dirContents = fs.readdirSync(`${stagingPath}/kitchen`); t.is(dirContents.indexOf('build'), -1); @@ -130,6 +135,8 @@ test.serial('generated json files should terminate with newline', async t => { .endsWith('\n')); t.truthy(fs.readFileSync(`${stagingPath}/kitchen/tsconfig.json`, 'utf8') .endsWith('\n')); + t.truthy(fs.readFileSync(`${stagingPath}/kitchen/tslint.json`, 'utf8') + .endsWith('\n')); }); test.serial('check before fix', async t => {