@lazycatcloud/lzc-cli 1.3.11 → 1.3.13

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.
@@ -1,129 +1,110 @@
1
- import logger from "loglevel"
2
- import inquirer from "inquirer"
3
- import {
4
- contextDirname,
5
- loadFromYaml,
6
- envTemplateFile,
7
- ensureDir,
8
- ensureDirectoryExists,
9
- dumpToYaml,
10
- isFileExist,
11
- isValidAppId,
12
- fakeLoadManifestYml
13
- } from "../utils.js"
14
- import path from "node:path"
15
- import { TemplateConfig } from "./lpk_create_generator.js"
16
- import fs from "node:fs"
17
- import yaml from "js-yaml"
18
- import chalk from "chalk"
19
- let fsPromises = fs.promises
1
+ import logger from 'loglevel';
2
+ import inquirer from 'inquirer';
3
+ import { contextDirname, loadFromYaml, envTemplateFile, ensureDir, ensureDirectoryExists, dumpToYaml, isFileExist, isValidAppId, fakeLoadManifestYml } from '../utils.js';
4
+ import path from 'node:path';
5
+ import { TemplateConfig } from './lpk_create_generator.js';
6
+ import fs from 'node:fs';
7
+ import yaml from 'js-yaml';
8
+ import chalk from 'chalk';
9
+ import { t } from '../i18n/index.js';
10
+
11
+ let fsPromises = fs.promises;
20
12
 
21
13
  export class LpkManifest {
22
- constructor(defaultAppID) {
23
- this.pwd = contextDirname(import.meta.url)
24
- this.manifest = {}
25
- this.defaultAppID = defaultAppID
26
- }
27
-
28
- async init(manifestFilePath, askInfo = false) {
29
- if (!manifestFilePath) {
30
- manifestFilePath = path.join(
31
- this.pwd,
32
- "..",
33
- "..",
34
- "template",
35
- "_lpk",
36
- "manifest.yml.in"
37
- )
38
- }
39
-
40
- if (askInfo) {
41
- let answer = await this.askLpkInfo()
42
- this.manifest = yaml.load(await envTemplateFile(manifestFilePath, answer))
43
- } else {
44
- try {
45
- this.manifest = loadFromYaml(manifestFilePath)
46
- } catch (err) {
47
- this.manifest = fakeLoadManifestYml(manifestFilePath)
48
- this.excpManifest = true
49
- }
50
- }
51
- }
52
-
53
- async create(outputDir) {
54
- let outputFilePath = path.join(outputDir, "lzc-manifest.yml")
55
- logger.debug(`创建文件 ${outputFilePath}`)
56
- ensureDir(outputFilePath)
57
- return dumpToYaml(this.manifest, outputFilePath)
58
- }
59
-
60
- async askLpkInfo() {
61
- const { appId } = await inquirer.prompt([
62
- {
63
- type: "input",
64
- name: "appId",
65
- message: `请输入应用ID,例如app-demo1`,
66
- default: this.defaultAppID,
67
- validate: (input) => {
68
- if (isValidAppId(input)) {
69
- return true
70
- } else {
71
- return `允许小写字母或数字或-的组合,但不以数字开头,且不能以 - 开头或结束,也不能有连续的短横线 --`
72
- }
73
- }
74
- }
75
- ])
76
- const answer = {
77
- name: appId,
78
- package: `cloud.lazycat.app.${appId}`,
79
- subdomain: appId
80
- }
81
- return answer
82
- }
14
+ constructor(defaultAppID) {
15
+ this.pwd = contextDirname(import.meta.url);
16
+ this.manifest = {};
17
+ this.defaultAppID = defaultAppID;
18
+ }
19
+
20
+ async init(manifestFilePath, askInfo = false) {
21
+ if (!manifestFilePath) {
22
+ manifestFilePath = path.join(this.pwd, '..', '..', 'template', '_lpk', 'manifest.yml.in');
23
+ }
24
+
25
+ if (askInfo) {
26
+ let answer = await this.askLpkInfo();
27
+ this.manifest = yaml.load(await envTemplateFile(manifestFilePath, answer));
28
+ } else {
29
+ try {
30
+ this.manifest = loadFromYaml(manifestFilePath);
31
+ } catch (err) {
32
+ this.manifest = fakeLoadManifestYml(manifestFilePath);
33
+ this.excpManifest = true;
34
+ }
35
+ }
36
+ }
37
+
38
+ async create(outputDir) {
39
+ let outputFilePath = path.join(outputDir, 'lzc-manifest.yml');
40
+ logger.debug(`create file ${outputFilePath}`);
41
+ ensureDir(outputFilePath);
42
+ return dumpToYaml(this.manifest, outputFilePath);
43
+ }
44
+
45
+ async askLpkInfo() {
46
+ const { appId } = await inquirer.prompt([
47
+ {
48
+ type: 'input',
49
+ name: 'appId',
50
+ message: t('lzc_cli.lib.app.lpk_create.ask_lpk_info_message', `请输入应用ID,例如 app-demo1`),
51
+ default: this.defaultAppID,
52
+ validate: (input) => {
53
+ if (isValidAppId(input)) {
54
+ return true;
55
+ } else {
56
+ return t(
57
+ 'lzc_cli.lib.app.lpk_create.ask_lpk_info_validate_fail',
58
+ `允许小写字母或数字或-的组合,但不以数字开头,且不能以 - 开头或结束,也不能有连续的短横线 --`,
59
+ );
60
+ }
61
+ },
62
+ },
63
+ ]);
64
+ const answer = {
65
+ name: appId,
66
+ package: `cloud.lazycat.app.${appId}`,
67
+ subdomain: appId,
68
+ };
69
+ return answer;
70
+ }
83
71
  }
