Skip to content

Commit

Permalink
build: allow for "yarn lint:versions" to "--fix" those versions and i…
Browse files Browse the repository at this point in the history
…nstall
  • Loading branch information
Westbrook Johnson authored and Westbrook committed Feb 16, 2022
1 parent 102cbe5 commit cdeb543
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 14 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,9 @@ The project will be linted on a pre-commit hook, but you can also run the lint s

### Dependency linting

There are downstream issues that can arise from multiple packages in this mono-repo using dependencies with mismatching version strings. This is particularly an issue for dependencies below `1.0.0` but can be exacerbated in that context and others by more strict settings that can be applied in various package managers. By default, Lerna will bump version numbers of internal dependencies when the various packages are published and the depended version is pointing to the latest release, which can help to mitigate this issue. This can be further mitigated by using `^0.0.0` structured dependency versions, the `^` allowing for the highest amount of upward flexibility in satisfying the dependency. When using these version strings, `yarn lint: versions` which ensure that all instances of those strings for the same dependency match across the repo.
There are downstream issues that can arise from multiple packages in this mono-repo using dependencies with mismatching version strings. This is particularly an issue for dependencies below `1.0.0` but can be exacerbated in that context and others by more strict settings that can be applied in various package managers. By default, Lerna will bump version numbers of internal dependencies when the various packages are published and the depended version is pointing to the latest release, which can help to mitigate this issue. This can be further mitigated by using `^0.0.0` structured dependency versions, the `^` allowing for the highest amount of upward flexibility in satisfying the dependency. When using these version strings, `yarn lint:versions` which ensure that all instances of those strings for the same dependency match across the repo.

`yarn list:versions --fix` will reach into the `package.json` files and update all dependencies to the latest version available in the library, _a possibly dangerous operation_. If you know this is what you want to do when there are mismatched versions found by `yarn lint:versions`, this can make greatly shorten the amount of work to catch the versions up to each other.

## Testing

