moontraze 1.0.10 → 2.0.0
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/bin/moontraze.js +14 -6
- package/lib/auth.js +11 -12
- package/lib/config.js +34 -2
- package/lib/paths.js +25 -0
- package/lib/selfUpdate.js +53 -18
- package/package.json +1 -1
package/bin/moontraze.js
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
const { Command } = require('commander');
|
|
3
3
|
const chalk = require('chalk');
|
|
4
|
+
const path = require('path');
|
|
4
5
|
const { login } = require('../lib/auth');
|
|
5
|
-
const { getConfig, setConfig, getProject, setProject } = require('../lib/config');
|
|
6
|
+
const { getConfig, setConfig, getProject, setProject, GLOBAL_FILE } = require('../lib/config');
|
|
6
7
|
const { deploy } = require('../lib/deploy');
|
|
7
|
-
const { selfUpdate } = require('../lib/selfUpdate');
|
|
8
|
+
const { selfUpdate, INSTALL_DIR } = require('../lib/selfUpdate');
|
|
8
9
|
|
|
9
10
|
const program = new Command();
|
|
10
11
|
|
|
@@ -13,10 +14,16 @@ const platformName = process.env.MOON_PLATFORM || 'moontraze';
|
|
|
13
14
|
const apiUrl = process.env.MOON_API || getConfig().apiUrl || 'https://api.moontraze.com';
|
|
14
15
|
const domain = process.env.MOON_DOMAIN || getConfig().domain || 'moontraze.com';
|
|
15
16
|
|
|
17
|
+
// version package.json se (update ke baad naya version dikhe)
|
|
18
|
+
let version = '1.0.6';
|
|
19
|
+
try {
|
|
20
|
+
version = require('../package.json').version || version;
|
|
21
|
+
} catch (_) {}
|
|
22
|
+
|
|
16
23
|
program
|
|
17
24
|
.name(platformName)
|
|
18
25
|
.description(`Deploy sites using ${platformName}`)
|
|
19
|
-
.version(
|
|
26
|
+
.version(version);
|
|
20
27
|
|
|
21
28
|
// ---------- login ----------
|
|
22
29
|
program
|
|
@@ -26,7 +33,6 @@ program
|
|
|
26
33
|
.action(async (opts) => {
|
|
27
34
|
try {
|
|
28
35
|
await login(opts.token, { apiUrl });
|
|
29
|
-
// auth.js already prints ✓ Logged in — avoid double line if you want
|
|
30
36
|
} catch (e) {
|
|
31
37
|
console.error(chalk.red('Login failed:'), e.message);
|
|
32
38
|
process.exit(1);
|
|
@@ -73,6 +79,7 @@ program
|
|
|
73
79
|
const proj = getProject();
|
|
74
80
|
console.log('');
|
|
75
81
|
console.log(chalk.bold('Moontraze'));
|
|
82
|
+
console.log(` Version: ${version}`);
|
|
76
83
|
console.log(` API: ${cfg.apiUrl || apiUrl}`);
|
|
77
84
|
console.log(` Domain: ${cfg.domain || domain}`);
|
|
78
85
|
console.log(` Logged in: ${cfg.token ? 'yes' : 'no'}`);
|
|
@@ -81,10 +88,12 @@ program
|
|
|
81
88
|
proj?.projectName || '(none — run: moon moontraze link <name>)'
|
|
82
89
|
}`
|
|
83
90
|
);
|
|
91
|
+
console.log(chalk.gray(` Config: ${GLOBAL_FILE}`));
|
|
92
|
+
console.log(chalk.gray(` Install: ${INSTALL_DIR}`));
|
|
84
93
|
console.log('');
|
|
85
94
|
});
|
|
86
95
|
|
|
87
|
-
// ---------- update
|
|
96
|
+
// ---------- update ----------
|
|
88
97
|
program
|
|
89
98
|
.command('update')
|
|
90
99
|
.description('Download latest Moontraze CLI from api.moontraze.com')
|
|
@@ -143,7 +152,6 @@ Examples:
|
|
|
143
152
|
moon ${platformName} deploy --prod
|
|
144
153
|
moon ${platformName} update
|
|
145
154
|
moon ${platformName} --prod
|
|
146
|
-
|
|
147
155
|
npx moontraze login
|
|
148
156
|
npx moontraze deploy --prod
|
|
149
157
|
`
|
package/lib/auth.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
const prompts = require('prompts');
|
|
2
2
|
const chalk = require('chalk');
|
|
3
|
-
const { getConfig, setConfig } = require('./config');
|
|
3
|
+
const { getConfig, setConfig, GLOBAL_FILE } = require('./config');
|
|
4
4
|
|
|
5
5
|
async function login(tokenArg, options = {}) {
|
|
6
6
|
const apiUrl =
|
|
@@ -72,10 +72,11 @@ async function loginWithEmail({ apiUrl, label }) {
|
|
|
72
72
|
if (!data.token) throw new Error('Login succeeded but no token returned');
|
|
73
73
|
|
|
74
74
|
setConfig({ token: data.token, apiUrl });
|
|
75
|
+
|
|
75
76
|
console.log('');
|
|
76
77
|
console.log(chalk.green('✓ Logged in'));
|
|
77
78
|
if (data.user?.email) console.log(chalk.gray(` ${data.user.email}`));
|
|
78
|
-
console.log(chalk.gray(` Config:
|
|
79
|
+
console.log(chalk.gray(` Config: ${GLOBAL_FILE}`));
|
|
79
80
|
console.log('');
|
|
80
81
|
}
|
|
81
82
|
|
|
@@ -84,23 +85,21 @@ async function loginWithMoonID({ apiUrl, label }) {
|
|
|
84
85
|
const nonce = Math.random().toString(36).substring(2, 12);
|
|
85
86
|
const state = `${nonce}.login`;
|
|
86
87
|
|
|
87
|
-
// ★ Same flow as website (working)
|
|
88
88
|
const startRes = await fetch(`${apiUrl}/api/platform/moonid/web-start`, {
|
|
89
89
|
method: 'POST',
|
|
90
90
|
headers: { 'Content-Type': 'application/json' },
|
|
91
91
|
body: JSON.stringify({ state, action: 'login' }),
|
|
92
92
|
});
|
|
93
|
+
|
|
93
94
|
const startData = await startRes.json().catch(() => ({}));
|
|
94
95
|
if (!startRes.ok) {
|
|
95
96
|
throw new Error(startData.error || 'Could not start MoonID session');
|
|
96
97
|
}
|
|
97
98
|
|
|
98
99
|
const notifyUrl = `${apiUrl}/api/platform/moonid/web-complete`;
|
|
99
|
-
const redirectUri = `${apiUrl}/api/platform/moonid/done?state=${encodeURIComponent(
|
|
100
|
-
state
|
|
101
|
-
)}`;
|
|
102
|
-
|
|
100
|
+
const redirectUri = `${apiUrl}/api/platform/moonid/done?state=${encodeURIComponent(state)}`;
|
|
103
101
|
const appId = 'com.moonstorage.app';
|
|
102
|
+
|
|
104
103
|
const deepLink =
|
|
105
104
|
`moonid://auth` +
|
|
106
105
|
`?app_id=${encodeURIComponent(appId)}` +
|
|
@@ -110,9 +109,7 @@ async function loginWithMoonID({ apiUrl, label }) {
|
|
|
110
109
|
`&state=${encodeURIComponent(state)}` +
|
|
111
110
|
`&source=cli`;
|
|
112
111
|
|
|
113
|
-
const qrImageUrl = `https://api.qrserver.com/v1/create-qr-code/?size=220x220&data=${encodeURIComponent(
|
|
114
|
-
deepLink
|
|
115
|
-
)}`;
|
|
112
|
+
const qrImageUrl = `https://api.qrserver.com/v1/create-qr-code/?size=220x220&data=${encodeURIComponent(deepLink)}`;
|
|
116
113
|
|
|
117
114
|
console.log('');
|
|
118
115
|
console.log(chalk.cyan(`${label} — MoonID Login`));
|
|
@@ -145,7 +142,6 @@ async function pollWebStatus({ apiUrl, state, timeoutMs }) {
|
|
|
145
142
|
);
|
|
146
143
|
const data = await res.json().catch(() => ({}));
|
|
147
144
|
|
|
148
|
-
// web endpoint returns status: "success"
|
|
149
145
|
if (
|
|
150
146
|
(data.status === 'success' || data.status === 'done') &&
|
|
151
147
|
data.token
|
|
@@ -172,13 +168,16 @@ async function saveAndValidateToken(token, { apiUrl, label }) {
|
|
|
172
168
|
const res = await fetch(`${apiUrl}/api/hosting/quota`, {
|
|
173
169
|
headers: { Authorization: `Bearer ${token}` },
|
|
174
170
|
});
|
|
171
|
+
|
|
175
172
|
if (res.status === 401) {
|
|
176
173
|
throw new Error('Invalid or expired token');
|
|
177
174
|
}
|
|
175
|
+
|
|
178
176
|
setConfig({ token, apiUrl });
|
|
177
|
+
|
|
179
178
|
console.log('');
|
|
180
179
|
console.log(chalk.green('✓ Logged in'));
|
|
181
|
-
console.log(chalk.gray(` Config:
|
|
180
|
+
console.log(chalk.gray(` Config: ${GLOBAL_FILE}`));
|
|
182
181
|
console.log('');
|
|
183
182
|
}
|
|
184
183
|
|
package/lib/config.js
CHANGED
|
@@ -1,12 +1,18 @@
|
|
|
1
1
|
const fs = require('fs');
|
|
2
2
|
const path = require('path');
|
|
3
3
|
const os = require('os');
|
|
4
|
+
const { MOONTRAZE_HOME } = require('./paths');
|
|
4
5
|
|
|
5
|
-
const GLOBAL_DIR =
|
|
6
|
+
const GLOBAL_DIR = MOONTRAZE_HOME;
|
|
6
7
|
const GLOBAL_FILE = path.join(GLOBAL_DIR, 'config.json');
|
|
8
|
+
|
|
7
9
|
const LOCAL_DIR = path.join(process.cwd(), '.moontraze');
|
|
8
10
|
const LOCAL_FILE = path.join(LOCAL_DIR, 'project.json');
|
|
9
11
|
|
|
12
|
+
/** Purana location (migration) */
|
|
13
|
+
const OLD_GLOBAL_DIR = path.join(os.homedir(), '.moontraze');
|
|
14
|
+
const OLD_GLOBAL_FILE = path.join(OLD_GLOBAL_DIR, 'config.json');
|
|
15
|
+
|
|
10
16
|
function ensureDir(d) {
|
|
11
17
|
if (!fs.existsSync(d)) fs.mkdirSync(d, { recursive: true });
|
|
12
18
|
}
|
|
@@ -17,7 +23,22 @@ const DEFAULTS = {
|
|
|
17
23
|
domain: process.env.MOON_DOMAIN || 'moontraze.com',
|
|
18
24
|
};
|
|
19
25
|
|
|
26
|
+
/**
|
|
27
|
+
* Purane ~/.moontraze/config.json se naya location pe migrate
|
|
28
|
+
* (sirf tab jab naya config pehle se exist na karta ho)
|
|
29
|
+
*/
|
|
30
|
+
function migrateOldConfig() {
|
|
31
|
+
if (fs.existsSync(GLOBAL_FILE)) return;
|
|
32
|
+
if (!fs.existsSync(OLD_GLOBAL_FILE)) return;
|
|
33
|
+
|
|
34
|
+
try {
|
|
35
|
+
ensureDir(GLOBAL_DIR);
|
|
36
|
+
fs.copyFileSync(OLD_GLOBAL_FILE, GLOBAL_FILE);
|
|
37
|
+
} catch (_) {}
|
|
38
|
+
}
|
|
39
|
+
|
|
20
40
|
function getConfig() {
|
|
41
|
+
migrateOldConfig();
|
|
21
42
|
try {
|
|
22
43
|
if (fs.existsSync(GLOBAL_FILE)) {
|
|
23
44
|
return { ...DEFAULTS, ...JSON.parse(fs.readFileSync(GLOBAL_FILE, 'utf8')) };
|
|
@@ -45,6 +66,7 @@ function getProject() {
|
|
|
45
66
|
function setProject(data) {
|
|
46
67
|
ensureDir(LOCAL_DIR);
|
|
47
68
|
fs.writeFileSync(LOCAL_FILE, JSON.stringify(data, null, 2));
|
|
69
|
+
|
|
48
70
|
const ignore = path.join(process.cwd(), '.gitignore');
|
|
49
71
|
try {
|
|
50
72
|
let g = fs.existsSync(ignore) ? fs.readFileSync(ignore, 'utf8') : '';
|
|
@@ -52,7 +74,17 @@ function setProject(data) {
|
|
|
52
74
|
fs.appendFileSync(ignore, '\n.moontraze/\n');
|
|
53
75
|
}
|
|
54
76
|
} catch (_) {}
|
|
77
|
+
|
|
55
78
|
return data;
|
|
56
79
|
}
|
|
57
80
|
|
|
58
|
-
module.exports = {
|
|
81
|
+
module.exports = {
|
|
82
|
+
getConfig,
|
|
83
|
+
setConfig,
|
|
84
|
+
getProject,
|
|
85
|
+
setProject,
|
|
86
|
+
GLOBAL_DIR,
|
|
87
|
+
GLOBAL_FILE,
|
|
88
|
+
LOCAL_DIR,
|
|
89
|
+
LOCAL_FILE,
|
|
90
|
+
};
|
package/lib/paths.js
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
const path = require('path');
|
|
2
|
+
const os = require('os');
|
|
3
|
+
|
|
4
|
+
function dataHome() {
|
|
5
|
+
// Windows: AppData\Local
|
|
6
|
+
if (process.platform === 'win32') {
|
|
7
|
+
return process.env.LOCALAPPDATA || path.join(os.homedir(), 'AppData', 'Local');
|
|
8
|
+
}
|
|
9
|
+
// Linux / mac
|
|
10
|
+
return (
|
|
11
|
+
process.env.XDG_DATA_HOME ||
|
|
12
|
+
path.join(os.homedir(), '.local', 'share')
|
|
13
|
+
);
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
const MOON_HOME = path.join(dataHome(), 'Moon'); // global moon CLI
|
|
17
|
+
const MOONTRAZE_HOME = path.join(dataHome(), 'Moontraze'); // is platform ka data
|
|
18
|
+
const MOONTRAZE_CLI = path.join(MOONTRAZE_HOME, 'cli');
|
|
19
|
+
|
|
20
|
+
module.exports = {
|
|
21
|
+
dataHome,
|
|
22
|
+
MOON_HOME,
|
|
23
|
+
MOONTRAZE_HOME,
|
|
24
|
+
MOONTRAZE_CLI,
|
|
25
|
+
};
|
package/lib/selfUpdate.js
CHANGED
|
@@ -2,18 +2,30 @@ const fs = require('fs');
|
|
|
2
2
|
const path = require('path');
|
|
3
3
|
const os = require('os');
|
|
4
4
|
const { execSync } = require('child_process');
|
|
5
|
-
const fetch = require('node-fetch');
|
|
6
5
|
const chalk = require('chalk');
|
|
6
|
+
const { MOON_HOME, MOONTRAZE_CLI } = require('./paths');
|
|
7
|
+
|
|
8
|
+
// Node 18+ global fetch; fallback node-fetch
|
|
9
|
+
const fetchFn = global.fetch
|
|
10
|
+
? global.fetch.bind(global)
|
|
11
|
+
: (...args) => require('node-fetch')(...args);
|
|
7
12
|
|
|
8
13
|
const API = process.env.MOON_API || 'https://api.moontraze.com';
|
|
9
14
|
|
|
10
|
-
|
|
11
|
-
const INSTALL_DIR =
|
|
12
|
-
|
|
15
|
+
/** Moontraze CLI install folder */
|
|
16
|
+
const INSTALL_DIR = MOONTRAZE_CLI;
|
|
17
|
+
|
|
18
|
+
/** Global Moon config (platforms yahan store hote hain) */
|
|
19
|
+
const MOON_CFG = path.join(MOON_HOME, 'config.json');
|
|
20
|
+
|
|
21
|
+
/** Purane locations (migration) */
|
|
22
|
+
const OLD_INSTALL_DIR = path.join(os.homedir(), '.moontraze', 'cli');
|
|
23
|
+
const OLD_MOON_CFG = path.join(os.homedir(), '.moon', 'config.json');
|
|
13
24
|
|
|
14
|
-
// ★ Sirf yahan change — moontraze ka apna manifest
|
|
15
25
|
async function fetchManifest() {
|
|
16
|
-
const res = await
|
|
26
|
+
const res = await fetchFn(`${API}/cli/moontraze-manifest.json`, {
|
|
27
|
+
headers: { Accept: 'application/json' },
|
|
28
|
+
});
|
|
17
29
|
if (!res.ok) {
|
|
18
30
|
throw new Error(`Could not fetch moontraze manifest (${res.status})`);
|
|
19
31
|
}
|
|
@@ -21,21 +33,36 @@ async function fetchManifest() {
|
|
|
21
33
|
}
|
|
22
34
|
|
|
23
35
|
async function downloadTo(url, dest) {
|
|
24
|
-
const res = await
|
|
36
|
+
const res = await fetchFn(url);
|
|
25
37
|
if (!res.ok) throw new Error('Download failed: ' + url);
|
|
26
38
|
const buf = Buffer.from(await res.arrayBuffer());
|
|
27
39
|
fs.writeFileSync(dest, buf);
|
|
28
40
|
}
|
|
29
41
|
|
|
42
|
+
/**
|
|
43
|
+
* Purane ~/.moontraze/cli se kuch bhi nahi laana — pure extract se naya code aata hai.
|
|
44
|
+
* Sirf global moon config migrate karo agar naya location pe config nahi hai.
|
|
45
|
+
*/
|
|
46
|
+
function migrateOldMoonConfig() {
|
|
47
|
+
if (fs.existsSync(MOON_CFG)) return; // naya already hai
|
|
48
|
+
if (!fs.existsSync(OLD_MOON_CFG)) return;
|
|
49
|
+
|
|
50
|
+
try {
|
|
51
|
+
fs.mkdirSync(MOON_HOME, { recursive: true });
|
|
52
|
+
fs.copyFileSync(OLD_MOON_CFG, MOON_CFG);
|
|
53
|
+
console.log(chalk.gray(`Migrated moon config: ~/.moon → ${MOON_HOME}`));
|
|
54
|
+
} catch (err) {
|
|
55
|
+
console.log(chalk.yellow('Could not migrate old moon config:'), err.message);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
30
59
|
function extractTarball(tgz, destDir) {
|
|
31
60
|
const tmp = path.join(os.tmpdir(), 'moontraze-extract-' + Date.now());
|
|
32
61
|
fs.mkdirSync(tmp, { recursive: true });
|
|
33
|
-
|
|
34
62
|
execSync(`tar -xzf "${tgz}" -C "${tmp}"`, { stdio: 'inherit', shell: true });
|
|
35
63
|
|
|
36
|
-
const entries = fs.readdirSync(tmp).map(n => path.join(tmp, n));
|
|
37
|
-
const pkg = entries.find(p => fs.statSync(p).isDirectory());
|
|
38
|
-
|
|
64
|
+
const entries = fs.readdirSync(tmp).map((n) => path.join(tmp, n));
|
|
65
|
+
const pkg = entries.find((p) => fs.statSync(p).isDirectory());
|
|
39
66
|
if (!pkg) {
|
|
40
67
|
console.error('Extracted contents:', entries);
|
|
41
68
|
throw new Error('Invalid tarball layout (expected package/ directory)');
|
|
@@ -46,7 +73,6 @@ function extractTarball(tgz, destDir) {
|
|
|
46
73
|
}
|
|
47
74
|
fs.mkdirSync(path.dirname(destDir), { recursive: true });
|
|
48
75
|
fs.renameSync(pkg, destDir);
|
|
49
|
-
|
|
50
76
|
fs.rmSync(tmp, { recursive: true, force: true });
|
|
51
77
|
}
|
|
52
78
|
|
|
@@ -72,7 +98,6 @@ function pointMoonPlatformToInstall() {
|
|
|
72
98
|
}
|
|
73
99
|
|
|
74
100
|
const runner = path.join(INSTALL_DIR, runnerRel);
|
|
75
|
-
|
|
76
101
|
if (!fs.existsSync(runner)) {
|
|
77
102
|
console.error(chalk.red('\n--- Debug: files after extract ---'));
|
|
78
103
|
console.error('INSTALL_DIR:', INSTALL_DIR);
|
|
@@ -117,12 +142,16 @@ function pointMoonPlatformToInstall() {
|
|
|
117
142
|
async function selfUpdate() {
|
|
118
143
|
console.log(chalk.cyan('Checking Moontraze CLI updates...'));
|
|
119
144
|
|
|
145
|
+
// pehle purane moon config migrate kar do
|
|
146
|
+
migrateOldMoonConfig();
|
|
147
|
+
|
|
120
148
|
const manifest = await fetchManifest();
|
|
121
149
|
const ver = manifest.version;
|
|
122
150
|
const url = manifest.tarball;
|
|
123
151
|
|
|
124
152
|
console.log(chalk.gray(` Latest: v${ver}`));
|
|
125
153
|
console.log(chalk.gray(` ${url}`));
|
|
154
|
+
console.log(chalk.gray(` Install dir: ${INSTALL_DIR}`));
|
|
126
155
|
|
|
127
156
|
const tgz = path.join(os.tmpdir(), `moontraze-${ver}.tgz`);
|
|
128
157
|
await downloadTo(url, tgz);
|
|
@@ -139,14 +168,20 @@ async function selfUpdate() {
|
|
|
139
168
|
|
|
140
169
|
const runner = pointMoonPlatformToInstall();
|
|
141
170
|
|
|
142
|
-
try {
|
|
171
|
+
try {
|
|
172
|
+
fs.unlinkSync(tgz);
|
|
173
|
+
} catch (_) {}
|
|
143
174
|
|
|
144
175
|
console.log('');
|
|
145
176
|
console.log(chalk.green(`✓ Moontraze CLI v${ver} installed`));
|
|
146
|
-
console.log(chalk.gray(`
|
|
147
|
-
console.log(chalk.gray(`
|
|
148
|
-
console.log(chalk.gray('
|
|
177
|
+
console.log(chalk.gray(` ${INSTALL_DIR}`));
|
|
178
|
+
console.log(chalk.gray(` Runner: ${runner}`));
|
|
179
|
+
console.log(chalk.gray(' Try: moon moontraze list'));
|
|
149
180
|
console.log('');
|
|
150
181
|
}
|
|
151
182
|
|
|
152
|
-
module.exports = {
|
|
183
|
+
module.exports = {
|
|
184
|
+
selfUpdate,
|
|
185
|
+
INSTALL_DIR,
|
|
186
|
+
MOON_CFG,
|
|
187
|
+
};
|