Skip to content

Commit

Permalink
feat: generate tslint.json
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 1, 2018
1 parent 1238090 commit 246ed5d
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 18 deletions.
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 246ed5d

Please sign in to comment.