Skip to content

Commit

Permalink
chore(incremental): invalidate all packages if yarn.lock changes
Browse files Browse the repository at this point in the history
  • Loading branch information
redallen committed Jul 22, 2019
1 parent 15baf18 commit a8a6045
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 15 deletions.
30 changes: 18 additions & 12 deletions scripts/hashDir.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,25 @@ function filewalker(dir) {

function hashDir(dirPath) {
const md5 = crypto.createHash('md5');
const stat = fs.statSync(dirPath);

const files = filewalker(dirPath);
files
.sort((f1, f2) => f1.path.localeCompare(f2.path))
.forEach(file => {
if (file.type === 'file') {
const fileContents = fs.readFileSync(file.path);
md5.update(fileContents);
}
else if (file.type === 'dir') {
md5.update(file.path);
}
});
if (stat && stat.isFile()) {
const fileContents = fs.readFileSync(dirPath);
md5.update(fileContents);
}
else {
filewalker(dirPath)
.sort((f1, f2) => f1.path.localeCompare(f2.path))
.forEach(file => {
if (file.type === 'file') {
const fileContents = fs.readFileSync(file.path);
md5.update(fileContents);
}
else if (file.type === 'dir') {
md5.update(file.path);
}
});
}
return md5.digest('hex');
}

Expand Down
13 changes: 10 additions & 3 deletions scripts/incrementalBuild.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@ const Project = require('@lerna/project');
const RunCommand = require('@lerna/run')

// Load cache
const cacheFile = '.cache/incrementalCache'
let cache = {}
const cacheFile = '.cache/incrementalCache';
let cache = {};
if (fs.existsSync(cacheFile)) {
cache = JSON.parse(fs.readFileSync(cacheFile, 'utf8'));
}
const yarnLockHash = hashDir('yarn.lock');

// Package filtering
const isPf3 = process.argv.length > 2 && process.argv[2] === 'pf3';
Expand Down Expand Up @@ -48,7 +49,7 @@ async function getInvalidPackages() {
: true) // Based off argv
.filter(p => isPf4
? p.location.indexOf('patternfly-4') > 0 || commonPackages.indexOf(p.name) >= 0
: true) // Based off argv
: true); // Based off argv

for (let p of packages) {
p.hash = hashPackageSrc(p.location, p.name);
Expand All @@ -58,6 +59,11 @@ async function getInvalidPackages() {
}
}

// Invalidate everything if any deps change.
if (cache['yarn.lock'] !== yarnLockHash) {
return packages;
}

return packages.filter(p => !p.valid);
}

Expand All @@ -75,6 +81,7 @@ getInvalidPackages().then(packages => {
if (!fs.existsSync('.cache')) {
fs.mkdirSync('.cache');
}
cache['yarn.lock'] = yarnLockHash;
fs.writeFileSync(cacheFile, JSON.stringify(cache, null, 2));
})
}
Expand Down

0 comments on commit a8a6045

Please sign in to comment.