diff --git a/lib/options-manager.js b/lib/options-manager.js index 664cf423..dfd198fb 100644 --- a/lib/options-manager.js +++ b/lib/options-manager.js @@ -1,7 +1,8 @@ -import {existsSync, promises as fs} from 'node:fs'; +import {existsSync} from 'node:fs'; import process from 'node:process'; import os from 'node:os'; import path from 'node:path'; +import fsExtra from 'fs-extra'; import arrify from 'arrify'; import {mergeWith, flow, pick} from 'lodash-es'; import {findUpSync} from 'find-up'; @@ -127,9 +128,7 @@ const mergeWithFileConfig = async options => { options.tsConfigPath = await getTsConfigCachePath([options.filePath], options.tsConfigPath, options.cwd); options.ts = true; - const config = makeTSConfig(tsConfig, tsConfigPath, [options.filePath]); - await fs.mkdir(path.dirname(options.tsConfigPath), {recursive: true}); - await fs.writeFile(options.tsConfigPath, JSON.stringify(config)); + await fsExtra.outputJson(options.tsConfigPath, makeTSConfig(tsConfig, tsConfigPath, [options.filePath])); } return {options, prettierOptions}; diff --git a/package.json b/package.json index 0f55e1c1..6e55162e 100644 --- a/package.json +++ b/package.json @@ -75,6 +75,7 @@ "esm-utils": "^2.0.0", "find-cache-dir": "^3.3.2", "find-up": "^6.1.0", + "fs-extra": "^10.0.0", "get-stdin": "^9.0.0", "globby": "^12.0.2", "imurmurhash": "^0.1.4", diff --git a/test/options-manager.js b/test/options-manager.js index 937245e6..96d6f6cb 100644 --- a/test/options-manager.js +++ b/test/options-manager.js @@ -1,13 +1,14 @@ -import {promises as fs} from 'node:fs'; import process from 'node:process'; import path from 'node:path'; import test from 'ava'; import {omit} from 'lodash-es'; +import fsExtra from 'fs-extra'; import slash from 'slash'; import createEsmUtils from 'esm-utils'; import {DEFAULT_EXTENSION, DEFAULT_IGNORES} from '../lib/constants.js'; import * as manager from '../lib/options-manager.js'; +const {readJson} = fsExtra; const {__dirname, require, json} = createEsmUtils(import.meta); const parentConfig = json.loadSync('./fixtures/nested/package.json'); const childConfig = json.loadSync('./fixtures/nested/child/package.json'); @@ -557,7 +558,7 @@ test('mergeWithFileConfig: typescript files', async t => { const expectedConfigPath = new RegExp(`${slash(cwd)}/node_modules/.cache/xo-linter/tsconfig\\..*\\.json[\\/]?$`, 'u'); t.regex(slash(options.tsConfigPath), expectedConfigPath); t.deepEqual(omit(options, 'tsConfigPath'), expected); - t.deepEqual(JSON.parse(await fs.readFile(options.tsConfigPath)), { + t.deepEqual(await readJson(options.tsConfigPath), { extends: path.resolve(cwd, 'tsconfig.json'), files: [path.resolve(cwd, 'file.ts')], include: [slash(path.resolve(cwd, '**/*.ts')), slash(path.resolve(cwd, '**/*.tsx'))], @@ -579,7 +580,7 @@ test('mergeWithFileConfig: tsx files', async t => { const expectedConfigPath = new RegExp(`${slash(cwd)}/node_modules/.cache/xo-linter/tsconfig\\..*\\.json[\\/]?$`, 'u'); t.regex(slash(options.tsConfigPath), expectedConfigPath); t.deepEqual(omit(options, 'tsConfigPath'), expected); - t.deepEqual(JSON.parse(await fs.readFile(options.tsConfigPath)), { + t.deepEqual(await readJson(options.tsConfigPath), { extends: path.resolve(cwd, 'tsconfig.json'), files: [path.resolve(cwd, 'file.tsx')], include: [slash(path.resolve(cwd, '**/*.ts')), slash(path.resolve(cwd, '**/*.tsx'))],