Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 14 additions & 18 deletions src/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,15 +111,17 @@ async function addDependencies(
return edits;
}

function formatJson(object: any) {
// TODO: preserve the indent from the input file.
const json = JSON.stringify(object, null, ' ');
return `${json}\n`;
}

async function writePackageJson(
packageJson: any, options: Options): Promise<void> {
options.logger.log('Writing package.json...');
if (!options.dryRun) {
try {
await write('./package.json', JSON.stringify(packageJson, null, ' '));
} catch (err) {
throw err;
}
await write('./package.json', formatJson(packageJson));
}
const preview = {
scripts: packageJson.scripts,
Expand All @@ -145,14 +147,12 @@ async function generateTsConfig(options: Options): Promise<void> {
path.relative(
options.targetRootDir,
path.resolve(options.gtsRootDir, 'tsconfig-google.json'));
const tsconfig = JSON.stringify(
{
extends: baseConfig,
compilerOptions: {rootDir: '.', outDir: 'build'},
include: ['src/*.ts', 'src/**/*.ts', 'test/*.ts', 'test/**/*.ts'],
exclude: ['node_modules']
},
null, ' '); // TODO: preserve the indent from the input file.
const tsconfig = formatJson({
extends: baseConfig,
compilerOptions: {rootDir: '.', outDir: 'build'},
include: ['src/*.ts', 'src/**/*.ts', 'test/*.ts', 'test/**/*.ts'],
exclude: ['node_modules']
});

let writeTsConfig = true;
if (existing && existing === tsconfig) {
Expand All @@ -167,11 +167,7 @@ async function generateTsConfig(options: Options): Promise<void> {
if (writeTsConfig) {
options.logger.log('Writing tsconfig.json...');
if (!options.dryRun) {
try {
await write('./tsconfig.json', tsconfig);
} catch (err) {
throw err;
}
await write('./tsconfig.json', tsconfig);
}
options.logger.dir(JSON.parse(tsconfig));
}
Expand Down
8 changes: 8 additions & 0 deletions test/test-kitchen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,14 @@ test.serial('init', async t => {
t.pass();
});

test.serial('generated json files should terminate with newline', async t => {
await simpleExecp('./node_modules/.bin/gts init -y', execOpts);
t.truthy(fs.readFileSync(`${stagingPath}/kitchen/package.json`, 'utf8')
.endsWith('\n'));
t.truthy(fs.readFileSync(`${stagingPath}/kitchen/tsconfig.json`, 'utf8')
.endsWith('\n'));
});

test.serial('check before fix', async t => {
const {exitCode, stdout} = await execp('npm run check', execOpts);
t.deepEqual(exitCode, 1);
Expand Down