@goldstack/utils-typescript-references 0.2.2 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,152 +1,152 @@
1
- # TypeScript References Yarn Workspaces Sync Utility
2
-
3
- This library ensures that [project references](https://www.typescriptlang.org/docs/handbook/project-references.html) in TypeScript `tsconfig.js` files are automatically kept up to date in a project using **Yarn 2 workspaces**.
4
-
5
- Running this script will:
6
-
7
- 1. Add a list of all packages in the workspaces to the root `tsconfig.json` for the `"references"` attribute:
8
-
9
- ```json
10
- {
11
- "references": [
12
- {
13
- "path": "workspaces/apps"
14
- },
15
- {
16
- "path": "workspaces/docs"
17
- },
18
- {
19
- "path": "workspaces/templates"
20
- },
21
- {
22
- "path": "workspaces/templates-lib"
23
- }
24
- ]
25
- }
26
- ```
27
-
28
- 2. Update all the `"references"` in the `tsconfig.json` for all packages in the workspace so that it includes all the packages that it declares as a dependency in `package.json`.
29
-
30
- ## Usage
31
-
32
- Install as development dependency using
33
-
34
- ```
35
- npm i @goldstack/utils-typescript-references --save-dev
36
- -- or
37
- yarn add -D @goldstack/utils-typescript-references
38
- ```
39
-
40
- Add a script to your `package.json`
41
-
42
- ```json
43
- {
44
- "scripts": {
45
- "fix-typescript-references": "utils-typescript-references"
46
- }
47
- }
48
- ```
49
-
50
- Run the script using
51
-
52
- ```
53
- npm run fix-typescript-references
54
- -- or
55
- yarn fix-typescript-references
56
- ```
57
-
58
- ## Options
59
-
60
- The following parameters can be passed when invoking the script:
61
-
62
- ### --help
63
-
64
- Shows a reference of all available options:
65
-
66
- ```
67
- $ utils-typescript-references --help
68
- Options:
69
- --help Show help [boolean]
70
- --version Show version number [boolean]
71
- --skipPackages Only update project references in the root tsConfig
72
- [boolean]
73
- --skipRoot Skip updating project references in project root
74
- tsConfig [boolean]
75
- --excludeInReferences Exclude specific packages from being referenced by
76
- other packages [array]
77
- --excludeInRoot Exclude specific packages from being referenced in the
78
- root tsConfig [array]
79
- --tsConfigName Names of tsConfig files to be updated [array]
80
- ```
81
-
82
- ### --tsConfigName
83
-
84
- Provide one or more name of `tsconfig.json` files in projects across the monorepo that should be updated.
85
-
86
- ```
87
- $ utils-typescript-references --tsConfigName tsconfig.json --tsConfigName tsconfig.build.json
88
- ```
89
-
90
- Defaults to `tsconfig.json`
91
-
92
- This helpful for monorepos where for instance the `tsconfig.build.json` builds the modules that are exported from the package, and thus should be run when you are building using `tsc -b`. In this case the `tsconfig.json` can be set up to type check only (no emit) and have a manually inserted reference to `tsconfig.build.json` for running `tsc -b`.
93
-
94
- Note that once any `--tsConfigName` is provided, the default `tsconfig.json` is not updated any more. In order to continue updating `tsconfig.json` along with any custom configuration files, simply provide it as an extra option:
95
-
96
- ```
97
- $ utils-typescript-references --tsConfigName tsconfig.build.json --tsConfigName tsconfig.json
98
- ```
99
-
100
- This option can also be used if the `tsconfig.json` file is not in the root of packages (e.g. in the `src/` folder). In that case, provide an option as follows:
101
-
102
- ```
103
- $ utils-typescript-references --tsConfigName src/tsconfig.json --tsConfigName tsconfig.json
104
- ```
105
-
106
- ### --excludeInReferences
107
-
108
- Will prevent certain packages from being added to the `references` of other projects in the monorepo.
109
-
110
- ```
111
- $ utils-typescript-references --excludeInReferences @myproject/packageA --excludeInReferences @myproject/PackageB
112
- ```
113
-
114
- The above will cause `@myproject/packageA` and `@myproject/packageB` not to be inserted as referenced in all other packages of the monorepo.
115
-
116
- ### --excludeInRoot
117
-
118
- Will prevent certain packages from being added to the `references` section of the root `tsconfig.json` file.
119
-
120
- ```
121
- $ utils-typescript-references --excludeInRoot @myproject/packageA --excludeInRoot @myproject/PackageB
122
- ```
123
-
124
- ### --skipPackages
125
-
126
- Will skip updating the `references` in `tsconfig.json` files for all packages in the project.
127
-
128
- ```
129
- $ utils-typescript-references --skipPackages
130
- ```
131
-
132
- ### -skipRoot
133
-
134
- Will skip updating the `references` in the `tsconfig.json` file for the project root.
135
-
136
- ```
137
- $ utils-typescript-references --skipRoot
138
- ```
139
-
140
- ## Limitations
141
-
142
- - The root `tsconfig.json` file needs to be a vanilla JSON document (so no comments)
143
-
144
- If these limitations or anything else are an issues, please [raise a ticket in GitHub for the Goldstack Monorepo](https://github.com/goldstack/goldstack/issues).
145
-
146
- ## See Also
147
-
148
- - [Code with Joy - The Ultimate Guide to TypeScript Monorepos](https://maxrohde.com/2021/11/20/the-ultimate-guide-to-typescript-monorepos/)
149
- - [The Full Stack Blog - TypeScript Monorepo with Yarn and Project References](https://maxrohde.com/2021/10/01/typescript-monorepo-with-yarn-and-project-references/)
150
- - [@monorepo-utils/workspaces-to-typescript-project-references](https://github.com/azu/monorepo-utils/tree/master/packages/@monorepo-utils/workspaces-to-typescript-project-references#readme)
151
- - [Optimizing multi-package apps with TypeScript Project References](https://ebaytech.berlin/optimizing-multi-package-apps-with-typescript-project-references-d5c57a3b4440)
152
- - [TypeScript Monorepos with Yarn](https://semaphoreci.com/blog/typescript-monorepos-with-yarn)
1
+ # TypeScript References Yarn Workspaces Sync Utility
2
+
3
+ This library ensures that [project references](https://www.typescriptlang.org/docs/handbook/project-references.html) in TypeScript `tsconfig.js` files are automatically kept up to date in a project using **Yarn 2 workspaces**.
4
+
5
+ Running this script will:
6
+
7
+ 1. Add a list of all packages in the workspaces to the root `tsconfig.json` for the `"references"` attribute:
8
+
9
+ ```json
10
+ {
11
+ "references": [
12
+ {
13
+ "path": "workspaces/apps"
14
+ },
15
+ {
16
+ "path": "workspaces/docs"
17
+ },
18
+ {
19
+ "path": "workspaces/templates"
20
+ },
21
+ {
22
+ "path": "workspaces/templates-lib"
23
+ }
24
+ ]
25
+ }
26
+ ```
27
+
28
+ 2. Update all the `"references"` in the `tsconfig.json` for all packages in the workspace so that it includes all the packages that it declares as a dependency in `package.json`.
29
+
30
+ ## Usage
31
+
32
+ Install as development dependency using
33
+
34
+ ```
35
+ npm i @goldstack/utils-typescript-references --save-dev
36
+ -- or
37
+ yarn add -D @goldstack/utils-typescript-references
38
+ ```
39
+
40
+ Add a script to your `package.json`
41
+
42
+ ```json
43
+ {
44
+ "scripts": {
45
+ "fix-typescript-references": "utils-typescript-references"
46
+ }
47
+ }
48
+ ```
49
+
50
+ Run the script using
51
+
52
+ ```
53
+ npm run fix-typescript-references
54
+ -- or
55
+ yarn fix-typescript-references
56
+ ```
57
+
58
+ ## Options
59
+
60
+ The following parameters can be passed when invoking the script:
61
+
62
+ ### --help
63
+
64
+ Shows a reference of all available options:
65
+
66
+ ```
67
+ $ utils-typescript-references --help
68
+ Options:
69
+ --help Show help [boolean]
70
+ --version Show version number [boolean]
71
+ --skipPackages Only update project references in the root tsConfig
72
+ [boolean]
73
+ --skipRoot Skip updating project references in project root
74
+ tsConfig [boolean]
75
+ --excludeInReferences Exclude specific packages from being referenced by
76
+ other packages [array]
77
+ --excludeInRoot Exclude specific packages from being referenced in the
78
+ root tsConfig [array]
79
+ --tsConfigName Names of tsConfig files to be updated [array]
80
+ ```
81
+
82
+ ### --tsConfigName
83
+
84
+ Provide one or more name of `tsconfig.json` files in projects across the monorepo that should be updated.
85
+
86
+ ```
87
+ $ utils-typescript-references --tsConfigName tsconfig.json --tsConfigName tsconfig.build.json
88
+ ```
89
+
90
+ Defaults to `tsconfig.json`
91
+
92
+ This helpful for monorepos where for instance the `tsconfig.build.json` builds the modules that are exported from the package, and thus should be run when you are building using `tsc -b`. In this case the `tsconfig.json` can be set up to type check only (no emit) and have a manually inserted reference to `tsconfig.build.json` for running `tsc -b`.
93
+
94
+ Note that once any `--tsConfigName` is provided, the default `tsconfig.json` is not updated any more. In order to continue updating `tsconfig.json` along with any custom configuration files, simply provide it as an extra option:
95
+
96
+ ```
97
+ $ utils-typescript-references --tsConfigName tsconfig.build.json --tsConfigName tsconfig.json
98
+ ```
99
+
100
+ This option can also be used if the `tsconfig.json` file is not in the root of packages (e.g. in the `src/` folder). In that case, provide an option as follows:
101
+
102
+ ```
103
+ $ utils-typescript-references --tsConfigName src/tsconfig.json --tsConfigName tsconfig.json
104
+ ```
105
+
106
+ ### --excludeInReferences
107
+
108
+ Will prevent certain packages from being added to the `references` of other projects in the monorepo.
109
+
110
+ ```
111
+ $ utils-typescript-references --excludeInReferences @myproject/packageA --excludeInReferences @myproject/PackageB
112
+ ```
113
+
114
+ The above will cause `@myproject/packageA` and `@myproject/packageB` not to be inserted as referenced in all other packages of the monorepo.
115
+
116
+ ### --excludeInRoot
117
+
118
+ Will prevent certain packages from being added to the `references` section of the root `tsconfig.json` file.
119
+
120
+ ```
121
+ $ utils-typescript-references --excludeInRoot @myproject/packageA --excludeInRoot @myproject/PackageB
122
+ ```
123
+
124
+ ### --skipPackages
125
+
126
+ Will skip updating the `references` in `tsconfig.json` files for all packages in the project.
127
+
128
+ ```
129
+ $ utils-typescript-references --skipPackages
130
+ ```
131
+
132
+ ### --skipRoot
133
+
134
+ Will skip updating the `references` in the `tsconfig.json` file for the project root.
135
+
136
+ ```
137
+ $ utils-typescript-references --skipRoot
138
+ ```
139
+
140
+ ## Limitations
141
+
142
+ - The root `tsconfig.json` file needs to be a vanilla JSON document (so no comments)
143
+
144
+ If these limitations or anything else are an issues, please [raise a ticket in GitHub for the Goldstack Monorepo](https://github.com/goldstack/goldstack/issues).
145
+
146
+ ## See Also
147
+
148
+ - [Code with Joy - The Ultimate Guide to TypeScript Monorepos](https://maxrohde.com/2021/11/20/the-ultimate-guide-to-typescript-monorepos/)
149
+ - [The Full Stack Blog - TypeScript Monorepo with Yarn and Project References](https://maxrohde.com/2021/10/01/typescript-monorepo-with-yarn-and-project-references/)
150
+ - [@monorepo-utils/workspaces-to-typescript-project-references](https://github.com/azu/monorepo-utils/tree/master/packages/@monorepo-utils/workspaces-to-typescript-project-references#readme)
151
+ - [Optimizing multi-package apps with TypeScript Project References](https://ebaytech.berlin/optimizing-multi-package-apps-with-typescript-project-references-d5c57a3b4440)
152
+ - [TypeScript Monorepos with Yarn](https://semaphoreci.com/blog/typescript-monorepos-with-yarn)
@@ -1,25 +1,25 @@
1
- export interface PackageData {
2
- path: string;
3
- name: string;
4
- }
5
- export declare function getPackages(cmdRes: string): PackageData[];
6
- /**
7
- * Search a folder for the preferred tsconfig.json file based
8
- * on the options in tsConfigNames.
9
- *
10
- * @param workspacePath Directory to search in
11
- * @param tsConfigNames List of tsconfig.json files to search for in order
12
- * of preference
13
- */
14
- export declare function getTsConfigPath(workspacePath: string, tsConfigNames: string[]): string | undefined;
15
- /**
16
- * Calculate the references array for the package
17
- *
18
- * @param packagePath Path to the package we are updating references for
19
- * @param packages Packages the package wants to reference
20
- * @param tsConfigNames Configured tsconfig file names
21
- */
22
- export declare function makeReferences(packagePath: string, packages: Array<PackageData | null | undefined>, tsConfigNames: string[]): Array<{
23
- path: string;
24
- }>;
1
+ export interface PackageData {
2
+ path: string;
3
+ name: string;
4
+ }
5
+ export declare function getPackages(cmdRes: string): PackageData[];
6
+ /**
7
+ * Search a folder for the preferred tsconfig.json file based
8
+ * on the options in tsConfigNames.
9
+ *
10
+ * @param workspacePath Directory to search in
11
+ * @param tsConfigNames List of tsconfig.json files to search for in order
12
+ * of preference
13
+ */
14
+ export declare function getTsConfigPath(workspacePath: string, tsConfigNames: string[]): string | undefined;
15
+ /**
16
+ * Calculate the references array for the package
17
+ *
18
+ * @param packagePath Path to the package we are updating references for
19
+ * @param packages Packages the package wants to reference
20
+ * @param tsConfigNames Configured tsconfig file names
21
+ */
22
+ export declare function makeReferences(packagePath: string, packages: Array<PackageData | null | undefined>, tsConfigNames: string[]): Array<{
23
+ path: string;
24
+ }>;
25
25
  //# sourceMappingURL=sharedUtils.d.ts.map
@@ -1,75 +1,75 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.makeReferences = exports.getTsConfigPath = exports.getPackages = void 0;
7
- const fs_1 = __importDefault(require("fs"));
8
- const path_1 = __importDefault(require("path"));
9
- function getPackages(cmdRes) {
10
- return cmdRes
11
- .split('\n')
12
- .map((line) => {
13
- if (line.trim() === '') {
14
- return {
15
- path: undefined,
16
- name: undefined,
17
- };
18
- }
19
- const packageData = JSON.parse(line);
20
- if (packageData.location === '.') {
21
- return {
22
- path: undefined,
23
- name: undefined,
24
- };
25
- }
26
- // ignore packages without TypeScript configuration
27
- if (!fs_1.default.existsSync(`${packageData.location}/tsconfig.json`)) {
28
- return {
29
- path: undefined,
30
- name: undefined,
31
- };
32
- }
33
- return {
34
- path: packageData.location,
35
- name: packageData.name,
36
- };
37
- })
38
- .filter((packageData) => packageData.path && packageData.name);
39
- }
40
- exports.getPackages = getPackages;
41
- /**
42
- * Search a folder for the preferred tsconfig.json file based
43
- * on the options in tsConfigNames.
44
- *
45
- * @param workspacePath Directory to search in
46
- * @param tsConfigNames List of tsconfig.json files to search for in order
47
- * of preference
48
- */
49
- function getTsConfigPath(workspacePath, tsConfigNames) {
50
- return tsConfigNames
51
- .map((tsConfigName) => path_1.default.posix.join(workspacePath, tsConfigName))
52
- .find((tsConfigPath) => fs_1.default.existsSync(tsConfigPath));
53
- }
54
- exports.getTsConfigPath = getTsConfigPath;
55
- /**
56
- * Calculate the references array for the package
57
- *
58
- * @param packagePath Path to the package we are updating references for
59
- * @param packages Packages the package wants to reference
60
- * @param tsConfigNames Configured tsconfig file names
61
- */
62
- function makeReferences(packagePath, packages, tsConfigNames) {
63
- return (packages
64
- .filter((p) => !!p)
65
- .map((dependencyData) => getTsConfigPath(dependencyData.path, tsConfigNames))
66
- .filter((p) => !!p)
67
- // for each add a path
68
- .map((tsConfigPath) => ({
69
- path: path_1.default.posix.relative(packagePath, path_1.default.basename(tsConfigPath) === 'tsconfig.json'
70
- ? path_1.default.dirname(tsConfigPath)
71
- : tsConfigPath),
72
- })));
73
- }
74
- exports.makeReferences = makeReferences;
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.makeReferences = exports.getTsConfigPath = exports.getPackages = void 0;
7
+ const fs_1 = __importDefault(require("fs"));
8
+ const path_1 = __importDefault(require("path"));
9
+ function getPackages(cmdRes) {
10
+ return cmdRes
11
+ .split('\n')
12
+ .map((line) => {
13
+ if (line.trim() === '') {
14
+ return {
15
+ path: undefined,
16
+ name: undefined,
17
+ };
18
+ }
19
+ const packageData = JSON.parse(line);
20
+ if (packageData.location === '.') {
21
+ return {
22
+ path: undefined,
23
+ name: undefined,
24
+ };
25
+ }
26
+ // ignore packages without TypeScript configuration
27
+ if (!fs_1.default.existsSync(`${packageData.location}/tsconfig.json`)) {
28
+ return {
29
+ path: undefined,
30
+ name: undefined,
31
+ };
32
+ }
33
+ return {
34
+ path: packageData.location,
35
+ name: packageData.name,
36
+ };
37
+ })
38
+ .filter((packageData) => packageData.path && packageData.name);
39
+ }
40
+ exports.getPackages = getPackages;
41
+ /**
42
+ * Search a folder for the preferred tsconfig.json file based
43
+ * on the options in tsConfigNames.
44
+ *
45
+ * @param workspacePath Directory to search in
46
+ * @param tsConfigNames List of tsconfig.json files to search for in order
47
+ * of preference
48
+ */
49
+ function getTsConfigPath(workspacePath, tsConfigNames) {
50
+ return tsConfigNames
51
+ .map((tsConfigName) => path_1.default.posix.join(workspacePath, tsConfigName))
52
+ .find((tsConfigPath) => fs_1.default.existsSync(tsConfigPath));
53
+ }
54
+ exports.getTsConfigPath = getTsConfigPath;
55
+ /**
56
+ * Calculate the references array for the package
57
+ *
58
+ * @param packagePath Path to the package we are updating references for
59
+ * @param packages Packages the package wants to reference
60
+ * @param tsConfigNames Configured tsconfig file names
61
+ */
62
+ function makeReferences(packagePath, packages, tsConfigNames) {
63
+ return (packages
64
+ .filter((p) => !!p)
65
+ .map((dependencyData) => getTsConfigPath(dependencyData.path, tsConfigNames))
66
+ .filter((p) => !!p)
67
+ // for each add a path
68
+ .map((tsConfigPath) => ({
69
+ path: path_1.default.posix.relative(packagePath, path_1.default.basename(tsConfigPath) === 'tsconfig.json'
70
+ ? path_1.default.dirname(tsConfigPath)
71
+ : tsConfigPath),
72
+ })));
73
+ }
74
+ exports.makeReferences = makeReferences;
75
75
  //# sourceMappingURL=sharedUtils.js.map
@@ -1,5 +1,5 @@
1
- export declare const updatePackageProjectReferences: ({ tsConfigNames, excludeInReferences, }: {
2
- tsConfigNames: string[];
3
- excludeInReferences: string[];
4
- }) => void;
1
+ export declare const updatePackageProjectReferences: ({ tsConfigNames, excludeInReferences, }: {
2
+ tsConfigNames: string[];
3
+ excludeInReferences: string[];
4
+ }) => void;
5
5
  //# sourceMappingURL=updatePackageProjectReferences.d.ts.map
@@ -1,81 +1,81 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.updatePackageProjectReferences = void 0;
7
- const fs_1 = __importDefault(require("fs"));
8
- const child_process_1 = require("child_process");
9
- const sharedUtils_1 = require("./sharedUtils");
10
- const path_1 = __importDefault(require("path"));
11
- const updatePackageProjectReferences = ({ tsConfigNames, excludeInReferences, }) => {
12
- const cmdRes = (0, child_process_1.execSync)('yarn workspaces list --json').toString();
13
- const allPackages = (0, sharedUtils_1.getPackages)(cmdRes);
14
- let isSuccess = true;
15
- for (const packageData of allPackages) {
16
- const packageDir = packageData.path;
17
- if (fs_1.default.existsSync(path_1.default.resolve(packageDir, './tsconfig.json'))) {
18
- isSuccess =
19
- processPackage({
20
- packageDir,
21
- allPackages,
22
- tsConfigNames,
23
- excludeInReferences,
24
- }) === 'success' && isSuccess;
25
- }
26
- else {
27
- console.log(`Skipping package ${packageDir}`);
28
- }
29
- }
30
- if (!isSuccess) {
31
- throw new Error('One or more packages failed to update');
32
- }
33
- };
34
- exports.updatePackageProjectReferences = updatePackageProjectReferences;
35
- function processPackage({ packageDir, allPackages, tsConfigNames, excludeInReferences, }) {
36
- const packageJson = fs_1.default
37
- .readFileSync(path_1.default.resolve(packageDir, './package.json'))
38
- .toString();
39
- const packageJsonData = JSON.parse(packageJson);
40
- const tsConfigPath = (0, sharedUtils_1.getTsConfigPath)(packageDir, tsConfigNames);
41
- if (!tsConfigPath) {
42
- return 'success';
43
- }
44
- try {
45
- const tsConfig = fs_1.default.readFileSync(tsConfigPath).toString();
46
- const tsConfigData = JSON.parse(tsConfig);
47
- const oldReferences = tsConfigData.references || [];
48
- const newReferences = (0, sharedUtils_1.makeReferences)(packageDir, [
49
- ...Object.keys(packageJsonData.dependencies || {}),
50
- ...Object.keys(packageJsonData.devDependencies || {}),
51
- ]
52
- // all dependencies that are workspace dependencies and have a tsconfig.json
53
- .map((dependencyData) => allPackages.find((packageData) => packageData.name === dependencyData))
54
- // remove packages that should be excluded in references
55
- .filter((packageData) => packageData && excludeInReferences.indexOf(packageData.name) === -1), tsConfigNames);
56
- // Exit early if references are unchanged (using JSON for deep comparison)
57
- if (JSON.stringify(oldReferences) === JSON.stringify(newReferences)) {
58
- return 'success';
59
- }
60
- const newData = JSON.stringify({
61
- ...tsConfigData,
62
- // Override references; or omit them if empty
63
- references: newReferences.length ? newReferences : undefined,
64
- }, null, 2);
65
- // only update the config file when it has changed
66
- if (newReferences.length) {
67
- console.log(`Updating project references in ${tsConfigPath} to:` +
68
- newReferences.map((refData) => `\n ${refData.path}`).join(''));
69
- }
70
- else {
71
- console.log(`Removing project references in ${tsConfigPath}`);
72
- }
73
- fs_1.default.writeFileSync(tsConfigPath, newData);
74
- return 'success';
75
- }
76
- catch (e) {
77
- console.error(`Error while processing ${tsConfigPath}\n${e}`);
78
- return 'failure';
79
- }
80
- }
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.updatePackageProjectReferences = void 0;
7
+ const fs_1 = __importDefault(require("fs"));
8
+ const child_process_1 = require("child_process");
9
+ const sharedUtils_1 = require("./sharedUtils");
10
+ const path_1 = __importDefault(require("path"));
11
+ const updatePackageProjectReferences = ({ tsConfigNames, excludeInReferences, }) => {
12
+ const cmdRes = (0, child_process_1.execSync)('yarn workspaces list --json').toString();
13
+ const allPackages = (0, sharedUtils_1.getPackages)(cmdRes);
14
+ let isSuccess = true;
15
+ for (const packageData of allPackages) {
16
+ const packageDir = packageData.path;
17
+ if (fs_1.default.existsSync(path_1.default.resolve(packageDir, './tsconfig.json'))) {
18
+ isSuccess =
19
+ processPackage({
20
+ packageDir,
21
+ allPackages,
22
+ tsConfigNames,
23
+ excludeInReferences,
24
+ }) === 'success' && isSuccess;
25
+ }
26
+ else {
27
+ console.log(`Skipping package ${packageDir}`);
28
+ }
29
+ }
30
+ if (!isSuccess) {
31
+ throw new Error('One or more packages failed to update');
32
+ }
33
+ };
34
+ exports.updatePackageProjectReferences = updatePackageProjectReferences;
35
+ function processPackage({ packageDir, allPackages, tsConfigNames, excludeInReferences, }) {
36
+ const packageJson = fs_1.default
37
+ .readFileSync(path_1.default.resolve(packageDir, './package.json'))
38
+ .toString();
39
+ const packageJsonData = JSON.parse(packageJson);
40
+ const tsConfigPath = (0, sharedUtils_1.getTsConfigPath)(packageDir, tsConfigNames);
41
+ if (!tsConfigPath) {
42
+ return 'success';
43
+ }
44
+ try {
45
+ const tsConfig = fs_1.default.readFileSync(tsConfigPath).toString();
46
+ const tsConfigData = JSON.parse(tsConfig);
47
+ const oldReferences = tsConfigData.references || [];
48
+ const newReferences = (0, sharedUtils_1.makeReferences)(packageDir, [
49
+ ...Object.keys(packageJsonData.dependencies || {}),
50
+ ...Object.keys(packageJsonData.devDependencies || {}),
51
+ ]
52
+ // all dependencies that are workspace dependencies and have a tsconfig.json
53
+ .map((dependencyData) => allPackages.find((packageData) => packageData.name === dependencyData))
54
+ // remove packages that should be excluded in references
55
+ .filter((packageData) => packageData && excludeInReferences.indexOf(packageData.name) === -1), tsConfigNames);
56
+ // Exit early if references are unchanged (using JSON for deep comparison)
57
+ if (JSON.stringify(oldReferences) === JSON.stringify(newReferences)) {
58
+ return 'success';
59
+ }
60
+ const newData = JSON.stringify({
61
+ ...tsConfigData,
62
+ // Override references; or omit them if empty
63
+ references: newReferences.length ? newReferences : undefined,
64
+ }, null, 2);
65
+ // only update the config file when it has changed
66
+ if (newReferences.length) {
67
+ console.log(`Updating project references in ${tsConfigPath} to:` +
68
+ newReferences.map((refData) => `\n ${refData.path}`).join(''));
69
+ }
70
+ else {
71
+ console.log(`Removing project references in ${tsConfigPath}`);
72
+ }
73
+ fs_1.default.writeFileSync(tsConfigPath, newData);
74
+ return 'success';
75
+ }
76
+ catch (e) {
77
+ console.error(`Error while processing ${tsConfigPath}\n${e}`);
78
+ return 'failure';
79
+ }
80
+ }
81
81
  //# sourceMappingURL=updatePackageProjectReferences.js.map
@@ -1,5 +1,5 @@
1
- export declare const updateRootProjectReferences: ({ tsConfigNames, excludeProjects, }: {
2
- tsConfigNames: string[];
3
- excludeProjects: string[];
4
- }) => void;
1
+ export declare const updateRootProjectReferences: ({ tsConfigNames, excludeProjects, }: {
2
+ tsConfigNames: string[];
3
+ excludeProjects: string[];
4
+ }) => void;
5
5
  //# sourceMappingURL=updateRootProjectReferences.d.ts.map
@@ -1,47 +1,47 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.updateRootProjectReferences = void 0;
7
- const fs_1 = __importDefault(require("fs"));
8
- const child_process_1 = require("child_process");
9
- const sharedUtils_1 = require("./sharedUtils");
10
- const updateRootProjectReferences = ({ tsConfigNames, excludeProjects, }) => {
11
- const cmdRes = (0, child_process_1.execSync)('yarn workspaces list --json').toString();
12
- const allPackages = (0, sharedUtils_1.getPackages)(cmdRes);
13
- const tsConfigPath = (0, sharedUtils_1.getTsConfigPath)('.', tsConfigNames);
14
- if (!tsConfigPath) {
15
- console.error('No root-level tsconfig.json found');
16
- return;
17
- }
18
- try {
19
- const tsConfig = fs_1.default.readFileSync(tsConfigPath).toString();
20
- const tsConfigData = JSON.parse(tsConfig);
21
- const oldReferences = tsConfigData.references;
22
- const newReferences = (0, sharedUtils_1.makeReferences)('.', allPackages.filter((packageData) => excludeProjects.indexOf(packageData.name) === -1), tsConfigNames);
23
- // Don't continue if references are unchanged
24
- if (JSON.stringify(newReferences) === JSON.stringify(oldReferences)) {
25
- return;
26
- }
27
- const newContent = JSON.stringify({
28
- ...tsConfigData,
29
- // Override references; or omit them if empty
30
- references: newReferences.length ? newReferences : undefined,
31
- }, null, 2);
32
- if (newReferences.length) {
33
- console.log(`Updating project references in ${tsConfigPath} to:` +
34
- newReferences.map((refData) => `\n ${refData.path}`).join(''));
35
- }
36
- else {
37
- console.log(`Removing project references in ${tsConfigPath}`);
38
- }
39
- fs_1.default.writeFileSync(tsConfigPath, newContent);
40
- }
41
- catch (e) {
42
- console.error(`Error while processing top level config file ${tsConfigPath} \n${e}`);
43
- throw e;
44
- }
45
- };
46
- exports.updateRootProjectReferences = updateRootProjectReferences;
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.updateRootProjectReferences = void 0;
7
+ const fs_1 = __importDefault(require("fs"));
8
+ const child_process_1 = require("child_process");
9
+ const sharedUtils_1 = require("./sharedUtils");
10
+ const updateRootProjectReferences = ({ tsConfigNames, excludeProjects, }) => {
11
+ const cmdRes = (0, child_process_1.execSync)('yarn workspaces list --json').toString();
12
+ const allPackages = (0, sharedUtils_1.getPackages)(cmdRes);
13
+ const tsConfigPath = (0, sharedUtils_1.getTsConfigPath)('.', tsConfigNames);
14
+ if (!tsConfigPath) {
15
+ console.error('No root-level tsconfig.json found');
16
+ return;
17
+ }
18
+ try {
19
+ const tsConfig = fs_1.default.readFileSync(tsConfigPath).toString();
20
+ const tsConfigData = JSON.parse(tsConfig);
21
+ const oldReferences = tsConfigData.references;
22
+ const newReferences = (0, sharedUtils_1.makeReferences)('.', allPackages.filter((packageData) => excludeProjects.indexOf(packageData.name) === -1), tsConfigNames);
23
+ // Don't continue if references are unchanged
24
+ if (JSON.stringify(newReferences) === JSON.stringify(oldReferences)) {
25
+ return;
26
+ }
27
+ const newContent = JSON.stringify({
28
+ ...tsConfigData,
29
+ // Override references; or omit them if empty
30
+ references: newReferences.length ? newReferences : undefined,
31
+ }, null, 2);
32
+ if (newReferences.length) {
33
+ console.log(`Updating project references in ${tsConfigPath} to:` +
34
+ newReferences.map((refData) => `\n ${refData.path}`).join(''));
35
+ }
36
+ else {
37
+ console.log(`Removing project references in ${tsConfigPath}`);
38
+ }
39
+ fs_1.default.writeFileSync(tsConfigPath, newContent);
40
+ }
41
+ catch (e) {
42
+ console.error(`Error while processing top level config file ${tsConfigPath} \n${e}`);
43
+ throw e;
44
+ }
45
+ };
46
+ exports.updateRootProjectReferences = updateRootProjectReferences;
47
47
  //# sourceMappingURL=updateRootProjectReferences.js.map
@@ -1,2 +1,2 @@
1
- export declare const run: (args: string[]) => Promise<void>;
1
+ export declare const run: (args: string[]) => Promise<void>;
2
2
  //# sourceMappingURL=utilsTypeScriptReferences.d.ts.map
@@ -1,75 +1,75 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.run = void 0;
7
- const updatePackageProjectReferences_1 = require("./updatePackageProjectReferences");
8
- const updateRootProjectReferences_1 = require("./updateRootProjectReferences");
9
- const yargs_1 = __importDefault(require("yargs"));
10
- const run = async (args) => {
11
- var _a;
12
- const argv = await yargs_1.default
13
- .scriptName('utils-typescript-references')
14
- .version('0.2.0')
15
- .option('skipPackages', {
16
- description: 'Only update project references in the root tsConfig',
17
- type: 'boolean',
18
- })
19
- .option('skipRoot', {
20
- description: 'Skip updating project references in project root tsConfig',
21
- type: 'boolean',
22
- })
23
- .option('excludeInReferences', {
24
- type: 'array',
25
- description: 'Exclude specific packages from being referenced by other packages',
26
- })
27
- .option('excludeInRoot', {
28
- type: 'array',
29
- description: 'Exclude specific packages from being referenced in the root tsConfig',
30
- })
31
- .option('tsConfigName', {
32
- type: 'array',
33
- description: 'Names of tsConfig files to be updated',
34
- })
35
- .parse();
36
- const options = {
37
- tsConfigNames: [],
38
- excludeInReferences: [],
39
- excludeInRoot: [],
40
- skipPackages: false,
41
- skipRoot: false,
42
- };
43
- if (argv.skipPackages) {
44
- options.skipPackages = true;
45
- }
46
- if (argv.skipRoot) {
47
- options.skipRoot = true;
48
- }
49
- if (argv.tsConfigName && ((_a = argv.tsConfigName) === null || _a === void 0 ? void 0 : _a.length) > 0) {
50
- options.tsConfigNames = argv.tsConfigName;
51
- }
52
- else {
53
- options.tsConfigNames.push('tsconfig.json');
54
- }
55
- if (argv.excludeInReferences) {
56
- options.excludeInReferences = argv.excludeInReferences;
57
- }
58
- if (argv.excludeInRoot) {
59
- options.excludeInRoot = argv.excludeInRoot;
60
- }
61
- if (!options.skipPackages) {
62
- (0, updatePackageProjectReferences_1.updatePackageProjectReferences)({
63
- tsConfigNames: options.tsConfigNames,
64
- excludeInReferences: options.excludeInReferences,
65
- });
66
- }
67
- if (!options.skipRoot) {
68
- (0, updateRootProjectReferences_1.updateRootProjectReferences)({
69
- tsConfigNames: options.tsConfigNames,
70
- excludeProjects: options.excludeInRoot,
71
- });
72
- }
73
- };
74
- exports.run = run;
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.run = void 0;
7
+ const updatePackageProjectReferences_1 = require("./updatePackageProjectReferences");
8
+ const updateRootProjectReferences_1 = require("./updateRootProjectReferences");
9
+ const yargs_1 = __importDefault(require("yargs"));
10
+ const run = async (args) => {
11
+ var _a;
12
+ const argv = await yargs_1.default
13
+ .scriptName('utils-typescript-references')
14
+ .version('0.2.0')
15
+ .option('skipPackages', {
16
+ description: 'Only update project references in the root tsConfig',
17
+ type: 'boolean',
18
+ })
19
+ .option('skipRoot', {
20
+ description: 'Skip updating project references in project root tsConfig',
21
+ type: 'boolean',
22
+ })
23
+ .option('excludeInReferences', {
24
+ type: 'array',
25
+ description: 'Exclude specific packages from being referenced by other packages',
26
+ })
27
+ .option('excludeInRoot', {
28
+ type: 'array',
29
+ description: 'Exclude specific packages from being referenced in the root tsConfig',
30
+ })
31
+ .option('tsConfigName', {
32
+ type: 'array',
33
+ description: 'Names of tsConfig files to be updated',
34
+ })
35
+ .parse();
36
+ const options = {
37
+ tsConfigNames: [],
38
+ excludeInReferences: [],
39
+ excludeInRoot: [],
40
+ skipPackages: false,
41
+ skipRoot: false,
42
+ };
43
+ if (argv.skipPackages) {
44
+ options.skipPackages = true;
45
+ }
46
+ if (argv.skipRoot) {
47
+ options.skipRoot = true;
48
+ }
49
+ if (argv.tsConfigName && ((_a = argv.tsConfigName) === null || _a === void 0 ? void 0 : _a.length) > 0) {
50
+ options.tsConfigNames = argv.tsConfigName;
51
+ }
52
+ else {
53
+ options.tsConfigNames.push('tsconfig.json');
54
+ }
55
+ if (argv.excludeInReferences) {
56
+ options.excludeInReferences = argv.excludeInReferences;
57
+ }
58
+ if (argv.excludeInRoot) {
59
+ options.excludeInRoot = argv.excludeInRoot;
60
+ }
61
+ if (!options.skipPackages) {
62
+ (0, updatePackageProjectReferences_1.updatePackageProjectReferences)({
63
+ tsConfigNames: options.tsConfigNames,
64
+ excludeInReferences: options.excludeInReferences,
65
+ });
66
+ }
67
+ if (!options.skipRoot) {
68
+ (0, updateRootProjectReferences_1.updateRootProjectReferences)({
69
+ tsConfigNames: options.tsConfigNames,
70
+ excludeProjects: options.excludeInRoot,
71
+ });
72
+ }
73
+ };
74
+ exports.run = run;
75
75
  //# sourceMappingURL=utilsTypeScriptReferences.js.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@goldstack/utils-typescript-references",
3
- "version": "0.2.2",
3
+ "version": "0.3.0",
4
4
  "description": "Utility for keeping TypeScript references in sync in a Yarn monorepo",
5
5
  "keywords": [
6
6
  "goldstack",
@@ -39,15 +39,15 @@
39
39
  "yargs": "^17.5.1"
40
40
  },
41
41
  "devDependencies": {
42
- "@goldstack/utils-git": "0.1.35",
43
- "@types/jest": "^27.5.1",
44
- "@types/node": "^17.0.33",
42
+ "@goldstack/utils-git": "0.2.0",
43
+ "@types/jest": "^28.1.8",
44
+ "@types/node": "^18.7.13",
45
45
  "@types/source-map-support": "^0.5.4",
46
46
  "@types/yargs": "^17.0.10",
47
47
  "jest": "^28.1.0",
48
48
  "rimraf": "^3.0.2",
49
49
  "ts-jest": "^28.0.2",
50
- "typescript": "^4.6.4"
50
+ "typescript": "^4.7.4"
51
51
  },
52
52
  "publishConfig": {
53
53
  "main": "dist/src/utilsTypeScriptReferences.js"