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

cli v0.1.0 #176

Draft
wants to merge 27 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
95f1fc2
update no project root found message
softmarshmallow Aug 12, 2022
5937190
add fpat prompt for `grida code` command
softmarshmallow Aug 12, 2022
fb7585f
update init proc
softmarshmallow Aug 12, 2022
e1bb7c3
add framework prompt for `grida code x` command
softmarshmallow Aug 12, 2022
8cd66a8
add pacakge.json insert (write only)
softmarshmallow Aug 13, 2022
fad4513
add npm package manager
softmarshmallow Aug 13, 2022
6813472
add init package.json
softmarshmallow Aug 13, 2022
dd1c712
add global error message handling
softmarshmallow Aug 13, 2022
dce8649
update logging
softmarshmallow Aug 13, 2022
a634d0b
add npm initializer based on givven framework config
softmarshmallow Aug 13, 2022
4c21c99
organize
softmarshmallow Aug 13, 2022
4a4b60a
v0.0.16
softmarshmallow Aug 13, 2022
d4e0205
update readme
softmarshmallow Aug 13, 2022
2d6540a
rm cache flag when installing npm
softmarshmallow Aug 13, 2022
6e5f53e
v0.0.18
softmarshmallow Aug 13, 2022
acc5829
Merge branch 'main' of https://github.com/gridaco/designto-code into cli
softmarshmallow Aug 15, 2022
d3b2b91
add fpat doc link
softmarshmallow Aug 15, 2022
543ee53
add login & logout to grida cli
softmarshmallow Aug 15, 2022
a8dd2db
v0.0.19
softmarshmallow Aug 15, 2022
e227a9a
Merge branch 'main' of https://github.com/gridaco/designto-code into cli
softmarshmallow Aug 20, 2022
18520df
0.0.20 hotfix
softmarshmallow Aug 20, 2022
f2f37d2
add runtime env loader
softmarshmallow Aug 29, 2022
68169b7
fix .env syncer
softmarshmallow Aug 29, 2022
88a7138
v0.0.21
softmarshmallow Aug 29, 2022
586caff
update .env seeding line format
softmarshmallow Sep 11, 2022
0aca463
update npm keywords
softmarshmallow Sep 23, 2022
230c303
v0.0.22
softmarshmallow Sep 23, 2022
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
1 change: 1 addition & 0 deletions cli/.public-credentials/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
https://bit.ly/3dxp0xW
1 change: 1 addition & 0 deletions cli/.runtime-env/.env
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
NODE_ENV=production
1 change: 1 addition & 0 deletions cli/.runtime-env/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
!.env
18 changes: 18 additions & 0 deletions cli/.runtime-env/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# this directory is for explicit .env for settings `NODE_ENV` for ncc build

Since the ncc does not opt us to set runtime env, so we'll need to load & set the env manually.

```ts
import dotenv from "dotenv";
dotenv.load({
path: "./.runtime-env/.env",
});
```

## Note for contributors

Is this required?:

Not essential, but some of our modules use logging conditioning based on `NODE_ENV !== "production"` not `NODE_ENV === "development"`.

So in most case, this will not change the core engine's logic behind, this is for disable verbose logging at this point.
25 changes: 15 additions & 10 deletions cli/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,23 @@ Visit https://grida.co/cli for more information.
npm install -g grida
```

## Documentation

Visit https://grida.co/docs/cli

## Commands

| Commands | | |
| -------------------- | --------------------------------------- | ---------------------------------------------------- |
| `grida code react` | convert input uri (file or url) to code | `designto react https://www.figma.com/files/XXX` |
| `grida code rn` | convert input uri (file or url) to code | `designto rn https://www.figma.com/files/XXX` |
| `grida code vue` | convert input uri (file or url) to code | `grida code vue https://www.figma.com/files/XXX` |
| `grida code svelte` | convert input uri (file or url) to code | `grida code svelte https://www.figma.com/files/XXX` |
| `grida code solid` | convert input uri (file or url) to code | `grida code flutter https://www.figma.com/files/XXX` |
| `grida code flutter` | help | `designto flutter https://www.figma.com/files/XXX` |
| `grida code auth` | signin to design services | `auto` \| `figma` \| `sketch` \| `xd` |
| `grida code init` | configure the preferences |
| Commands | | |
| ----------------------- | --------------------------------------- | ----------------------------------------------------- |
| `grida init` | configure the preferences | |
| `grida login` | signin to design services | `auto` \| `figma` \| `sketch` \| `xd` |
| `grida add [modules..]` | add new modules to existing project | `grida add` |
| `grida code react` | convert input uri (file or url) to code | `grida code react https://www.figma.com/files/XXX` |
| `grida code rn` | convert input uri (file or url) to code | `grida code rn https://www.figma.com/files/XXX` |
| `grida code vue` | convert input uri (file or url) to code | `grida code vue https://www.figma.com/files/XXX` |
| `grida code svelte` | convert input uri (file or url) to code | `grida code svelte https://www.figma.com/files/XXX` |
| `grida code solid-js` | convert input uri (file or url) to code | `grida code solid-js https://www.figma.com/files/XXX` |
| `grida code flutter` | help | `grida code flutter https://www.figma.com/files/XXX` |

## Args

Expand Down
13 changes: 7 additions & 6 deletions cli/auth/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ import Axios from "axios";
import { machineIdSync } from "node-machine-id";
import { AuthStore } from "./store";