84
72
 
85
73
  export class TemplateInit {
86
- constructor(cwd, name, type) {
87
- this.cwd = cwd
88
- this.name = name
89
- this.type = type
90
- this.lpkManifest = new LpkManifest(name)
91
- }
92
-
93
- async init() {
94
- if (!this.type) {
95
- this.type = await chooseTemplate()
96
- }
97
-
98
- // 优先使用模板项目自定义的模板文件
99
- const typeTemplatePath = path.join(
100
- contextDirname(import.meta.url),
101
- "..",
102
- "..",
103
- "template",
104
- "_lpk",
105
- `${this.type}.manifest.yml.in`
106
- )
107
- const templatePath = isFileExist(typeTemplatePath) ? typeTemplatePath : ""
108
- await this.lpkManifest.init(templatePath, true)
109
- }
110
-
111
- async create() {
112
- await this.lpkManifest.create(path.resolve(this.cwd, this.name))
113
- }
74
+ constructor(cwd, name, type) {
75
+ this.cwd = cwd;
76
+ this.name = name;
77
+ this.type = type;
78
+ this.lpkManifest = new LpkManifest(name);
79
+ }
80
+
81
+ async init() {
82
+ if (!this.type) {
83
+ this.type = await chooseTemplate();
84
+ }
85
+
86
+ // 优先使用模板项目自定义的模板文件
87
+ const typeTemplatePath = path.join(contextDirname(import.meta.url), '..', '..', 'template', '_lpk', `${this.type}.manifest.yml.in`);
88
+ const templatePath = isFileExist(typeTemplatePath) ? typeTemplatePath : '';
89
+ await this.lpkManifest.init(templatePath, true);
90
+ }
91
+
92
+ async create() {
93
+ await this.lpkManifest.create(path.resolve(this.cwd, this.name));
94
+ }
114
95
  }
115
96
 
116
97
  export async function chooseTemplate() {
117
- return (
118
- await inquirer.prompt([
119
- {
120
- name: "type",
121
- message: "选择项目构建模板",
122
- type: "list",
123
- choices: ["vue3"]
124
- }
125
- ])
126
- )["type"]
98
+ return (
99
+ await inquirer.prompt([
100
+ {
101
+ name: 'type',
102
+ message: t('lzc_cli.lib.app.lpk_create.choose_template_message', '选择项目构建模板'),
103
+ type: 'list',
104
+ choices: ['vue3'],
105
+ },
106
+ ])
107
+ )['type'];
127
108
  }
