Skip to content

Commit

Permalink
build(ts): require project references to all dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
Tim Seckinger authored and jeysal committed May 21, 2020
1 parent e05d6cf commit bed4a14
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 5 deletions.
3 changes: 2 additions & 1 deletion apps/orcid-user-profile-script/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@
"rootDir": "src",
"outDir": "build"
},
"include": ["src"]
"include": ["src"],
"references": [{ "path": "../../packages/auth" }]
}
54 changes: 50 additions & 4 deletions scripts/get-composite-ts-projects.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ we still want to typecheck everything that's in the repo.

const { readdirSync } = require('fs');
const { resolve } = require('path');
const { stdout } = require('process');
const { stdout, exit } = require('process');
const {
findConfigFile,
readConfigFile,
Expand All @@ -22,26 +22,72 @@ const packagesDir = resolve(rootDir, 'packages');
const appsDir = resolve(rootDir, 'apps');

const compositeProjectPaths = [];
const configErrors = [];

[packagesDir, appsDir].forEach((parentDir) => {
const projects = readdirSync(parentDir);
projects.forEach((projectDir) => {
const dir = resolve(parentDir, projectDir);

let dependencies, devDependencies;
// read package.json
try {
({ dependencies = {}, devDependencies = {} } = require(resolve(
dir,
'package.json',
)));
} catch {
// obsolete project dir without a package.json, pretend it doesn't exist
return;
}

// read tsconfig.json
const { error, config } = readConfigFile(
findConfigFile(dir, tsSys.fileExists),
tsSys.readFile,
);
if (error) {
throw new Error(error.messageText);
}
const { errors, options } = parseJsonConfigFileContent(config, tsSys, dir);
const {
errors,
options,
projectReferences = [],
} = parseJsonConfigFileContent(config, tsSys, dir);
if (errors.length) {
throw new Error(errors.map((error) => error.messageText).join('\n'));
}
if (options.composite) {
compositeProjectPaths.push(dir);
// Non-composite projects cannot be built using `tsc -b`
if (!options.composite) {
return;
}
// add to TS projects to compile
compositeProjectPaths.push(dir);

// make sure the TS project is not missing any project references
const dependencyWorkspacePaths = Object.values({
...dependencies,
...devDependencies,
}).flatMap((version) => (/workspace:(.+)/.exec(version) || []).slice(1));
const projectReferencePaths = projectReferences.map(({ path }) => path);
dependencyWorkspacePaths.forEach((dependencyWorkspacePath) => {
if (
!projectReferencePaths.includes(
resolve(rootDir, dependencyWorkspacePath),
)
) {
configErrors.push(
`Error: tsconfig.json of project ${projectDir} is missing a project reference ` +
`to its workspace dependency ${dependencyWorkspacePath}.`,
);
}
});
});
});

if (configErrors.length) {
configErrors.forEach((error) => console.error(error));
exit(1);
}

stdout.write(compositeProjectPaths.join(' '));

0 comments on commit bed4a14

Please sign in to comment.