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

Publish #140

Merged
merged 4 commits into from
Jun 7, 2022
Merged
Show file tree
Hide file tree
Changes from 3 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
2 changes: 2 additions & 0 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import telemetry from './cli/telemetry/index.js';
import webhook from './cli/webhook/index.js';
import app from './cli/app/index.js';
import vercel from './cli/vercel/index.js';
import github from './cli/github/index.js';

import * as login from './cli/login.js';
import * as logout from './cli/logout.js';
Expand Down Expand Up @@ -77,6 +78,7 @@ yargs(hideBin(process.argv))
.command(['webhook [command]', 'hook'], '', webhook)
.command(['app [command]'], '', app)
.command(['vercel [command]'], '', vercel)
.command(['github [command]'], '', github)
.option('json', { type: "boolean", desc: "Output the data as JSON" })
.strictCommands()
.middleware(useTelemetry(pkg.version))
Expand Down
6 changes: 4 additions & 2 deletions src/cli/app/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import * as create from "./create.js";
import * as generate from "./generate.js";
import * as token from "./token.js";
import * as permission from "./permission.js";
import * as publish from "./publish.js";

export default function (_: any) {
_.command([
Expand All @@ -17,7 +18,8 @@ export default function (_: any) {

// no auth needed
generate,
publish,
])
// .middleware([useToken, useOrganization, useEnvironment])
.demandCommand(1, "You need at least one command before moving on");
// .middleware([useToken, useOrganization, useEnvironment])
.demandCommand(1, "You need at least one command before moving on");
}
32 changes: 32 additions & 0 deletions src/cli/app/publish.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import type { CommandBuilder } from "yargs";
import fs from 'fs-extra';
import path from "path";
import got from "got";

import { marketplaceAppSubmit } from "../../graphql/mutation/marketplaceAppSubmit.js";
import { Config } from "../../lib/config.js";

export const command = "publish";
export const desc = "Publish a Saleor App to the Saleor Marketplace";

export const builder: CommandBuilder = (_) => _
export const handler = async (): Promise<void> => {
const { github_token } = await Config.get();
const { saleorApp: input } = JSON.parse(await fs.readFile(path.join(process.cwd(), 'package.json'), 'utf-8'));


const { data, errors }: any = await got.post(`https://saleor-graph.deno.dev`, {
headers: {
"Authorization": github_token
},
json: {
query: marketplaceAppSubmit,
variables: {
input
}
}
}).json()

console.log(data, errors)
};

8 changes: 8 additions & 0 deletions src/cli/github/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import * as login from "./login.js";

export default function (_: any) {
_.command([
login,
])
.demandCommand(1, "You need at least one command before moving on");
}
79 changes: 79 additions & 0 deletions src/cli/github/login.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import type { Arguments, CommandBuilder } from "yargs";

import EventEmitter from 'events'
import { CliUx } from "@oclif/core";
import { ServerApp } from "retes";
import { GET } from "retes/route";
import { Response } from "retes/response"
import { nanoid } from 'nanoid';
import got from "got";

import { Options } from "../../types.js";
import { Config } from "../../lib/config.js";
import detectPort from "detect-port";

const { ux: cli } = CliUx;

export const command = "login";
export const desc = "Add integration for Saleor CLI";

export const builder: CommandBuilder = (_) => _

export const handler = async (argv: Arguments<Options>) => {
const port = await detectPort(3000);
const RedirectURI = `http://localhost:${port}/github/callback`;

const generatedState = nanoid();
const emitter = new EventEmitter();

const { GithubClientID, GithubClientSecret } = await Config.get()

const Params = {
client_id: GithubClientID,
redirect_uri: RedirectURI,
scope: "repo"
}

const QueryParams = new URLSearchParams({ ...Params, state: generatedState });
const url = `https://github.com/login/oauth/authorize?${QueryParams}`;
cli.open(url);

const app = new ServerApp([
GET("/github/callback", async ({ params }) => {
const { state, code } = params;

if (state !== generatedState) {
return Response.BadRequest("Wrong state")
}

const Params = {
client_id: GithubClientID,
client_secret: GithubClientSecret,
code,
redirect_uri: RedirectURI,
}

try {
const data: any = await got.post(`https://github.com/login/oauth/access_token`, {
form: Params,
}).json();

const { access_token } = data;

await Config.set("github_token", `Bearer ${access_token}`);

} catch (error: any) {
console.log(error.message);
}

emitter.emit('finish');

return Response.Redirect('https://cloud.saleor.io');
})
])
await app.start(port);

emitter.on('finish', async () => {
await app.stop();
});
};
49 changes: 49 additions & 0 deletions src/cli/webhook/trigger.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import got from "got";
2can marked this conversation as resolved.
Show resolved Hide resolved
import type { Arguments, CommandBuilder } from "yargs";
import { Config } from "../../lib/config.js";
import { API, GET } from "../../lib/index.js";
import { Options } from "../../types.js";

export const command = "trigger <name> [id]";
export const desc = "This triggers a Saleor event";

export const builder: CommandBuilder = (_) =>
_.positional("name", { type: "string", demandOption: true })
.positional("id", { type: "string" });

export const handler = async (argv: Arguments<Options>) => {
const { name, id } = argv;
const greeting = `Hello, ${name} - ${id}!`;

const { domain } = await GET(API.Environment, argv) as any;
const headers = await Config.getBearerHeader();

const { data, errors }: any = await got.post(`https://${domain}/graphql`, {
headers,
json: {
query: '',
variables: {
}
}
}).json()

process.stdout.write(greeting);
process.exit(0);
};



`
mutation ProductUpdate {
productUpdate(id: "UHJvZHVjdDo3Mg==", input: {}) {
product {
name
}
errors {
field
code
message
}
}
}
`
8 changes: 8 additions & 0 deletions src/graphql/mutation/marketplaceAppSubmit.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export const marketplaceAppSubmit = /* GraphQL */`
mutation marketplaceAppSubmit($input: MarketplaceAppInput!) {
marketplaceAppSubmit(input: $input) {
status
reviewURL
}
}
`;
7 changes: 5 additions & 2 deletions src/lib/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ export const doSaleorAppInstall = async (argv: any) => {
const { domain } = await GET(API.Environment, argv) as any;
const headers = await Config.getBearerHeader();

console.log(chalk.green(' Configure your Saleor App'))
if (!argv.manifestURL) {
console.log(chalk.green(' Configure your Saleor App'))
}

const { manifestURL } = await Enquirer.prompt<{ manifestURL: string }>({
type: 'input',
name: 'manifestURL',
Expand All @@ -32,7 +35,7 @@ export const doSaleorAppInstall = async (argv: any) => {
try {
manifest = await got.get(manifestURL).json();
} catch {
console.log(chalk.red('\nThere was a problem while fetching provided manifest URL\n'));
console.log(chalk.red('\n There was a problem while fetching provided manifest URL\n'));
process.exit(1);
}

Expand Down
3 changes: 3 additions & 0 deletions src/lib/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ export type ConfigField =
| "VercelClientID"
| "VercelClientSecret"
| "SentryDSN"
| "GithubClientID"
| "GithubClientSecret"
| "github_token"

type ConfigProps = Record<ConfigField, string>;

Expand Down