Skip to content

Commit

Permalink
refactor: rename cli-plugin-sync-dependencies (#4441)
Browse files Browse the repository at this point in the history
  • Loading branch information
brunozoric authored Dec 16, 2024
1 parent 7588daa commit 76ed2a8
Show file tree
Hide file tree
Showing 36 changed files with 206 additions and 12,589 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/pullRequests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,9 @@ jobs:
- name: ESLint
run: yarn eslint
working-directory: ${{ github.base_ref }}
- name: Sync Dependencies Verification
run: yarn webiny verify-dependencies
working-directory: ${{ github.base_ref }}
runs-on: ubuntu-latest
env:
NODE_OPTIONS: '--max_old_space_size=4096'
Expand Down
3 changes: 3 additions & 0 deletions .github/workflows/pushDev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,9 @@ jobs:
- name: ESLint
run: yarn eslint
working-directory: dev
- name: Sync Dependencies Verification
run: yarn webiny verify-dependencies
working-directory: dev
runs-on: ubuntu-latest
env:
NODE_OPTIONS: '--max_old_space_size=4096'
Expand Down
3 changes: 3 additions & 0 deletions .github/workflows/pushNext.yml
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,9 @@ jobs:
- name: ESLint
run: yarn eslint
working-directory: next
- name: Sync Dependencies Verification
run: yarn webiny verify-dependencies
working-directory: next
runs-on: ubuntu-latest
env:
NODE_OPTIONS: '--max_old_space_size=4096'
Expand Down
6 changes: 5 additions & 1 deletion .github/workflows/wac/pullRequests.wac.ts
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,11 @@ export const pullRequests = createWorkflow({
{ name: "Check code formatting", run: "yarn prettier:check" },
{ name: "Check dependencies", run: "yarn adio" },
{ name: "Check TS configs", run: "yarn check-ts-configs" },
{ name: "ESLint", run: "yarn eslint" }
{ name: "ESLint", run: "yarn eslint" },
{
name: "Sync Dependencies Verification",
run: "yarn webiny verify-dependencies"
}
],
{ "working-directory": DIR_WEBINY_JS }
)
Expand Down
6 changes: 5 additions & 1 deletion .github/workflows/wac/push.wac.ts
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,11 @@ const createPushWorkflow = (branchName: string) => {
{ name: "Check code formatting", run: "yarn prettier:check" },
{ name: "Check dependencies", run: "yarn adio" },
{ name: "Check TS configs", run: "yarn check-ts-configs" },
{ name: "ESLint", run: "yarn eslint" }
{ name: "ESLint", run: "yarn eslint" },
{
name: "Sync Dependencies Verification",
run: "yarn webiny verify-dependencies"
}
],
{ "working-directory": DIR_WEBINY_JS }
)
Expand Down
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ packages/ui/src/RichTextEditor/editorjs/**
packages/cli-plugin-deploy-pulumi/commands/newWatch/handler/mqtt.js
lerna.json
coverage/**
packages/cli/files/**/*.json
2 changes: 1 addition & 1 deletion packages/api-headless-cms-tasks/tsconfig.build.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
"include": ["src"],
"references": [
{ "path": "../api/tsconfig.build.json" },
{ "path": "../db/tsconfig.build.json" },
{ "path": "../api-aco/tsconfig.build.json" },
{ "path": "../api-headless-cms/tsconfig.build.json" },
{ "path": "../api-headless-cms-bulk-actions/tsconfig.build.json" },
{ "path": "../api-headless-cms-import-export/tsconfig.build.json" },
{ "path": "../db/tsconfig.build.json" },
{ "path": "../error/tsconfig.build.json" },
{ "path": "../handler-graphql/tsconfig.build.json" },
{ "path": "../tasks/tsconfig.build.json" },
Expand Down
6 changes: 3 additions & 3 deletions packages/api-headless-cms-tasks/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
"include": ["src", "__tests__"],
"references": [
{ "path": "../api" },
{ "path": "../db" },
{ "path": "../api-aco" },
{ "path": "../api-headless-cms" },
{ "path": "../api-headless-cms-bulk-actions" },
{ "path": "../api-headless-cms-import-export" },
{ "path": "../db" },
{ "path": "../error" },
{ "path": "../handler-graphql" },
{ "path": "../tasks" },
Expand All @@ -22,8 +22,6 @@
"~tests/*": ["./__tests__/*"],
"@webiny/api/*": ["../api/src/*"],
"@webiny/api": ["../api/src"],
"@webiny/db/*": ["../db/src/*"],
"@webiny/db": ["../db/src"],
"@webiny/api-aco/*": ["../api-aco/src/*"],
"@webiny/api-aco": ["../api-aco/src"],
"@webiny/api-headless-cms/*": ["../api-headless-cms/src/*"],
Expand All @@ -32,6 +30,8 @@
"@webiny/api-headless-cms-bulk-actions": ["../api-headless-cms-bulk-actions/src"],
"@webiny/api-headless-cms-import-export/*": ["../api-headless-cms-import-export/src/*"],
"@webiny/api-headless-cms-import-export": ["../api-headless-cms-import-export/src"],
"@webiny/db/*": ["../db/src/*"],
"@webiny/db": ["../db/src"],
"@webiny/error/*": ["../error/src/*"],
"@webiny/error": ["../error/src"],
"@webiny/handler-graphql/*": ["../handler-graphql/src/*"],
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# @webiny/cli-plugin-sync-dependencies
# @webiny/cli-plugin-dependencies

[![](https://img.shields.io/npm/dw/@webiny/cli-plugin-sync-dependencies.svg)](https://www.npmjs.com/package/@webiny/cli-plugin-sync-dependencies)
[![](https://img.shields.io/npm/v/@webiny/cli-plugin-sync-dependencies.svg)](https://www.npmjs.com/package/@webiny/cli-plugin-sync-dependencies)
[![](https://img.shields.io/npm/dw/@webiny/cli-plugin-dependencies.svg)](https://www.npmjs.com/package/@webiny/cli-plugin-dependencies)
[![](https://img.shields.io/npm/v/@webiny/cli-plugin-dependencies.svg)](https://www.npmjs.com/package/@webiny/cli-plugin-dependencies)
[![code style: prettier](https://img.shields.io/badge/code_style-prettier-ff69b4.svg?style=flat-square)](https://github.com/prettier/prettier)
[![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg?style=flat-square)](http://makeapullrequest.com)

Expand All @@ -10,7 +10,7 @@ A plugin for @webiny/cli that provides scaffolding capabilities by means of scaf
## Install

```
yarn add @webiny/cli-plugin-sync-dependencies
yarn add @webiny/cli-plugin-dependencies
```

Add plugin to your project by editing `webiny.project.js`:
Expand All @@ -19,7 +19,7 @@ Add plugin to your project by editing `webiny.project.js`:
module.exports = {
projectName: "my-project",
cli: {
plugins: ["@webiny/cli-plugin-sync-dependencies"]
plugins: ["@webiny/cli-plugin-dependencies"]
}
};
```
Original file line number Diff line number Diff line change
@@ -1,29 +1,27 @@
{
"name": "@webiny/cli-plugin-sync-dependencies",
"name": "@webiny/cli-plugin-dependencies",
"version": "0.0.0",
"main": "index.js",
"repository": {
"type": "git",
"url": "https://github.com/webiny/webiny-js.git",
"directory": "packages/cli-plugin-sync-dependencies"
"directory": "packages/cli-plugin-dependencies"
},
"author": "Webiny Ltd.",
"contributors": [
"Bruno Zorić <[email protected]>"
],
"description": "A tool to generate sync files for Webiny dependencies.",
"description": "A tool to generate and verify dependency sync files for Webiny.",
"dependencies": {
"@babel/runtime": "^7.26.0",
"@webiny/cli": "0.0.0",
"@webiny/cli-plugin-scaffold": "0.0.0",
"get-yarn-workspaces": "^1.0.2",
"glob": "^7.2.3",
"load-json-file": "^6.2.0",
"semver": "^7.6.3",
"write-json-file": "^4.3.0"
},
"devDependencies": {
"@webiny/cli": "0.0.0",
"@webiny/project-utils": "0.0.0",
"execa": "^5.1.1",
"prettier": "^2.8.8",
Expand Down
79 changes: 79 additions & 0 deletions packages/cli-plugin-dependencies/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import path from "path";
import { CliCommandPlugin, CliCommandPluginArgs } from "@webiny/cli/types";
import { createDependencyTree } from "./references";
import { createReferenceFile } from "./references/createReferenceFile";
import { verifyDependencies } from "./references/verifyDependencies";

const getDirname = (): string => {
let name = __dirname;
if (name.endsWith("/dist")) {
name = name.replace("/dist", "");
}
return path.join(name, "../cli/files/");
};

const createReferenceFileCommandExecutor = ({ context }: Pick<CliCommandPluginArgs, "context">) => {
return async () => {
const dirname = getDirname();
const tree = createDependencyTree({
context,
dirname
});

createReferenceFile({
tree,
dirname,
context
});
};
};

const verifyDependenciesFileCommandExecutor = ({
context
}: Pick<CliCommandPluginArgs, "context">) => {
return async () => {
const dirname = getDirname();
const tree = createDependencyTree({
context,
dirname
});

verifyDependencies({
tree,
dirname
});
};
};

export default (): CliCommandPlugin[] => {
return [
{
type: "cli-command",
name: "cli-command-sync-dependencies",
create({ yargs, context }) {
yargs.command(
"sync-dependencies",
"Sync dependencies for all packages.",
(yargs: Record<string, any>) => {
yargs.example("$0 sync-dependencies");
},
createReferenceFileCommandExecutor({ context })
);
}
},
{
type: "cli-command",
name: "cli-command-verify-dependencies",
create({ yargs, context }) {
yargs.command(
"verify-dependencies",
"Verify dependencies for all packages.",
(yargs: Record<string, any>) => {
yargs.example("$0 verify-dependencies");
},
verifyDependenciesFileCommandExecutor({ context })
);
}
}
];
};
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ import { DependencyTree } from "./DependencyTree";
import { PackageJson } from "type-fest";
import loadJsonFile from "load-json-file";

export interface IListAllDependenciesListParams {
export interface IBuildDependencyTreeParams {
basePath: string;
files: string[];
ignore?: RegExp;
}
export class ListAllDependencies {
public async list(params: IListAllDependenciesListParams): Promise<IDependencyTree> {
export class BuildDependencyTree {
public build(params: IBuildDependencyTreeParams): IDependencyTree {
const { basePath, files, ignore } = params;
const tree = new DependencyTree();
for (const file of files) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export interface IListAllPackageJsonFilesListParams {
}

export class ListAllPackageJsonFiles {
public async list(params: IListAllPackageJsonFilesListParams): Promise<string[]> {
public list(params: IListAllPackageJsonFilesListParams): string[] {
const targets = params.targets;
const ignore = defaultIgnore.concat(params.ignore || []);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import getPackages from "get-yarn-workspaces";

export class ListAllPackages {
public async list(path: string): Promise<string[]> {
return await getPackages(path);
public list(path: string): string[] {
return getPackages(path);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export interface ICreateReferenceFileParams {
target?: string;
}

export const createReferenceFile = async (params: ICreateReferenceFileParams): Promise<void> => {
export const createReferenceFile = (params: ICreateReferenceFileParams): void => {
const { context, dirname, tree, target } = params;

const referencesPath = path.join(dirname, target || "", "references.json");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,31 @@ import { CliContext } from "@webiny/cli/types";
import { IDependencyTree } from "~/types";
import { ListAllPackages } from "~/references/ListAllPackages";
import { ListAllPackageJsonFiles } from "./ListAllPackageJsonFiles";
import { ListAllDependencies } from "./ListAllDependencies";
import { BuildDependencyTree } from "./BuildDependencyTree";

export interface IListAllReferencesParams {
context: CliContext;
dirname: string;
}

export const listAllReferences = async (
params: IListAllReferencesParams
): Promise<IDependencyTree> => {
export const createDependencyTree = (params: IListAllReferencesParams): IDependencyTree => {
const { context } = params;
const basePath = context.project.root;
const target = path.join(basePath, "packages");

const listAllPackages = new ListAllPackages();
const listAllPackageJsonFiles = new ListAllPackageJsonFiles();
const listAllDependencies = new ListAllDependencies();
const buildDependencyTree = new BuildDependencyTree();

const allPackages = await listAllPackages.list(target);
const allPackages = listAllPackages.list(target);

const allPackageJsonFiles = await listAllPackageJsonFiles.list({
const allPackageJsonFiles = listAllPackageJsonFiles.list({
targets: allPackages
});

const files = [path.join(basePath, "package.json"), ...allPackageJsonFiles];

return await listAllDependencies.list({
return buildDependencyTree.build({
basePath,
files,
ignore: /^@webiny\//
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export interface IVerifyDependenciesParams {
dirname: string;
}

export const verifyDependencies = async (params: IVerifyDependenciesParams): Promise<void> => {
export const verifyDependencies = (params: IVerifyDependenciesParams): void => {
const { tree, dirname } = params;

const referencesFile = getReferencesFilePath({
Expand All @@ -27,22 +27,30 @@ export const verifyDependencies = async (params: IVerifyDependenciesParams): Pro
};

if (fs.existsSync(referencesFile)) {
const json = await loadJsonFile(referencesFile);
const json = loadJsonFile.sync(referencesFile);
if (JSON.stringify(references) !== JSON.stringify(json)) {
throw new Error("References are not in sync.");
throw new Error(
"References are not in sync. Please run `yarn webiny sync-dependencies` command."
);
}
} else {
throw new Error("References file does not exist.");
throw new Error(
"References file does not exist. Please run `yarn webiny sync-dependencies` command."
);
}

if (fs.existsSync(duplicatesFile)) {
const json = await loadJsonFile(duplicatesFile);
const json = loadJsonFile.sync(duplicatesFile);
if (JSON.stringify(tree.duplicates) !== JSON.stringify(json)) {
throw new Error("Duplicates are not in sync.");
throw new Error(
"Duplicates are not in sync. Please run `yarn webiny sync-dependencies` command."
);
}
} else {
throw new Error("Duplicates file does not exist.");
throw new Error(
"Duplicates file does not exist. Please run `yarn webiny sync-dependencies` command."
);
}

console.log("All package reference files are in sync in sync.");
console.log("All package reference files are in sync.");
};
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"extends": "../../tsconfig.build.json",
"include": ["src"],
"references": [{ "path": "../cli-plugin-scaffold/tsconfig.build.json" }],
"references": [],
"compilerOptions": {
"rootDir": "./src",
"outDir": "./dist",
Expand Down
Loading

0 comments on commit 76ed2a8

Please sign in to comment.