Skip to content

Open source website builder and Webflow alternative. Webstudio is an advanced visual builder that connects to any headless CMS, supports all CSS properties, and can be hosted anywhere, including with us.

Setup for `cli` to interact with the deployed projects (#2018)

Jaya Krishna Namburu6b9e6d3708d6
diff --git a/apps/cli/.gitignore b/apps/cli/.gitignore
new file mode 100644
index 0000000000..c3af857904
--- /dev/null
+++ b/apps/cli/.gitignore
@@ -0,0 +1 @@
+lib/
diff --git a/apps/cli/bin.js b/apps/cli/bin.js
new file mode 100755
index 0000000000..08e5120bd1
--- /dev/null
+++ b/apps/cli/bin.js
@@ -0,0 +1,11 @@
+#!/usr/bin/env node --experimental-specifier-resolution=node
+/* eslint-disable-next-line */
+import { main } from "./lib/cli";
+
+try {
+ await main();
+ process.exit(0);
+} catch (error) {
+ console.error(error);
+ process.exit(1);
+}
diff --git a/apps/cli/package.json b/apps/cli/package.json
new file mode 100644
index 0000000000..54b95f18fb
--- /dev/null
+++ b/apps/cli/package.json
@@ -0,0 +1,24 @@
+{
+ "name": "@webstudio-is/cli",
+ "version": "0.83.0",
+ "description": "Webstudio CLI",
+ "author": "Webstudio <[email protected]>",
+ "homepage": "https://webstudio.is",
+ "type": "module",
+ "bin": {
+ "webstudio": "./bin.js"
+ },
+ "scripts": {
+ "dev": "build-package --watch",
+ "build": "build-package"
+ },
+ "license": "MIT",
+ "dependencies": {
+ "env-paths": "^3.0.0"
+ },
+ "devDependencies": {
+ "@types/node": "^18.13.0",
+ "@webstudio-is/scripts": "workspace:^",
+ "@webstudio-is/tsconfig": "workspace:^"
+ }
+}
diff --git a/apps/cli/src/args.ts b/apps/cli/src/args.ts
new file mode 100644
index 0000000000..a357ca3a51
--- /dev/null
+++ b/apps/cli/src/args.ts
@@ -0,0 +1,42 @@
+import { parseArgs } from "node:util";
+
+export const showHelp = () => console.info(HELP);
+
+export enum Commands {
+ "link" = "link",
+}
+
+type DefaultArgs = Pick<
+ ReturnType<
+ typeof parseArgs<{
+ options: typeof CLI_ARGS_OPTIONS;
+ }>
+ >,
+ "values"
+>;
+
+export type Command = (
+ args: DefaultArgs & { positionals: string[] }
+) => Promise<void>;
+export type SupportedCommands = Record<Commands, Command>;
+
+export const CLI_ARGS_OPTIONS = {
+ version: {
+ type: "boolean" as const,
+ short: "v",
+ },
+ help: {
+ type: "boolean" as const,
+ short: "h",
+ },
+};
+
+export const HELP = `Usage:
+ $ webstudio commands [flags...]
+ Commands:
+ link <shared link> Login to Webstudio with shared link
+
+ Flags:
+ --help, -h Show this help message
+ --version, -v Show the version of this script
+`;
diff --git a/apps/cli/src/cli.ts b/apps/cli/src/cli.ts
new file mode 100644
index 0000000000..31c6d946d3
--- /dev/null
+++ b/apps/cli/src/cli.ts
@@ -0,0 +1,42 @@
+import { parseArgs } from "node:util";
+import { GLOBAL_CONFIG_FILE } from "./constants";
+import { ensureFileInPath } from "./utils";
+import { link } from "./link";
+import { showHelp, CLI_ARGS_OPTIONS, Commands } from "./args";
+import type { SupportedCommands } from "./args";
+import packageJSON from "../package.json" assert { type: "json" };
+
+const commands: SupportedCommands = {
+ link,
+};
+
+export const main = async () => {
+ try {
+ await ensureFileInPath(GLOBAL_CONFIG_FILE, "{}");
+ const args = parseArgs({
+ args: process.argv.slice(2),
+ options: CLI_ARGS_OPTIONS,
+ allowPositionals: true,
+ });
+
+ if (args.values?.version) {
+ console.info(packageJSON.version);
+ return;
+ }
+
+ if (args.values?.help) {
+ showHelp();
+ return;
+ }
+
+ const command = commands[args.positionals[0] as Commands];
+ if (command === undefined) {
+ throw new Error(`No command provided`);
+ }
+
+ await command(args);
+ } catch (error) {
+ console.error(error);
+ showHelp();
+ }
+};
diff --git a/apps/cli/src/constants.ts b/apps/cli/src/constants.ts
new file mode 100644
index 0000000000..af87fa0a77
--- /dev/null
+++ b/apps/cli/src/constants.ts
@@ -0,0 +1,6 @@
+import { join } from "node:path";
+import envPaths from "env-paths";
+
+const APP_CONFIG = envPaths("webstudio").config;
+const GLOBAL_CONFIG_FILE_NAME = "webstudio-config.json";
+export const GLOBAL_CONFIG_FILE = join(APP_CONFIG, GLOBAL_CONFIG_FILE_NAME);
diff --git a/apps/cli/src/link.ts b/apps/cli/src/link.ts
new file mode 100644
index 0000000000..ebe036fed1
--- /dev/null
+++ b/apps/cli/src/link.ts
@@ -0,0 +1,47 @@
+import { stdin as input, stdout as output, exit } from "node:process";
+import * as readline from "node:readline/promises";
+import { readFile, writeFile } from "node:fs/promises";
+import { GLOBAL_CONFIG_FILE } from "./constants";
+import type { Command } from "./args";
+
+export const link: Command = async () => {
+ const rl = readline.createInterface({ input, output });
+ const shareLink = await rl.question(`Paste share link (with build access): `);
+
+ try {
+ const shareLinkUrl = new URL(shareLink);
+ const host = shareLinkUrl.origin;
+ const token = shareLinkUrl.searchParams.get("authToken");
+ const paths = shareLinkUrl.pathname.split("/").filter(Boolean);
+ if (paths[0] !== "builder" || paths.length !== 2) {
+ throw new Error("Invalid share link.");
+ }
+ const projectId = paths[1];
+ if (token === undefined || projectId === undefined || host === undefined) {
+ throw new Error("Invalid share link.");
+ }
+
+ const currentConfig = await readFile(GLOBAL_CONFIG_FILE, "utf-8");
+ const currentConfigJson = JSON.parse(currentConfig);
+
+ const newConfig = {
+ ...currentConfigJson,
+ [projectId]: {
+ host,
+ token,
+ },
+ };
+
+ await writeFile(GLOBAL_CONFIG_FILE, JSON.stringify(newConfig, null, 2));
+ rl.close();
+ console.info(`Saved credentials for project ${projectId}.
+You can find your config at ${GLOBAL_CONFIG_FILE}
+ `);
+
+ exit(0);
+ } catch (error) {
+ console.error(error);
+ console.error("Invalid share link.");
+ exit(1);
+ }
+};
diff --git a/apps/cli/src/utils.ts b/apps/cli/src/utils.ts
new file mode 100644
index 0000000000..2374239bad
--- /dev/null
+++ b/apps/cli/src/utils.ts
@@ -0,0 +1,33 @@
+import { dirname } from "node:path";
+import { access, mkdir, writeFile, constants, rm } from "node:fs/promises";
+
+export const ensureFileInPath = async (filePath: string, content?: string) => {
+ const dir = dirname(filePath);
+
+ await ensureFolderExists(dir);
+
+ try {
+ await access(filePath, constants.F_OK);
+ } catch {
+ await writeFile(filePath, content || "", "utf8");
+ }
+};
+
+export const ensureFolderExists = async (folderPath: string) => {
+ try {
+ await access(folderPath, constants.F_OK);
+ } catch {
+ await mkdir(folderPath, { recursive: true });
+ }
+};
+
+export const deleteFolderIfExists = async (generatedDir: string) => {
+ try {
+ await rm(generatedDir, { recursive: true });
+ } catch (error) {
+ if (error.code === "ENOENT") {
+ return;
+ }
+ throw error;
+ }
+};
diff --git a/apps/cli/tsconfig.json b/apps/cli/tsconfig.json
new file mode 100644
index 0000000000..2f3fdb5b2f
--- /dev/null
+++ b/apps/cli/tsconfig.json
@@ -0,0 +1,6 @@
+{
+ "extends": "@webstudio-is/tsconfig/base.json",
+ "compilerOptions": {
+ "module": "esnext"
+ }
+}
diff --git a/packages/eslint-config-custom/index.js b/packages/eslint-config-custom/index.js
index 679237c0d5..eb3f02b9fa 100644
--- a/packages/eslint-config-custom/index.js
+++ b/packages/eslint-config-custom/index.js
@@ -70,5 +70,11 @@ module.exports = {
"unicorn/filename-case": "off",
},
},
+ {
+ files: ["**/cli/**/*.{js,ts,tsx}"],
+ rules: {
+ "no-console": "off",
+ },
+ },
],
};
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index c083cea167..bcf13728b5 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -359,6 +359,22 @@ importers:
specifier: 5.1.6
version: 5.1.6
+ apps/cli:
+ dependencies:
+ env-paths:
+ specifier: ^3.0.0
+ version: 3.0.0
+ devDependencies:
+ '@types/node':
+ specifier: ^18.13.0
+ version: 18.13.0
+ '@webstudio-is/scripts':
+ specifier: workspace:^
+ version: link:../../packages/scripts
+ '@webstudio-is/tsconfig':
+ specifier: workspace:^
+ version: link:../../packages/tsconfig
+
packages/asset-uploader:
dependencies:
'@aws-crypto/sha256-js':
@@ -7108,7 +7124,7 @@ packages:
resolution: {integrity: sha512-dzrRZCYPXIXfSR1/surNbJ/grU3scTaygS0OMzjlGf71i9sc2fGyHPXXiXmEvNIoE0cGwsanEFMVJxPXmco9Eg==}
dependencies:
- '@types/node': 18.13.0
+ '@types/node': 18.17.1
dev: true
@@ -7122,14 +7138,14 @@ packages:
dependencies:
'@types/http-cache-semantics': 4.0.1
'@types/keyv': 3.1.4
- '@types/node': 18.13.0
+ '@types/node': 18.17.1
'@types/responselike': 1.0.0
dev: true
resolution: {integrity: sha512-cdeYyv4KWoEgpBISTxWvqYsVy444DOqehiF3fM3ne10AmJ62RSyNkUnxMJXHQWRQQX2eR94m5y1IZyDwBjV9FQ==}
dependencies:
- '@types/node': 18.13.0
+ '@types/node': 18.17.1
resolution: {integrity: sha512-XW/Aa8APYr6jSVVA1y/DEIZX0/GMKLEVekNG727R8cs56ahETkRAy/3DR7+fJyh7oUgGwNQaRfXCun0+KbWY7Q==}
@@ -7263,7 +7279,7 @@ packages:
resolution: {integrity: sha512-BQ5aZNSCpj7D6K2ksrRCTmKRLEpnPvWDiLPfoGyhZ++8YtiK9d/3DBKPJgry359X/P1PfruyYwvnvwFjuEiEIg==}
dependencies:
- '@types/node': 18.13.0
+ '@types/node': 18.17.1
dev: true
@@ -7325,6 +7341,9 @@ packages:
resolution: {integrity: sha512-gC3TazRzGoOnoKAhUx+Q0t8S9Tzs74z7m0ipwGpSqQrleP14hKxP4/JUeEQcD3W1/aIpnWl8pHowI7WokuZpXg==}
+ resolution: {integrity: sha512-xlR1jahfizdplZYRU59JlUx9uzF1ARa8jbhM11ccpCJya8kvos5jwdm2ZAgxSCwOl0fq21svP18EVwPBXMQudw==}
+
resolution: {integrity: sha512-Gj7cI7z+98M282Tqmp2K5EIsoouUEzbBJhQQzDE3jSIRk6r9gsz0oUokqIUR4u1R3dMHo0pDHM7sNOHyhulypw==}
@@ -7372,7 +7391,7 @@ packages:
resolution: {integrity: sha512-85Y2BjiufFzaMIlvJDvTTB8Fxl2xfLo4HgmHzVBz08w4wDePCTjYw66PdrolO0kzli3yam/YCgRufyo1DdQVTA==}
dependencies:
- '@types/node': 18.13.0
+ '@types/node': 18.17.1
dev: true
@@ -7385,7 +7404,7 @@ packages:
resolution: {integrity: sha512-Cwo8LE/0rnvX7kIIa3QHCkcuF21c05Ayb0ZfxPiv0W8VRiZiNW/WuRupHKpqqGVGf7SUA44QSOUKaEd9lIrd/Q==}
dependencies:
'@types/mime': 1.3.2
- '@types/node': 18.13.0
+ '@types/node': 18.17.1
resolution: {integrity: sha512-J2LqtvFYCzaj8pVYKw8klQXrLLk7TBZmQ4ShlcdkELFKGwGMfevMLneMMRkMgZxotOD9wg497LpC7O8PcvAmfw==}
@@ -9131,6 +9150,11 @@ packages:
engines: {node: '>=0.12'}
dev: true
+ resolution: {integrity: sha512-dtJUTepzMW3Lm/NPxRf3wP4642UWhjL2sQxc+ym2YMj1m/H2zDNQOlezafzkHwn6sMstjHTwG6iQQsctDW/b1A==}
+ engines: {node: ^12.20.0 || ^14.13.1 || >=16.0.0}
+ dev: false
+
resolution: {integrity: sha512-RODB4txU+xImYDemN5DqaKC0CHk05XSVkOX4pq0hK26Qx+1LChkuOyUDlGEjYb3ACr0n9qBhFjg37hQuJvpkRQ==}
engines: {node: '>=4'}
@@ -12802,6 +12826,9 @@ packages:
resolution: {integrity: sha512-bzfL1YUZsP41gmu/qjrEk0Q6i2ix/cVeAhbCbqH9u3zYutS1cLg00qhrD0M2MVdCcx4Sc0UpP2eBWo9rotpq6g==}
+ resolution: {integrity: sha512-2yyAR8qBkN3YuheJanUpWC5U3bb5osDywNB8RzDVlDwDHbocAJveqqj1u8+SVD7jkWT4yvsHCpWqqWqAxb0zCA==}
+
resolution: {integrity: sha512-6T6lH0H8OG9kITm/Jm6tdooIbogG9e0tLgpY6mphXSm/A9u8Nq1ryBG+Qspiub9LjWlBPsPS3tWQ/Botq4FdxA==}
engines: {node: '>= 8'}
@@ -12851,7 +12878,7 @@ packages:
resolution: {integrity: sha512-FP+p8RB8OWpF3YZBCrP5gtADmtXApB5AMLn+vdyA+PyxCjrCs00mjyUozssO33cwDeT3wNGdLxJ5M//YqtHAJw==}
hasBin: true
dependencies:
- minimist: 1.2.7
+ minimist: 1.2.8
resolution: {integrity: sha512-vVqVZQyf3WLx2Shd0qJ9xuvqgAyKPLAiqITEtqW0oIUjzo3PePDd6fW9iFz30ef7Ysp/oiWqbhszeGWW2T6Gzw==}