128
109
 
129
110
  // 创建一个应用
@@ -131,43 +112,42 @@ export async function chooseTemplate() {
131
112
  // - 根据 lpk manifest 模板中所需要的字段填充manifest
132
113
  // - 调用模板的hook
133
114
  class LpkCreate {
134
- constructor(name, cwd = process.cwd()) {
135
- this.name = name
136
- this.cwd = cwd ?? process.cwd()
137
- this.appDir = path.resolve(this.cwd, this.name)
138
- }
139
-
140
- async init() {
141
- return this
142
- }
143
-
144
- async exec() {
145
- const type = await chooseTemplate()
146
- const template = new TemplateInit(this.cwd, this.name, type)
147
- await template.init()
148
-
149
- console.log(chalk.green(`✨ 初始化项目 ${this.name}`))
150
- const { isExists, renamedFileName } = ensureDirectoryExists(
151
- this.appDir,
152
- true
153
- )
154
- if (isExists && renamedFileName) {
155
- console.warn(
156
- chalk.yellow(
157
- `! 检测到相同目录,已自动将旧目录重命名为 ${renamedFileName}`
158
- )
159
- )
160
- }
161
- await TemplateConfig[type].build(path.resolve(this.cwd, this.name))
162
-
163
- console.log(chalk.green(`✨ 初始化懒猫云应用`))
164
- await TemplateConfig[type].after(this.name, this.cwd)
165
-
166
- await template.create()
167
- }
115
+ constructor(name, cwd = process.cwd()) {
116
+ this.name = name;
117
+ this.cwd = cwd ?? process.cwd();
118
+ this.appDir = path.resolve(this.cwd, this.name);
119
+ }
120
+
121
+ async init() {
122
+ return this;
123
+ }
124
+
125
+ async exec() {
126
+ const type = await chooseTemplate();
127
+ const template = new TemplateInit(this.cwd, this.name, type);
128
+ await template.init();
129
+
130
+ console.log(chalk.green(t('lzc_cli.lib.app.lpk_create.exec_init_project_tips', `✨ 初始化项目 {{name}}`, { name: this.name })));
131
+ const { isExists, renamedFileName } = ensureDirectoryExists(this.appDir, true);
132
+ if (isExists && renamedFileName) {
133
+ console.warn(
134
+ chalk.yellow(
135
+ t('lzc_cli.lib.app.lpk_create.exec_init_project_name_exist_tips', `! 检测到相同目录,已自动将旧目录重命名为 {{renamedFileName}}`, {
136
+ renamedFileName,
137
+ }),
138
+ ),
139
+ );
140
+ }
141
+ await TemplateConfig[type].build(path.resolve(this.cwd, this.name));
142
+
143
+ console.log(chalk.green(t('lzc_cli.lib.app.lpk_create.exec_init_project_success_tips', `✨ 初始化懒猫云应用`)));
144
+ await TemplateConfig[type].after(this.name, this.cwd);
145
+
146
+ await template.create();
147
+ }
168
148
  }
169
149
 
170
150
  export default async (name, cwd = process.cwd()) => {
171
- const lpk = await new LpkCreate(name, cwd).init()
172
- return await lpk.exec()
173
- }
151
+ const lpk = await new LpkCreate(name, cwd).init();
152
+ return await lpk.exec();
153
+ };
@@ -1,91 +1,99 @@
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 spawn from "cross-spawn"
8
- import logger from "loglevel"
1
+ import glob from 'fast-glob';
2
+ import path from 'path';
3
+ import fs from 'fs';
4
+ import { isBinaryFileSync } from 'isbinaryfile';
5
+ import { contextDirname } from '../utils.js';
6
+ import chalk from 'chalk';
7
+ // import spawn from "cross-spawn"
8
+ import logger from 'loglevel';
9
+ import { t } from '../i18n/index.js';
9
10
 
10
- const __dirname = contextDirname(import.meta.url)
11
+ const __dirname = contextDirname(import.meta.url);
11
12
 
