Skip to content

Commit 25b60ee

Browse files
committed
feat: improve tests, rename packageRootPath -> workspaceRootPath
Signed-off-by: Charlike Mike Reagent <[email protected]>
1 parent 8a0ca03 commit 25b60ee

File tree

7 files changed

+68
-26
lines changed

7 files changed

+68
-26
lines changed

@tunnckocore/utils/src/index.js

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ const EXTENSIONS = Object.keys(Module._extensions).filter(
99

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

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

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

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

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

7677
function parseJson(fp) {
77-
return fs.existsSync(fp) ? JSON.parse(fs.readFileSync(fp, 'utf8')) : {};
78+
if (fs.existsSync(fp)) {
79+
let res = {};
80+
try {
81+
res = JSON.parse(fs.readFileSync(fp, 'utf8'));
82+
} catch (err) {}
83+
return res;
84+
}
85+
86+
return {};
7887
}
7988

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

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

108117
return {
109118
workspaces,
@@ -113,6 +122,6 @@ function getWorkspacesAndExtensions(cwd = process.cwd()) {
113122
lernaJsonPath,
114123
packageJson,
115124
packageJsonPath,
116-
packageRootPath,
125+
workspaceRootPath,
117126
};
118127
}

@tunnckocore/utils/test/fixtures/lerna/@helios/foo/package.json

Lines changed: 0 additions & 3 deletions
This file was deleted.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"packages": [
33
"@tunnckocore/*",
4-
"@helios/*"
4+
"packages/*"
55
]
66
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"name": "@helios/qux"
3+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"name": "numb"
3+
}

@tunnckocore/utils/test/index.js

