@defuy/micro-cli 1.0.0 → 1.0.2

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/README.md CHANGED
@@ -5,7 +5,7 @@
5
5
  ## 安装
6
6
 
7
7
  ```bash
8
- npm install -g @micro/cli
8
+ npm install -g @defuy/micro-cli
9
9
  ```
10
10
 
11
11
  ## 使用
package/lib/build.js CHANGED
@@ -1,11 +1,9 @@
1
1
  import { execSync } from 'child_process';
2
2
  import fs from 'fs';
3
3
  import path from 'path';
4
- import { fileURLToPath } from 'url';
5
4
  import chalk from 'chalk';
6
5
  import ora from 'ora';
7
-
8
- const __dirname = path.dirname(fileURLToPath(import.meta.url));
6
+ import { getRepoPaths } from './paths.js';
9
7
 
10
8
  // 后处理:修复全局变量引用
11
9
  function fixGlobals(distDir) {
@@ -30,8 +28,7 @@ function fixGlobals(distDir) {
30
28
  }
31
29
 
32
30
  export async function build(appName) {
33
- const packagesDir = path.resolve(__dirname, '../../');
34
- const distDir = path.resolve(packagesDir, '../dist');
31
+ const { packagesDir, distDir } = getRepoPaths();
35
32
 
36
33
  // 获取要打包的应用列表
37
34
  let apps = [];
package/lib/create.js CHANGED
@@ -1,12 +1,10 @@
1
1
  import fs from 'fs';
2
2
  import path from 'path';
3
- import { fileURLToPath } from 'url';
4
3
  import chalk from 'chalk';
5
4
  import ora from 'ora';
6
5
  import inquirer from 'inquirer';
7
6
  import { getNextPort, PORT_MAP } from './config.js';
8
-
9
- const __dirname = path.dirname(fileURLToPath(import.meta.url));
7
+ import { getRepoPaths } from './paths.js';
10
8
 
11
9
  export async function createApp(name, options = {}) {
12
10
  // 确保名称格式正确
@@ -14,7 +12,7 @@ export async function createApp(name, options = {}) {
14
12
  const appName = appId.replace('app-', '');
15
13
 
16
14
  // 获取 packages 目录
17
- const packagesDir = path.resolve(__dirname, '../../');
15
+ const { packagesDir } = getRepoPaths();
18
16
  const appDir = path.join(packagesDir, appId);
19
17
 
20
18
  // 检查是否已存在
package/lib/deploy.js CHANGED
@@ -1,23 +1,15 @@
1
1
  import { execSync } from 'child_process';
2
2
  import fs from 'fs';
3
3
  import path from 'path';
4
- import { fileURLToPath } from 'url';
5
4
  import chalk from 'chalk';
6
5
  import ora from 'ora';
7
6
  import { NodeSSH } from 'node-ssh';
8
7
  import { SERVER_CONFIG } from './config.js';
9
-
10
- const __dirname = path.dirname(fileURLToPath(import.meta.url));
8
+ import { getRepoPaths } from './paths.js';
11
9
 
12
10
  // 获取项目路径
13
11
  function getPaths() {
14
- const cliDir = path.resolve(__dirname, '..');
15
- const packagesDir = path.resolve(cliDir, '..');
16
- const microAppsDir = path.resolve(packagesDir, '..');
17
- const distDir = path.resolve(microAppsDir, 'dist');
18
- const hostDir = path.resolve(microAppsDir, '..', 'host');
19
-
20
- return { cliDir, packagesDir, microAppsDir, distDir, hostDir };
12
+ return getRepoPaths();
21
13
  }
22
14
 
23
15
  // 连接服务器
package/lib/paths.js ADDED
@@ -0,0 +1,47 @@
1
+ import fs from 'fs';
2
+ import path from 'path';
3
+ import { fileURLToPath } from 'url';
4
+
5
+ const __dirname = path.dirname(fileURLToPath(import.meta.url));
6
+
7
+ function looksLikeMicroAppsRoot(dir) {
8
+ try {
9
+ const workspaceYaml = path.join(dir, 'pnpm-workspace.yaml');
10
+ const packagesDir = path.join(dir, 'packages');
11
+ const cliPkg = path.join(packagesDir, 'cli', 'package.json');
12
+ return fs.existsSync(workspaceYaml) && fs.existsSync(packagesDir) && fs.existsSync(cliPkg);
13
+ } catch {
14
+ return false;
15
+ }
16
+ }
17
+
18
+ function findMicroAppsRoot(startDir) {
19
+ let current = path.resolve(startDir);
20
+ while (true) {
21
+ if (looksLikeMicroAppsRoot(current)) return current;
22
+ const parent = path.dirname(current);
23
+ if (parent === current) return null;
24
+ current = parent;
25
+ }
26
+ }
27
+
28
+ export function getMicroAppsRoot() {
29
+ // Prefer project cwd so global-installed CLI works in repo
30
+ const fromCwd = findMicroAppsRoot(process.cwd());
31
+ if (fromCwd) return fromCwd;
32
+
33
+ // Fallback: relative to where this file lives (works in workspace install)
34
+ // lib -> cli -> packages -> micro-apps
35
+ const fromInstallDir = path.resolve(__dirname, '..', '..', '..');
36
+ return fromInstallDir;
37
+ }
38
+
39
+ export function getRepoPaths() {
40
+ const microAppsDir = getMicroAppsRoot();
41
+ const packagesDir = path.join(microAppsDir, 'packages');
42
+ const distDir = path.join(microAppsDir, 'dist');
43
+ const hostDir = path.resolve(microAppsDir, '..', 'host');
44
+ const cliDir = path.join(packagesDir, 'cli');
45
+ return { microAppsDir, packagesDir, distDir, hostDir, cliDir };
46
+ }
47
+
package/package.json CHANGED
@@ -1,11 +1,11 @@
1
1
  {
2
2
  "name": "@defuy/micro-cli",
3
- "version": "1.0.0",
3
+ "version": "1.0.2",
4
4
  "description": "微前端脚手架工具",
5
5
  "type": "module",
6
6
  "main": "bin/micro.js",
7
7
  "bin": {
8
- "mf": "./bin/micro.js"
8
+ "mf": "bin/micro.js"
9
9
  },
10
10
  "files": [
11
11
  "bin",