Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: improve CLI bootstrap #4528

Draft
wants to merge 6 commits into
base: next
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 5 additions & 19 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,19 +52,6 @@
"@commitlint/cli": "^11.0.0",
"@commitlint/config-conventional": "^11.0.0",
"@grpc/grpc-js": "^1.11.1",
"@lexical/code": "0.23.1",
"@lexical/hashtag": "0.23.1",
"@lexical/headless": "0.23.1",
"@lexical/history": "0.23.1",
"@lexical/html": "0.23.1",
"@lexical/list": "0.23.1",
"@lexical/mark": "0.23.1",
"@lexical/overflow": "0.23.1",
"@lexical/react": "0.23.1",
"@lexical/rich-text": "0.23.1",
"@lexical/selection": "0.23.1",
"@lexical/text": "0.23.1",
"@lexical/utils": "0.23.1",
"@octokit/rest": "^20.0.2",
"@types/fs-extra": "^11.0.4",
"@types/jest": "^29.5.14",
Expand Down Expand Up @@ -115,7 +102,6 @@
"jest-environment-jsdom": "^29.7.0",
"jest-extended": "4.0.2",
"lerna": "8.1.2",
"lexical": "0.23.1",
"lint-staged": "^15.3.0",
"listr": "^0.14.3",
"load-json-file": "^6.2.0",
Expand All @@ -130,10 +116,10 @@
"semver": "^7.6.3",
"ts-expect": "^1.3.0",
"ts-jest": "29.1.5",
"ts-node": "^10.9.2",
"tsx": "^4.16.2",
"type-fest": "4.14.0",
"typescript": "5.3.3",
"typescript-transform-paths": "^3.5.3",
"typescript-transform-paths": "^3.4.6",
"verdaccio": "^6.0.5",
"write-json-file": "^4.3.0",
"yargs": "^17.7.2"
Expand All @@ -152,8 +138,8 @@
"open": "yarn webiny open",
"eslint": "eslint \"**/*.{js,jsx,ts,tsx}\" --max-warnings=0",
"eslint:fix": "yarn eslint --fix",
"build": "node scripts/buildPackages",
"build:quick": "node scripts/buildPackages --build-overrides='{\"tsConfig\":{\"compilerOptions\":{\"skipLibCheck\":true}}}'",
"build": "tsx scripts/buildPackages",
"build:quick": "yarn build --build-overrides='{\"tsConfig\":{\"compilerOptions\":{\"skipLibCheck\":true}}}'",
"build:apps": "yarn webiny ws run build --scope='@webiny/app*'",
"build:api": "yarn webiny ws run build --scope='@webiny/api*' --scope='@webiny/handler*'",
"watch": "yarn webiny watch",
Expand All @@ -173,7 +159,7 @@
"dispatch-github-event": "node ./scripts/dispatchGitHubEvent.js",
"lint-staged": "lint-staged",
"postinstall": "yarn node ./scripts/linkWorkspaces.js",
"prepublishOnly": "node scripts/prepublishOnly",
"prepublishOnly": "tsx scripts/prepublishOnly",
"prettier": "prettier \"**/**/*.{js,jsx,ts,tsx,json,scss,yml}\" --config .prettierrc.js",
"prettier:check": "yarn prettier --check",
"prettier:fix": "yarn prettier --write",
Expand Down
2 changes: 1 addition & 1 deletion packages/cli-plugin-deploy-pulumi/src/commands/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export const buildCommand = (params: IBuildParams, context: Context) => {
console.log();

const builder = new PackagesBuilder({
packages: projectApplication.packages,
packages: await projectApplication.getPackages(),
inputs,
context
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { parentPort, workerData } from "worker_threads";
import { cli } from "@webiny/cli";
import { getCli, initializeProject } from "@webiny/cli";
import { requireConfigWithExecute } from "~/utils";

let processStdout = "";
Expand All @@ -21,11 +21,7 @@ process.stdout.write = (chunk, encoding, callback) => {
};

const originalStderrWrite = process.stderr.write.bind(process.stderr);
/**
* TODO @adrian
*
* Check this out.
*/

// @ts-expect-error
process.stderr.write = (chunk, encoding, callback) => {
if (typeof chunk === "string") {
Expand All @@ -37,36 +33,40 @@ process.stderr.write = (chunk, encoding, callback) => {

const { options, package: pckg } = workerData;

const config = requireConfigWithExecute(pckg.config, {
options: {
...options,
cwd: pckg.root
},
context: cli
});

const hasBuildCommand = config.commands && typeof config.commands.build === "function";
if (!hasBuildCommand) {
throw new Error("Build command not found.");
}
(async () => {
await initializeProject();

config.commands
.build(options)
.then(() => {
parentPort!.postMessage(
JSON.stringify({ type: "success", stdout: processStdout, stderr: processStderr })
);
})
.catch((e: Error) => {
parentPort!.postMessage(
JSON.stringify({
type: "error",
stdout: processStdout,
stderr: processStderr,
error: {
message: e.message,
stack: e.stack
}
})
);
const config = requireConfigWithExecute(pckg.config, {
options: {
...options,
cwd: pckg.root
},
context: getCli()
});

const hasBuildCommand = config.commands && typeof config.commands.build === "function";
if (!hasBuildCommand) {
throw new Error("Build command not found.");
}

config.commands
.build(options)
.then(() => {
parentPort!.postMessage(
JSON.stringify({ type: "success", stdout: processStdout, stderr: processStderr })
);
})
.catch((e: Error) => {
parentPort!.postMessage(
JSON.stringify({
type: "error",
stdout: processStdout,
stderr: processStderr,
error: {
message: e.message,
stack: e.stack
}
})
);
});
})();
4 changes: 2 additions & 2 deletions packages/cli-plugin-deploy-pulumi/src/commands/deploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export const deployCommand = (params: IDeployParams, context: CliContext) => {

const hookArgs = { context, env, variant, inputs, projectApplication };

context.info("Webiny version: %s", context.version);
context.info("Webiny version: %s", context.project.version);
console.log();

// Just so the version stays on the screen for a second, before the process continues.
Expand All @@ -47,7 +47,7 @@ export const deployCommand = (params: IDeployParams, context: CliContext) => {
console.log();

const builder = new PackagesBuilder({
packages: projectApplication.packages,
packages: await projectApplication.getPackages(),
inputs,
context
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export const executeMigrationsCommand = async (
});

const result = await runner.runMigration({
version: process.env.WEBINY_VERSION || context.version,
version: process.env.WEBINY_VERSION || context.project.version,
pattern: params.pattern,
force: params.force
});
Expand Down
2 changes: 1 addition & 1 deletion packages/cli-plugin-deploy-pulumi/src/commands/newWatch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export const newWatch = async (inputs: IUserCommandInput, context: Context) => {
}

// Get project application metadata. Will throw an error if invalid folder specified.
projectApplication = getProjectApplication({
projectApplication = await getProjectApplication({
cwd: path.join(process.cwd(), inputs.folder)
});

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { parentPort, workerData } from "worker_threads";
import { cli } from "@webiny/cli";
import { getCli, initializeProject } from "@webiny/cli";
import { requireConfigWithExecute } from "~/utils";

// We need this because tools have internal console.log calls. So,
Expand Down Expand Up @@ -37,6 +37,10 @@ for (let i = 0; i < types.length; i++) {
}

(async () => {
await initializeProject();

const cli = getCli();

try {
const { options, package: pckg } = workerData;

Expand Down
2 changes: 1 addition & 1 deletion packages/cli-plugin-deploy-pulumi/src/commands/watch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export const watchCommand = async (inputs: IUserCommandInput, context: Context)
}

// Get project application metadata. Will throw an error if invalid folder specified.
projectApplication = getProjectApplication({
projectApplication = await getProjectApplication({
cwd: path.join(process.cwd(), inputs.folder)
});

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { parentPort, workerData } from "worker_threads";
import { cli } from "@webiny/cli";
import { getCli, initializeProject } from "@webiny/cli";
import { requireConfigWithExecute } from "~/utils";

// We need this because tools have internal console.log calls. So,
Expand All @@ -24,6 +24,10 @@ for (let i = 0; i < types.length; i++) {
}

(async () => {
await initializeProject();

const cli = getCli();

try {
const { options, package: pckg } = workerData;
const config = requireConfigWithExecute(pckg.config, {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { parentPort, workerData } from "worker_threads";
import { cli } from "@webiny/cli";
import { getCli, initializeProject } from "@webiny/cli";
import { requireConfigWithExecute } from "~/utils";

// We need this because tools have internal console.log calls. So,
Expand All @@ -24,6 +24,10 @@ for (let i = 0; i < types.length; i++) {
}

(async () => {
await initializeProject();

const cli = getCli();

try {
const { options, package: pckg } = workerData;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ export const createPulumiCommand = ({
const cwd = path.join(process.cwd(), params.folder);

// Get project application metadata.
projectApplication = getProjectApplication({ cwd });
projectApplication = await getProjectApplication({ cwd });

if (createProjectApplicationWorkspaceParam !== false) {
await createProjectApplicationWorkspace({
Expand All @@ -113,8 +113,11 @@ export const createPulumiCommand = ({

// Check if there are any plugins that need to be registered. This needs to happen
// always, no matter the value of `createProjectApplicationWorkspaceParam` parameter.
if (projectApplication.config.plugins) {
context.plugins.register(projectApplication.config.plugins);

// @ts-expect-error Update types of pulumi app `config`.
const projectApplicationPlugins = await projectApplication.config.getPlugins();
if (projectApplicationPlugins) {
context.plugins.register(projectApplicationPlugins);
}

// Load env vars specified via .env files located in project application folder.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import { initializeProject } from "@webiny/cli";
import projectApplication from "../webiny.application";

export = async () => {
return projectApplication.pulumi.run({
await initializeProject();
const pulumi = await projectApplication.getPulumi();

return pulumi.run({
env: "{DEPLOY_ENV}",
variant: "{DEPLOY_VARIANT}"
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ export const downloadAndLinkExtension = async ({
context,
ora
}: DownloadAndLinkExtensionParams) => {
const currentWebinyVersion = context.version;
const currentWebinyVersion = context.project.version;

try {
ora.start(`Downloading extension...`);
Expand Down
4 changes: 2 additions & 2 deletions packages/cli-plugin-extensions/src/generateExtension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ export const generateExtension = async ({ input, ora, context }: GenerateExtensi
for (const packageName of packages) {
const isWebinyPackage = packageName.startsWith("@webiny/");
if (isWebinyPackage) {
packageJsonUpdates[packageName] = context.version;
packageJsonUpdates[packageName] = context.project.version;
continue;
}

Expand Down Expand Up @@ -138,7 +138,7 @@ export const generateExtension = async ({ input, ora, context }: GenerateExtensi
// this because the `package.json` file that the selected template creates might also have
// Webiny packages that need to be updated. For example, this is the case with the `pbElement`
// extension (see: `packages/cli-plugin-extensions/templates/pbElement/package.json`).
await setWebinyPackageVersions(extension, context.version);
await setWebinyPackageVersions(extension, context.project.version);

await extension.link();

Expand Down
2 changes: 1 addition & 1 deletion packages/cli-plugin-scaffold-workspaces/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ export default (): CliCommandScaffoldTemplate<Input> => ({
for (const packageName of packages) {
const isWebinyPackage = packageName.startsWith("@webiny/");
if (isWebinyPackage) {
packageJson.dependencies[packageName] = context.version;
packageJson.dependencies[packageName] = context.project.version;
continue;
}

Expand Down
Loading
Loading