Lines changed: 42 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,11 @@ test('getWorkspacesAndExtensions - correct workspaces', () => {
2828

2929
const result = getWorkspacesAndExtensions(rootDir);
3030

31-
expect(result.workspaces).toStrictEqual(['@tunnckocore', '@helios']);
31+
expect(result.workspaces).toStrictEqual(['@tunnckocore', 'packages']);
3232
expect(result.extensions).toStrictEqual(['.js', '.jsx', '.ts']);
3333
expect(result.exts).toStrictEqual(['js', 'jsx', 'ts']);
3434
expect(result.lernaJson).toStrictEqual({
35-
packages: ['@tunnckocore/*', '@helios/*'],
35+
packages: ['@tunnckocore/*', 'packages/*'],
3636
});
3737
expect(result.packageJson).toStrictEqual({
3838
name: 'lerna-monorepo',
@@ -51,7 +51,7 @@ test('createAliases return empty alias object', () => {
5151
expect(typeof result.lernaJsonPath).toStrictEqual('string');
5252
expect(result.lernaJsonPath.startsWith(cwd)).toStrictEqual(true);
5353

54-
expect(result.packageRootPath).toStrictEqual(null);
54+
expect(result.workspaceRootPath).toStrictEqual(null);
5555
});
5656

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

8181
test('createAliases return correct aliases for Lerna workspaces', () => {
8282
const lernaRoot = path.join(__dirname, 'fixtures', 'lerna');
83-
const res = createAliases(lernaRoot);
83+
const res = createAliases(lernaRoot, 'source');
84+
85+
/**
86+
* The scenario is that we have package named `@helios/qux` inside `packages/foo`
87+
* and that's the correct result of `alias`.
88+
* The key is the package name the value is path to source directory, not its root.
89+
* {
90+
* '@tunnckocore/barry': '/home/charlike//fixtures/lerna/@tunnckocore/barry/source',
91+
* '@helios/qux': '/home/charlike/fixtures/lerna/packages/foo/source',
92+
* numb: '/home/charlike/fixtures/lerna/packages/numb/source'
93+
* }
94+
*/
95+
96+
expect(Object.keys(res.alias)).toStrictEqual([
97+
'@tunnckocore/barry',
98+
'@helios/qux',
99+
'numb',
100+
]);
84101

85-
// eslint-disable-next-line unicorn/consistent-function-scoping
86-
const toAliases = (src) =>
87-
['@helios/foo', '@tunnckocore/barry'].reduce((acc, name) => {
88-
acc[name] = path.join(lernaRoot, name, src);
89-
return acc;
90-
}, {});
102+
const bases = Object.values(res.alias).map((filepath) =>
103+
filepath
104+
.split('/')
105+
.slice(-3)
106+
.join('/'),
107+
);
91108

92-
expect(res.alias).toStrictEqual(toAliases(''));
109+
// These are the real actual directories of above packages
110+
expect(bases).toStrictEqual([
111+
'@tunnckocore/barry/source',
112+
'packages/foo/source',
113+
'packages/numb/source',
114+
]);
93115
expect(res.lernaJson).toStrictEqual({
94-
packages: ['@tunnckocore/*', '@helios/*'],
116+
packages: ['@tunnckocore/*', 'packages/*'],
95117
});
96118
expect(res.exts).toStrictEqual(['js', 'jsx', 'ts']);
97119
expect(res.packageJson).toStrictEqual({
@@ -109,8 +131,11 @@ test('isMonorepo - true for workspaces root', () => {
109131
expect(isMonorepo(lernaRepo)).toStrictEqual(true);
110132
});
111133

112-
test('isMonorepo - false regular repository', () => {
134+
test('isMonorepo - false, regular repository', () => {
113135
expect(isMonorepo(path.dirname(__dirname))).toStrictEqual(false);
136+
137+
// we cannot test it, because we are in monorepo where the cwd is.
138+
// expect(isMonorepo()).toStrictEqual(false);
114139
});
115140

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

120145
expect(typeof result.lernaJsonPath).toStrictEqual('string');
121146
expect(typeof result.packageJsonPath).toStrictEqual('string');
122-
expect(typeof result.packageRootPath).toStrictEqual('string');
147+
expect(typeof result.workspaceRootPath).toStrictEqual('string');
123148

124149
expect(path.dirname(result.lernaJsonPath)).toStrictEqual(
125-
result.packageRootPath,
150+
result.workspaceRootPath,
126151
);
127152

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

136161
expect(typeof result.lernaJsonPath).toStrictEqual('string');
137162
expect(typeof result.packageJsonPath).toStrictEqual('string');
138-
expect(typeof result.packageRootPath).toStrictEqual('string');
163+
expect(typeof result.workspaceRootPath).toStrictEqual('string');
139164

140165
expect(path.dirname(result.packageJsonPath)).toStrictEqual(
141-
result.packageRootPath,
166+
result.workspaceRootPath,
142167
);
143168

144169
expect(result.packageJson).toBeTruthy();

yarn.lock

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2294,6 +2294,11 @@
22942294
resolved "https://registry.yarnpkg.com/@tunnckocore/typescript-config/-/typescript-config-0.3.1.tgz#2ec44bc5e4e7cad023e5e2f34360c0bc0f5cd597"
22952295
integrity sha512-SNrvUSzWuMi5Qp9SMGrJAlir7zn7xEQOquRxv8Y71stf4v+OkHGs+yl2N51Hb2DdlAg/ouMdFzqhiXmXlTZnSw==
22962296

2297+
"@tunnckocore/utils@^0.5.2":
2298+
version "0.5.3"
2299+
resolved "https://registry.yarnpkg.com/@tunnckocore/utils/-/utils-0.5.3.tgz#1d2f442c7803dc620f56487a10a72043d4d91b57"
2300+
integrity sha512-tMpAQyWZk0u+g7U5zBorPIjXzI4C3mbWz7DYpqMh/deEmwS6HDzEWY9s/HfJLeciZmtAASN37zfvqzgqEzwK2Q==
2301+
22972302
"@types/babel__core@^7.1.0":
22982303
version "7.1.3"
22992304
resolved "https://registry.yarnpkg.com/@types/babel__core/-/babel__core-7.1.3.tgz#e441ea7df63cd080dfcd02ab199e6d16a735fc30"

0 commit comments

Comments
 (0)