Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thechriswalker/efd2bfa02aefc34811f68f8adb2fae77 to your computer and use it in GitHub Desktop.
Save thechriswalker/efd2bfa02aefc34811f68f8adb2fae77 to your computer and use it in GitHub Desktop.
Use yarn workspace info to create TS project references.
const { execSync } = require('child_process');
const { readFileSync, writeFileSync } = require('fs');
const { relative } = require('path');
const utf8 = { encoding: 'utf8' };
function writeJson(path, content) {
writeFileSync(path, JSON.stringify(content, null, 2) + '\n', utf8);
}
function readJson(path) {
return JSON.parse(readFileSync(path, utf8));
}
function exec(cmd) {
return execSync(cmd, utf8).trim();
}
function refsMatch(a, b) {
if (a.length !== b.length) {
return false;
}
a.sort();
b.sort();
return a.every((v, i) => {
aa = v.path;
bb = b[i].path;
return aa && bb && aa === bb;
});
}
function main() {
const packages = JSON.parse(exec(`yarn --silent workspaces info`));
const updated = [];
Object.entries(packages).forEach(([pkg, data]) => {
// all the "workspaceDependencies" should be
// typescript references.
if (data.workspaceDependencies.length > 0) {
const refs = data.workspaceDependencies.map(dep => {
return {
path: relative(`/${data.location}`, `/${packages[dep].location}`)
};
});
// now update our tsconfig.json
const file = `./${data.location}/tsconfig.json`;
const tsConfig = readJson(file);
if (
!Array.isArray(tsConfig.references) ||
!refsMatch(tsConfig.references, refs)
) {
updated.push(pkg);
tsConfig.references = refs;
writeJson(file, tsConfig);
}
}
});
if (updated.length) {
console.log('Updated references in packages:\n');
updated.map(p => console.log(' - ' + p));
} else {
console.log('All references correct');
}
console.log(
'\nRemember to add `composite:true` and `incremental:true` to get ' +
'the most out of the project references.'
);
}
main();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment