@lazycatcloud/lzc-cli 1.1.8 → 1.1.9

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.
Files changed (105) hide show
  1. package/README.md +69 -11
  2. package/lib/api.js +71 -39
  3. package/lib/app/index.js +76 -21
  4. package/lib/app/lpk_build.js +95 -63
  5. package/lib/app/lpk_create.js +63 -41
  6. package/lib/app/lpk_create_generator.js +202 -0
  7. package/lib/app/lpk_devshell.js +393 -328
  8. package/lib/app/lpk_devshell_docker.js +211 -0
  9. package/lib/app/lpk_installer.js +63 -26
  10. package/lib/app/lpk_log.js +68 -0
  11. package/lib/app/lpk_status.js +18 -0
  12. package/lib/app/lpk_uninstall.js +19 -0
  13. package/lib/appstore/index.js +37 -0
  14. package/lib/appstore/login.js +96 -93
  15. package/lib/appstore/publish.js +62 -0
  16. package/lib/autologin.js +0 -78
  17. package/lib/box/api/clientapi.js +1322 -0
  18. package/lib/box/api/empty.js +35 -0
  19. package/lib/box/check_qemu.js +1 -0
  20. package/lib/box/index.js +41 -94
  21. package/lib/box/qemu_vm_mgr.js +208 -239
  22. package/lib/box/schemes/vm_box_system_debian.json +1 -1
  23. package/lib/docker-compose.js +1 -2
  24. package/lib/env.js +19 -101
  25. package/lib/key.js +1 -0
  26. package/lib/sdk.js +10 -25
  27. package/lib/utils.js +156 -132
  28. package/package.json +19 -10
  29. package/scripts/cli.js +14 -135
  30. package/template/_lpk/README.md +31 -0
  31. package/template/_lpk/exec.sh +19 -0
  32. package/template/_lpk/golang.manifest.yml.in +16 -0
  33. package/template/_lpk/lazycat.png +0 -0
  34. package/template/_lpk/lite.manifest.yml.in +19 -0
  35. package/template/_lpk/local_devshell/Dockerfile +16 -0
  36. package/template/_lpk/local_devshell/build.sh +5 -0
  37. package/template/_lpk/local_devshell/entrypoint.sh +87 -0
  38. package/template/{_lazycat/debug/shell → _lpk/local_devshell}/sshd_config +8 -8
  39. package/template/_lpk/manifest.yml.in +0 -1
  40. package/template/{vue/lzc-build.yml → _lpk/vue.lzc-build.yml.in} +9 -1
  41. package/template/golang/README.md +0 -2
  42. package/template/golang/_gitignore +2 -0
  43. package/template/golang/build.sh +6 -0
  44. package/template/golang/lazycat.png +0 -0
  45. package/template/golang/lzc-build.yml +9 -1
  46. package/template/golang/rego.go +15 -16
  47. package/template/golang/rego_test.go +13 -0
  48. package/template/ionic_vue3/lazycat.png +0 -0
  49. package/template/ionic_vue3/lzc-build.yml +9 -1
  50. package/template/lite/error_pages/502.html.tpl +13 -0
  51. package/template/lite/lazycat.png +0 -0
  52. package/template/lite/lzc-build.yml +60 -0
  53. package/cmds/app.js +0 -133
  54. package/cmds/config.js +0 -55
  55. package/cmds/create.js +0 -55
  56. package/cmds/dev.js +0 -130
  57. package/cmds/init.js +0 -125
  58. package/cmds/log.js +0 -103
  59. package/cmds/publish.js +0 -116
  60. package/lib/archiver.js +0 -105
  61. package/lib/box/hportal.js +0 -120
  62. package/lib/builder.js +0 -313
  63. package/lib/dev.js +0 -314
  64. package/lib/generator.js +0 -146
  65. package/template/_lazycat/_gitignore +0 -1
  66. package/template/_lazycat/app-config +0 -1
  67. package/template/_lazycat/debug/devforward/50x.html +0 -30
  68. package/template/_lazycat/debug/devforward/Dockerfile +0 -16
  69. package/template/_lazycat/debug/devforward/docker-compose.override.yml.in +0 -11
  70. package/template/_lazycat/debug/devforward/entrypoint.sh +0 -10
  71. package/template/_lazycat/debug/devforward/nginx.conf.template +0 -56
  72. package/template/_lazycat/debug/devforward/sshd_config +0 -116
  73. package/template/_lazycat/debug/shell/50x.html +0 -32
  74. package/template/_lazycat/debug/shell/Dockerfile +0 -18
  75. package/template/_lazycat/debug/shell/build.sh +0 -15
  76. package/template/_lazycat/debug/shell/docker-compose.override.yml.in +0 -13
  77. package/template/_lazycat/debug/shell/entrypoint.sh +0 -12
  78. package/template/_lazycat/docker-compose.yml.in +0 -15
  79. package/template/_lazycat/icon.svg +0 -1
  80. package/template/_lazycat/screenshot.png +0 -0
  81. package/template/_lpk/sync/Dockerfile +0 -16
  82. package/template/_lpk/sync/build.sh +0 -5
  83. package/template/_lpk/sync/entrypoint.sh +0 -8
  84. package/template/_lpk/sync/sshd_config +0 -117
  85. package/template/_lpk/sync.manifest.yml.in +0 -3
  86. package/template/release/golang/Dockerfile +0 -18
  87. package/template/release/golang/build.sh +0 -13
  88. package/template/release/ionic_vue3/Dockerfile +0 -10
  89. package/template/release/ionic_vue3/build.sh +0 -7
  90. package/template/release/ionic_vue3/docker-compose.yml.in +0 -3
  91. package/template/release/vue/Dockerfile +0 -10
  92. package/template/release/vue/build.sh +0 -10
  93. package/template/release/vue/docker-compose.yml.in +0 -3
  94. package/template/vue/README.md +0 -29
  95. package/template/vue/_dockerignore +0 -1
  96. package/template/vue/babel.config.js +0 -3
  97. package/template/vue/package.json +0 -43
  98. package/template/vue/public/favicon.ico +0 -0
  99. package/template/vue/public/index.html +0 -33
  100. package/template/vue/src/App.vue +0 -39
  101. package/template/vue/src/main.js +0 -8
  102. package/template/vue/src/todo.vue +0 -640
  103. package/template/vue/src/top-bar.vue +0 -100
  104. package/template/vue/src/webdav.vue +0 -183
  105. package/template/vue/vue.config.js +0 -21
@@ -2,18 +2,17 @@ import logger from "loglevel";
2
2
  import inquirer from "inquirer";
3
3
  import {
4
4
  contextDirname,
5
- createTemplateFile,
6
5
  loadFromYaml,
7
6
  envTemplateFile,
8
7
  ensureDir,
9
8
  dumpToYaml,
9
+ isFileExist,
10
10
  } from "../utils.js";
11
11
  import path from "node:path";
12
- import Generator, { TemplateConfig } from "../generator.js";
13
- import fs from "fs";
12
+ import { TemplateConfig } from "./lpk_create_generator.js";
13
+ import fs from "node:fs";
14
14
  import yaml from "js-yaml";
15
15
  let fsPromises = fs.promises;
16
- import { parse2CorrectName } from "../utils.js";
17
16
 
18
17
  export class LpkManifest {
19
18
  constructor(defaultAppID) {
@@ -22,23 +21,30 @@ export class LpkManifest {
22
21
  this.defaultAppID = defaultAppID;
23
22
  }
24
23
 
25
- async init(manifestFilePath) {
26
- if (manifestFilePath) {
27
- this.manifest = loadFromYaml(manifestFilePath);
28
- } else {
29
- let templatePath = path.join(
24
+ async init(manifestFilePath, askInfo = false) {
25
+ if (!manifestFilePath) {
26
+ manifestFilePath = path.join(
30
27
  this.pwd,
31
28
  "../../template/_lpk/manifest.yml.in"
32
29
  );
30
+ }
31
+
32
+ if (askInfo) {
33
33
  let answer = await this.askLpkInfo();
34
- this.manifest = yaml.load(await envTemplateFile(templatePath, answer));
34
+ this.manifest = yaml.load(
35
+ await envTemplateFile(manifestFilePath, answer)
36
+ );
37
+ } else {
38
+ this.manifest = loadFromYaml(manifestFilePath);
35
39
  }
36
40
  }
37
41
 
38
42
  async create(outputDir) {
39
- let outputFilePath = path.join(outputDir, "manifest.yml");
40
- logger.info(`创建文件 ${outputFilePath}`);
43
+ let outputFilePath = path.join(outputDir, "lzc-manifest.yml");
44
+ logger.debug(`创建文件 ${outputFilePath}`);
45
+
41
46
  ensureDir(outputFilePath);
47
+
42
48
  return fsPromises
43
49
  .access(outputFilePath, fs.constants.F_OK | fs.constants.W_OK)
44
50
  .then(
@@ -48,7 +54,7 @@ export class LpkManifest {
48
54
  name: "override",
49
55
  type: "input",
50
56
  default: "n",
51
- message: "manifest.yml已经存在,是否覆盖(y/n): ",
57
+ message: "lzc-manifest.yml已经存在,是否覆盖(y/n): ",
52
58
  },
53
59
  ];
54
60
  const answers = await inquirer.prompt(questions);
@@ -123,18 +129,29 @@ export class LpkManifest {
123
129
  }
124
130
 
125
131
  export class TemplateInit {
126
- constructor(context) {
127
- this.context = context;
128
- this.type = context.type;
132
+ constructor(cwd, name, type) {
133
+ this.cwd = cwd;
134
+ this.name = name;
135
+ this.type = type;
136
+ this.lpkManifest = new LpkManifest(name);
129
137
  }
130
138
 
131
- async create() {
139
+ async init() {
132
140
  if (!this.type) {
133
141
  this.type = await chooseTemplate();
134
142
  }
135
- let lpkManifest = new LpkManifest(path.basename(this.context.cwd));
136
- await lpkManifest.init();
137
- await lpkManifest.create(this.context.cwd);
143
+
144
+ // 优先使用模板项目自定义的模板文件
145
+ const typeTemplatePath = path.join(
146
+ contextDirname(import.meta.url),
147
+ `../../template/_lpk/${this.type}.manifest.yml.in`
148
+ );
149
+ const templatePath = isFileExist(typeTemplatePath) ? typeTemplatePath : "";
150
+ await this.lpkManifest.init(templatePath, true);
151
+ }
152
+
153
+ async create() {
154
+ await this.lpkManifest.create(path.resolve(this.cwd, this.name));
138
155
  }
139
156
  }
140
157
 
@@ -145,41 +162,44 @@ export async function chooseTemplate() {
145
162
  name: "type",
146
163
  message: "选择项目构建模板",
147
164
  type: "list",
148
- choices: ["vue", "golang", "ionic_vue3"],
165
+ choices: ["lite", "vue3", "vue2", "golang", "ionic_vue3"],
149
166
  },
150
167
  ])
151
168
  )["type"];
152
169
  }
153
170
 
154
171
  // 创建一个应用
155
- // - 先选择模板信息(vue, golang, vue3)
172
+ // - 先选择模板信息(golang, vue3)
156
173
  // - 根据 lpk manifest 模板中所需要的字段填充manifest
157
- // - 将 .lazycat 生成(包含 manifest, 项目文件)
158
174
  // - 调用模板的hook
159
175
  class LpkCreate {
160
- constructor({ name }) {
176
+ constructor(name, cwd = process.cwd()) {
161
177
  this.name = name;
178
+ this.cwd = cwd ?? process.cwd();
179
+ }
180
+
181
+ async init() {
182
+ ensureDir(path.resolve(this.cwd, this.name, "file"));
183
+ return this;
162
184
  }
163
185
 
164
186
  async exec() {
165
187
  const type = await chooseTemplate();
166
- const template = new TemplateInit({
167
- cwd: path.join(process.cwd(), this.name),
168
- type: type,
169
- });
170
- if (TemplateConfig[type]) {
171
- await template.create();
172
- logger.info(`初始化项目 ${this.name}`);
173
- await Generator().generate(type, this.name);
174
- logger.info("设置懒猫云应用");
175
- await TemplateConfig[type].after(this.name);
176
- }
188
+ const template = new TemplateInit(this.cwd, this.name, type);
189
+ await template.init();
190
+
191
+ console.log(`初始化项目 ${this.name}`);
192
+ await TemplateConfig[type].build(path.resolve(this.cwd, this.name));
193
+
194
+ console.log("初始化懒猫云应用");
195
+ await TemplateConfig[type].after(this.name, this.cwd);
196
+
197
+ await template.create();
177
198
  }
178
199
  }
179
200
 
180
- export default async (context) => {
181
- context.name = parse2CorrectName(context.name);
182
- await fsPromises.access(process.cwd() + "/" + context.name).then(
201
+ export default async (name, cwd = process.cwd()) => {
202
+ await fsPromises.access(path.resolve(cwd, name)).then(
183
203
  async () => {
184
204
  const questions = [
185
205
  {
@@ -191,11 +211,13 @@ export default async (context) => {
191
211
  ];
192
212
  const answers = await inquirer.prompt(questions);
193
213
  if (answers.override.toLowerCase() === "y") {
194
- return new LpkCreate(context).exec();
214
+ const lpk = await new LpkCreate(name, cwd).init();
215
+ return await lpk.exec();
195
216
  }
196
217
  },
197
- () => {
198
- return new LpkCreate(context).exec();
218
+ async () => {
219
+ const lpk = await new LpkCreate(name, cwd).init();
220
+ return await lpk.exec();
199
221
  }
200
222
  );
201
223
  };
@@ -0,0 +1,202 @@
1
+ import glob from "fast-glob";
2
+ import path from "path";
3
+ import fs from "fs";
4
+ import { isBinaryFileSync } from "isbinaryfile";
5
+ import { contextDirname, isFileExist } from "../utils.js";
6
+ import chalk from "chalk";
7
+ import { spawnSync } from "node:child_process";
8
+ import logger from "loglevel";
9
+
10
+ const __dirname = contextDirname(import.meta.url);
11
+
12
+ async function loadFiles(dirPath, prefix = "") {
13
+ const templateRoot = path.join(__dirname, "../../template/");
14
+ const templateDir = path.join(templateRoot, dirPath);
15
+
16
+ const _files = await glob(["**/*"], { cwd: templateDir });
17
+ let content;
18
+ let files = {};
19
+ for (let p of _files) {
20
+ const sourcePath = path.join(templateDir, p);
21
+
22
+ const statInfo = fs.statSync(sourcePath);
23
+ const mode = statInfo.mode;
24
+ if (isBinaryFileSync(sourcePath)) {
25
+ content = fs.readFileSync(sourcePath);
26
+ } else {
27
+ content = fs.readFileSync(sourcePath, "utf8");
28
+ }
29
+ // change _ prefix file into dot file
30
+ if (p.startsWith("_")) {
31
+ p = p.replace("_", ".");
32
+ }
33
+
34
+ files[path.join(prefix, p)] = {
35
+ content,
36
+ mode,
37
+ };
38
+ }
39
+ return files;
40
+ }
41
+
42
+ function writeFileTree(target, files) {
43
+ Object.keys(files).forEach((p) => {
44
+ const filePath = path.join(target, p);
45
+ if (!fs.existsSync(path.dirname(filePath))) {
46
+ fs.mkdirSync(path.dirname(filePath), { recursive: true });
47
+ }
48
+ logger.debug(`创建文件 ${filePath}`);
49
+ fs.writeFileSync(filePath, files[p].content, { mode: files[p].mode });
50
+ });
51
+ }
52
+
53
+ async function generate(type, to, opts = {}) {
54
+ const files = await loadFiles(type, opts.prefix);
55
+ writeFileTree(to, files);
56
+ }
57
+
58
+ function appendREADME(dir) {
59
+ const readme = path.resolve(dir, "README.md");
60
+ const readmeTemplate = path.resolve(
61
+ __dirname,
62
+ "../../template/_lpk/README.md"
63
+ );
64
+ if (!isFileExist(readme)) {
65
+ fs.copyFileSync(readmeTemplate, readme);
66
+ } else {
67
+ const contentStream = fs.readFileSync(readmeTemplate, {
68
+ encoding: "utf-8",
69
+ });
70
+ fs.writeFileSync(readme, contentStream, { flag: "a" });
71
+ }
72
+ }
73
+
74
+ function vueTemplate(type) {
75
+ const v = type == "vue3" ? "vue@3" : "vue@2";
76
+ return {
77
+ build: async (to, option) => {
78
+ spawnSync("npm", ["init", v, path.basename(to)], { stdio: "inherit" });
79
+ },
80
+ after: async function (name, cwd) {
81
+ spawnSync("npm", ["install"], {
82
+ cwd: path.resolve(cwd, name),
83
+ stdio: "inherit",
84
+ });
85
+
86
+ const lzcBuildTemplatePath = path.resolve(
87
+ __dirname,
88
+ `../../template/_lpk/vue.lzc-build.yml.in`
89
+ );
90
+ logger.debug("复制", lzcBuildTemplatePath);
91
+ fs.copyFileSync(
92
+ lzcBuildTemplatePath,
93
+ path.resolve(name, "lzc-build.yml")
94
+ );
95
+
96
+ const iconPngPath = path.resolve(
97
+ __dirname,
98
+ "../../template/_lpk/lazycat.png"
99
+ );
100
+ fs.copyFileSync(iconPngPath, path.resolve(name, "lazycat.png"));
101
+
102
+ // 更改默认的端口
103
+ const packageJson = path.resolve(name, "package.json");
104
+ const pkgInfo = JSON.parse(fs.readFileSync(packageJson));
105
+ pkgInfo["scripts"]["dev"] = `vite --port 3000 --host`;
106
+ fs.writeFileSync(packageJson, JSON.stringify(pkgInfo, null, " "));
107
+
108
+ appendREADME(path.resolve(cwd, name));
109
+
110
+ console.log(
111
+ chalk.green(
112
+ `
113
+ 🚀 构建 lpk 包
114
+ ${chalk.yellow(`lzc-cli project build -o ${name}.lpk`)}
115
+ ⚙️ 将应用安装至设备中:
116
+ ${chalk.yellow(`lzc-cli app install ${name}.lpk`)}
117
+ `.trim()
118
+ )
119
+ );
120
+ },
121
+ };
122
+ }
123
+
124
+ export const TemplateConfig = {
125
+ vue3: vueTemplate("vue3"),
126
+ vue2: vueTemplate("vue2"),
127
+ lite: {
128
+ build: async (to, option) => {
129
+ await generate("lite", to, option);
130
+ },
131
+ after: async function (name, cwd) {
132
+ appendREADME(path.resolve(cwd, name));
133
+
134
+ console.log(
135
+ chalk.green(
136
+ `
137
+ ✨ 懒猫云应用 ${name} 已创建:
138
+ ${chalk.yellow(`cd ${name}`)}
139
+ 🚀 构建 lpk 包
140
+ ${chalk.yellow(`lzc-cli project build -o ${name}.lpk`)}
141
+ ⚙️ 将应用安装至设备中:
142
+ ${chalk.yellow(`lzc-cli app install ${name}.lpk`)}
143
+ `.trim()
144
+ )
145
+ );
146
+ },
147
+ },
148
+ ionic_vue3: {
149
+ build: async (to, option) => {
150
+ await generate("ionic_vue3", to, option);
151
+ },
152
+ after: async function (name, cwd) {
153
+ appendREADME(path.resolve(cwd, name));
154
+
155
+ console.log(chalk.green("开始安装依赖"));
156
+ spawnSync("npm", ["install"], {
157
+ cwd: path.join(cwd, name),
158
+ stdio: "inherit",
159
+ });
160
+
161
+ console.log(
162
+ chalk.green(
163
+ `
164
+ ✨ 懒猫云应用 ${name} 已创建:
165
+ ${chalk.yellow(`cd ${name}`)}
166
+ ${chalk.yellow(`npm run dev`)}
167
+ 🚀 构建 lpk 包
168
+ ${chalk.yellow(`lzc-cli project build -o ${name}.lpk`)}
169
+ ⚙️ 将应用安装至设备中:
170
+ ${chalk.yellow(`lzc-cli app install ${name}.lpk`)}
171
+ 📄 生成android代码:
172
+ ${chalk.yellow(`npm run build_android`)}
173
+ 📄 生成ios代码:
174
+ ${chalk.yellow(`npm run build_ios`)}
175
+ `.trim()
176
+ )
177
+ );
178
+ },
179
+ },
180
+ golang: {
181
+ build: async (to, option) => {
182
+ await generate("golang", to, option);
183
+ },
184
+ after: function (name, cwd) {
185
+ appendREADME(path.resolve(cwd, name));
186
+
187
+ console.log(
188
+ chalk.green(
189
+ `
190
+ ✨ 懒猫云应用 ${name} 已创建:
191
+ ${chalk.yellow(`cd ${name}`)}
192
+ ${chalk.yellow(`go run .`)}
193
+ 🚀 构建 lpk 包
194
+ ${chalk.yellow(`lzc-cli project build -o ${name}.lpk`)}
195
+ ⚙️ 将应用安装至设备中:
196
+ ${chalk.yellow(`lzc-cli app install ${name}.lpk`)}
197
+ `.trim()
198
+ )
199
+ );
200
+ },
201
+ },
202
+ };