Skip to content

Commit

Permalink
feat: introduce update-ts-reference.yaml (#34)
Browse files Browse the repository at this point in the history
  • Loading branch information
MKruschke committed Nov 17, 2023
1 parent a588e64 commit 483da24
Show file tree
Hide file tree
Showing 23 changed files with 181 additions and 8 deletions.
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,18 @@ The output for the created file looks like the following
},
"references": [ // will be added after running update-ts-references
{
"path": "../some-other-package
"path": "../some-other-package"
}
]
}
```

## using update-ts-references.yaml for configurations
You can configure paths via the _update-ts-references.yaml_ file. This is useful if your repo is having **no** _package.json_ or _pnp-workspace.yaml_ in the root folder. Additional to that it can also being used to extend the paths config next to the normal workspace setup via npm, pnpm, yarn and lerna so you can include or exclude some packages.

Example configuration see [here](./test-scenarios/ts-ref-yaml/update-ts-references.yaml)



## FAQ
### Why is my pnpm workspace alias not working?
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "update-ts-references",
"version": "2.7.0",
"version": "2.8.0",
"description": "Updates TypeScript references automatically while using workspaces",
"bin": "src/index.js",
"scripts": {
Expand Down
24 changes: 18 additions & 6 deletions src/update-ts-references.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ const getAllPackageJsons = async (workspaces,cwd) => {

workspaces.forEach((workspaceGlob) => {
if (workspaceGlob.startsWith('!')) {
ignoreGlobs.push(workspaceGlob);
ignoreGlobs.push(!workspaceGlob.includes('/') ? `${workspaceGlob}/${PACKAGE_JSON}`: workspaceGlob);
} else {
workspaceGlobs.push(workspaceGlob);
}
Expand Down Expand Up @@ -60,6 +60,7 @@ const getAllPackageJsons = async (workspaces,cwd) => {
(packageName) =>
ignoreGlobs.reduce((prev, actualPattern) => {
if (!prev) return prev;

return minimatch(packageName, actualPattern);
}, true) && !packageName.includes('node_modules')
)
Expand Down Expand Up @@ -134,7 +135,6 @@ const getReferencesFromDependencies = (
}

return Object.keys(mergedDependencies)
.filter(name => !name.includes('test-utils'))
.reduce((referenceArray, dependency) => {
if (packagesMap.has(dependency)) {
const { packageDir: dependencyDir } = packagesMap.get(dependency);
Expand Down Expand Up @@ -214,6 +214,10 @@ const execute = async ({
const packageJson = require(path.join(cwd, PACKAGE_JSON));

let workspaces = packageJson.workspaces;
if(workspaces && !Array.isArray(workspaces)) {
workspaces = workspaces.packages;
}

if (!workspaces && fs.existsSync(path.join(cwd, 'lerna.json'))) {
const lernaJson = require(path.join(cwd, 'lerna.json'));
workspaces = lernaJson.packages;
Expand All @@ -226,16 +230,24 @@ const execute = async ({
workspaces = pnpmConfig.packages;
}

if (fs.existsSync(path.join(cwd, 'update-ts-references.yaml'))) {
const config = yaml.load(
fs.readFileSync(path.join(cwd, 'update-ts-references.yaml'))
);

workspaces = [...(config.packages ? config.packages : []), ...(workspaces ? workspaces : [])];

if (verbose) {
console.log('joined workspaces config', workspaces);
}
}

if (!workspaces) {
throw new Error(
'could not detect yarn/npm/pnpm workspaces or lerna in this repository'
);
}

if (!Array.isArray(workspaces)) {
workspaces = workspaces.packages;
}

const packageFilePaths = await getAllPackageJsons(workspaces, cwd);
if (verbose) {
console.log('packageFilePaths', packageFilePaths);
Expand Down
13 changes: 13 additions & 0 deletions test-scenarios/ts-ref-yaml/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"name": "ts-ref-yaml-workspace",
"version": "0.0.1",
"private": true,
"workspaces": [
"workspace-b",
"shared/*",
"utils/**/"
],
"devDependencies": {
"typescript": "latest"
}
}
10 changes: 10 additions & 0 deletions test-scenarios/ts-ref-yaml/shared/workspace-c/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"name": "workspace-c",
"version": "1.0.0",
"dependencies": {
"cross-env": "5.0.5"
},
"peerDependencies": {
"foo-a": "1.0.0"
}
}
6 changes: 6 additions & 0 deletions test-scenarios/ts-ref-yaml/shared/workspace-c/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"compilerOptions": {
"outDir": "dist",
"rootDir": "src"
}
}
7 changes: 7 additions & 0 deletions test-scenarios/ts-ref-yaml/shared/workspace-d/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"name": "workspace-d",
"version": "1.0.0",
"dependencies": {
"workspace-c": "1.0.0"
}
}
6 changes: 6 additions & 0 deletions test-scenarios/ts-ref-yaml/shared/workspace-d/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"compilerOptions": {
"outDir": "dist",
"rootDir": "src"
}
}
8 changes: 8 additions & 0 deletions test-scenarios/ts-ref-yaml/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"files": [],
"compilerOptions": {
/* Basic Options */
// "allowJs": true,
"composite": true
}
}
6 changes: 6 additions & 0 deletions test-scenarios/ts-ref-yaml/update-ts-references.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
packages:
# all packages in subdirs of packages/ and components/
- 'workspace-a'
# exclude packages that are inside test directories
- '!**/tests/**'
- '!workspace-ignore'
7 changes: 7 additions & 0 deletions test-scenarios/ts-ref-yaml/utils/foos/foo-a/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"name": "foo-a",
"version": "1.0.0",
"dependencies": {
"foo-b": "1.0.0"
}
}
6 changes: 6 additions & 0 deletions test-scenarios/ts-ref-yaml/utils/foos/foo-a/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"compilerOptions": {
"outDir": "dist",
"rootDir": "src"
}
}
4 changes: 4 additions & 0 deletions test-scenarios/ts-ref-yaml/utils/foos/foo-b/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"name": "foo-b",
"version": "1.0.0"
}
6 changes: 6 additions & 0 deletions test-scenarios/ts-ref-yaml/utils/foos/foo-b/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"compilerOptions": {
"outDir": "dist",
"rootDir": "src"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"name": "test-a",
"version": "1.0.0"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"compilerOptions": {
"outDir": "dist",
"rootDir": "src"
}
}
10 changes: 10 additions & 0 deletions test-scenarios/ts-ref-yaml/workspace-a/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"name": "workspace-a",
"version": "1.0.0",
"dependencies": {
"workspace-b": "1.0.0"
},
"devDependencies": {
"foo-a": "1.0.0"
}
}
6 changes: 6 additions & 0 deletions test-scenarios/ts-ref-yaml/workspace-a/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"compilerOptions": {
"outDir": "dist",
"rootDir": "src"
}
}
10 changes: 10 additions & 0 deletions test-scenarios/ts-ref-yaml/workspace-b/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"name": "workspace-b",
"version": "1.0.0",
"dependencies": {
"cross-env": "5.0.5"
},
"devDependencies": {
"foo-b": "1.0.0"
}
}
6 changes: 6 additions & 0 deletions test-scenarios/ts-ref-yaml/workspace-b/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"compilerOptions": {
"outDir": "dist",
"rootDir": "src"
}
}
10 changes: 10 additions & 0 deletions test-scenarios/ts-ref-yaml/workspace-ignore/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"name": "ignore",
"version": "1.0.0",
"dependencies": {
"cross-env": "5.0.5"
},
"devDependencies": {
"foo-b": "1.0.0"
}
}
6 changes: 6 additions & 0 deletions test-scenarios/ts-ref-yaml/workspace-ignore/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"compilerOptions": {
"outDir": "dist",
"rootDir": "src"
}
}
18 changes: 18 additions & 0 deletions tests/update-ts-references.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const rootFolderYarnCreate = path.join(
'yarn-ws-create'
);
const rootFolderPnpm = path.join(process.cwd(), 'test-run', 'pnpm');
const rootFolderTsRefYaml = path.join(process.cwd(), 'test-run', 'ts-ref-yaml');
const rootFolderYarnCheck = path.join(
process.cwd(),
'test-run',
Expand Down Expand Up @@ -247,6 +248,23 @@ test('Support pnpm workspaces', async () => {
});
});

test('Support update-ts-reference.yaml workspaces', async () => {
await setup(rootFolderTsRefYaml);

tsconfigs.forEach((tsconfig) => {
const [configPath, config] = tsconfig;

expect(
parse(fs.readFileSync(path.join(rootFolderTsRefYaml, configPath, 'tsconfig.json')).toString())
).toEqual(config);
});

// should not touch the ignore config
expect(
parse(fs.readFileSync(path.join(rootFolderTsRefYaml,'workspace-ignore', 'tsconfig.json')).toString())
).toEqual( {compilerOptions});
});

test('Test create tsconfig', async () => {
await setup(rootFolderYarnCreate, undefined, true);
const r = [
Expand Down

0 comments on commit 483da24

Please sign in to comment.