Skip to content

Commit

Permalink
feat: generate tslint.json (#160)
Browse files Browse the repository at this point in the history
`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: #127
Fixes: #119
  • Loading branch information
ofrobots committed Jun 3, 2018
1 parent cef0d68 commit d2c325b
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 20 deletions.
2 changes: 0 additions & 2 deletions README.md
Expand Up @@ -43,8 +43,6 @@ When you run the `npx gts init` command, it's going to do a few things for you:
- `compile`: Compiles the source code using TypeScript compiler.
- `pretest`, `posttest` and `prepare`: convenience integrations.

We strongly recommend you use the default style config, but if you must tweak, you can edit the generated `tsconfig.json`. For linter, we use the default `tslint.json` unless we find that file in your project directory.

## Individual files
The commands above will all run in the scope of the current folder. Some commands can be run on individual files:

Expand Down
45 changes: 27 additions & 18 deletions src/init.ts
Expand Up @@ -142,40 +142,48 @@ async function writePackageJson(
options.logger.dir(preview);
}

async function generateTsLintConfig(options: Options): Promise<void> {
const config = formatJson({extends: 'gts/tslint.json'});
return generateConfigFile(options, './tslint.json', config);
}

async function generateTsConfig(options: Options): Promise<void> {
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));
}
}

Expand Down Expand Up @@ -213,6 +221,7 @@ export async function init(options: Options): Promise<boolean> {
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) {
Expand Down
7 changes: 7 additions & 0 deletions test/test-kitchen.ts
Expand Up @@ -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);
Expand Down Expand Up @@ -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 => {
Expand Down

0 comments on commit d2c325b

Please sign in to comment.