@goldstack/utils-typescript-references
Advanced tools
Comparing version
@@ -1,2 +0,5 @@ | ||
export declare const updatePackageProjectReferences: (tsConfigNames: string[]) => void; | ||
export declare const updatePackageProjectReferences: ({ tsConfigNames, excludeInReferences, }: { | ||
tsConfigNames: string[]; | ||
excludeInReferences: string[]; | ||
}) => void; | ||
//# sourceMappingURL=updatePackageProjectReferences.d.ts.map |
@@ -11,3 +11,3 @@ "use strict"; | ||
const path_1 = __importDefault(require("path")); | ||
const updatePackageProjectReferences = (tsConfigNames) => { | ||
const updatePackageProjectReferences = ({ tsConfigNames, excludeInReferences, }) => { | ||
const cmdRes = (0, child_process_1.execSync)('yarn workspaces list --json').toString(); | ||
@@ -20,4 +20,8 @@ const allPackages = (0, sharedUtils_1.getPackages)(cmdRes); | ||
isSuccess = | ||
processPackage(packageDir, allPackages, packageData, tsConfigNames) === | ||
'success' && isSuccess; | ||
processPackage({ | ||
packageDir, | ||
allPackages, | ||
tsConfigNames, | ||
excludeInReferences, | ||
}) === 'success' && isSuccess; | ||
} | ||
@@ -33,3 +37,3 @@ else { | ||
exports.updatePackageProjectReferences = updatePackageProjectReferences; | ||
function processPackage(packageDir, allPackages, packageData, tsConfigNames) { | ||
function processPackage({ packageDir, allPackages, tsConfigNames, excludeInReferences, }) { | ||
const packageJson = fs_1.default | ||
@@ -52,3 +56,5 @@ .readFileSync(path_1.default.resolve(packageDir, './package.json')) | ||
// all dependencies that are workspace dependencies and have a tsconfig.json | ||
.map((dependencyData) => allPackages.find((packageData) => packageData.name === dependencyData)), tsConfigNames); | ||
.map((dependencyData) => allPackages.find((packageData) => packageData.name === dependencyData)) | ||
// remove packages that should be excluded in references | ||
.filter((packageData) => packageData && excludeInReferences.indexOf(packageData.name) === -1), tsConfigNames); | ||
// Exit early if references are unchanged (using JSON for deep comparison) | ||
@@ -55,0 +61,0 @@ if (JSON.stringify(oldReferences) === JSON.stringify(newReferences)) { |
@@ -1,2 +0,5 @@ | ||
export declare const updateRootProjectReferences: (tsConfigNames: string[]) => void; | ||
export declare const updateRootProjectReferences: ({ tsConfigNames, excludeProjects, }: { | ||
tsConfigNames: string[]; | ||
excludeProjects: string[]; | ||
}) => void; | ||
//# sourceMappingURL=updateRootProjectReferences.d.ts.map |
@@ -10,3 +10,3 @@ "use strict"; | ||
const sharedUtils_1 = require("./sharedUtils"); | ||
const updateRootProjectReferences = (tsConfigNames) => { | ||
const updateRootProjectReferences = ({ tsConfigNames, excludeProjects, }) => { | ||
const cmdRes = (0, child_process_1.execSync)('yarn workspaces list --json').toString(); | ||
@@ -16,3 +16,3 @@ const allPackages = (0, sharedUtils_1.getPackages)(cmdRes); | ||
if (!tsConfigPath) { | ||
console.warn('No root-level tsconfig.json found'); | ||
console.error('No root-level tsconfig.json found'); | ||
return; | ||
@@ -24,3 +24,3 @@ } | ||
const oldReferences = tsConfigData.references; | ||
const newReferences = (0, sharedUtils_1.makeReferences)('.', allPackages, tsConfigNames); | ||
const newReferences = (0, sharedUtils_1.makeReferences)('.', allPackages.filter((packageData) => excludeProjects.indexOf(packageData.name) === -1), tsConfigNames); | ||
// Don't continue if references are unchanged | ||
@@ -27,0 +27,0 @@ if (JSON.stringify(newReferences) === JSON.stringify(oldReferences)) { |
@@ -1,2 +0,2 @@ | ||
export declare const run: (args: string[]) => void; | ||
export declare const run: (args: string[]) => Promise<void>; | ||
//# sourceMappingURL=utilsTypeScriptReferences.d.ts.map |
"use strict"; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
@@ -6,37 +9,65 @@ exports.run = void 0; | ||
const updateRootProjectReferences_1 = require("./updateRootProjectReferences"); | ||
const run = (args) => { | ||
const yargs_1 = __importDefault(require("yargs")); | ||
const run = async (args) => { | ||
var _a; | ||
const argv = await yargs_1.default | ||
.scriptName('utils-typescript-references') | ||
.version('0.2.0') | ||
.option('skipPackages', { | ||
description: 'Only update project references in the root tsConfig', | ||
type: 'boolean', | ||
}) | ||
.option('skipRoot', { | ||
description: 'Skip updating project references in project root tsConfig', | ||
type: 'boolean', | ||
}) | ||
.option('excludeInReferences', { | ||
type: 'array', | ||
description: 'Exclude specific packages from being referenced by other packages', | ||
}) | ||
.option('excludeInRoot', { | ||
type: 'array', | ||
description: 'Exclude specific packages from being referenced in the root tsConfig', | ||
}) | ||
.option('tsConfigName', { | ||
type: 'array', | ||
description: 'Names of tsConfig files to be updated', | ||
}) | ||
.parse(); | ||
const options = { | ||
tsConfigNames: [], | ||
excludeInReferences: [], | ||
excludeInRoot: [], | ||
skipPackages: false, | ||
skipRoot: false, | ||
}; | ||
const defaultArgFallback = (arg) => { | ||
console.warn(`Unexpected command line argument: ${arg}`); | ||
}; | ||
let argFallback = defaultArgFallback; | ||
for (const arg of args.slice(2)) { | ||
if (arg === '--skipPackages') { | ||
options.skipPackages = true; | ||
} | ||
else if (arg === '--skipRoot') { | ||
options.skipRoot = true; | ||
} | ||
else if (arg === '--tsConfigName') { | ||
argFallback = (arg) => { | ||
options.tsConfigNames.push(arg); | ||
argFallback = defaultArgFallback; | ||
}; | ||
} | ||
else { | ||
argFallback(arg); | ||
} | ||
if (argv.skipPackages) { | ||
options.skipPackages = true; | ||
} | ||
if (!options.tsConfigNames.length) { | ||
if (argv.skipRoot) { | ||
options.skipRoot = true; | ||
} | ||
if (argv.tsConfigName && ((_a = argv.tsConfigName) === null || _a === void 0 ? void 0 : _a.length) > 0) { | ||
options.tsConfigNames = argv.tsConfigName; | ||
} | ||
else { | ||
options.tsConfigNames.push('tsconfig.json'); | ||
} | ||
if (argv.excludeInReferences) { | ||
options.excludeInReferences = argv.excludeInReferences; | ||
} | ||
if (argv.excludeInRoot) { | ||
options.excludeInRoot = argv.excludeInRoot; | ||
} | ||
if (!options.skipPackages) { | ||
(0, updatePackageProjectReferences_1.updatePackageProjectReferences)(options.tsConfigNames); | ||
(0, updatePackageProjectReferences_1.updatePackageProjectReferences)({ | ||
tsConfigNames: options.tsConfigNames, | ||
excludeInReferences: options.excludeInReferences, | ||
}); | ||
} | ||
if (!options.skipRoot) { | ||
(0, updateRootProjectReferences_1.updateRootProjectReferences)(options.tsConfigNames); | ||
(0, updateRootProjectReferences_1.updateRootProjectReferences)({ | ||
tsConfigNames: options.tsConfigNames, | ||
excludeProjects: options.excludeInRoot, | ||
}); | ||
} | ||
@@ -43,0 +74,0 @@ }; |
{ | ||
"name": "@goldstack/utils-typescript-references", | ||
"version": "0.1.14", | ||
"version": "0.2.1", | ||
"description": "Utility for keeping TypeScript references in sync in a Yarn monorepo", | ||
@@ -38,3 +38,4 @@ "keywords": [ | ||
"dependencies": { | ||
"source-map-support": "^0.5.21" | ||
"source-map-support": "^0.5.21", | ||
"yargs": "^17.5.1" | ||
}, | ||
@@ -46,2 +47,3 @@ "devDependencies": { | ||
"@types/source-map-support": "^0.5.4", | ||
"@types/yargs": "^17.0.10", | ||
"jest": "^28.1.0", | ||
@@ -48,0 +50,0 @@ "rimraf": "^3.0.2", |
@@ -62,28 +62,72 @@ # TypeScript References Yarn Workspaces Sync Utility | ||
`utils-typescript-references --skipPackages` | ||
### --help | ||
Will skip updating the `references` in `tsconfig.json` files for all packages in the project. | ||
Shows a reference of all available options: | ||
`utils-typescript-references --skipRoot` | ||
``` | ||
$ utils-typescript-references --help | ||
Options: | ||
--help Show help [boolean] | ||
--version Show version number [boolean] | ||
--skipPackages Only update project references in the root tsConfig | ||
[boolean] | ||
--skipRoot Skip updating project references in project root | ||
tsConfig [boolean] | ||
--excludeInReferences Exclude specific packages from being referenced by | ||
other packages [array] | ||
--excludeInRoot Exclude specific packages from being referenced in the | ||
root tsConfig [array] | ||
--tsConfigName Names of tsConfig files to be updated [array] | ||
``` | ||
Will skip updating the `references` in the `tsconfig.json` file for the project root. | ||
### --tsConfigName | ||
`utils-typescript-references --tsConfigName tsconfig.build.json` | ||
Provide one or more name of `tsconfig.json` files in projects across the monorepo that should be updated. | ||
Will update and reference `tsconfig.build.json` files only; `tsconfig.json` files are ignored, and packages | ||
with only `tsconfig.json` and not `tsconfig.build.json` will not have references inserted. This is intended | ||
for monorepos where 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`. | ||
``` | ||
$ utils-typescript-references --tsConfigName tsconfig.json --tsConfigName tsconfig.build.json | ||
``` | ||
`utils-typescript-references --tsConfigName tsconfig.build.json --tsConfigName tsconfig.json` | ||
Defaults to `tsconfig.json` | ||
This is similar to the above but allows a fallback to referencing / updating `tsconfig.json` for some packages | ||
where `tsconfig.build.json` is not present (maybe it's not needed as there are no tests to compile separately). | ||
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`. | ||
`utils-typescript-references --tsConfigName src/tsconfig.json --tsConfigName tsconfig.json` | ||
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: | ||
Prefer to reference and update `tsconfig.json` inside a `src` subfolder rather than at the top | ||
of the package / project. | ||
``` | ||
$ utils-typescript-references --tsConfigName tsconfig.build.json --tsConfigName tsconfig.json | ||
``` | ||
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: | ||
``` | ||
$ utils-typescript-references --tsConfigName src/tsconfig.json --tsConfigName tsconfig.json | ||
``` | ||
### --excludeInReferences | ||
Will prevent certain packages from being added to the `references` of other projects in the monorepo. | ||
``` | ||
$ utils-typescript-references --excludeInReferences @myproject/packageA --excludeInReferences @myproject/PackageB | ||
``` | ||
The above will cause `@myproject/packageA` and `@myproject/packageB` not to be inserted as referenced in all other packages of the monorepo. | ||
### --skipPackages | ||
Will skip updating the `references` in `tsconfig.json` files for all packages in the project. | ||
``` | ||
$ utils-typescript-references --skipPackages | ||
``` | ||
### -skipRoot | ||
Will skip updating the `references` in the `tsconfig.json` file for the project root. | ||
``` | ||
$ utils-typescript-references --skipRoot | ||
``` | ||
## Limitations | ||
@@ -97,2 +141,3 @@ | ||
- [Code with Joy - The Ultimate Guide to TypeScript Monorepos](https://maxrohde.com/2021/11/20/the-ultimate-guide-to-typescript-monorepos/) | ||
- [The Full Stack Blog - TypeScript Monorepo with Yarn and Project References](https://maxrohde.com/2021/10/01/typescript-monorepo-with-yarn-and-project-references/) | ||
@@ -99,0 +144,0 @@ - [@monorepo-utils/workspaces-to-typescript-project-references](https://github.com/azu/monorepo-utils/tree/master/packages/@monorepo-utils/workspaces-to-typescript-project-references#readme) |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
29062
17.71%307
16.29%145
45%2
100%9
12.5%+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added