Skip to content

Commit

Permalink
use monorepo prettier config in update-tsconfig
Browse files Browse the repository at this point in the history
  • Loading branch information
etrepum committed Apr 23, 2024
1 parent 5cdcab5 commit dba9d16
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 29 deletions.
8 changes: 4 additions & 4 deletions packages/lexical-devtools/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,15 @@
"zustand": "^4.5.1"
},
"devDependencies": {
"@lexical/devtools-core": "0.14.5",
"@rollup/plugin-babel": "^6.0.4",
"@types/react": "^18.2.46",
"@types/react-dom": "^18.2.18",
"@vitejs/plugin-react": "^4.2.1",
"typescript": "^5.3.3",
"lexical": "0.14.5",
"@lexical/devtools-core": "0.14.5",
"wxt": "^0.17.0",
"typescript": "^5.3.3",
"vite": "^5.2.2",
"@rollup/plugin-babel": "^6.0.4"
"wxt": "^0.17.0"
},
"sideEffects": false
}
68 changes: 43 additions & 25 deletions scripts/update-tsconfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,24 @@ const {packagesManager} = require('./shared/packagesManager');

/**
* @typedef {Object} UpdateTsconfigOptions
* @property {Array<[string, Array<string>]>} extraPaths additional paths to add
* @property {string} jsonFileName path to the tsconfig.json
* @property {import('prettier').Options} prettierConfig the monorepo prettier config
* @property {boolean} test true to include the test paths (default: false)
* @property {Array<[string, Array<string>]>} extraPaths additional paths to add
*/

/**
* @param {opts} UpdateTsconfigOptions
* @returns {Promise<void>}
*/
function updateTsconfig({jsonFileName, test = false, extraPaths = []}) {
const tsconfig = fs.readJsonSync(jsonFileName);
const prev = JSON.stringify(tsconfig.compilerOptions.paths);
async function updateTsconfig({
extraPaths,
jsonFileName,
prettierConfig,
test,
}) {
const prevTsconfigContents = fs.readFileSync(jsonFileName, 'utf8');
const tsconfig = JSON.parse(prevTsconfigContents);
const publicPaths = [];
const privatePaths = [];
const testPaths = [];
Expand Down Expand Up @@ -67,26 +74,37 @@ function updateTsconfig({jsonFileName, test = false, extraPaths = []}) {
...privatePaths,
...testPaths,
]);
if (JSON.stringify(paths) !== prev) {
tsconfig.compilerOptions.paths = paths;
fs.writeFileSync(
jsonFileName,
prettier.format(JSON.stringify(tsconfig), {filepath: jsonFileName}),
);
tsconfig.compilerOptions.paths = paths;
// This is async in future versions of prettier
const nextTsconfigContents = await prettier.format(JSON.stringify(tsconfig), {
...prettierConfig,
filepath: jsonFileName,
});
if (prevTsconfigContents !== nextTsconfigContents) {
fs.writeFileSync(jsonFileName, nextTsconfigContents);
}
}
updateTsconfig({
extraPaths: [],
jsonFileName: './tsconfig.json',
test: true,
});
updateTsconfig({
extraPaths: [],
jsonFileName: './tsconfig.build.json',
test: false,
});
updateTsconfig({
extraPaths: [['lexicalOriginal', ['../lexical/src/']]],
jsonFileName: './packages/lexical-devtools/tsconfig.json',
test: false,
});

async function updateAllTsconfig() {
const prettierConfig = (await prettier.resolveConfig('./')) || {};
await updateTsconfig({
extraPaths: [],
jsonFileName: './tsconfig.json',
prettierConfig,
test: true,
});
await updateTsconfig({
extraPaths: [],
jsonFileName: './tsconfig.build.json',
prettierConfig,
test: false,
});
await updateTsconfig({
extraPaths: [['lexicalOriginal', ['../lexical/src/']]],
jsonFileName: './packages/lexical-devtools/tsconfig.json',
prettierConfig,
test: false,
});
}

updateAllTsconfig();

0 comments on commit dba9d16

Please sign in to comment.