Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for lerna-based eslint runs #10

Merged
merged 6 commits into from
Oct 15, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
module.exports = {
env: {
node: 1,
mocha: 1,
},
parserOptions: {
ecmaVersion: 2020,
Expand Down
49 changes: 46 additions & 3 deletions lib/utils.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,51 @@
'use strict';

const getPackages = require('get-monorepo-packages');
const { dirname, join, relative, sep, normalize, isAbsolute } = require('path');
const fs = require('fs');
const {
dirname,
join,
relative,
sep,
normalize,
isAbsolute,
resolve,
} = require('path');

const isSubPath = (parent, path) => {
const relativePath = relative(parent, path);
return relativePath.split(sep)[0] !== '..' && !isAbsolute(relativePath);
};

const packages = getPackages(process.cwd()).map(
const directoryContainsRootPackageJson = (path) => {
const pathToCheck = resolve(path, 'package.json');
if (!fs.existsSync(pathToCheck)) return false;
const pkg = require(pathToCheck);
if (pkg && {}.hasOwnProperty.call(pkg, 'workspaces')) {
return true;
}
return false;
};

const getRepositoryRoot = (path = process.cwd()) => {
let pathForCheck = path;

if (directoryContainsRootPackageJson(pathForCheck)) {
return pathForCheck;
}

while (pathForCheck !== resolve(pathForCheck, '..')) {
pathForCheck = resolve(pathForCheck, '..');

if (directoryContainsRootPackageJson(pathForCheck)) {
return pathForCheck;
}
}

return path;
};

const packages = getPackages(getRepositoryRoot()).map(
({
package: { name, dependencies = {}, devDependencies = {} },
location,
Expand Down Expand Up @@ -74,4 +111,10 @@ const getImport = (context, callback) => {
};
};

module.exports = { isSubPath, packages, getImport, pathToImport };
module.exports = {
isSubPath,
packages,
getImport,
pathToImport,
getRepositoryRoot,
};
29 changes: 29 additions & 0 deletions tests/utils/getRepositoryRoot.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
'use strict';

const assert = require('assert');
const path = require('path');

describe('getRepositoryRoot', () => {
it('should detect root path from package in monorepository', () => {
const pkgPath = path.resolve(
__dirname,
'./mocks/repo-with-workspaces/package/',
);

process.chdir(pkgPath);

const utils = require('../../lib/utils');

assert.ok(utils.getRepositoryRoot() === path.resolve(pkgPath, '..'));
});

it('should detect root path from root in monorepository', () => {
const pkgPath = path.resolve(__dirname, './mocks/repo-with-workspaces/');

process.chdir(pkgPath);

const utils = require('../../lib/utils');

assert.ok(utils.getRepositoryRoot() === pkgPath);
});
});
4 changes: 4 additions & 0 deletions tests/utils/mocks/repo-with-workspaces/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"private": true,
"workspaces": ["./"]
}
Empty file.