@spcsn/taro-cli 0.1.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/LICENSE +174 -0
- package/README.md +0 -0
- package/bin/taro +7 -0
- package/dist/cli.d.ts +6 -0
- package/dist/cli.js +266 -0
- package/dist/cli.js.map +1 -0
- package/dist/commands/customCommand.d.ts +5 -0
- package/dist/commands/customCommand.js +23 -0
- package/dist/commands/customCommand.js.map +1 -0
- package/dist/config/index.d.ts +8 -0
- package/dist/config/index.js +10 -0
- package/dist/config/index.js.map +1 -0
- package/dist/config/packagesManagement.d.ts +19 -0
- package/dist/config/packagesManagement.js +22 -0
- package/dist/config/packagesManagement.js.map +1 -0
- package/dist/create/constants.d.ts +1 -0
- package/dist/create/constants.js +5 -0
- package/dist/create/constants.js.map +1 -0
- package/dist/create/creator.d.ts +9 -0
- package/dist/create/creator.js +64 -0
- package/dist/create/creator.js.map +1 -0
- package/dist/create/fetchTemplate.d.ts +8 -0
- package/dist/create/fetchTemplate.js +169 -0
- package/dist/create/fetchTemplate.js.map +1 -0
- package/dist/create/page.d.ts +65 -0
- package/dist/create/page.js +266 -0
- package/dist/create/page.js.map +1 -0
- package/dist/create/plugin.d.ts +16 -0
- package/dist/create/plugin.js +77 -0
- package/dist/create/plugin.js.map +1 -0
- package/dist/create/project.d.ts +70 -0
- package/dist/create/project.js +481 -0
- package/dist/create/project.js.map +1 -0
- package/dist/doctor/index.d.ts +4 -0
- package/dist/doctor/index.js +23 -0
- package/dist/doctor/index.js.map +1 -0
- package/dist/index.d.ts +16 -0
- package/dist/index.js +24 -0
- package/dist/index.js.map +1 -0
- package/dist/presets/commands/build.d.ts +3 -0
- package/dist/presets/commands/build.js +236 -0
- package/dist/presets/commands/build.js.map +1 -0
- package/dist/presets/commands/config.d.ts +3 -0
- package/dist/presets/commands/config.js +121 -0
- package/dist/presets/commands/config.js.map +1 -0
- package/dist/presets/commands/create.d.ts +3 -0
- package/dist/presets/commands/create.js +112 -0
- package/dist/presets/commands/create.js.map +1 -0
- package/dist/presets/commands/global-config.d.ts +3 -0
- package/dist/presets/commands/global-config.js +175 -0
- package/dist/presets/commands/global-config.js.map +1 -0
- package/dist/presets/commands/help.d.ts +3 -0
- package/dist/presets/commands/help.js +16 -0
- package/dist/presets/commands/help.js.map +1 -0
- package/dist/presets/commands/info.d.ts +3 -0
- package/dist/presets/commands/info.js +96 -0
- package/dist/presets/commands/info.js.map +1 -0
- package/dist/presets/commands/init.d.ts +3 -0
- package/dist/presets/commands/init.js +46 -0
- package/dist/presets/commands/init.js.map +1 -0
- package/dist/presets/commands/inspect.d.ts +3 -0
- package/dist/presets/commands/inspect.js +155 -0
- package/dist/presets/commands/inspect.js.map +1 -0
- package/dist/presets/commands/update.d.ts +3 -0
- package/dist/presets/commands/update.js +187 -0
- package/dist/presets/commands/update.js.map +1 -0
- package/dist/presets/constant/hooks.d.ts +13 -0
- package/dist/presets/constant/hooks.js +17 -0
- package/dist/presets/constant/hooks.js.map +1 -0
- package/dist/presets/constant/index.d.ts +1 -0
- package/dist/presets/constant/index.js +18 -0
- package/dist/presets/constant/index.js.map +1 -0
- package/dist/presets/files/generateFrameworkInfo.d.ts +3 -0
- package/dist/presets/files/generateFrameworkInfo.js +30 -0
- package/dist/presets/files/generateFrameworkInfo.js.map +1 -0
- package/dist/presets/files/generateProjectConfig.d.ts +3 -0
- package/dist/presets/files/generateProjectConfig.js +72 -0
- package/dist/presets/files/generateProjectConfig.js.map +1 -0
- package/dist/presets/files/writeFileToDist.d.ts +3 -0
- package/dist/presets/files/writeFileToDist.js +50 -0
- package/dist/presets/files/writeFileToDist.js.map +1 -0
- package/dist/presets/hooks/build.d.ts +3 -0
- package/dist/presets/hooks/build.js +55 -0
- package/dist/presets/hooks/build.js.map +1 -0
- package/dist/presets/hooks/create.d.ts +3 -0
- package/dist/presets/hooks/create.js +42 -0
- package/dist/presets/hooks/create.js.map +1 -0
- package/dist/presets/index.d.ts +4 -0
- package/dist/presets/index.js +50 -0
- package/dist/presets/index.js.map +1 -0
- package/dist/presets/platforms/plugin.d.ts +3 -0
- package/dist/presets/platforms/plugin.js +106 -0
- package/dist/presets/platforms/plugin.js.map +1 -0
- package/dist/presets/platforms/rn.d.ts +3 -0
- package/dist/presets/platforms/rn.js +130 -0
- package/dist/presets/platforms/rn.js.map +1 -0
- package/dist/util/appConfig.d.ts +11 -0
- package/dist/util/appConfig.js +27 -0
- package/dist/util/appConfig.js.map +1 -0
- package/dist/util/createPage.d.ts +9 -0
- package/dist/util/createPage.js +114 -0
- package/dist/util/createPage.js.map +1 -0
- package/dist/util/defineConfig.d.ts +17 -0
- package/dist/util/defineConfig.js +11 -0
- package/dist/util/defineConfig.js.map +1 -0
- package/dist/util/index.d.ts +23 -0
- package/dist/util/index.js +149 -0
- package/dist/util/index.js.map +1 -0
- package/dist/util/types.d.ts +58 -0
- package/dist/util/types.js +3 -0
- package/dist/util/types.js.map +1 -0
- package/global.d.ts +4 -0
- package/index.js +3 -0
- package/package.json +74 -0
- package/postinstall.js +17 -0
- package/src/__tests__/__mocks__/presets.ts +15 -0
- package/src/__tests__/build-config.spec.ts +66 -0
- package/src/__tests__/cli.spec.ts +226 -0
- package/src/__tests__/config.spec.ts +224 -0
- package/src/__tests__/doctor-config.spec.ts +943 -0
- package/src/__tests__/doctor-recommand.spec.ts +136 -0
- package/src/__tests__/doctor.spec.ts +94 -0
- package/src/__tests__/dotenv-parse.spec.ts +105 -0
- package/src/__tests__/env/.env +2 -0
- package/src/__tests__/fixtures/default/.env +3 -0
- package/src/__tests__/fixtures/default/.env.development +1 -0
- package/src/__tests__/fixtures/default/.env.local +1 -0
- package/src/__tests__/fixtures/default/.env.pre +3 -0
- package/src/__tests__/fixtures/default/.env.production +1 -0
- package/src/__tests__/fixtures/default/.env.uat +5 -0
- package/src/__tests__/fixtures/default/.env.uat.local +1 -0
- package/src/__tests__/fixtures/default/babel.config.js +10 -0
- package/src/__tests__/fixtures/default/config/dev.js +9 -0
- package/src/__tests__/fixtures/default/config/index.js +84 -0
- package/src/__tests__/fixtures/default/config/prod.js +18 -0
- package/src/__tests__/fixtures/default/package.json +27 -0
- package/src/__tests__/fixtures/default/src/app.config.js +11 -0
- package/src/__tests__/fixtures/default/src/app.js +16 -0
- package/src/__tests__/fixtures/default/src/app.scss +0 -0
- package/src/__tests__/fixtures/default/src/index.html +19 -0
- package/src/__tests__/fixtures/default/src/pages/index/index.config.js +3 -0
- package/src/__tests__/fixtures/default/src/pages/index/index.jsx +22 -0
- package/src/__tests__/fixtures/default/src/pages/index/index.scss +0 -0
- package/src/__tests__/info.spec.ts +72 -0
- package/src/__tests__/inspect.spec.ts +160 -0
- package/src/__tests__/update.spec.ts +310 -0
- package/src/__tests__/utils/index.ts +54 -0
- package/src/cli.ts +240 -0
- package/src/commands/customCommand.ts +22 -0
- package/src/config/index.ts +7 -0
- package/src/config/manifest.default.json +33 -0
- package/src/config/packagesManagement.ts +20 -0
- package/src/config/rn-stylelint.json +4 -0
- package/src/config/tsconfig.json +22 -0
- package/src/create/constants.ts +1 -0
- package/src/create/creator.ts +37 -0
- package/src/create/fetchTemplate.ts +161 -0
- package/src/create/page.ts +325 -0
- package/src/create/plugin.ts +55 -0
- package/src/create/project.ts +539 -0
- package/src/doctor/index.ts +21 -0
- package/src/index.ts +24 -0
- package/src/presets/commands/build.ts +215 -0
- package/src/presets/commands/config.ts +86 -0
- package/src/presets/commands/create.ts +104 -0
- package/src/presets/commands/global-config.ts +140 -0
- package/src/presets/commands/help.ts +17 -0
- package/src/presets/commands/info.ts +81 -0
- package/src/presets/commands/init.ts +63 -0
- package/src/presets/commands/inspect.ts +137 -0
- package/src/presets/commands/update.ts +175 -0
- package/src/presets/constant/hooks.ts +14 -0
- package/src/presets/constant/index.ts +1 -0
- package/src/presets/files/generateFrameworkInfo.ts +30 -0
- package/src/presets/files/generateProjectConfig.ts +41 -0
- package/src/presets/files/writeFileToDist.ts +17 -0
- package/src/presets/hooks/build.ts +22 -0
- package/src/presets/hooks/create.ts +9 -0
- package/src/presets/index.ts +15 -0
- package/src/presets/platforms/plugin.ts +75 -0
- package/src/presets/platforms/rn.ts +125 -0
- package/src/util/appConfig.ts +32 -0
- package/src/util/createPage.ts +139 -0
- package/src/util/defineConfig.ts +28 -0
- package/src/util/index.ts +126 -0
- package/src/util/types.ts +81 -0
- package/templates/default/_editorconfig +12 -0
- package/templates/default/_env.development +2 -0
- package/templates/default/_env.production +1 -0
- package/templates/default/_env.test +1 -0
- package/templates/default/_gitignore +8 -0
- package/templates/default/babel.config.js +14 -0
- package/templates/default/config/dev.js +5 -0
- package/templates/default/config/index.js +61 -0
- package/templates/default/config/prod.js +5 -0
- package/templates/default/package.json.tmpl +52 -0
- package/templates/default/project.config.json +15 -0
- package/templates/default/src/app.config.js +11 -0
- package/templates/default/src/app.css +0 -0
- package/templates/default/src/app.js +15 -0
- package/templates/default/src/pages/index/index.config.js +3 -0
- package/templates/default/src/pages/index/index.css +0 -0
- package/templates/default/src/pages/index/index.jsx +15 -0
- package/templates/default/template_creator.js +61 -0
- package/templates/default/tsconfig.json +30 -0
- package/templates/default/types/global.d.ts +27 -0
- package/templates/global-config/index.json +4 -0
- package/templates/global-config/package.json +5 -0
- package/templates/plugin-compile/README.md +32 -0
- package/templates/plugin-compile/index.js +3 -0
- package/templates/plugin-compile/package.json.tmpl +43 -0
- package/templates/plugin-compile/src/index.ts +153 -0
- package/templates/plugin-compile/tsconfig.json +28 -0
|
@@ -0,0 +1,325 @@
|
|
|
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 };
|
|
@@ -0,0 +1,55 @@
|
|
|
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
|
+
}
|