Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: generate tslint.json #160

Merged
merged 3 commits into from Jun 3, 2018
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Intentionally removed. If we are going to generate tslint.json – we do not need to document further. Let's not invite customization.

## 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