Expand Down
2 changes: 1 addition & 1 deletion packages/icons-ui/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
"fs": "^0.0.1-security",
"glob": "^7.1.3",
"path": "^0.12.7",
"prettier": "^2.2.1"
"prettier": "^2.4.1"
},
"types": "./src/index.d.ts",
"customElements": "custom-elements.json",
Expand Down
2 changes: 1 addition & 1 deletion packages/icons-workflow/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
"fs": "^0.0.1-security",
"glob": "^7.1.3",
"path": "^0.12.7",
"prettier": "^2.2.1"
"prettier": "^2.4.1"
},
"types": "./src/index.d.ts",
"customElements": "custom-elements.json",
Expand Down
2 changes: 1 addition & 1 deletion packages/slider/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
"lit-html"
],
"dependencies": {
"@internationalized/number": "^3.0.0",
"@internationalized/number": "^3.0.2",
"@spectrum-web-components/base": "^0.5.1",
"@spectrum-web-components/field-label": "^0.7.3",
"@spectrum-web-components/number-field": "^0.3.4",
Expand Down
2 changes: 1 addition & 1 deletion packages/tray/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
"dependencies": {
"@spectrum-web-components/base": "^0.5.1",
"@spectrum-web-components/modal": "^0.6.0",
"@spectrum-web-components/reactive-controllers": "0.2.0",
"@spectrum-web-components/reactive-controllers": "^0.2.0",
"@spectrum-web-components/shared": "^0.13.3",
"@spectrum-web-components/underlay": "^0.8.2",
"tslib": "^2.0.0"
Expand Down
52 changes: 45 additions & 7 deletions scripts/lint-versions.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,13 @@ the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTA
OF ANY KIND, either express or implied. See the License for the specific language
governing permissions and limitations under the License.
*/
import { readdirSync, existsSync, readFileSync } from 'fs';
import { existsSync, readdirSync, readFileSync, writeFileSync } from 'fs';
import { execSync } from 'child_process';
import path from 'path';
import yargs from 'yargs';
import { hideBin } from 'yargs/helpers';

const { fix } = yargs(hideBin(process.argv)).argv;

const getDirectories = (source) =>
readdirSync(source, { withFileTypes: true })
Expand Down Expand Up @@ -46,15 +51,25 @@ function readPackageJsonNameVersion(filePath) {
return {};
}

function readPackageJsonName(filePath) {
if (existsSync(filePath)) {
const jsonData = JSON.parse(readFileSync(filePath, 'utf-8'));
return jsonData.name;
}
return {};
}

function compareVersions(versionsA, versionsB) {
let output = '';
const newVersions = { ...versionsA };
const dependencyNamesAndVersions = [];
Object.keys(versionsB).forEach((dep) => {
if (
versionsA[dep] &&
versionsB[dep] &&
versionsA[dep] !== versionsB[dep]
) {
dependencyNamesAndVersions.push([dep, versionsA[dep]]);
output += ` - "${dep}" should be "${versionsA[dep]}" but is "${versionsB[dep]}"\n`;
}
if (!newVersions[dep]) {
Expand All @@ -63,13 +78,15 @@ function compareVersions(versionsA, versionsB) {
});

return {
dependencyNamesAndVersions,
output,
newVersions,
};
}

let currentVersions = readPackageJsonDeps('./package.json');
let endReturn = 0;
let written = false;

// find all versions in the monorepo
getDirectories('./packages').forEach((subPackage) => {
Expand All @@ -84,19 +101,40 @@ getDirectories('./packages').forEach((subPackage) => {
getDirectories('./packages').forEach((subPackage) => {
const filePath = path.normalize(`./packages/${subPackage}/package.json`);
const subPackageVersions = readPackageJsonDeps(filePath);
const { output, newVersions } = compareVersions(
const packageName = readPackageJsonName(filePath);
const { dependencyNamesAndVersions, output, newVersions } = compareVersions(
currentVersions,
subPackageVersions
subPackageVersions,
packageName
);
currentVersions = { ...newVersions };
if (output) {
console.log(`Version mismatches found in "${filePath}":`);
console.log(output);
console.log();
endReturn = 1;
if (fix) {
const jsonData = JSON.parse(readFileSync(filePath, 'utf-8'));
dependencyNamesAndVersions.forEach(([dep, version]) => {
if (jsonData.dependencies[dep]) {
jsonData.dependencies[dep] = version;
}
if (jsonData.devDependencies[dep]) {
jsonData.devDependencies[dep] = version;
}
});
writeFileSync(filePath, JSON.stringify(jsonData));
written = true;
} else {
console.log(`Version mismatches found in "${filePath}":`);
console.log(output);
console.log();
endReturn = 1;
}
}
});

if (written) {
execSync('yarn lint:packagejson');
execSync('yarn --ignore-scripts');
}

if (endReturn === 0) {
console.log('All versions are aligned 💪');
}
Expand Down
4 changes: 2 additions & 2 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2707,7 +2707,7 @@
resolved "https://registry.yarnpkg.com/@iarna/toml/-/toml-2.2.5.tgz#b32366c89b43c6f8cefbdefac778b9c828e3ba8c"
integrity sha512-trnsAYxU3xnS1gPHPyU961coFyLkh4gAD/0zQ5mymY4yOZ+CYvsPqUbOFSw0aDM4y0tV7tiFxL/1XfXPNC6IPg==

"@internationalized/number@^3.0.0", "@internationalized/number@^3.0.2":
"@internationalized/number@^3.0.2":
version "3.0.3"
resolved "https://registry.yarnpkg.com/@internationalized/number/-/number-3.0.3.tgz#d29003dffdff54ca6f2287ec0cb77ff3d045478f"
integrity sha512-ewFoVvsxSyd9QZnknvOWPjirYqdMQhXTeDhJg3hM6C/FeZt0banpGH1nZ0SGMZXHz8NK9uAa2KVIq+jqAIOg4w==
Expand Down Expand Up @@ -18575,7 +18575,7 @@ prettier-plugin-package@^1.3.0:
resolved "https://registry.yarnpkg.com/prettier-plugin-package/-/prettier-plugin-package-1.3.0.tgz#b42d12eda16b67c9fcf95054d799fe3f542aef54"
integrity sha512-KPNHR/Jm2zTevBp1SnjzMnooO1BOQW2bixVbOp8flOJoW+dxdDwEncObfsKZdkjwrv6AIH4oWqm5EO/etDmK9Q==

prettier@^2.2.1, prettier@^2.4.1:
prettier@^2.4.1:
version "2.5.1"
resolved "https://registry.yarnpkg.com/prettier/-/prettier-2.5.1.tgz#fff75fa9d519c54cf0fce328c1017d94546bc56a"
integrity sha512-vBZcPRUR5MZJwoyi3ZoyQlc1rXeEck8KgeC9AwwOn+exuxLxq5toTRDTSaVrXHxelDMHy9zlicw8u66yxoSUFg==
Expand Down

0 comments on commit cdeb543

Please sign in to comment.