12
- async function loadFiles(dirPath, prefix = "") {
13
- const templateRoot = path.join(__dirname, "..", "..", "template")
14
- const templateDir = path.join(templateRoot, dirPath)
13
+ async function loadFiles(dirPath, prefix = '') {
14
+ const templateRoot = path.join(__dirname, '..', '..', 'template');
15
+ const templateDir = path.join(templateRoot, dirPath);
15
16
 
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)
17
+ const _files = await glob(['**/*', '**/.*', '.*'], { cwd: templateDir });
18
+ let content;
19
+ let files = {};
20
+ for (let p of _files) {
21
+ const sourcePath = path.join(templateDir, p);
21
22
 
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
- }
23
+ const statInfo = fs.statSync(sourcePath);
24
+ const mode = statInfo.mode;
25
+ if (isBinaryFileSync(sourcePath)) {
26
+ content = fs.readFileSync(sourcePath);
27
+ } else {
28
+ content = fs.readFileSync(sourcePath, 'utf8');
29
+ }
30
+ // change _ prefix file into dot file
31
+ if (p.startsWith('_')) {
32
+ p = p.replace('_', '.');
33
+ }
33
34
 
34
- files[path.join(prefix, p)] = {
35
- content,
36
- mode
37
- }
38
- }
39
- return files
35
+ files[path.join(prefix, p)] = {
36
+ content,
37
+ mode,
38
+ };
39
+ }
40
+ return files;
40
41
  }
41
42
 
42
43
  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
- })
44
+ Object.keys(files).forEach((p) => {
45
+ const filePath = path.join(target, p);
46
+ if (!fs.existsSync(path.dirname(filePath))) {
47
+ fs.mkdirSync(path.dirname(filePath), { recursive: true });
48
+ }
49
+ logger.debug(t('lzc_cli.lib.app.lpk_create_generator.write_file_tree_create_file', `创建文件 {{filePath}}`, { filePath }));
50
+ fs.writeFileSync(filePath, files[p].content, { mode: files[p].mode });
51
+ });
51
52
  }
52
53
 
53
54
  export async function generate(type, to, opts = {}) {
54
- const files = await loadFiles(type, opts.prefix)
55
- writeFileTree(to, files)
55
+ const files = await loadFiles(type, opts.prefix);
56
+ writeFileTree(to, files);
56
57
  }
57
58
 
58
59
  function appCreateSuccessTip(name) {
59
- console.log(
60
- chalk.green(
61
- `
60
+ console.log(
61
+ chalk.green(
62
+ t(
63
+ 'lzc_cli.lib.app.lpk_create_generator.app_create_success_tip',
64
+ `
62
65
  ✨ 懒猫微服应用已创建成功 !
63
66
  ✨ 进行下面步骤后即可进入容器开发
64
- cd ${name}
67
+ cd {{name}}
65
68
  lzc-cli project devshell
66
- `.trim()
67
- )
68
- )
69
+ `,
70
+ { name },
71
+ ).trim(),
72
+ ),
73
+ );
69
74
  }
70
75
 
71
76
  export const TemplateConfig = {
72
- vue3: {
73
- build: async (to, option) => {
74
- await generate("vue", to, option)
75
- },
76
- after: async function (name) {
77
- appCreateSuccessTip(name)
78
- console.log(
79
- chalk.green(
80
- `
77
+ vue3: {
78
+ build: async (to, option) => {
79
+ await generate('vue', to, option);
80
+ },
81
+ after: async function (name) {
82
+ appCreateSuccessTip(name);
83
+ console.log(
84
+ chalk.green(
85
+ t(
86
+ 'lzc_cli.lib.app.lpk_create_generator.template_config_vue3_green',
87
+ `
81
88
  ⚙️ 进入应用容器后执行下面命令:
82
89
  npm install
83
90
  npm run dev
84
91
  🚀 启动应用:
85
92
  进入微服客户端启动器页面点击应用图标来测试应用
86
- `.trim()
87
- )
88
- )
89
- }
90
- }
91
- }
93
+ `,
94
+ ).trim(),
95
+ ),
96
+ );
97
+ },
98
+ },
99
+ };