Skip to content
Closed
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
2 changes: 1 addition & 1 deletion @tunnckocore/create-jest-runner/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
},
"devDependencies": {
"ansi-colors": "^4.1.1",
"execa": "^1.0.0",
"execa": "^2.0.0",
"jest": "^24.0.0"
},
"files": [
Expand Down
19 changes: 14 additions & 5 deletions @tunnckocore/utils/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ const EXTENSIONS = Object.keys(Module._extensions).filter(

module.exports = { createAliases, getWorkspacesAndExtensions, isMonorepo };

// we cannot test it because we are in monorepo where the cwd is.
/* istanbul ignore next */
function isMonorepo(cwd = process.cwd()) {
const { workspaces } = getWorkspacesAndExtensions(cwd);

Expand All @@ -25,7 +27,6 @@ function isMonorepo(cwd = process.cwd()) {
* We just don't use regex, we precompute them.
*/
function createAliases(cwd = process.cwd(), sourceDirectory) {
// const { workspaces, extensions, exts } = getWorkspacesAndExtensions(cwd);
const result = getWorkspacesAndExtensions(cwd);

const alias = result.workspaces
Expand All @@ -37,7 +38,6 @@ function createAliases(cwd = process.cwd(), sourceDirectory) {
.readdirSync(workspace)
.filter((x) => !result.workspaces.find((z) => z.endsWith(x)))
.map((directory) => {
// console.log(workspace);
const pkgDirectory = path.join(workspace, directory);
const pkgJsonPath = path.join(pkgDirectory, 'package.json');

Expand All @@ -50,6 +50,7 @@ function createAliases(cwd = process.cwd(), sourceDirectory) {
/* istanbul ignore next */
if (Object.keys(packageJson).length === 0) {
return null;
// skip silently
// throw new Error(
// `Cannot find package.json or cannot parse it: ${pkgJsonPath}`,
// );
Expand All @@ -74,7 +75,15 @@ function createAliases(cwd = process.cwd(), sourceDirectory) {
}

function parseJson(fp) {
return fs.existsSync(fp) ? JSON.parse(fs.readFileSync(fp, 'utf8')) : {};
if (fs.existsSync(fp)) {
let res = {};
try {
res = JSON.parse(fs.readFileSync(fp, 'utf8'));
} catch (err) {}
return res;
}

return {};
}

function getWorkspacesAndExtensions(cwd = process.cwd()) {
Expand Down Expand Up @@ -103,7 +112,7 @@ function getWorkspacesAndExtensions(cwd = process.cwd()) {
const extensions = exts.map((x) => `.${x}`);

const fpath = [lernaJsonPath, packageJsonPath].find((x) => fs.existsSync(x));
const packageRootPath = fpath ? path.dirname(fpath) : null;
const workspaceRootPath = fpath ? path.dirname(fpath) : null;

return {
workspaces,
Expand All @@ -113,6 +122,6 @@ function getWorkspacesAndExtensions(cwd = process.cwd()) {
lernaJsonPath,
packageJson,
packageJsonPath,
packageRootPath,
workspaceRootPath,
};
}

This file was deleted.

2 changes: 1 addition & 1 deletion @tunnckocore/utils/test/fixtures/lerna/lerna.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"packages": [
"@tunnckocore/*",
"@helios/*"
"packages/*"
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"name": "@helios/qux"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"name": "numb"
}
59 changes: 42 additions & 17 deletions @tunnckocore/utils/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ test('getWorkspacesAndExtensions - correct workspaces', () => {

const result = getWorkspacesAndExtensions(rootDir);

expect(result.workspaces).toStrictEqual(['@tunnckocore', '@helios']);
expect(result.workspaces).toStrictEqual(['@tunnckocore', 'packages']);
expect(result.extensions).toStrictEqual(['.js', '.jsx', '.ts']);
expect(result.exts).toStrictEqual(['js', 'jsx', 'ts']);
expect(result.lernaJson).toStrictEqual({
packages: ['@tunnckocore/*', '@helios/*'],
packages: ['@tunnckocore/*', 'packages/*'],
});
expect(result.packageJson).toStrictEqual({
name: 'lerna-monorepo',
Expand All @@ -51,7 +51,7 @@ test('createAliases return empty alias object', () => {
expect(typeof result.lernaJsonPath).toStrictEqual('string');
expect(result.lernaJsonPath.startsWith(cwd)).toStrictEqual(true);

expect(result.packageRootPath).toStrictEqual(null);
expect(result.workspaceRootPath).toStrictEqual(null);
});

test('createAliases return correct aliases for yarn workspaces', () => {
Expand Down Expand Up @@ -80,18 +80,40 @@ test('createAliases return correct aliases for yarn workspaces', () => {

test('createAliases return correct aliases for Lerna workspaces', () => {
const lernaRoot = path.join(__dirname, 'fixtures', 'lerna');
const res = createAliases(lernaRoot);
const res = createAliases(lernaRoot, 'source');

/**
* The scenario is that we have package named `@helios/qux` inside `packages/foo`
* and that's the correct result of `alias`.
* The key is the package name the value is path to source directory, not its root.
* {
* '@tunnckocore/barry': '/home/charlike//fixtures/lerna/@tunnckocore/barry/source',
* '@helios/qux': '/home/charlike/fixtures/lerna/packages/foo/source',
* numb: '/home/charlike/fixtures/lerna/packages/numb/source'
* }
*/

expect(Object.keys(res.alias)).toStrictEqual([
'@tunnckocore/barry',
'@helios/qux',
'numb',
]);

// eslint-disable-next-line unicorn/consistent-function-scoping
const toAliases = (src) =>
['@helios/foo', '@tunnckocore/barry'].reduce((acc, name) => {
acc[name] = path.join(lernaRoot, name, src);
return acc;
}, {});
const bases = Object.values(res.alias).map((filepath) =>
filepath
.split('/')
.slice(-3)
.join('/'),
);

expect(res.alias).toStrictEqual(toAliases(''));
// These are the real actual directories of above packages
expect(bases).toStrictEqual([
'@tunnckocore/barry/source',
'packages/foo/source',
'packages/numb/source',
]);
expect(res.lernaJson).toStrictEqual({
packages: ['@tunnckocore/*', '@helios/*'],
packages: ['@tunnckocore/*', 'packages/*'],
});
expect(res.exts).toStrictEqual(['js', 'jsx', 'ts']);
expect(res.packageJson).toStrictEqual({
Expand All @@ -109,8 +131,11 @@ test('isMonorepo - true for workspaces root', () => {
expect(isMonorepo(lernaRepo)).toStrictEqual(true);
});

test('isMonorepo - false regular repository', () => {
test('isMonorepo - false, regular repository', () => {
expect(isMonorepo(path.dirname(__dirname))).toStrictEqual(false);

// we cannot test it, because we are in monorepo where the cwd is.
// expect(isMonorepo()).toStrictEqual(false);
});

test('correct *Path properties when Lerna monorepo', () => {
Expand All @@ -119,10 +144,10 @@ test('correct *Path properties when Lerna monorepo', () => {

expect(typeof result.lernaJsonPath).toStrictEqual('string');
expect(typeof result.packageJsonPath).toStrictEqual('string');
expect(typeof result.packageRootPath).toStrictEqual('string');
expect(typeof result.workspaceRootPath).toStrictEqual('string');

expect(path.dirname(result.lernaJsonPath)).toStrictEqual(
result.packageRootPath,
result.workspaceRootPath,
);

expect(result.packageJson).toBeTruthy();
Expand All @@ -135,10 +160,10 @@ test('correct *Path properties when Yarn Workspaces monorepo', () => {

expect(typeof result.lernaJsonPath).toStrictEqual('string');
expect(typeof result.packageJsonPath).toStrictEqual('string');
expect(typeof result.packageRootPath).toStrictEqual('string');
expect(typeof result.workspaceRootPath).toStrictEqual('string');

expect(path.dirname(result.packageJsonPath)).toStrictEqual(
result.packageRootPath,
result.workspaceRootPath,
);

expect(result.packageJson).toBeTruthy();
Expand Down
7 changes: 6 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2294,6 +2294,11 @@
resolved "https://registry.yarnpkg.com/@tunnckocore/typescript-config/-/typescript-config-0.3.1.tgz#2ec44bc5e4e7cad023e5e2f34360c0bc0f5cd597"
integrity sha512-SNrvUSzWuMi5Qp9SMGrJAlir7zn7xEQOquRxv8Y71stf4v+OkHGs+yl2N51Hb2DdlAg/ouMdFzqhiXmXlTZnSw==

"@tunnckocore/utils@^0.5.2":
version "0.5.3"
resolved "https://registry.yarnpkg.com/@tunnckocore/utils/-/utils-0.5.3.tgz#1d2f442c7803dc620f56487a10a72043d4d91b57"
integrity sha512-tMpAQyWZk0u+g7U5zBorPIjXzI4C3mbWz7DYpqMh/deEmwS6HDzEWY9s/HfJLeciZmtAASN37zfvqzgqEzwK2Q==

"@types/babel__core@^7.1.0":
version "7.1.3"
resolved "https://registry.yarnpkg.com/@types/babel__core/-/babel__core-7.1.3.tgz#e441ea7df63cd080dfcd02ab199e6d16a735fc30"
Expand Down Expand Up @@ -4395,7 +4400,7 @@ execa@^1.0.0:
signal-exit "^3.0.0"
strip-eof "^1.0.0"

execa@^2.0.3:
execa@^2.0.0, execa@^2.0.3:
version "2.0.4"
resolved "https://registry.yarnpkg.com/execa/-/execa-2.0.4.tgz#2f5cc589c81db316628627004ea4e37b93391d8e"
integrity sha512-VcQfhuGD51vQUQtKIq2fjGDLDbL6N1DTQVpYzxZ7LPIXw3HqTuIz6uxRmpV1qf8i31LHf2kjiaGI+GdHwRgbnQ==
Expand Down