Skip to content

Commit

Permalink
fix: better support for workspaces
Browse files Browse the repository at this point in the history
  • Loading branch information
khaledosman committed Aug 16, 2021
1 parent c38da98 commit 215b6c3
Showing 1 changed file with 39 additions and 31 deletions.
70 changes: 39 additions & 31 deletions lib/util/lerna.js
Original file line number Diff line number Diff line change
@@ -1,81 +1,89 @@
const {execSync} = require('child_process');
const path = require('path');
const fs = require('fs');
const { execSync } = require('child_process')
const path = require('path')
const fs = require('fs')

const isLerna = (state) =>
fs.existsSync(path.join(state.root, 'lerna.json'));
fs.existsSync(path.join(state.root, 'lerna.json'))

const isDir = (root) => (name) => {
const filepath = path.join(root, name);
const filepath = path.join(root, name)

try {
const stats = fs.statSync(filepath);
const stats = fs.statSync(filepath)

return stats.isDirectory();
return stats.isDirectory()
} catch (error) {
return false;
return false
}
};
}

const removeLastDirectoryPartOf = (url) => {
return url.substring(0, url.lastIndexOf('/'));
return url.substring(0, url.lastIndexOf('/'))
}

const getPackageDirectory = (state) => {
const pkgFilename = path.join(state.root, 'package.json');
const getPackageDirectories = (state) => {
const pkgFilename = path.join(state.root, 'package.json')

if (fs.existsSync(pkgFilename)) {
try {
const packagesDirectory = require(pkgFilename).workspaces.packages;

if (packagesDirectory) {
const workspacePackages = Array.isArray(require(pkgFilename).workspaces) ? require(pkgFilename).workspaces : require(pkgFilename).workspaces.packages

if (workspacePackages && workspacePackages.length) {
return workspacePackages
.filter(workspacePackage => workspacePackage.endsWith('*'))
.map(workspacePackage => {
return removeLastDirectoryPartOf('' + workspacePackage)

// else {
// TODO: support paths that do not end with '*', in that case the package it self is the directory so we don't need to look at inner directories
// return workspacePackage
// }
})
// Remove the /* on the tail
return removeLastDirectoryPartOf("" + packagesDirectory);
}
// eslint-disable-next-line no-empty
} catch (error) {
}
}

return "packages";
return 'packages'
}

const getAllPackages = (state) => {
try {
const dir = path.join(state.root, getPackageDirectory(state));

return fs.readdirSync(dir).filter(isDir(dir));
const dirs = getPackageDirectories(state).map(dir => path.join(state.root, dir))
return dirs.flatMap(dir => fs.readdirSync(dir).filter(isDir(dir)))
} catch (error) {
return [];
return []
}
};
}

const getChangedFiles = () => {
const devNull = process.platform === 'win32' ? ' nul' : '/dev/null'
return execSync('git diff --cached --name-only 2>' + devNull)
.toString()
.trim()
.split('\n');
.split('\n')
}

const getChangedPackages = (state) => {
const unique = {};
const changedFiles = getChangedFiles();
const regex = new RegExp("^"+ getPackageDirectory(state) +"\/([^/]+)\/", "is");
const unique = {}
const changedFiles = getChangedFiles()
const regex = new RegExp('^' + getPackageDirectories(state) + '\/([^/]+)\/', 'is')

for (const filename of changedFiles) {
const matches = filename.match(regex);
const matches = filename.match(regex)

if (matches) {
unique[matches[1]] = 1;
unique[matches[1]] = 1
}
}

return Object.keys(unique);
};
return Object.keys(unique)
}

module.exports = {
getAllPackages,
getChangedPackages,
isLerna
};
}

0 comments on commit 215b6c3

Please sign in to comment.