-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feature: Extracted getPackageRoots() * chore: Added tests * chore: Added documentation * chore: Added changeset --------- Co-authored-by: ijlee2 <[email protected]>
- Loading branch information
Showing
8 changed files
with
188 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@codemod-utils/files": minor | ||
--- | ||
|
||
Extracted getPackageRoots() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import { join } from 'node:path'; | ||
|
||
import type { Options } from '../types/index.js'; | ||
import { findFiles } from './find-files.js'; | ||
import { parseFilePath } from './parse-file-path.js'; | ||
|
||
/** | ||
* Returns the roots of all packages in a project. | ||
* | ||
* @param options | ||
* | ||
* An object with `projectRoot`. | ||
* | ||
* @example | ||
* | ||
* Analyze each package by reading `package.json`. | ||
* | ||
* ```ts | ||
* import { readPackageJson } from '@codemod-utils/json'; | ||
* | ||
* const packageRoots = getPackageRoots({ | ||
* projectRoot, | ||
* }); | ||
* | ||
* packageRoots.forEach((packageRoot) => { | ||
* const packageJson = readPackageJson({ projectRoot: packageRoot }); | ||
* | ||
* // ... | ||
* }); | ||
* ``` | ||
*/ | ||
export function getPackageRoots( | ||
options: Options & { | ||
projectRoot: string; | ||
}, | ||
): string[] { | ||
const { projectRoot } = options; | ||
|
||
const filePaths = findFiles('**/package.json', { | ||
ignoreList: ['**/{dist,node_modules}/**/*'], | ||
projectRoot, | ||
}); | ||
|
||
const packageRoots = filePaths.map((filePath) => { | ||
const { dir } = parseFilePath(filePath); | ||
|
||
return join(projectRoot, dir); | ||
}); | ||
|
||
const isMonorepo = packageRoots.length > 1; | ||
|
||
if (isMonorepo) { | ||
// Exclude the workspace root | ||
return packageRoots.filter((packageRoot) => packageRoot !== projectRoot); | ||
} | ||
|
||
return packageRoots; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
packages/files/tests/files/get-package-roots/base-case.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { assert, loadFixture, test } from '@codemod-utils/tests'; | ||
|
||
import { getPackageRoots } from '../../../src/index.js'; | ||
import { codemodOptions, options } from '../../shared-test-setups/index.js'; | ||
|
||
test('files | get-package-roots > base case', function () { | ||
const inputProject = { | ||
'package.json': '', | ||
}; | ||
|
||
loadFixture(inputProject, codemodOptions); | ||
|
||
const packageRoots = getPackageRoots({ | ||
projectRoot: options.projectRoot, | ||
}); | ||
|
||
assert.deepStrictEqual(packageRoots, ['tmp/test-project']); | ||
}); |
20 changes: 20 additions & 0 deletions
20
packages/files/tests/files/get-package-roots/edge-case-file-name-is-incorrect.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { assert, loadFixture, test } from '@codemod-utils/tests'; | ||
|
||
import { getPackageRoots } from '../../../src/index.js'; | ||
import { codemodOptions, options } from '../../shared-test-setups/index.js'; | ||
|
||
test('files | get-package-roots > edge case (file name is incorrect)', function () { | ||
const inputProject = { | ||
packagejson: '', | ||
'package.json5': '', | ||
'somepackage.json': '', | ||
}; | ||
|
||
loadFixture(inputProject, codemodOptions); | ||
|
||
const packageRoots = getPackageRoots({ | ||
projectRoot: options.projectRoot, | ||
}); | ||
|
||
assert.deepStrictEqual(packageRoots, []); | ||
}); |
16 changes: 16 additions & 0 deletions
16
packages/files/tests/files/get-package-roots/edge-case-no-packages.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { assert, loadFixture, test } from '@codemod-utils/tests'; | ||
|
||
import { getPackageRoots } from '../../../src/index.js'; | ||
import { codemodOptions, options } from '../../shared-test-setups/index.js'; | ||
|
||
test('files | get-package-roots > edge case (no packages)', function () { | ||
const inputProject = {}; | ||
|
||
loadFixture(inputProject, codemodOptions); | ||
|
||
const packageRoots = getPackageRoots({ | ||
projectRoot: options.projectRoot, | ||
}); | ||
|
||
assert.deepStrictEqual(packageRoots, []); | ||
}); |
43 changes: 43 additions & 0 deletions
43
packages/files/tests/files/get-package-roots/monorepo.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { assert, loadFixture, test } from '@codemod-utils/tests'; | ||
|
||
import { getPackageRoots } from '../../../src/index.js'; | ||
import { codemodOptions, options } from '../../shared-test-setups/index.js'; | ||
|
||
test('files | get-package-roots > monorepo', function () { | ||
const inputProject = { | ||
'docs-app': { | ||
'package.json': '', | ||
}, | ||
packages: { | ||
'my-folder': { | ||
'my-package-1': { | ||
'package.json': '', | ||
}, | ||
}, | ||
'my-package-2': { | ||
'package.json': '', | ||
}, | ||
'my-package-3': { | ||
'package.json': '', | ||
}, | ||
}, | ||
'test-app': { | ||
'package.json': '', | ||
}, | ||
'package.json': '', | ||
}; | ||
|
||
loadFixture(inputProject, codemodOptions); | ||
|
||
const packageRoots = getPackageRoots({ | ||
projectRoot: options.projectRoot, | ||
}); | ||
|
||
assert.deepStrictEqual(packageRoots, [ | ||
'tmp/test-project/docs-app', | ||
'tmp/test-project/packages/my-folder/my-package-1', | ||
'tmp/test-project/packages/my-package-2', | ||
'tmp/test-project/packages/my-package-3', | ||
'tmp/test-project/test-app', | ||
]); | ||
}); |