Skip to content

Commit

Permalink
fix: make tsconfig work with comments/trailing commas
Browse files Browse the repository at this point in the history
  • Loading branch information
DuroCodes committed Jul 30, 2024
1 parent dac05ab commit fbece3e
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 3 deletions.
5 changes: 3 additions & 2 deletions src/commands/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { pathExists, pathExistsSync } from 'find-up';
import { mkdir, writeFile, readFile } from 'fs/promises';
import * as Preprocessor from '../utilities/preprocessor';
import { bold, magentaBright } from 'colorette';
import { parseTsConfig } from '../utilities/parseTsconfig';

const VALID_EXTENSIONS = ['.ts', '.js' ];

Expand Down Expand Up @@ -103,8 +104,8 @@ export async function build(options: Record<string, any>) {
}
assert(buildConfig.mode === 'development' || buildConfig.mode === 'production', 'Mode is not `production` or `development`');
try {
let config = JSON.parse(await readFile(buildConfig.tsconfig!, 'utf8'));
config.extends && console.warn("Extend the generated tsconfig")
let config = await parseTsConfig(buildConfig.tsconfig!);
config?.extends && console.warn("Extend the generated tsconfig")
} catch(e) {
console.error("no tsconfig / jsconfig found");
console.error(`Please create a ${sernConfig.language === 'javascript' ? 'jsconfig.json' : 'tsconfig.json' }`);
Expand Down
5 changes: 4 additions & 1 deletion src/utilities/edits.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { findUp } from 'find-up';
import { readFile, rename, writeFile } from 'node:fs/promises';
import { fromCwd } from './fromCwd.js';
import { parseTsConfig } from './parseTsconfig.js';

/**
* It takes a string, finds the package.json file in the directory of the string, and changes the name
Expand Down Expand Up @@ -61,10 +62,12 @@ export async function editDirs(
});

if (tsconfig) {
const output = JSON.parse(await readFile(tsconfig, 'utf8'));
const output = await parseTsConfig(tsconfig);
if (!output) throw new Error("Can't read your tsconfig.json.");
if (!output.compilerOptions) throw new Error("Can't find compilerOptions in your tsconfig.json.");
output.compilerOptions.rootDir = srcName;

// This will strip comments/trailing commas from the tsconfig.json file
await writeFile(tsconfig, JSON.stringify(output, null, 2));
}

Expand Down
50 changes: 50 additions & 0 deletions src/utilities/parseTsconfig.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { resolve } from 'node:path';
import { readFile } from 'node:fs/promises';

interface CompilerOptions {
target: string;
module: string;
lib: string[];
allowJs: boolean;
checkJs: boolean;
jsx: string;
declaration: boolean;
sourceMap: boolean;
outDir: string;
rootDir: string;
strict: boolean;
esModuleInterop: boolean;
forceConsistentCasingInFileNames: boolean;
noEmit: boolean;
importHelpers: boolean;
isolatedModules: boolean;
moduleResolution: string;
resolveJsonModule: boolean;
noEmitHelpers: boolean;
}

interface TsConfig {
compilerOptions: CompilerOptions;
files: string[];
include: string[];
exclude: string[];
extends: string;
}

const cleanJson = (json: string) =>
json
.replace(/\/\/.*$/gm, '')
.replace(/\/\*[\s\S]*?\*\//gm, '')
.replace(/,\s*([}\]])/g, '$1');

export const parseTsConfig = async (path: string) => {
const absPath = resolve(path);
const fileContent = await readFile(absPath, 'utf-8');
const cleanContent = cleanJson(fileContent);

try {
return JSON.parse(cleanContent) as Partial<TsConfig>;
} catch (e) {
return null;
}
};

0 comments on commit fbece3e

Please sign in to comment.