@milofly/cli 0.0.0-stage → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +12 -0
- package/LICENSE +21 -0
- package/README.md +29 -2
- package/package.json +29 -4
- package/src/arayuz.mjs +64 -0
- package/src/kasa.mjs +103 -0
- package/src/milofly.mjs +75 -0
- package/src/oturum.mjs +176 -0
- package/src/sablon-yolu.mjs +4 -0
- package/src/tema.mjs +423 -0
- package/src/uygulama.mjs +27 -0
- package/templates/first-app/README.md +42 -0
- package/templates/first-app/package.json +22 -0
- package/templates/first-app/public/app.js +51 -0
- package/templates/first-app/public/index.html +41 -0
- package/templates/first-app/src/config.ts +31 -0
- package/templates/first-app/src/server.ts +217 -0
- package/templates/first-app/src/store.ts +34 -0
- package/templates/first-app/tsconfig.json +14 -0
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
## 0.1.1 (2026-11-27)
|
|
4
|
+
|
|
5
|
+
- Released from GitHub Actions with npm trusted publishing (OIDC) and provenance. No functional change.
|
|
6
|
+
|
|
7
|
+
## 0.1.0 (2026-11-27)
|
|
8
|
+
|
|
9
|
+
- First version: `login` (device authorization in the browser), `logout`, `whoami`, `theme init|pull|check|dev|push|pack|submit|list`,
|
|
10
|
+
`app init`.
|
|
11
|
+
- Theme packages carry their own icons (SVG/PNG) and fonts (WOFF2): `theme check|push|dev` upload the files listed in
|
|
12
|
+
`theme/assets.json` that Milofly does not have yet (`/api/dev/theme-packages/assets*`); `theme dev` also reacts to asset changes.
|
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Alper Baydeniz
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,30 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @milofly/cli
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
The Milofly developer CLI. Sign in with your browser, develop composition themes against your development store, and
|
|
4
|
+
scaffold apps.
|
|
5
|
+
|
|
6
|
+
```
|
|
7
|
+
npm install -g @milofly/cli
|
|
8
|
+
milofly login
|
|
9
|
+
milofly theme init my-theme
|
|
10
|
+
cd my-theme
|
|
11
|
+
milofly theme dev
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
| Command | What it does |
|
|
15
|
+
|---|---|
|
|
16
|
+
| `milofly login` | Shows a short code and opens the developer portal; you confirm there. No password in the terminal. The session is kept in your OS keychain (Windows DPAPI, macOS Keychain, Linux Secret Service). |
|
|
17
|
+
| `milofly logout` / `whoami` | Close the session / show the developer and publisher key. |
|
|
18
|
+
| `milofly theme init [dir]` | Pick a development store and a theme in it, create the project and pull it. |
|
|
19
|
+
| `milofly theme pull` | Bring the theme from your development store into `theme/` (your local version and catalog texts are kept). |
|
|
20
|
+
| `milofly theme check` | Validate with the Milofly rules: release, install, publishing to all stores. Exit code 1 if it cannot be released or installed. |
|
|
21
|
+
| `milofly theme dev` | On every change: a draft development release (`x.y.z-dev.<time>`), installed and published in your development store; the previous development install is removed. |
|
|
22
|
+
| `milofly theme push [--activate]` | Write the version in `theme/package.json` as a draft release and install it in your development store. |
|
|
23
|
+
| `milofly theme pack` | Write `dist/<package>-<version>.json` (no upload). |
|
|
24
|
+
| `milofly theme submit` | Send a pushed version to Milofly review. |
|
|
25
|
+
| `milofly theme list [--all]` | Your packages and releases. |
|
|
26
|
+
| `milofly app init <dir>` | Start an app from the first-app template (install, webhooks, embedded page, API call). |
|
|
27
|
+
|
|
28
|
+
Only your own development stores are ever written to. Docs: https://developer.milofly.com/docs/cli
|
|
29
|
+
|
|
30
|
+
Requires Node.js 20 or newer. Depends only on `@milofly/theme-sdk`.
|
package/package.json
CHANGED
|
@@ -1,6 +1,31 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@milofly/cli",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.1",
|
|
4
|
+
"description": "Milofly CLI: sign in with your browser, develop composition themes against your development store, scaffold apps.",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"homepage": "https://developer.milofly.com",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "git+https://github.com/Milofly/milofly-sdk.git",
|
|
10
|
+
"directory": "js/cli"
|
|
11
|
+
},
|
|
12
|
+
"bugs": {
|
|
13
|
+
"url": "https://github.com/Milofly/milofly-sdk/issues"
|
|
14
|
+
},
|
|
15
|
+
"type": "module",
|
|
16
|
+
"engines": {
|
|
17
|
+
"node": ">=20"
|
|
18
|
+
},
|
|
19
|
+
"bin": {
|
|
20
|
+
"milofly": "src/milofly.mjs"
|
|
21
|
+
},
|
|
22
|
+
"exports": {
|
|
23
|
+
".": "./src/milofly.mjs"
|
|
24
|
+
},
|
|
25
|
+
"dependencies": {
|
|
26
|
+
"@milofly/theme-sdk": "0.1.1"
|
|
27
|
+
},
|
|
28
|
+
"publishConfig": {
|
|
29
|
+
"access": "public"
|
|
30
|
+
}
|
|
31
|
+
}
|
package/src/arayuz.mjs
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
// Terminal yardımcıları: yazdırma, soru sorma, listeden seçme, tarayıcı açma. Bağımlılık yok.
|
|
2
|
+
import { spawn } from "node:child_process";
|
|
3
|
+
import { createInterface } from "node:readline/promises";
|
|
4
|
+
import { stdin, stdout, stderr } from "node:process";
|
|
5
|
+
|
|
6
|
+
const renk = stdout.isTTY && !process.env.NO_COLOR;
|
|
7
|
+
const boya = (kod, metin) => (renk ? `\x1b[${kod}m${metin}\x1b[0m` : metin);
|
|
8
|
+
export const kalin = (m) => boya("1", m);
|
|
9
|
+
export const soluk = (m) => boya("2", m);
|
|
10
|
+
export const yesil = (m) => boya("32", m);
|
|
11
|
+
export const kirmizi = (m) => boya("31", m);
|
|
12
|
+
export const sari = (m) => boya("33", m);
|
|
13
|
+
|
|
14
|
+
export const yaz = (m = "") => stdout.write(`${m}\n`);
|
|
15
|
+
export const uyar = (m) => stderr.write(`${sari("!")} ${m}\n`);
|
|
16
|
+
export const hataYaz = (m) => stderr.write(`${kirmizi("✖")} ${m}\n`);
|
|
17
|
+
|
|
18
|
+
/** Etkileşimli değilse (CI) soru sorulmaz: eksik bilgi bayrakla verilmelidir. */
|
|
19
|
+
function etkilesimliMi(neIcin) {
|
|
20
|
+
if (stdin.isTTY) return;
|
|
21
|
+
throw new Error(`${neIcin} is required (non-interactive terminal: pass it as a flag).`);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export async function sor(soru, { varsayilan } = {}) {
|
|
25
|
+
etkilesimliMi(soru);
|
|
26
|
+
const rl = createInterface({ input: stdin, output: stdout });
|
|
27
|
+
try {
|
|
28
|
+
const cevap = (await rl.question(`${soru}${varsayilan ? soluk(` (${varsayilan})`) : ""}: `)).trim();
|
|
29
|
+
return cevap || varsayilan || "";
|
|
30
|
+
} finally {
|
|
31
|
+
rl.close();
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/** @template T @param {string} baslik @param {T[]} ogeler @param {(o: T) => string} etiket @returns {Promise<T>} */
|
|
36
|
+
export async function sec(baslik, ogeler, etiket) {
|
|
37
|
+
if (ogeler.length === 0) throw new Error(`${baslik}: nothing to choose from.`);
|
|
38
|
+
if (ogeler.length === 1) {
|
|
39
|
+
yaz(`${baslik}: ${etiket(ogeler[0])}`);
|
|
40
|
+
return ogeler[0];
|
|
41
|
+
}
|
|
42
|
+
etkilesimliMi(baslik);
|
|
43
|
+
yaz(kalin(baslik));
|
|
44
|
+
ogeler.forEach((o, i) => yaz(` ${i + 1}) ${etiket(o)}`));
|
|
45
|
+
for (;;) {
|
|
46
|
+
const n = Number(await sor("Number"));
|
|
47
|
+
if (Number.isInteger(n) && n >= 1 && n <= ogeler.length) return ogeler[n - 1];
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/** Varsayılan tarayıcıda açar; açamazsa adres zaten yazılmıştır. */
|
|
52
|
+
export function tarayiciAc(adres) {
|
|
53
|
+
const [komut, argumanlar] =
|
|
54
|
+
process.platform === "win32" ? ["cmd", ["/c", "start", "", adres.replaceAll("&", "^&")]]
|
|
55
|
+
: process.platform === "darwin" ? ["open", [adres]]
|
|
56
|
+
: ["xdg-open", [adres]];
|
|
57
|
+
try {
|
|
58
|
+
const c = spawn(komut, argumanlar, { stdio: "ignore", detached: true, windowsHide: true });
|
|
59
|
+
c.on("error", () => {});
|
|
60
|
+
c.unref();
|
|
61
|
+
} catch {
|
|
62
|
+
// Tarayıcı açılamadı: adres terminalde yazılı, kullanıcı elle açar.
|
|
63
|
+
}
|
|
64
|
+
}
|
package/src/kasa.mjs
ADDED
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
// Kimlik kasası — CLI'ın yenileme jetonunu işletim sisteminin gizli deposunda tutar. Parola hiç görülmez ve saklanmaz.
|
|
2
|
+
//
|
|
3
|
+
// Windows : DPAPI (geçerli kullanıcı) ile şifrelenmiş dosya, ~/.milofly/credentials.<ad>.dpapi
|
|
4
|
+
// macOS : Anahtar Zinciri (security add-generic-password / find-generic-password)
|
|
5
|
+
// Linux : Secret Service (secret-tool); yoksa ~/.milofly/credentials.<ad>.json, yalnız sahibi okur (0600) — bu durum her girişte
|
|
6
|
+
// açıkça yazılır (`konum`).
|
|
7
|
+
// Kayıt anahtarı API kökenidir: aynı makinede canlı ve yerel Milofly'a ayrı oturum açılabilir.
|
|
8
|
+
import { spawnSync } from "node:child_process";
|
|
9
|
+
import { createHash } from "node:crypto";
|
|
10
|
+
import { chmodSync, existsSync, mkdirSync, readFileSync, rmSync, writeFileSync } from "node:fs";
|
|
11
|
+
import { homedir } from "node:os";
|
|
12
|
+
import { join } from "node:path";
|
|
13
|
+
|
|
14
|
+
const HIZMET = "milofly-cli";
|
|
15
|
+
export const KLASOR = join(homedir(), ".milofly");
|
|
16
|
+
|
|
17
|
+
function ad(api) {
|
|
18
|
+
return createHash("sha256").update(api).digest("hex").slice(0, 16);
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
function calistir(komut, argumanlar, girdi) {
|
|
22
|
+
const s = spawnSync(komut, argumanlar, { input: girdi, encoding: "utf8", windowsHide: true });
|
|
23
|
+
if (s.error) return { ok: false, cikti: "", hata: s.error.message, yok: s.error.code === "ENOENT" };
|
|
24
|
+
return { ok: s.status === 0, cikti: (s.stdout ?? "").trim(), hata: (s.stderr ?? "").trim(), yok: false };
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
function powershell(betik, girdi) {
|
|
28
|
+
return calistir("powershell.exe", ["-NoProfile", "-NonInteractive", "-Command", betik], girdi);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
const DPAPI_YAZ =
|
|
32
|
+
"Add-Type -AssemblyName System.Security; $g=[Console]::In.ReadToEnd(); " +
|
|
33
|
+
"$b=[Text.Encoding]::UTF8.GetBytes($g); " +
|
|
34
|
+
"[Convert]::ToBase64String([Security.Cryptography.ProtectedData]::Protect($b,$null,'CurrentUser'))";
|
|
35
|
+
const DPAPI_OKU =
|
|
36
|
+
"Add-Type -AssemblyName System.Security; $g=[Console]::In.ReadToEnd().Trim(); " +
|
|
37
|
+
"$b=[Security.Cryptography.ProtectedData]::Unprotect([Convert]::FromBase64String($g),$null,'CurrentUser'); " +
|
|
38
|
+
"[Text.Encoding]::UTF8.GetString($b)";
|
|
39
|
+
|
|
40
|
+
function linuxSecretTool() {
|
|
41
|
+
return calistir("secret-tool", ["--version"]).yok ? null : "secret-tool";
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/** Kasanın nerede tuttuğu (girişte kullanıcıya söylenir). */
|
|
45
|
+
export function konum() {
|
|
46
|
+
if (process.platform === "win32") return "Windows DPAPI (current user)";
|
|
47
|
+
if (process.platform === "darwin") return "macOS Keychain";
|
|
48
|
+
return linuxSecretTool() ? "Secret Service (secret-tool)" : `${join(KLASOR, "credentials.*.json")} (0600, no keyring found)`;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/** @param {string} api @param {object} kayit */
|
|
52
|
+
export function yaz(api, kayit) {
|
|
53
|
+
mkdirSync(KLASOR, { recursive: true, mode: 0o700 });
|
|
54
|
+
const metin = JSON.stringify(kayit);
|
|
55
|
+
if (process.platform === "win32") {
|
|
56
|
+
const r = powershell(DPAPI_YAZ, metin);
|
|
57
|
+
if (!r.ok) throw new Error(`Could not encrypt credentials with DPAPI: ${r.hata}`);
|
|
58
|
+
writeFileSync(join(KLASOR, `credentials.${ad(api)}.dpapi`), r.cikti, { mode: 0o600 });
|
|
59
|
+
return;
|
|
60
|
+
}
|
|
61
|
+
if (process.platform === "darwin") {
|
|
62
|
+
const r = calistir("security", ["add-generic-password", "-U", "-s", HIZMET, "-a", api, "-w", metin]);
|
|
63
|
+
if (!r.ok) throw new Error(`Could not write to the macOS Keychain: ${r.hata}`);
|
|
64
|
+
return;
|
|
65
|
+
}
|
|
66
|
+
if (linuxSecretTool()) {
|
|
67
|
+
const r = calistir("secret-tool", ["store", `--label=Milofly CLI (${api})`, "service", HIZMET, "account", api], metin);
|
|
68
|
+
if (!r.ok) throw new Error(`Could not write to the Secret Service: ${r.hata}`);
|
|
69
|
+
return;
|
|
70
|
+
}
|
|
71
|
+
const dosya = join(KLASOR, `credentials.${ad(api)}.json`);
|
|
72
|
+
writeFileSync(dosya, metin, { mode: 0o600 });
|
|
73
|
+
chmodSync(dosya, 0o600);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/** @param {string} api @returns {object|null} */
|
|
77
|
+
export function oku(api) {
|
|
78
|
+
if (process.platform === "win32") {
|
|
79
|
+
const dosya = join(KLASOR, `credentials.${ad(api)}.dpapi`);
|
|
80
|
+
if (!existsSync(dosya)) return null;
|
|
81
|
+
const r = powershell(DPAPI_OKU, readFileSync(dosya, "utf8"));
|
|
82
|
+
if (!r.ok) throw new Error(`Could not decrypt credentials (DPAPI): ${r.hata}. Run "milofly login" again.`);
|
|
83
|
+
return JSON.parse(r.cikti);
|
|
84
|
+
}
|
|
85
|
+
if (process.platform === "darwin") {
|
|
86
|
+
const r = calistir("security", ["find-generic-password", "-s", HIZMET, "-a", api, "-w"]);
|
|
87
|
+
return r.ok && r.cikti ? JSON.parse(r.cikti) : null;
|
|
88
|
+
}
|
|
89
|
+
if (linuxSecretTool()) {
|
|
90
|
+
const r = calistir("secret-tool", ["lookup", "service", HIZMET, "account", api]);
|
|
91
|
+
return r.ok && r.cikti ? JSON.parse(r.cikti) : null;
|
|
92
|
+
}
|
|
93
|
+
const dosya = join(KLASOR, `credentials.${ad(api)}.json`);
|
|
94
|
+
return existsSync(dosya) ? JSON.parse(readFileSync(dosya, "utf8")) : null;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/** @param {string} api */
|
|
98
|
+
export function sil(api) {
|
|
99
|
+
if (process.platform === "win32") return rmSync(join(KLASOR, `credentials.${ad(api)}.dpapi`), { force: true });
|
|
100
|
+
if (process.platform === "darwin") return void calistir("security", ["delete-generic-password", "-s", HIZMET, "-a", api]);
|
|
101
|
+
if (linuxSecretTool()) return void calistir("secret-tool", ["clear", "service", HIZMET, "account", api]);
|
|
102
|
+
rmSync(join(KLASOR, `credentials.${ad(api)}.json`), { force: true });
|
|
103
|
+
}
|
package/src/milofly.mjs
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
// Milofly CLI — giriş noktası. Belge: https://developer.milofly.com/docs/cli
|
|
3
|
+
import { parseArgs } from "node:util";
|
|
4
|
+
import { readFileSync } from "node:fs";
|
|
5
|
+
import { cikis, giris, kimim, URETIM_API } from "./oturum.mjs";
|
|
6
|
+
import { komutlar as tema } from "./tema.mjs";
|
|
7
|
+
import { komutlar as uygulama } from "./uygulama.mjs";
|
|
8
|
+
import { hataYaz, kalin, soluk, yaz } from "./arayuz.mjs";
|
|
9
|
+
|
|
10
|
+
const SURUM = JSON.parse(readFileSync(new URL("../package.json", import.meta.url), "utf8")).version;
|
|
11
|
+
|
|
12
|
+
const YARDIM = `${kalin("milofly")} ${SURUM} — Milofly developer CLI
|
|
13
|
+
|
|
14
|
+
${kalin("Account")}
|
|
15
|
+
milofly login [--api <url>] [--no-browser] Sign in with your browser (no password in the terminal)
|
|
16
|
+
milofly logout Close this machine's CLI session
|
|
17
|
+
milofly whoami Show the signed-in developer and publisher key
|
|
18
|
+
|
|
19
|
+
${kalin("Themes")} (composition themes, built in your development store)
|
|
20
|
+
milofly theme init [dir] [--store <id>] [--theme <id>] [--name <name>]
|
|
21
|
+
milofly theme pull Bring the theme from your development store into theme/
|
|
22
|
+
milofly theme check [--json] Validate with the Milofly rules (release, install, publishing)
|
|
23
|
+
milofly theme dev Push every change to your development store and publish it there
|
|
24
|
+
milofly theme push [--activate] Write the version in theme/package.json as a draft release and install it
|
|
25
|
+
milofly theme pack Write dist/<package>-<version>.json (no upload)
|
|
26
|
+
milofly theme submit [--version <x>] Send a pushed version to Milofly review
|
|
27
|
+
milofly theme list [--all] Your theme packages and releases
|
|
28
|
+
|
|
29
|
+
${kalin("Apps")}
|
|
30
|
+
milofly app init <dir> Start from the first-app template (install, webhooks, embedded page)
|
|
31
|
+
|
|
32
|
+
${kalin("Options")}
|
|
33
|
+
--path <dir> Theme project folder (default: current folder)
|
|
34
|
+
--api <url> Milofly API (default after login: the one you signed in to; first login: ${URETIM_API})
|
|
35
|
+
MILOFLY_LANG Language of Milofly error messages (tr, en, de, fr, ar)
|
|
36
|
+
`;
|
|
37
|
+
|
|
38
|
+
const { values: secenekler, positionals } = parseArgs({
|
|
39
|
+
allowPositionals: true,
|
|
40
|
+
options: {
|
|
41
|
+
api: { type: "string" },
|
|
42
|
+
path: { type: "string" },
|
|
43
|
+
store: { type: "string" },
|
|
44
|
+
theme: { type: "string" },
|
|
45
|
+
name: { type: "string" },
|
|
46
|
+
version: { type: "string" },
|
|
47
|
+
activate: { type: "boolean" },
|
|
48
|
+
all: { type: "boolean" },
|
|
49
|
+
json: { type: "boolean" },
|
|
50
|
+
"no-browser": { type: "boolean" },
|
|
51
|
+
help: { type: "boolean", short: "h" },
|
|
52
|
+
v: { type: "boolean" },
|
|
53
|
+
},
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
const [grup, alt, ...kalan] = positionals;
|
|
57
|
+
|
|
58
|
+
async function main() {
|
|
59
|
+
if (secenekler.v) return yaz(SURUM);
|
|
60
|
+
if (!grup || secenekler.help) return yaz(YARDIM);
|
|
61
|
+
if (grup === "login") return giris(secenekler);
|
|
62
|
+
if (grup === "logout") return cikis(secenekler);
|
|
63
|
+
if (grup === "whoami") return kimim(secenekler);
|
|
64
|
+
const tablo = grup === "theme" ? tema : grup === "app" ? uygulama : null;
|
|
65
|
+
if (!tablo) throw new Error(`Unknown command "${grup}". Run "milofly --help".`);
|
|
66
|
+
const komut = tablo[alt];
|
|
67
|
+
if (!komut) throw new Error(`Unknown command "${grup} ${alt ?? ""}". Run "milofly --help".`);
|
|
68
|
+
return komut(secenekler, kalan);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
main().catch((hata) => {
|
|
72
|
+
hataYaz(hata.message);
|
|
73
|
+
if (process.env.MILOFLY_DEBUG) yaz(soluk(hata.stack));
|
|
74
|
+
process.exitCode = 1;
|
|
75
|
+
});
|
package/src/oturum.mjs
ADDED
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+
// Oturum ve API istemcisi. Giriş tarayıcıyla, cihaz onayıyla (RFC 8628): CLI kısa bir kod alır, portalın /cli sayfası açılır,
|
|
2
|
+
// geliştirici onaylar; CLI kısa ömürlü erişim jetonu + yenileme jetonu alır. Yenileme jetonu her kullanımda döner ve kasaya yazılır.
|
|
3
|
+
// Uçlar: /api/dev/cli/* (belge: https://developer.milofly.com/docs/cli).
|
|
4
|
+
import { existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs";
|
|
5
|
+
import { join } from "node:path";
|
|
6
|
+
import { platform, release } from "node:os";
|
|
7
|
+
import * as kasa from "./kasa.mjs";
|
|
8
|
+
import { tarayiciAc, yaz, uyar } from "./arayuz.mjs";
|
|
9
|
+
|
|
10
|
+
/** Yayındaki Milofly API'si. Başka bir Milofly (yerel, prova) `--api` ya da MILOFLY_API_URL ile açıkça seçilir. */
|
|
11
|
+
export const URETIM_API = "https://api.milofly.com";
|
|
12
|
+
const PROFIL = join(kasa.KLASOR, "cli.json");
|
|
13
|
+
const SURUM = JSON.parse(readFileSync(new URL("../package.json", import.meta.url), "utf8")).version;
|
|
14
|
+
|
|
15
|
+
export class ApiHatasi extends Error {
|
|
16
|
+
constructor(durum, govde, mesaj) {
|
|
17
|
+
super(mesaj);
|
|
18
|
+
this.durum = durum;
|
|
19
|
+
this.govde = govde;
|
|
20
|
+
this.kod = govde?.code || govde?.error || null;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/** Hangi API: bayrak > ortam değişkeni > son girişin profili. Hiçbiri yoksa giriş istenir (sessiz varsayılan yok). */
|
|
25
|
+
export function apiKoku(secenekler, { girisIcin = false } = {}) {
|
|
26
|
+
const acik = secenekler.api || process.env.MILOFLY_API_URL;
|
|
27
|
+
if (acik) return acik.replace(/\/+$/, "");
|
|
28
|
+
if (existsSync(PROFIL)) return JSON.parse(readFileSync(PROFIL, "utf8")).api;
|
|
29
|
+
if (girisIcin) return URETIM_API;
|
|
30
|
+
throw new Error('Not signed in. Run "milofly login" first.');
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
function dil() {
|
|
34
|
+
const d = (process.env.MILOFLY_LANG || Intl.DateTimeFormat().resolvedOptions().locale || "").slice(0, 2).toLowerCase();
|
|
35
|
+
return d || null;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
async function istek(api, yol, { method = "GET", govde, jeton } = {}) {
|
|
39
|
+
const basliklar = { Accept: "application/json", "User-Agent": `milofly-cli/${SURUM} (${platform()} ${release()})` };
|
|
40
|
+
// Çok parçalı form (paket varlığı yüklemesi): içerik türünü ve sınırı fetch kendisi yazar.
|
|
41
|
+
const form = govde instanceof FormData;
|
|
42
|
+
if (govde !== undefined && !form) basliklar["Content-Type"] = "application/json";
|
|
43
|
+
if (jeton) basliklar.Authorization = `Bearer ${jeton}`;
|
|
44
|
+
const d = dil();
|
|
45
|
+
if (d) basliklar["X-Dev-Language"] = d;
|
|
46
|
+
let yanit;
|
|
47
|
+
try {
|
|
48
|
+
yanit = await fetch(`${api}${yol}`, { method, headers: basliklar, body: govde === undefined ? undefined : form ? govde : JSON.stringify(govde) });
|
|
49
|
+
} catch (hata) {
|
|
50
|
+
throw new Error(`Cannot reach ${api}: ${hata.cause?.message ?? hata.message}`);
|
|
51
|
+
}
|
|
52
|
+
const metin = await yanit.text();
|
|
53
|
+
let veri = null;
|
|
54
|
+
try {
|
|
55
|
+
veri = metin ? JSON.parse(metin) : null;
|
|
56
|
+
} catch {
|
|
57
|
+
veri = { raw: metin.slice(0, 500) };
|
|
58
|
+
}
|
|
59
|
+
return { durum: yanit.status, veri };
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/** Kodlu API hatasının okunur metni: portalın kullandığı katalogdan (`/api/dev/api-messages/{kod}`), parametreler yerine konur. */
|
|
63
|
+
async function hataMetni(api, veri) {
|
|
64
|
+
const kod = veri?.code;
|
|
65
|
+
if (!kod || kod.includes(".")) return veri?.message || veri?.error || null;
|
|
66
|
+
const r = await istek(api, `/api/dev/api-messages/${encodeURIComponent(kod)}`).catch(() => null);
|
|
67
|
+
let metin = r?.durum === 200 ? r.veri?.message : null;
|
|
68
|
+
if (metin && veri.params && typeof veri.params === "object")
|
|
69
|
+
for (const [k, v] of Object.entries(veri.params)) metin = metin.replaceAll(`{${k}}`, String(v));
|
|
70
|
+
return metin ? `${metin} (${kod})` : kod;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/** Oturumlu API istemcisi: erişim jetonu bitince yenilenir, yeni yenileme jetonu kasaya yazılır. */
|
|
74
|
+
export async function istemci(secenekler) {
|
|
75
|
+
const api = apiKoku(secenekler);
|
|
76
|
+
let kayit = kasa.oku(api);
|
|
77
|
+
if (!kayit?.refreshToken) throw new Error(`Not signed in to ${api}. Run "milofly login${secenekler.api ? ` --api ${api}` : ""}".`);
|
|
78
|
+
|
|
79
|
+
async function jeton() {
|
|
80
|
+
if (kayit.accessToken && kayit.accessExpiresAt > Date.now() + 30_000) return kayit.accessToken;
|
|
81
|
+
const r = await istek(api, "/api/dev/cli/token", { method: "POST", govde: { grantType: "refresh_token", refreshToken: kayit.refreshToken } });
|
|
82
|
+
if (r.durum !== 200) {
|
|
83
|
+
kasa.sil(api);
|
|
84
|
+
throw new Error(`${(await hataMetni(api, r.veri)) ?? "The session has ended."} Run "milofly login" again.`);
|
|
85
|
+
}
|
|
86
|
+
kayit = oturumKaydi(r.veri, kayit.email);
|
|
87
|
+
kasa.yaz(api, kayit);
|
|
88
|
+
return kayit.accessToken;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
async function cagir(yol, ayar = {}) {
|
|
92
|
+
const r = await istek(api, yol, { ...ayar, jeton: await jeton() });
|
|
93
|
+
if (r.durum >= 200 && r.durum < 300) return r.veri;
|
|
94
|
+
throw new ApiHatasi(r.durum, r.veri, (await hataMetni(api, r.veri)) ?? `HTTP ${r.durum} on ${yol}`);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
return { api, cagir, get eposta() { return kayit.email; } };
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
function oturumKaydi(veri, email) {
|
|
101
|
+
return {
|
|
102
|
+
accessToken: veri.access_token,
|
|
103
|
+
accessExpiresAt: Date.now() + (veri.expires_in ?? 0) * 1000,
|
|
104
|
+
refreshToken: veri.refresh_token,
|
|
105
|
+
email: email ?? jetondakiEposta(veri.access_token),
|
|
106
|
+
};
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
function jetondakiEposta(jwt) {
|
|
110
|
+
try {
|
|
111
|
+
const g = JSON.parse(Buffer.from(jwt.split(".")[1], "base64url").toString("utf8"));
|
|
112
|
+
return g.email ?? g["http://schemas.xmlsoap.org/ws/2005/05/identity/claims/emailaddress"] ?? null;
|
|
113
|
+
} catch {
|
|
114
|
+
return null;
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
const bekle = (ms) => new Promise((r) => setTimeout(r, ms));
|
|
119
|
+
|
|
120
|
+
/** `milofly login` */
|
|
121
|
+
export async function giris(secenekler) {
|
|
122
|
+
const api = apiKoku(secenekler, { girisIcin: true });
|
|
123
|
+
const istemciAdi = `milofly-cli ${SURUM} on ${platform()}`;
|
|
124
|
+
const k = await istek(api, "/api/dev/cli/device", { method: "POST", govde: { clientName: istemciAdi } });
|
|
125
|
+
if (k.durum !== 200) throw new Error(`Could not start sign-in on ${api}: ${(await hataMetni(api, k.veri)) ?? `HTTP ${k.durum}`}`);
|
|
126
|
+
const { device_code, user_code, verification_uri, verification_uri_complete, expires_in, interval } = k.veri;
|
|
127
|
+
|
|
128
|
+
yaz(`\nMilofly API: ${api}`);
|
|
129
|
+
yaz(`Open ${verification_uri} and confirm this code:\n\n ${user_code}\n`);
|
|
130
|
+
if (!secenekler["no-browser"]) tarayiciAc(verification_uri_complete);
|
|
131
|
+
|
|
132
|
+
let aralik = (interval ?? 5) * 1000;
|
|
133
|
+
const bitis = Date.now() + (expires_in ?? 600) * 1000;
|
|
134
|
+
while (Date.now() < bitis) {
|
|
135
|
+
await bekle(aralik);
|
|
136
|
+
const r = await istek(api, "/api/dev/cli/token", {
|
|
137
|
+
method: "POST",
|
|
138
|
+
govde: { grantType: "urn:ietf:params:oauth:grant-type:device_code", deviceCode: device_code },
|
|
139
|
+
});
|
|
140
|
+
if (r.durum === 200) {
|
|
141
|
+
const kayit = oturumKaydi(r.veri);
|
|
142
|
+
kasa.yaz(api, kayit);
|
|
143
|
+
mkdirSync(kasa.KLASOR, { recursive: true, mode: 0o700 });
|
|
144
|
+
writeFileSync(PROFIL, JSON.stringify({ api }, null, 2) + "\n");
|
|
145
|
+
yaz(`Signed in as ${kayit.email ?? "developer"}. Credentials: ${kasa.konum()}.`);
|
|
146
|
+
return;
|
|
147
|
+
}
|
|
148
|
+
const hata = r.veri?.error;
|
|
149
|
+
if (hata === "authorization_pending") continue;
|
|
150
|
+
if (hata === "slow_down") { aralik += 5000; continue; }
|
|
151
|
+
if (hata === "access_denied") throw new Error("Sign-in was denied in the browser.");
|
|
152
|
+
if (hata === "expired_token") break;
|
|
153
|
+
throw new Error(`Sign-in failed: ${(await hataMetni(api, r.veri)) ?? hata ?? `HTTP ${r.durum}`}`);
|
|
154
|
+
}
|
|
155
|
+
throw new Error('The code expired before it was confirmed. Run "milofly login" again.');
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
/** `milofly logout` */
|
|
159
|
+
export async function cikis(secenekler) {
|
|
160
|
+
const api = apiKoku(secenekler);
|
|
161
|
+
const kayit = kasa.oku(api);
|
|
162
|
+
if (kayit?.refreshToken)
|
|
163
|
+
await istek(api, "/api/dev/cli/logout", { method: "POST", govde: { refreshToken: kayit.refreshToken } }).catch((h) => uyar(String(h.message)));
|
|
164
|
+
kasa.sil(api);
|
|
165
|
+
yaz(`Signed out of ${api}.`);
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
/** `milofly whoami` */
|
|
169
|
+
export async function kimim(secenekler) {
|
|
170
|
+
const c = await istemci(secenekler);
|
|
171
|
+
const r = await c.cagir("/api/dev/auth/me");
|
|
172
|
+
const yayinci = await c.cagir("/api/dev/publisher");
|
|
173
|
+
yaz(`${r.profile.email}${r.profile.fullName ? ` (${r.profile.fullName})` : ""}`);
|
|
174
|
+
yaz(`API: ${c.api}`);
|
|
175
|
+
yaz(`Publisher key: ${yayinci.publisherKey ?? "(not chosen yet — choose it once in the portal › Themes)"}`);
|
|
176
|
+
}
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
// `milofly app init`'in kopyaladığı başlangıç uygulamasının yeri. Depo içinde örneğin KENDİSİDİR (tek kaynak:
|
|
2
|
+
// milofly.sdk/ornekler/ilk-uygulama); yayın paketinde paketle.mjs örneği dist/templates/first-app'e kopyalar ve bu dosyayı oraya
|
|
3
|
+
// gösterecek biçimde yeniden yazar.
|
|
4
|
+
export const ILK_UYGULAMA = new URL("../templates/first-app/", import.meta.url);
|