diff --git a/lib/defaults.js b/lib/defaults.js index ed49470c..7d3ae413 100644 --- a/lib/defaults.js +++ b/lib/defaults.js @@ -79,7 +79,7 @@ const questions = [ 'body', 'breaking', 'issues', - // 'lerna' + 'lerna' ]; module.exports = { diff --git a/lib/lernaUtils.js b/lib/lernaUtils.js deleted file mode 100755 index e2d77076..00000000 --- a/lib/lernaUtils.js +++ /dev/null @@ -1,31 +0,0 @@ -const path = require('path'); -const fs = require('fs'); - -const getAllPackages = function () { - -}; - -const getChangedPackages = function () { - const changedFiles = shell.exec('git diff --cached --name-only', {silent: true}) - .stdout - .split('\n'); - - return getAllPackages() - .filter((pkg) => { - const packagePrefix = path.relative('.', pkg.location) + path.sep; - - for (const changedFile of changedFiles) { - if (changedFile.indexOf(packagePrefix) === 0) { - return true; - } - } - - return false; - }) - .map((pkg) => pkg.name); -}; - -module.exports = { - getAllPackages, - getChangedPackages -}; diff --git a/lib/questions/lerna.js b/lib/questions/lerna.js index c241debb..5e902943 100644 --- a/lib/questions/lerna.js +++ b/lib/questions/lerna.js @@ -1,6 +1,9 @@ +const {getAllPackages, getChangedPackages} = require('../util/lerna'); + exports.createQuestion = (state) => { + const changedPackages = getChangedPackages(state); const question = { - choices: allPackages, + choices: getAllPackages(state), default: changedPackages, message: `The packages that this commit has affected (${changedPackages.length} detected)\n`, name: 'packages', diff --git a/lib/util/lerna.js b/lib/util/lerna.js new file mode 100755 index 00000000..1dcaa40b --- /dev/null +++ b/lib/util/lerna.js @@ -0,0 +1,52 @@ +const {execSync} = require('child_process'); +const path = require('path'); +const fs = require('fs'); + +const isDir = (root) => (name) => { + const filepath = path.join(root, name); + + try { + const stats = fs.statSync(filepath); + + return stats.isDirectory(); + } catch (error) { + return false; + } +}; + +const getAllPackages = (state) => { + try { + const dir = path.join(state.root, 'packages'); + + return fs.readdirSync(dir).map(isDir(dir)); + } catch (error) { + return []; + } +}; + +const getChangedFiles = () => + execSync('git diff --cached --name-only 2>/dev/null') + .toString() + .trim() + .split('\n'); + +const getChangedPackages = () => { + const unique = {}; + const changedFiles = getChangedFiles(); + const regex = /^packages\/([^/]+)\//; + + for (const filename of changedFiles) { + const matches = filename.match(regex); + + if (matches) { + unique[matches[1]] = 1; + } + } + + return Object.keys(unique); +}; + +module.exports = { + getAllPackages, + getChangedPackages +};