Skip to content

Commit

Permalink
fix: improve code readability (#4479)
Browse files Browse the repository at this point in the history
  • Loading branch information
adrians5j authored Jan 9, 2025
1 parent 2eb1183 commit c82b918
Show file tree
Hide file tree
Showing 30 changed files with 140 additions and 212 deletions.
1 change: 1 addition & 0 deletions apps/admin/src/Extensions.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// This file is automatically updated via scaffolding utilities.
// Learn more about extensions: https://webiny.link/extensions
import React from "react";

export const Extensions = () => {
Expand Down
3 changes: 1 addition & 2 deletions apps/api/graphql/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,19 @@
"@webiny/api-headless-cms-ddb": "0.0.0",
"@webiny/api-headless-cms-tasks": "0.0.0",
"@webiny/api-i18n": "0.0.0",
"@webiny/api-i18n-content": "0.0.0",
"@webiny/api-i18n-ddb": "0.0.0",
"@webiny/api-log": "0.0.0",
"@webiny/api-page-builder": "0.0.0",
"@webiny/api-page-builder-aco": "0.0.0",
"@webiny/api-page-builder-import-export": "0.0.0",
"@webiny/api-page-builder-import-export-so-ddb": "0.0.0",
"@webiny/api-page-builder-so-ddb": "0.0.0",
"@webiny/api-prerendering-service": "0.0.0",
"@webiny/api-prerendering-service-aws": "0.0.0",
"@webiny/api-record-locking": "0.0.0",
"@webiny/api-security": "0.0.0",
"@webiny/api-security-cognito": "0.0.0",
"@webiny/api-security-so-ddb": "0.0.0",
"@webiny/api-serverless-cms": "0.0.0",
"@webiny/api-tenancy": "0.0.0",
"@webiny/api-tenancy-so-ddb": "0.0.0",
"@webiny/api-tenant-manager": "0.0.0",
Expand Down
1 change: 1 addition & 0 deletions apps/api/graphql/src/extensions.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// This file is automatically updated via scaffolding utilities.
// Learn more about extensions: https://webiny.link/extensions
export const extensions = () => {
return [];
};
2 changes: 1 addition & 1 deletion apps/api/graphql/src/plugins/continuingTask.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { createTaskDefinition } from "@webiny/tasks";
import { Context } from "../types";
import { Context } from "@webiny/api-serverless-cms";
import { ITaskResponseContinueOptions } from "@webiny/tasks/response/abstractions";

const MAX_RUNS = 5;
Expand Down
2 changes: 1 addition & 1 deletion apps/api/graphql/src/security.okta.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import anonymousAuthorization from "@webiny/api-security/plugins/anonymousAuthor
import tenantLinkAuthorization from "@webiny/api-security/plugins/tenantLinkAuthorization";
import createAdminUsersApp from "@webiny/api-admin-users";
import { createStorageOperations as createAdminUsersStorageOperations } from "@webiny/api-admin-users-so-ddb";
import { Context } from "./types";
import { Context } from "@webiny/api-serverless-cms";

export default ({ documentClient }: { documentClient: DynamoDBDocument }) => [
/**
Expand Down
32 changes: 0 additions & 32 deletions apps/api/graphql/src/types.ts

This file was deleted.

19 changes: 11 additions & 8 deletions apps/api/graphql/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,6 @@
{
"path": "../../../packages/api-i18n/tsconfig.build.json"
},
{
"path": "../../../packages/api-i18n-content/tsconfig.build.json"
},
{
"path": "../../../packages/api-i18n-ddb/tsconfig.build.json"
},
Expand All @@ -75,7 +72,7 @@
"path": "../../../packages/api-page-builder-so-ddb/tsconfig.build.json"
},
{
"path": "../../../packages/api-prerendering-service/tsconfig.build.json"
"path": "../../../packages/api-prerendering-service-aws/tsconfig.build.json"
},
{
"path": "../../../packages/api-security/tsconfig.build.json"
Expand All @@ -86,6 +83,9 @@
{
"path": "../../../packages/api-security-so-ddb/tsconfig.build.json"
},
{
"path": "../../../packages/api-serverless-cms/tsconfig.build.json"
},
{
"path": "../../../packages/api-tenancy/tsconfig.build.json"
},
Expand Down Expand Up @@ -165,8 +165,6 @@
"@webiny/api-record-locking": ["../../../packages/api-record-locking/src"],
"@webiny/api-i18n/*": ["../../../packages/api-i18n/src/*"],
"@webiny/api-i18n": ["../../../packages/api-i18n/src"],
"@webiny/api-i18n-content/*": ["../../../packages/api-i18n-content/src/*"],
"@webiny/api-i18n-content": ["../../../packages/api-i18n-content/src"],
"@webiny/api-i18n-ddb/*": ["../../../packages/api-i18n-ddb/src/*"],
"@webiny/api-i18n-ddb": ["../../../packages/api-i18n-ddb/src"],
"@webiny/api-page-builder/*": ["../../../packages/api-page-builder/src/*"],
Expand All @@ -187,14 +185,19 @@
],
"@webiny/api-page-builder-so-ddb/*": ["../../../packages/api-page-builder-so-ddb/src/*"],
"@webiny/api-page-builder-so-ddb": ["../../../packages/api-page-builder-so-ddb/src"],
"@webiny/api-prerendering-service/*": ["../../../packages/api-prerendering-service/src/*"],
"@webiny/api-prerendering-service": ["../../../packages/api-prerendering-service/src"],
"@webiny/api-prerendering-service-aws/*": [
"../../../packages/api-prerendering-service-aws/src/*"
],
"@webiny/api-prerendering-service-aws": [
"../../../packages/api-prerendering-service-aws/src"
],
"@webiny/api-security/*": ["../../../packages/api-security/src/*"],
"@webiny/api-security": ["../../../packages/api-security/src"],
"@webiny/api-security-cognito/*": ["../../../packages/api-security-cognito/src/*"],
"@webiny/api-security-cognito": ["../../../packages/api-security-cognito/src"],
"@webiny/api-security-so-ddb/*": ["../../../packages/api-security-so-ddb/src/*"],
"@webiny/api-security-so-ddb": ["../../../packages/api-security-so-ddb/src"],
"@webiny/api-serverless-cms": ["../../../packages/api-serverless-cms/src"],
"@webiny/api-tenancy/*": ["../../../packages/api-tenancy/src/*"],
"@webiny/api-tenancy": ["../../../packages/api-tenancy/src"],
"@webiny/api-tenancy-so-ddb/*": ["../../../packages/api-tenancy-so-ddb/src/*"],
Expand Down
1 change: 1 addition & 0 deletions apps/website/src/Extensions.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// This file is automatically updated via scaffolding utilities.
// Learn more about extensions: https://webiny.link/extensions
import React from "react";

export const Extensions = () => {
Expand Down
65 changes: 53 additions & 12 deletions packages/cli-plugin-extensions/src/downloadAndLinkExtension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { Extension } from "./extensions/Extension";
import glob from "fast-glob";
import { CliContext } from "@webiny/cli/types";
import { Ora } from "ora";
import { ExtensionJson, ExtensionMessage } from "~/types";

const EXTENSIONS_ROOT_FOLDER = "extensions";
const S3_BUCKET_NAME = "webiny-examples";
Expand Down Expand Up @@ -87,6 +88,7 @@ export const downloadAndLinkExtension = async ({
await setTimeout(1000);

let extensionsFolderToCopyPath = path.join(downloadFolderPath, "extensions");
let extensionJsonPath = path.join(downloadFolderPath, "extension.json");

// If we have `extensions` folder in the root of the downloaded extension.
// it means the example extension is not versioned, and we can just copy it.
Expand All @@ -106,8 +108,14 @@ export const downloadAndLinkExtension = async ({
);

extensionsFolderToCopyPath = path.join(downloadFolderPath, versionToUse, "extensions");
extensionJsonPath = path.join(downloadFolderPath, versionToUse, "extension.json");
}

const extensionJsonExists = fs.existsSync(extensionJsonPath);
const extensionJson: ExtensionJson = extensionJsonExists
? JSON.parse(fs.readFileSync(extensionJsonPath, "utf-8"))
: {};

await fsAsync.cp(extensionsFolderToCopyPath, EXTENSIONS_ROOT_FOLDER, {
recursive: true
});
Expand Down Expand Up @@ -137,6 +145,8 @@ export const downloadAndLinkExtension = async ({
await linkAllExtensions();
await runYarnInstall();

const nextStepsToDisplay: ExtensionMessage[] = [];

if (downloadedExtensions.length === 1) {
const [downloadedExtension] = downloadedExtensions;
ora.succeed(
Expand All @@ -145,13 +155,7 @@ export const downloadAndLinkExtension = async ({
)}.`
);

const nextSteps = downloadedExtension.getNextSteps();

console.log();
console.log(chalk.bold("Next Steps"));
nextSteps.forEach(message => {
console.log(`‣ ${message}`);
});
nextStepsToDisplay.push(...downloadedExtension.getNextSteps());
} else {
const paths = downloadedExtensions.map(ext => ext.getLocation());
ora.succeed("Multiple extensions downloaded successfully in:");
Expand All @@ -160,13 +164,50 @@ export const downloadAndLinkExtension = async ({
});
}

// Next Steps section.
const nextStepsFromExtensionJson = extensionJson.nextSteps;
if (nextStepsFromExtensionJson) {
const { clearExisting, messages } = nextStepsFromExtensionJson;
if (clearExisting) {
nextStepsToDisplay.length = 0;
}

if (Array.isArray(messages)) {
nextStepsToDisplay.push(...messages);
}
}

console.log();
console.log(chalk.bold("Next Steps"));
nextStepsToDisplay.forEach(({ text, variables = [] }) => {
console.log(`‣ ${text}`, ...variables.map(v => context.success.hl(v)));
});

// Additional Notes section.
const additionalNotesToDisplay: ExtensionMessage[] = [
{
text: `if you already have the %s command running, you'll need to restart it`,
variables: ["webiny watch"]
}
];

const additionalNotesFromExtensionJson = extensionJson.additionalNotes;
if (additionalNotesFromExtensionJson) {
const { clearExisting, messages } = additionalNotesFromExtensionJson;
if (clearExisting) {
additionalNotesToDisplay.length = 0;
}

if (Array.isArray(messages)) {
additionalNotesToDisplay.push(...messages);
}
}

console.log();
console.log(chalk.bold("Additional Notes"));
console.log(
`‣ note that if you already have the ${context.success.hl(
"webiny watch"
)} command running, you'll need to restart it`
);
additionalNotesToDisplay.forEach(({ text, variables = [] }) => {
console.log(`‣ ${text}`, ...variables.map(v => context.success.hl(v)));
});
} catch (e) {
switch (e.code) {
case "NO_OBJECTS_FOUND":
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { ExtensionMessage } from "~/types";

export interface ExtensionTypeConstructorParams {
name: string;
type: string;
Expand All @@ -14,7 +16,7 @@ export abstract class AbstractExtension {

abstract link(): Promise<void>;

abstract getNextSteps(): string[];
abstract getNextSteps(): ExtensionMessage[];

getPackageJsonPath(): string {
return `${this.params.location}/package.json`;
Expand Down
15 changes: 8 additions & 7 deletions packages/cli-plugin-extensions/src/extensions/AdminExtension.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { AbstractExtension } from "./AbstractExtension";
import path from "path";
import { EXTENSIONS_ROOT_FOLDER } from "~/utils/constants";
import chalk from "chalk";
import { JsxFragment, Node, Project } from "ts-morph";
import { formatCode } from "@webiny/cli-plugin-scaffold/utils";
import { updateDependencies, updateWorkspaces } from "~/utils";
import Case from "case";
import { ExtensionMessage } from "~/types";

export class AdminExtension extends AbstractExtension {
async link() {
Expand All @@ -20,7 +20,7 @@ export class AdminExtension extends AbstractExtension {
await updateWorkspaces(this.params.location);
}

getNextSteps(): string[] {
getNextSteps(): ExtensionMessage[] {
let { location: extensionsFolderPath } = this.params;
if (!extensionsFolderPath) {
extensionsFolderPath = `${EXTENSIONS_ROOT_FOLDER}/${this.params.name}`;
Expand All @@ -30,11 +30,12 @@ export class AdminExtension extends AbstractExtension {
const indexTsxFilePath = `${extensionsFolderPath}/src/index.tsx`;

return [
`run ${chalk.green(watchCommand)} to start local development`,
`open ${chalk.green(indexTsxFilePath)} and start coding`,
`to install additional dependencies, run ${chalk.green(
`yarn workspace ${this.params.packageName} add <package-name>`
)}`
{ text: `run %s to start local development`, variables: [watchCommand] },
{ text: `open %s and start coding`, variables: [indexTsxFilePath] },
{
text: `to install additional dependencies, run %s`,
variables: [`yarn workspace ${this.params.packageName} add <package-name>`]
}
];
}

Expand Down
17 changes: 9 additions & 8 deletions packages/cli-plugin-extensions/src/extensions/ApiExtension.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { AbstractExtension } from "./AbstractExtension";
import path from "path";
import { EXTENSIONS_ROOT_FOLDER } from "~/utils/constants";
import chalk from "chalk";
import { ArrayLiteralExpression, Node, Project } from "ts-morph";
import { formatCode } from "@webiny/cli-plugin-scaffold/utils";
import { updateDependencies, updateWorkspaces } from "~/utils";
import Case from "case";
import { ExtensionMessage } from "~/types";

export class ApiExtension extends AbstractExtension {
async link() {
Expand All @@ -20,21 +20,22 @@ export class ApiExtension extends AbstractExtension {
await updateWorkspaces(this.params.location);
}

getNextSteps(): string[] {
getNextSteps(): ExtensionMessage[] {
let { location: extensionsFolderPath } = this.params;
if (!extensionsFolderPath) {
extensionsFolderPath = `${EXTENSIONS_ROOT_FOLDER}/${this.params.name}`;
}

const watchCommand = `yarn webiny watch api --env dev`;
const indexTsxFilePath = `${extensionsFolderPath}/src/index.ts`;
const indexTsFilePath = `${extensionsFolderPath}/src/index.ts`;

return [
`run ${chalk.green(watchCommand)} to start local development`,
`open ${chalk.green(indexTsxFilePath)} and start coding`,
`to install additional dependencies, run ${chalk.green(
`yarn workspace ${this.params.packageName} add <package-name>`
)}`
{ text: `run %s to start local development`, variables: [watchCommand] },
{ text: `open %s and start coding`, variables: [indexTsFilePath] },
{
text: `to install additional dependencies, run %s`,
variables: [`yarn workspace ${this.params.packageName} add <package-name>`]
}
];
}

Expand Down
3 changes: 2 additions & 1 deletion packages/cli-plugin-extensions/src/extensions/Extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import loadJson from "load-json-file";
import { PackageJson } from "@webiny/cli-plugin-scaffold/types";
import path from "path";
import { WebsiteExtension } from "~/extensions/WebsiteExtension";
import { ExtensionMessage } from "~/types";

type PackageJsonPath = string;

Expand Down Expand Up @@ -47,7 +48,7 @@ export class Extension extends AbstractExtension {
await this.extension.link();
}

getNextSteps(): string[] {
getNextSteps(): ExtensionMessage[] {
return this.extension.getNextSteps();
}

Expand Down
Loading

0 comments on commit c82b918

Please sign in to comment.