@spcsn/taro-cli 0.1.6 → 1.0.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/LICENSE +1 -153
- package/README.md +10 -7
- package/define-config.d.ts +1 -0
- package/define-config.js +1 -0
- package/dist/cli.js +140 -110
- package/dist/cli.js.map +1 -1
- package/dist/commands/{customCommand.d.ts → custom-command.d.ts} +1 -1
- package/dist/commands/custom-command.js +23 -0
- package/dist/commands/custom-command.js.map +1 -0
- package/dist/create/creator.js +2 -0
- package/dist/create/creator.js.map +1 -1
- package/dist/create/{fetchTemplate.js → fetch-template.js} +26 -33
- package/dist/create/fetch-template.js.map +1 -0
- package/dist/create/project.d.ts +2 -6
- package/dist/create/project.js +240 -285
- package/dist/create/project.js.map +1 -1
- package/dist/create/template-creator.d.ts +75 -0
- package/dist/create/template-creator.js +275 -0
- package/dist/create/template-creator.js.map +1 -0
- package/dist/doctor/validators.d.ts +7 -6
- package/dist/doctor/validators.js +6 -7
- package/dist/doctor/validators.js.map +1 -1
- package/dist/platform-weapp/apis-list.d.ts +1 -0
- package/dist/platform-weapp/apis-list.js +37 -0
- package/dist/platform-weapp/apis-list.js.map +1 -0
- package/dist/platform-weapp/apis.d.ts +16 -0
- package/dist/platform-weapp/apis.js +34 -0
- package/dist/platform-weapp/apis.js.map +1 -0
- package/dist/platform-weapp/components-react.d.ts +80 -0
- package/dist/platform-weapp/components-react.js +86 -0
- package/dist/platform-weapp/components-react.js.map +1 -0
- package/dist/platform-weapp/components.d.ts +4 -0
- package/dist/platform-weapp/components.js +520 -0
- package/dist/platform-weapp/components.js.map +1 -0
- package/dist/platform-weapp/index.d.ts +11 -0
- package/dist/platform-weapp/index.js +17 -0
- package/dist/platform-weapp/index.js.map +1 -0
- package/dist/platform-weapp/program.d.ts +35 -0
- package/dist/platform-weapp/program.js +64 -0
- package/dist/platform-weapp/program.js.map +1 -0
- package/dist/platform-weapp/runtime-utils.d.ts +27 -0
- package/dist/platform-weapp/runtime-utils.js +48 -0
- package/dist/platform-weapp/runtime-utils.js.map +1 -0
- package/dist/platform-weapp/runtime.d.ts +1 -0
- package/dist/platform-weapp/runtime.js +7 -0
- package/dist/platform-weapp/runtime.js.map +1 -0
- package/dist/platform-weapp/template.d.ts +39 -0
- package/dist/platform-weapp/template.js +119 -0
- package/dist/platform-weapp/template.js.map +1 -0
- package/dist/presets/commands/build.js +25 -45
- package/dist/presets/commands/build.js.map +1 -1
- package/dist/presets/commands/init.js +2 -4
- package/dist/presets/commands/init.js.map +1 -1
- package/dist/presets/constant/hooks.d.ts +0 -2
- package/dist/presets/constant/hooks.js +1 -3
- package/dist/presets/constant/hooks.js.map +1 -1
- package/dist/presets/files/generate-framework-info.js +68 -0
- package/dist/presets/files/generate-framework-info.js.map +1 -0
- package/dist/presets/files/{generateProjectConfig.js → generate-project-config.js} +1 -1
- package/dist/presets/files/generate-project-config.js.map +1 -0
- package/dist/presets/files/{writeFileToDist.js → write-file-to-dist.js} +1 -1
- package/dist/presets/files/write-file-to-dist.js.map +1 -0
- package/dist/presets/hooks/build.js +1 -3
- package/dist/presets/hooks/build.js.map +1 -1
- package/dist/presets/index.js +3 -5
- package/dist/presets/index.js.map +1 -1
- package/dist/util/{appConfig.d.ts → app-config.d.ts} +2 -2
- package/dist/util/{appConfig.js → app-config.js} +4 -3
- package/dist/util/app-config.js.map +1 -0
- package/dist/util/defineConfig.d.ts +5 -5
- package/dist/util/defineConfig.js.map +1 -1
- package/dist/util/index.d.ts +9 -1
- package/dist/util/index.js +54 -23
- package/dist/util/index.js.map +1 -1
- package/package.json +24 -27
- package/postinstall.js +1 -17
- package/src/cli.ts +185 -132
- package/src/commands/custom-command.ts +22 -0
- package/src/create/{fetchTemplate.ts → fetch-template.ts} +30 -35
- package/src/create/project.ts +31 -78
- package/src/create/template-creator.ts +325 -0
- package/src/doctor/validators.js +1 -0
- package/src/doctor/validators.ts +8 -6
- package/src/platform-weapp/apis-list.ts +33 -0
- package/src/platform-weapp/apis.ts +48 -0
- package/src/platform-weapp/components-react.ts +81 -0
- package/src/platform-weapp/components.ts +521 -0
- package/src/platform-weapp/index.ts +25 -0
- package/src/platform-weapp/program.ts +70 -0
- package/src/platform-weapp/runtime-utils.ts +57 -0
- package/src/platform-weapp/runtime.ts +6 -0
- package/src/platform-weapp/template.ts +150 -0
- package/src/presets/commands/build.ts +20 -40
- package/src/presets/commands/init.ts +1 -4
- package/src/presets/constant/hooks.js +12 -0
- package/src/presets/constant/hooks.ts +0 -2
- package/src/presets/files/{generateFrameworkInfo.ts → generate-framework-info.ts} +7 -1
- package/src/presets/hooks/build.ts +1 -3
- package/src/presets/index.ts +3 -5
- package/src/util/app-config.js +1 -0
- package/src/util/{appConfig.ts → app-config.ts} +5 -4
- package/src/util/defineConfig.ts +6 -6
- package/src/util/index.ts +67 -25
- package/templates/default/config/dev.js +1 -1
- package/templates/default/config/index.js +28 -40
- package/templates/default/config/prod.js +1 -1
- package/templates/default/package.json.tmpl +2 -17
- package/templates/default/types/global.d.ts +0 -4
- package/templates/plugin-compile/README.md +3 -1
- package/templates/plugin-compile/package.json.tmpl +1 -2
- package/templates/plugin-compile/src/index.ts +18 -28
- package/dist/commands/customCommand.js +0 -23
- package/dist/commands/customCommand.js.map +0 -1
- package/dist/config/packagesManagement.d.ts +0 -19
- package/dist/config/packagesManagement.js +0 -22
- package/dist/config/packagesManagement.js.map +0 -1
- package/dist/create/fetchTemplate.js.map +0 -1
- package/dist/create/page.d.ts +0 -65
- package/dist/create/page.js +0 -266
- package/dist/create/page.js.map +0 -1
- package/dist/create/plugin.d.ts +0 -16
- package/dist/create/plugin.js +0 -77
- package/dist/create/plugin.js.map +0 -1
- package/dist/presets/commands/config.js +0 -121
- package/dist/presets/commands/config.js.map +0 -1
- package/dist/presets/commands/create.js +0 -112
- package/dist/presets/commands/create.js.map +0 -1
- package/dist/presets/commands/doctor.js +0 -15
- package/dist/presets/commands/doctor.js.map +0 -1
- package/dist/presets/commands/global-config.d.ts +0 -3
- package/dist/presets/commands/global-config.js +0 -175
- package/dist/presets/commands/global-config.js.map +0 -1
- package/dist/presets/commands/help.d.ts +0 -3
- package/dist/presets/commands/help.js +0 -16
- package/dist/presets/commands/help.js.map +0 -1
- package/dist/presets/commands/info.d.ts +0 -3
- package/dist/presets/commands/info.js +0 -96
- package/dist/presets/commands/info.js.map +0 -1
- package/dist/presets/commands/inspect.d.ts +0 -3
- package/dist/presets/commands/inspect.js +0 -155
- package/dist/presets/commands/inspect.js.map +0 -1
- package/dist/presets/commands/update.d.ts +0 -3
- package/dist/presets/commands/update.js +0 -187
- package/dist/presets/commands/update.js.map +0 -1
- package/dist/presets/files/generateFrameworkInfo.d.ts +0 -3
- package/dist/presets/files/generateFrameworkInfo.js +0 -30
- package/dist/presets/files/generateFrameworkInfo.js.map +0 -1
- package/dist/presets/files/generateProjectConfig.d.ts +0 -3
- package/dist/presets/files/generateProjectConfig.js.map +0 -1
- package/dist/presets/files/writeFileToDist.d.ts +0 -3
- package/dist/presets/files/writeFileToDist.js.map +0 -1
- package/dist/presets/hooks/create.d.ts +0 -3
- package/dist/presets/hooks/create.js +0 -42
- package/dist/presets/hooks/create.js.map +0 -1
- package/dist/presets/platforms/plugin.d.ts +0 -3
- package/dist/presets/platforms/plugin.js +0 -106
- package/dist/presets/platforms/plugin.js.map +0 -1
- package/dist/presets/platforms/rn.d.ts +0 -3
- package/dist/presets/platforms/rn.js +0 -130
- package/dist/presets/platforms/rn.js.map +0 -1
- package/dist/util/appConfig.js.map +0 -1
- package/dist/util/createPage.d.ts +0 -9
- package/dist/util/createPage.js +0 -114
- package/dist/util/createPage.js.map +0 -1
- package/src/commands/customCommand.ts +0 -22
- package/src/config/packagesManagement.ts +0 -20
- package/src/config/rn-stylelint.json +0 -4
- package/src/create/page.ts +0 -325
- package/src/create/plugin.ts +0 -55
- package/src/presets/commands/config.ts +0 -86
- package/src/presets/commands/create.ts +0 -104
- package/src/presets/commands/doctor.ts +0 -21
- package/src/presets/commands/global-config.ts +0 -140
- package/src/presets/commands/help.ts +0 -17
- package/src/presets/commands/info.ts +0 -81
- package/src/presets/commands/inspect.ts +0 -137
- package/src/presets/commands/update.ts +0 -175
- package/src/presets/hooks/create.ts +0 -9
- package/src/presets/platforms/plugin.ts +0 -75
- package/src/presets/platforms/rn.ts +0 -125
- package/src/util/createPage.ts +0 -139
- package/templates/default/babel.config.js +0 -14
- /package/dist/create/{fetchTemplate.d.ts → fetch-template.d.ts} +0 -0
- /package/dist/presets/{commands/config.d.ts → files/generate-framework-info.d.ts} +0 -0
- /package/dist/presets/{commands/create.d.ts → files/generate-project-config.d.ts} +0 -0
- /package/dist/presets/{commands/doctor.d.ts → files/write-file-to-dist.d.ts} +0 -0
- /package/src/presets/files/{generateProjectConfig.ts → generate-project-config.ts} +0 -0
- /package/src/presets/files/{writeFileToDist.ts → write-file-to-dist.ts} +0 -0
package/src/create/page.ts
DELETED
|
@@ -1,325 +0,0 @@
|
|
|
1
|
-
import * as path from 'node:path';
|
|
2
|
-
|
|
3
|
-
import {
|
|
4
|
-
CompilerType,
|
|
5
|
-
createPage as createPageBinding,
|
|
6
|
-
CSSType,
|
|
7
|
-
FrameworkType,
|
|
8
|
-
NpmType,
|
|
9
|
-
PeriodType,
|
|
10
|
-
} from '@spcsn/taro-binding';
|
|
11
|
-
import {
|
|
12
|
-
babelKit,
|
|
13
|
-
chalk,
|
|
14
|
-
DEFAULT_TEMPLATE_SRC,
|
|
15
|
-
fs,
|
|
16
|
-
getUserHomeDir,
|
|
17
|
-
resolveScriptPath,
|
|
18
|
-
TARO_BASE_CONFIG,
|
|
19
|
-
TARO_CONFIG_FOLDER,
|
|
20
|
-
} from '@spcsn/taro-helper';
|
|
21
|
-
|
|
22
|
-
import { getPkgVersion, getRootPath, isNil } from '../util';
|
|
23
|
-
import { modifyPagesOrSubPackages } from '../util/createPage';
|
|
24
|
-
import { TEMPLATE_CREATOR } from './constants';
|
|
25
|
-
import Creator from './creator';
|
|
26
|
-
import fetchTemplate from './fetchTemplate';
|
|
27
|
-
|
|
28
|
-
export interface IPageConf {
|
|
29
|
-
projectDir: string;
|
|
30
|
-
projectName: string;
|
|
31
|
-
npm: NpmType;
|
|
32
|
-
template: string;
|
|
33
|
-
clone?: boolean;
|
|
34
|
-
templateSource?: string;
|
|
35
|
-
description?: string;
|
|
36
|
-
pageName: string;
|
|
37
|
-
date?: string;
|
|
38
|
-
framework: FrameworkType;
|
|
39
|
-
css: CSSType;
|
|
40
|
-
typescript?: boolean;
|
|
41
|
-
compiler?: CompilerType;
|
|
42
|
-
isCustomTemplate?: boolean;
|
|
43
|
-
customTemplatePath?: string;
|
|
44
|
-
pageDir?: string;
|
|
45
|
-
subPkg?: string;
|
|
46
|
-
}
|
|
47
|
-
interface IPageArgs extends IPageConf {
|
|
48
|
-
modifyCustomTemplateConfig: TGetCustomTemplate;
|
|
49
|
-
afterCreate?: TAfterCreate;
|
|
50
|
-
}
|
|
51
|
-
interface ITemplateInfo {
|
|
52
|
-
css: CSSType;
|
|
53
|
-
typescript?: boolean;
|
|
54
|
-
compiler?: CompilerType;
|
|
55
|
-
template?: string;
|
|
56
|
-
templateSource?: string;
|
|
57
|
-
clone?: boolean;
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
type TCustomTemplateInfo = Omit<
|
|
61
|
-
ITemplateInfo & {
|
|
62
|
-
isCustomTemplate?: boolean;
|
|
63
|
-
customTemplatePath?: string;
|
|
64
|
-
},
|
|
65
|
-
'template'
|
|
66
|
-
>;
|
|
67
|
-
|
|
68
|
-
export type TSetCustomTemplateConfig = (customTemplateConfig: TCustomTemplateInfo) => void;
|
|
69
|
-
|
|
70
|
-
type TGetCustomTemplate = (cb: TSetCustomTemplateConfig) => Promise<void>;
|
|
71
|
-
type TAfterCreate = (state: boolean) => void;
|
|
72
|
-
|
|
73
|
-
const DEFAULT_TEMPLATE_INFO = {
|
|
74
|
-
name: 'default',
|
|
75
|
-
css: CSSType.None,
|
|
76
|
-
typescript: false,
|
|
77
|
-
compiler: CompilerType.Vite,
|
|
78
|
-
framework: FrameworkType.React,
|
|
79
|
-
};
|
|
80
|
-
|
|
81
|
-
export enum ConfigModificationState {
|
|
82
|
-
Success,
|
|
83
|
-
Fail,
|
|
84
|
-
NeedLess,
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
export type ModifyCallback = (state: ConfigModificationState) => void;
|
|
88
|
-
|
|
89
|
-
export default class Page extends Creator {
|
|
90
|
-
public rootPath: string;
|
|
91
|
-
public conf: IPageConf;
|
|
92
|
-
private modifyCustomTemplateConfig: TGetCustomTemplate;
|
|
93
|
-
private afterCreate: TAfterCreate | undefined;
|
|
94
|
-
private pageEntryPath!: string;
|
|
95
|
-
|
|
96
|
-
constructor(args: IPageArgs) {
|
|
97
|
-
super();
|
|
98
|
-
this.rootPath = this._rootPath;
|
|
99
|
-
const { modifyCustomTemplateConfig, afterCreate, ...otherOptions } = args;
|
|
100
|
-
this.conf = Object.assign(
|
|
101
|
-
{
|
|
102
|
-
projectDir: '',
|
|
103
|
-
projectName: '',
|
|
104
|
-
template: '',
|
|
105
|
-
description: '',
|
|
106
|
-
pageDir: '',
|
|
107
|
-
},
|
|
108
|
-
otherOptions,
|
|
109
|
-
);
|
|
110
|
-
|
|
111
|
-
this.conf.projectName = path.basename(this.conf.projectDir);
|
|
112
|
-
this.modifyCustomTemplateConfig = modifyCustomTemplateConfig;
|
|
113
|
-
this.afterCreate = afterCreate;
|
|
114
|
-
this.processPageName();
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
processPageName() {
|
|
118
|
-
const { pageName } = this.conf;
|
|
119
|
-
// todo 目前还没有对 subPkg 和 pageName 这两个字段做 格式验证或者处理
|
|
120
|
-
const lastDirSplitSymbolIndex = pageName.lastIndexOf('/');
|
|
121
|
-
if (lastDirSplitSymbolIndex !== -1) {
|
|
122
|
-
this.conf.pageDir = pageName.substring(0, lastDirSplitSymbolIndex);
|
|
123
|
-
this.conf.pageName = pageName.substring(lastDirSplitSymbolIndex + 1);
|
|
124
|
-
}
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
getPkgPath() {
|
|
128
|
-
const projectDir = this.conf.projectDir as string;
|
|
129
|
-
let pkgPath = path.join(projectDir, 'package.json');
|
|
130
|
-
if (!fs.existsSync(pkgPath)) {
|
|
131
|
-
// 适配 云开发 项目
|
|
132
|
-
pkgPath = path.join(projectDir, 'client', 'package.json');
|
|
133
|
-
if (!fs.existsSync(pkgPath)) {
|
|
134
|
-
console.log(chalk.yellow('请在项目根目录下执行 taro create 命令!'));
|
|
135
|
-
process.exit(0);
|
|
136
|
-
}
|
|
137
|
-
}
|
|
138
|
-
return pkgPath;
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
getPkgTemplateInfo() {
|
|
142
|
-
const pkg = fs.readJSONSync(this.getPkgPath());
|
|
143
|
-
const templateInfo = pkg.templateInfo || DEFAULT_TEMPLATE_INFO;
|
|
144
|
-
// set template name
|
|
145
|
-
templateInfo.template = templateInfo.name;
|
|
146
|
-
delete templateInfo.name;
|
|
147
|
-
return templateInfo;
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
setPageEntryPath(files: string[], handler) {
|
|
151
|
-
const configFileName = files.find((filename) => /\.config\.(js|ts)$/.test(filename));
|
|
152
|
-
if (!configFileName) return;
|
|
153
|
-
const getPageFn = handler[configFileName];
|
|
154
|
-
const { setPageName = '', setSubPkgName = '' } = getPageFn?.(() => {}, this.conf) || {};
|
|
155
|
-
if (this.conf.subPkg) {
|
|
156
|
-
this.pageEntryPath = setSubPkgName.replace(/\.config\.(js|ts)$/, '');
|
|
157
|
-
} else {
|
|
158
|
-
this.pageEntryPath = setPageName.replace(/\.config\.(js|ts)$/, '');
|
|
159
|
-
}
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
setCustomTemplateConfig(customTemplateConfig: TCustomTemplateInfo) {
|
|
163
|
-
const pkgTemplateInfo = this.getPkgTemplateInfo();
|
|
164
|
-
const { compiler, css, customTemplatePath, typescript } = customTemplateConfig;
|
|
165
|
-
const conf = {
|
|
166
|
-
compiler: compiler || pkgTemplateInfo.compiler,
|
|
167
|
-
css: css || pkgTemplateInfo.css,
|
|
168
|
-
typescript: !isNil(typescript) ? typescript : pkgTemplateInfo.typescript,
|
|
169
|
-
customTemplatePath,
|
|
170
|
-
isCustomTemplate: true,
|
|
171
|
-
};
|
|
172
|
-
this.setTemplateConfig(conf);
|
|
173
|
-
}
|
|
174
|
-
|
|
175
|
-
setTemplateConfig(templateInfo: ITemplateInfo) {
|
|
176
|
-
this.conf = Object.assign(this.conf, templateInfo);
|
|
177
|
-
}
|
|
178
|
-
|
|
179
|
-
async fetchTemplates() {
|
|
180
|
-
const homedir = getUserHomeDir();
|
|
181
|
-
let templateSource = DEFAULT_TEMPLATE_SRC;
|
|
182
|
-
if (!homedir) chalk.yellow('找不到用户根目录,使用默认模版源!');
|
|
183
|
-
|
|
184
|
-
if (this.conf.templateSource) {
|
|
185
|
-
templateSource = this.conf.templateSource;
|
|
186
|
-
} else {
|
|
187
|
-
const taroConfigPath = path.join(homedir, TARO_CONFIG_FOLDER);
|
|
188
|
-
const taroConfig = path.join(taroConfigPath, TARO_BASE_CONFIG);
|
|
189
|
-
if (fs.existsSync(taroConfig)) {
|
|
190
|
-
const config = await fs.readJSON(taroConfig);
|
|
191
|
-
templateSource = config && config.templateSource ? config.templateSource : DEFAULT_TEMPLATE_SRC;
|
|
192
|
-
} else {
|
|
193
|
-
await fs.createFile(taroConfig);
|
|
194
|
-
await fs.writeJSON(taroConfig, { templateSource });
|
|
195
|
-
templateSource = DEFAULT_TEMPLATE_SRC;
|
|
196
|
-
}
|
|
197
|
-
}
|
|
198
|
-
|
|
199
|
-
// 从模板源下载模板
|
|
200
|
-
await fetchTemplate(templateSource, this.templatePath(''), this.conf.clone);
|
|
201
|
-
}
|
|
202
|
-
|
|
203
|
-
async create() {
|
|
204
|
-
const date = new Date();
|
|
205
|
-
this.conf.date = `${date.getFullYear()}-${date.getMonth() + 1}-${date.getDate()}`;
|
|
206
|
-
// apply 插件,由插件设置自定义模版 config
|
|
207
|
-
await this.modifyCustomTemplateConfig(this.setCustomTemplateConfig.bind(this));
|
|
208
|
-
if (!this.conf.isCustomTemplate) {
|
|
209
|
-
const pkgTemplateInfo = this.getPkgTemplateInfo();
|
|
210
|
-
this.setTemplateConfig(pkgTemplateInfo);
|
|
211
|
-
if (!fs.existsSync(this.templatePath(this.conf.template))) {
|
|
212
|
-
await this.fetchTemplates();
|
|
213
|
-
}
|
|
214
|
-
}
|
|
215
|
-
this.write();
|
|
216
|
-
}
|
|
217
|
-
|
|
218
|
-
updateAppConfig() {
|
|
219
|
-
const { parse, generate, traverse } = babelKit;
|
|
220
|
-
|
|
221
|
-
let modifyState: ConfigModificationState = ConfigModificationState.Fail;
|
|
222
|
-
const { subPkg, projectDir, typescript } = this.conf;
|
|
223
|
-
const [sourceString, pageString] = this.pageEntryPath.split('/src/');
|
|
224
|
-
const appConfigPath = resolveScriptPath(path.join(projectDir, sourceString, 'src', 'app.config'));
|
|
225
|
-
if (!fs.existsSync(appConfigPath)) {
|
|
226
|
-
return console.log(
|
|
227
|
-
`${chalk.red('x ')}${chalk.grey(`无法获取 ${appConfigPath} 配置文件,请手动到配置文件中补全新页面信息`)}`,
|
|
228
|
-
);
|
|
229
|
-
}
|
|
230
|
-
const configFileContent = fs.readFileSync(appConfigPath, 'utf-8');
|
|
231
|
-
const ast = parse(configFileContent, {
|
|
232
|
-
sourceType: 'module',
|
|
233
|
-
plugins: typescript ? ['typescript'] : [],
|
|
234
|
-
});
|
|
235
|
-
|
|
236
|
-
const callback = (state: ConfigModificationState) => {
|
|
237
|
-
modifyState = state;
|
|
238
|
-
};
|
|
239
|
-
|
|
240
|
-
traverse(ast as any, {
|
|
241
|
-
ExportDefaultDeclaration(path) {
|
|
242
|
-
modifyPagesOrSubPackages({
|
|
243
|
-
path: path as any,
|
|
244
|
-
fullPagePath: pageString,
|
|
245
|
-
subPkgRootPath: subPkg,
|
|
246
|
-
callback,
|
|
247
|
-
});
|
|
248
|
-
},
|
|
249
|
-
});
|
|
250
|
-
|
|
251
|
-
switch (modifyState as ConfigModificationState) {
|
|
252
|
-
case ConfigModificationState.Fail:
|
|
253
|
-
console.log(
|
|
254
|
-
`${chalk.red('x ')}${chalk.grey(`自动补全新页面信息失败, 请手动到 ${appConfigPath} 文件中补全新页面信息`)}`,
|
|
255
|
-
);
|
|
256
|
-
break;
|
|
257
|
-
case ConfigModificationState.Success: {
|
|
258
|
-
const newCode = generate(ast as any, { retainLines: true });
|
|
259
|
-
fs.writeFileSync(appConfigPath, newCode.code);
|
|
260
|
-
console.log(`${chalk.green('✔ ')}${chalk.grey(`新页面信息已在 ${appConfigPath} 文件中自动补全`)}`);
|
|
261
|
-
break;
|
|
262
|
-
}
|
|
263
|
-
case ConfigModificationState.NeedLess:
|
|
264
|
-
console.log(`${chalk.green('✔ ')}${chalk.grey(`新页面信息已存在在 ${appConfigPath} 文件中,不需要补全`)}`);
|
|
265
|
-
break;
|
|
266
|
-
}
|
|
267
|
-
}
|
|
268
|
-
|
|
269
|
-
write() {
|
|
270
|
-
const { projectName, projectDir, template, pageName, isCustomTemplate, customTemplatePath, subPkg, pageDir } = this
|
|
271
|
-
.conf as IPageConf;
|
|
272
|
-
let templatePath;
|
|
273
|
-
|
|
274
|
-
if (isCustomTemplate) {
|
|
275
|
-
templatePath = customTemplatePath;
|
|
276
|
-
} else {
|
|
277
|
-
templatePath = this.templatePath(template);
|
|
278
|
-
}
|
|
279
|
-
|
|
280
|
-
if (!fs.existsSync(templatePath)) return console.log(chalk.red(`创建页面错误:找不到模板${templatePath}`));
|
|
281
|
-
|
|
282
|
-
// 引入模板编写者的自定义逻辑
|
|
283
|
-
const handlerPath = path.join(templatePath, TEMPLATE_CREATOR);
|
|
284
|
-
const basePageFiles = fs.existsSync(handlerPath) ? require(handlerPath).basePageFiles : [];
|
|
285
|
-
const files = Array.isArray(basePageFiles) ? basePageFiles : [];
|
|
286
|
-
const handler = fs.existsSync(handlerPath) ? require(handlerPath).handler : {};
|
|
287
|
-
|
|
288
|
-
this.setPageEntryPath(files, handler);
|
|
289
|
-
|
|
290
|
-
createPageBinding(
|
|
291
|
-
{
|
|
292
|
-
pageDir,
|
|
293
|
-
subPkg,
|
|
294
|
-
projectDir,
|
|
295
|
-
projectName,
|
|
296
|
-
template,
|
|
297
|
-
framework: this.conf.framework,
|
|
298
|
-
css: this.conf.css || CSSType.None,
|
|
299
|
-
typescript: this.conf.typescript,
|
|
300
|
-
compiler: this.conf.compiler,
|
|
301
|
-
templateRoot: getRootPath(),
|
|
302
|
-
version: getPkgVersion(),
|
|
303
|
-
date: this.conf.date,
|
|
304
|
-
description: this.conf.description,
|
|
305
|
-
pageName,
|
|
306
|
-
isCustomTemplate,
|
|
307
|
-
customTemplatePath,
|
|
308
|
-
basePageFiles: files,
|
|
309
|
-
period: PeriodType.CreatePage,
|
|
310
|
-
},
|
|
311
|
-
handler,
|
|
312
|
-
)
|
|
313
|
-
.then(() => {
|
|
314
|
-
console.log(`${chalk.green('✔ ')}${chalk.grey(`创建页面 ${this.conf.pageName} 成功!`)}`);
|
|
315
|
-
this.updateAppConfig();
|
|
316
|
-
this.afterCreate && this.afterCreate(true);
|
|
317
|
-
})
|
|
318
|
-
.catch((err) => {
|
|
319
|
-
console.log(err);
|
|
320
|
-
this.afterCreate && this.afterCreate(false);
|
|
321
|
-
});
|
|
322
|
-
}
|
|
323
|
-
}
|
|
324
|
-
|
|
325
|
-
export type { Page as PageCreator };
|
package/src/create/plugin.ts
DELETED
|
@@ -1,55 +0,0 @@
|
|
|
1
|
-
import * as path from 'node:path';
|
|
2
|
-
|
|
3
|
-
import { createPlugin } from '@spcsn/taro-binding';
|
|
4
|
-
import { chalk, fs } from '@spcsn/taro-helper';
|
|
5
|
-
|
|
6
|
-
import { getRootPath } from '../util';
|
|
7
|
-
import Creator from './creator';
|
|
8
|
-
|
|
9
|
-
export interface IPluginConf {
|
|
10
|
-
pluginName: string;
|
|
11
|
-
type: string;
|
|
12
|
-
description?: string;
|
|
13
|
-
projectDir: string;
|
|
14
|
-
projectName: string;
|
|
15
|
-
template: string;
|
|
16
|
-
version: string;
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
export default class Plugin extends Creator {
|
|
20
|
-
public conf: IPluginConf;
|
|
21
|
-
|
|
22
|
-
constructor(options: IPluginConf) {
|
|
23
|
-
super();
|
|
24
|
-
this.conf = {
|
|
25
|
-
...options,
|
|
26
|
-
projectName: path.basename(options.projectDir),
|
|
27
|
-
version: this.getCliVersion(),
|
|
28
|
-
};
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
getCliVersion() {
|
|
32
|
-
const pkgPath = path.join(this._rootPath, 'package.json');
|
|
33
|
-
const pkg = fs.readJSONSync(pkgPath);
|
|
34
|
-
return pkg.version;
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
async create() {
|
|
38
|
-
const { projectDir, template, pluginName } = this.conf;
|
|
39
|
-
const templatePath = this.templatePath(template);
|
|
40
|
-
|
|
41
|
-
if (!fs.existsSync(templatePath)) {
|
|
42
|
-
console.log(chalk.red(`创建插件失败:找不到模板${templatePath}`));
|
|
43
|
-
return;
|
|
44
|
-
}
|
|
45
|
-
createPlugin({
|
|
46
|
-
projectRoot: projectDir,
|
|
47
|
-
projectName: pluginName,
|
|
48
|
-
templateRoot: getRootPath(),
|
|
49
|
-
template,
|
|
50
|
-
version: this.conf.version,
|
|
51
|
-
description: this.conf.description,
|
|
52
|
-
pluginType: this.conf.type,
|
|
53
|
-
});
|
|
54
|
-
}
|
|
55
|
-
}
|
|
@@ -1,86 +0,0 @@
|
|
|
1
|
-
import * as path from 'node:path';
|
|
2
|
-
|
|
3
|
-
import type { IPluginContext } from '@spcsn/taro-service';
|
|
4
|
-
|
|
5
|
-
export default (ctx: IPluginContext) => {
|
|
6
|
-
ctx.registerCommand({
|
|
7
|
-
name: 'config',
|
|
8
|
-
optionsMap: {
|
|
9
|
-
'--json': '以 JSON 形式输出',
|
|
10
|
-
},
|
|
11
|
-
synopsisList: [
|
|
12
|
-
'taro config set <key> <value>',
|
|
13
|
-
'taro config get <key>',
|
|
14
|
-
'taro config delete <key>',
|
|
15
|
-
'taro config list [--json]',
|
|
16
|
-
],
|
|
17
|
-
fn({ _, options }) {
|
|
18
|
-
const [, cmd, key, value] = _;
|
|
19
|
-
const json = !!options.json;
|
|
20
|
-
const { fs, getUserHomeDir, TARO_CONFIG_FOLDER, TARO_BASE_CONFIG } = ctx.helper;
|
|
21
|
-
const homedir = getUserHomeDir();
|
|
22
|
-
const configPath = path.join(homedir, `${TARO_CONFIG_FOLDER}/${TARO_BASE_CONFIG}`);
|
|
23
|
-
if (!homedir) return console.log('找不到用户根目录');
|
|
24
|
-
|
|
25
|
-
function displayConfigPath(configPath) {
|
|
26
|
-
console.log(`Config path: ${configPath}`);
|
|
27
|
-
console.log();
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
switch (cmd) {
|
|
31
|
-
case 'get':
|
|
32
|
-
if (!key) return console.log('Usage: taro config get <key>');
|
|
33
|
-
if (fs.existsSync(configPath)) {
|
|
34
|
-
displayConfigPath(configPath);
|
|
35
|
-
const config = fs.readJSONSync(configPath);
|
|
36
|
-
console.log(`key: ${key}, value: ${config[key]}`);
|
|
37
|
-
}
|
|
38
|
-
break;
|
|
39
|
-
case 'set':
|
|
40
|
-
if (!key || !value) return console.log('Usage: taro config set <key> <value>');
|
|
41
|
-
|
|
42
|
-
if (fs.existsSync(configPath)) {
|
|
43
|
-
displayConfigPath(configPath);
|
|
44
|
-
const config = fs.readJSONSync(configPath);
|
|
45
|
-
config[key] = value;
|
|
46
|
-
fs.writeJSONSync(configPath, config);
|
|
47
|
-
} else {
|
|
48
|
-
fs.ensureFileSync(configPath);
|
|
49
|
-
fs.writeJSONSync(configPath, {
|
|
50
|
-
[key]: value,
|
|
51
|
-
});
|
|
52
|
-
}
|
|
53
|
-
console.log(`set key: ${key}, value: ${value}`);
|
|
54
|
-
break;
|
|
55
|
-
case 'delete':
|
|
56
|
-
if (!key) return console.log('Usage: taro config delete <key>');
|
|
57
|
-
|
|
58
|
-
if (fs.existsSync(configPath)) {
|
|
59
|
-
displayConfigPath(configPath);
|
|
60
|
-
const config = fs.readJSONSync(configPath);
|
|
61
|
-
delete config[key];
|
|
62
|
-
fs.writeJSONSync(configPath, config);
|
|
63
|
-
}
|
|
64
|
-
console.log(`deleted: ${key}`);
|
|
65
|
-
break;
|
|
66
|
-
case 'list':
|
|
67
|
-
case 'ls':
|
|
68
|
-
if (fs.existsSync(configPath)) {
|
|
69
|
-
displayConfigPath(configPath);
|
|
70
|
-
console.log('Config info:');
|
|
71
|
-
const config = fs.readJSONSync(configPath);
|
|
72
|
-
if (json) {
|
|
73
|
-
console.log(JSON.stringify(config, null, 2));
|
|
74
|
-
} else {
|
|
75
|
-
for (const key in config) {
|
|
76
|
-
console.log(`${key}=${config[key]}`);
|
|
77
|
-
}
|
|
78
|
-
}
|
|
79
|
-
}
|
|
80
|
-
break;
|
|
81
|
-
default:
|
|
82
|
-
break;
|
|
83
|
-
}
|
|
84
|
-
},
|
|
85
|
-
});
|
|
86
|
-
};
|
|
@@ -1,104 +0,0 @@
|
|
|
1
|
-
import * as hooks from '../constant';
|
|
2
|
-
|
|
3
|
-
import type { IPluginContext } from '@spcsn/taro-service';
|
|
4
|
-
import type { TSetCustomTemplateConfig } from '../../create/page';
|
|
5
|
-
|
|
6
|
-
declare const enum createTemplateTypeEnum {
|
|
7
|
-
/**
|
|
8
|
-
* taro页面,taro使用者使用
|
|
9
|
-
*/
|
|
10
|
-
PAGE = 'page',
|
|
11
|
-
/**
|
|
12
|
-
* taro插件,用于扩展命令行
|
|
13
|
-
*/
|
|
14
|
-
PLUGIN_COMMAND = 'plugin-command',
|
|
15
|
-
/**
|
|
16
|
-
* taro插件,用于扩展编译过程
|
|
17
|
-
*/
|
|
18
|
-
PLUGIN_BUILD = 'plugin-build',
|
|
19
|
-
/**
|
|
20
|
-
* taro插件,用于扩展 taro create 自定义模版
|
|
21
|
-
*/
|
|
22
|
-
PLUGIN_TEMPLATE = 'plugin-template',
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
export default (ctx: IPluginContext) => {
|
|
26
|
-
ctx.registerCommand({
|
|
27
|
-
name: 'create',
|
|
28
|
-
optionsMap: {
|
|
29
|
-
'--name [name]': '名称',
|
|
30
|
-
'--dir [dir]': '路径',
|
|
31
|
-
'--subpkg [subpkg]': '分包路径',
|
|
32
|
-
'--description [description]': '介绍',
|
|
33
|
-
'--type [type]': '模版类型(page(默认)|plugin-command|plugin-build|plugin-template)',
|
|
34
|
-
},
|
|
35
|
-
synopsisList: [
|
|
36
|
-
'taro create page',
|
|
37
|
-
'taro create --name=page --description=desc',
|
|
38
|
-
'taro create my-plugin --type=plugin-command',
|
|
39
|
-
],
|
|
40
|
-
fn({ _, options }) {
|
|
41
|
-
const type = options.type || createTemplateTypeEnum.PAGE;
|
|
42
|
-
const name = _[1] || options.name;
|
|
43
|
-
const description = options.description || '';
|
|
44
|
-
const afterCreate = options.afterCreate;
|
|
45
|
-
const templateSource = options.templateSource;
|
|
46
|
-
const framework = options.framework;
|
|
47
|
-
const css = options.css;
|
|
48
|
-
const typescript = options.typescript;
|
|
49
|
-
const clone = options.clone;
|
|
50
|
-
const { chalk } = ctx.helper;
|
|
51
|
-
const { appPath } = ctx.paths;
|
|
52
|
-
|
|
53
|
-
switch (type) {
|
|
54
|
-
case createTemplateTypeEnum.PAGE: {
|
|
55
|
-
if (typeof name !== 'string') {
|
|
56
|
-
return console.log(chalk.red('请输入需要创建的页面名称'));
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
const Page = require('../../create/page').default;
|
|
60
|
-
const page = new Page({
|
|
61
|
-
clone,
|
|
62
|
-
subPkg: options.subpkg,
|
|
63
|
-
framework,
|
|
64
|
-
css,
|
|
65
|
-
typescript,
|
|
66
|
-
pageDir: options.dir,
|
|
67
|
-
pageName: name,
|
|
68
|
-
projectDir: appPath,
|
|
69
|
-
description,
|
|
70
|
-
templateSource,
|
|
71
|
-
afterCreate,
|
|
72
|
-
async modifyCustomTemplateConfig(cb: TSetCustomTemplateConfig) {
|
|
73
|
-
await ctx.applyPlugins({ name: hooks.MODIFY_CREATE_TEMPLATE, opts: cb });
|
|
74
|
-
},
|
|
75
|
-
});
|
|
76
|
-
|
|
77
|
-
page.create();
|
|
78
|
-
break;
|
|
79
|
-
}
|
|
80
|
-
case createTemplateTypeEnum.PLUGIN_COMMAND:
|
|
81
|
-
case createTemplateTypeEnum.PLUGIN_BUILD:
|
|
82
|
-
case createTemplateTypeEnum.PLUGIN_TEMPLATE: {
|
|
83
|
-
if (typeof name !== 'string') {
|
|
84
|
-
return console.log(chalk.red('请输入需要创建的插件名称'));
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
const Plugin = require('../../create/plugin').default;
|
|
88
|
-
const plugin = new Plugin({
|
|
89
|
-
pluginName: name,
|
|
90
|
-
projectDir: appPath,
|
|
91
|
-
type,
|
|
92
|
-
description,
|
|
93
|
-
template: 'plugin-compile',
|
|
94
|
-
});
|
|
95
|
-
|
|
96
|
-
plugin.create();
|
|
97
|
-
break;
|
|
98
|
-
}
|
|
99
|
-
default:
|
|
100
|
-
break;
|
|
101
|
-
}
|
|
102
|
-
},
|
|
103
|
-
});
|
|
104
|
-
};
|
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
validateConfig,
|
|
3
|
-
validateEnv,
|
|
4
|
-
validateEslint,
|
|
5
|
-
validatePackage,
|
|
6
|
-
validateRecommend,
|
|
7
|
-
} from '../../doctor/validators';
|
|
8
|
-
|
|
9
|
-
import type { IPluginContext } from '@spcsn/taro-service';
|
|
10
|
-
|
|
11
|
-
export default (ctx: IPluginContext) => {
|
|
12
|
-
ctx.registerCommand({
|
|
13
|
-
name: 'doctor',
|
|
14
|
-
optionsMap: {},
|
|
15
|
-
synopsisList: ['taro doctor'],
|
|
16
|
-
async fn() {
|
|
17
|
-
await Promise.all([validateEnv(), validateConfig(), validatePackage(), validateRecommend(), validateEslint()]);
|
|
18
|
-
console.log(ctx.helper.chalk.green('基础环境检查通过。'));
|
|
19
|
-
},
|
|
20
|
-
});
|
|
21
|
-
};
|