Skip to content

Commit

Permalink
chore: read package.json less during build (#12310)
Browse files Browse the repository at this point in the history
  • Loading branch information
SimenB committed Feb 6, 2022
1 parent 5a95898 commit dd29978
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 25 deletions.
7 changes: 1 addition & 6 deletions .eslintrc.js
Expand Up @@ -5,15 +5,10 @@
* LICENSE file in the root directory of this source tree.
*/

const {sync: readPkg} = require('read-pkg');
const {getPackages} = require('./scripts/buildUtils');

const internalPackages = getPackages()
.map(packageDir => {
const pkg = readPkg({cwd: packageDir});

return pkg.name;
})
.map(({pkg}) => pkg.name)
.sort();

module.exports = {
Expand Down
2 changes: 1 addition & 1 deletion scripts/build.js
Expand Up @@ -142,5 +142,5 @@ if (files.length) {
} else {
const packages = getPackages();
process.stdout.write(chalk.inverse(' Building packages \n'));
packages.forEach(buildNodePackage);
packages.map(({packageDir}) => packageDir).forEach(buildNodePackage);
}
33 changes: 19 additions & 14 deletions scripts/buildTs.js
Expand Up @@ -25,7 +25,7 @@ const readFilePromise = util.promisify(fs.readFile);
const packages = getPackages();

const packagesWithTs = packages.filter(p =>
fs.existsSync(path.resolve(p, 'tsconfig.json')),
fs.existsSync(path.resolve(p.packageDir, 'tsconfig.json')),
);

const {stdout: allWorkspacesString} = await execa('yarn', [
Expand All @@ -37,14 +37,12 @@ const readFilePromise = util.promisify(fs.readFile);
const workspacesWithTs = new Map(
JSON.parse(`[${allWorkspacesString.split('\n').join(',')}]`)
.filter(({location}) =>
packagesWithTs.some(pkg => pkg.endsWith(location)),
packagesWithTs.some(({packageDir}) => packageDir.endsWith(location)),
)
.map(({location, name}) => [name, location]),
);

packagesWithTs.forEach(pkgDir => {
const pkg = require(`${pkgDir}/package.json`);

packagesWithTs.forEach(({packageDir, pkg}) => {
assert.ok(pkg.types, `Package ${pkg.name} is missing \`types\` field`);

assert.strictEqual(
Expand Down Expand Up @@ -72,13 +70,18 @@ const readFilePromise = util.promisify(fs.readFile);
return true;
})
.map(dep =>
path.relative(pkgDir, `${pkgDir}/../../${workspacesWithTs.get(dep)}`),
path.relative(
packageDir,
`${packageDir}/../../${workspacesWithTs.get(dep)}`,
),
)
.sort();

if (jestDependenciesOfPackage.length > 0) {
const tsConfig = JSON.parse(
stripJsonComments(fs.readFileSync(`${pkgDir}/tsconfig.json`, 'utf8')),
stripJsonComments(
fs.readFileSync(`${packageDir}/tsconfig.json`, 'utf8'),
),
);

const references = tsConfig.references.map(({path}) => path);
Expand All @@ -95,7 +98,12 @@ const readFilePromise = util.promisify(fs.readFile);
}
});

const args = ['tsc', '-b', ...packagesWithTs, ...process.argv.slice(2)];
const args = [
'tsc',
'-b',
...packagesWithTs.map(({packageDir}) => packageDir),
...process.argv.slice(2),
];

console.log(chalk.inverse(' Building TypeScript definition files '));

Expand All @@ -119,11 +127,10 @@ const readFilePromise = util.promisify(fs.readFile);
try {
await Promise.all(
packagesWithTs.map(
throat(cpus, async pkgDir => {
const buildDir = path.resolve(pkgDir, 'build/**/*.d.ts');
const ts3dot4 = path.resolve(pkgDir, 'build/ts3.4');
throat(cpus, async ({packageDir, pkg}) => {
const buildDir = path.resolve(packageDir, 'build/**/*.d.ts');

const globbed = await globby([buildDir, `!${ts3dot4}`]);
const globbed = await globby([buildDir]);

const files = await Promise.all(
globbed.map(file =>
Expand Down Expand Up @@ -164,8 +171,6 @@ const readFilePromise = util.promisify(fs.readFile);
);

if (filesWithNodeReference.length > 0) {
const pkg = require(pkgDir + '/package.json');

assert.ok(
pkg.dependencies,
`Package \`${pkg.name}\` is missing \`dependencies\``,
Expand Down
6 changes: 3 additions & 3 deletions scripts/buildUtils.js
Expand Up @@ -28,7 +28,7 @@ module.exports.getPackages = function getPackages() {

const nodeEngineRequirement = rootPackage.engines.node;

packages.forEach(packageDir => {
return packages.map(packageDir => {
const pkg = readPkg({cwd: packageDir});

assert.ok(pkg.engines, `Engine requirement in ${pkg.name} should exist`);
Expand Down Expand Up @@ -83,9 +83,9 @@ module.exports.getPackages = function getPackages() {
}
});
}
});

return packages;
return {packageDir, pkg};
});
};

module.exports.adjustToTerminalWidth = function adjustToTerminalWidth(str) {
Expand Down
2 changes: 1 addition & 1 deletion scripts/watch.js
Expand Up @@ -30,7 +30,7 @@ const rebuild = filename => filesToBuild.set(filename, true);

chokidar
.watch(
getPackages().map(p => path.resolve(p, 'src')),
getPackages().map(p => path.resolve(p.packageDir, 'src')),
{
ignoreInitial: true,
ignored: /(^|[\/\\])\../, // ignore dotfiles
Expand Down

0 comments on commit dd29978

Please sign in to comment.