const PROXY_AUTH_REQUEST_SECRET =
process.env.GRIDA_FIRST_PARTY_PROXY_AUTH_REQUEST_TOTP_SECRET;
// it is ok to load dynamically since its cli env.
const PROXY_AUTH_REQUEST_SECRET = () =>
process.env.PUBLIC_GRIDA_FIRST_PARTY_PROXY_AUTH_REQUEST_TOTP_SECRET;

function _termenv(): "vscode" | "terminal" | "unknown" {
switch (process.env.TERM_PROGRAM) {
Expand All @@ -33,7 +34,7 @@ function _make_request(): AuthProxySessionStartRequest {

export async function startAuthenticationSession(): Promise<AuthProxySessionStartResult> {
return __auth_proxy.openProxyAuthSession(
PROXY_AUTH_REQUEST_SECRET,
PROXY_AUTH_REQUEST_SECRET(),
_make_request()
);
}
Expand All @@ -42,7 +43,7 @@ export async function startAuthenticationWithSession(
session: AuthProxySessionStartResult
) {
const result = await __auth_proxy.requesetProxyAuthWithSession(
PROXY_AUTH_REQUEST_SECRET,
PROXY_AUTH_REQUEST_SECRET(),
session,
_make_request()
);
Expand All @@ -69,7 +70,7 @@ export async function getAccessToken(): Promise<string> {
export async function checkAuthSession(session: string): Promise<boolean> {
// TODO:
const res = await __auth_proxy.checkProxyAuthResult(
PROXY_AUTH_REQUEST_SECRET,
PROXY_AUTH_REQUEST_SECRET(),
session
);

Expand All @@ -88,7 +89,7 @@ const secure_axios = async () => {
},
});
cors.useAxiosCors(axios, {
apikey: process.env.GRIDA_FIRST_PARTY_CORS_API_KEY,
apikey: process.env.PUBLIC_GRIDA_FIRST_PARTY_CORS_API_KEY,
});
return axios;
};
Expand Down
1 change: 1 addition & 0 deletions cli/auth/login.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export async function login() {
url = authUrl;
await open(authUrl);
} catch (e) {
throw new Error("Error while starting authentication session");
exit(1);
}

Expand Down
33 changes: 22 additions & 11 deletions cli/bin.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import yargs from "yargs";
import { hideBin } from "yargs/helpers";
import { defaultConfigByFramework } from "@grida/builder-config-preset";
import { init } from "./init";
import {
init,
prompt_figma_personal_access_token,
prompt_framework_config,
} from "./init";
import { add } from "./add";
import { code } from "./code";
import { Framework } from "@grida/builder-platform-types";
Expand All @@ -11,14 +15,16 @@ import fs from "fs";
import { checkForUpdate } from "./update";
import { login, logout } from "./auth";
import { startFlutterDaemonServer } from "./flutter/daemon";
import { parseFileId } from "@design-sdk/figma-url";
import chalk from "chalk";

function loadenv(argv) {
const { cwd } = argv;
// Load .env file
const dotenvpath = path.join(cwd, ".env");
if (fs.existsSync(dotenvpath)) {
dotenv.config({ path: dotenvpath });
console.log("Loaded .env file");
console.info(chalk.dim("Loaded .env file"));
}
}

Expand Down Expand Up @@ -67,7 +73,7 @@ export default async function cli() {
async () => {
login();
},
[loadenv]
[]
)
.command(
"logout",
Expand All @@ -76,28 +82,33 @@ export default async function cli() {
async () => {
logout();
},
[loadenv]
[]
)
.command(
"code <framework> <uri>",
"code [framework] <uri>",
"generate code from input uri",
(argv) => {
// return;
},
async ({ cwd, framework, uri, out, ...argv }) => {
//
const _personal_access_token = argv[
"figma-personal-access-token"
] as string;

const filekey = parseFileId(uri as string);

// promp if not set
const _personal_access_token: string =
(argv["figma-personal-access-token"] as string) ??
(await prompt_figma_personal_access_token(filekey));

// make this path absolute if relative path is given.
const _outpath_abs: string = path.isAbsolute(out as string)
? (out as string)
: path.resolve(cwd, out as string);

const config_framework = defaultConfigByFramework(
framework as Framework
);
const config_framework = framework
? defaultConfigByFramework(framework as Framework)
: await prompt_framework_config(cwd, undefined, false);

if (!config_framework) {
throw new Error(`Unknown framework: "${framework}"`);
}
Expand Down
30 changes: 27 additions & 3 deletions cli/index.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,37 @@
#!/usr/bin/env node

import cli from "./bin";
import chalk from "chalk";
export type { GridaConfig } from "./config";
import dotenv from "dotenv";
import path from "path";

process.on("SIGINT", () => {
process.exit(0); // now the "exit" event will fire
});
// process
// .on("SIGINT", () => {
// process.exit(0); // now the "exit" event will fire
// })
// .on("uncaughtException", (err) => {
// console.log(chalk.bgRed(err.message ?? err));
// process.exit(1);
// })
// .on("unhandledRejection", (err: Error, p) => {
// console.error(chalk.bgRed(err.message ?? err));
// process.exit(1);
// });

// if main
if (require.main === module) {
// load env for accessing grida services
dotenv.config({
path: path.join(__dirname, ".public-credentials", ".env"),
});

/**
* load env for production @see {@link /cli/.runtime-env/readme.md}
*/
dotenv.config({
path: path.join(__dirname, ".runtime-env", ".env"),
});

cli();
}
Loading