create-umi 4.2.6-alpha.6 → 4.2.6

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/dist/cli.d.ts ADDED
@@ -0,0 +1 @@
1
+ export {};
package/dist/cli.js ADDED
@@ -0,0 +1,23 @@
1
+ // src/cli.ts
2
+ var import_utils = require("@umijs/utils");
3
+ var args = (0, import_utils.yParser)(process.argv.slice(2), {
4
+ alias: {
5
+ version: ["v"],
6
+ help: ["h"]
7
+ },
8
+ boolean: ["version"]
9
+ });
10
+ if (args.version && !args._[0]) {
11
+ args._[0] = "version";
12
+ const local = (0, import_utils.isLocalDev)() ? import_utils.chalk.cyan("@local") : "";
13
+ const { name, version } = require("../package.json");
14
+ console.log(`${name}@${version}${local}`);
15
+ } else {
16
+ require("./").default({
17
+ cwd: process.cwd(),
18
+ args
19
+ }).catch((err) => {
20
+ console.error(`Create failed, ${err.message}`);
21
+ console.error(err);
22
+ });
23
+ }
@@ -0,0 +1,45 @@
1
+ import { yParser } from '@umijs/utils';
2
+ import { type UmiTemplate } from './template';
3
+ interface ITemplateArgs {
4
+ template?: UmiTemplate;
5
+ }
6
+ interface IArgs extends yParser.Arguments, ITemplateArgs {
7
+ default?: boolean;
8
+ git?: boolean;
9
+ install?: boolean;
10
+ }
11
+ interface ITemplatePluginParams {
12
+ pluginName?: string;
13
+ }
14
+ interface ITemplateParams extends ITemplatePluginParams {
15
+ version: string;
16
+ npmClient: ENpmClient;
17
+ registry: string;
18
+ author: string;
19
+ email: string;
20
+ withHusky: boolean;
21
+ extraNpmrc: string;
22
+ }
23
+ declare enum ENpmClient {
24
+ npm = "npm",
25
+ cnpm = "cnpm",
26
+ tnpm = "tnpm",
27
+ yarn = "yarn",
28
+ pnpm = "pnpm"
29
+ }
30
+ declare enum ETemplate {
31
+ app = "app",
32
+ max = "max",
33
+ vueApp = "vue-app",
34
+ plugin = "plugin"
35
+ }
36
+ export interface IDefaultData extends ITemplateParams {
37
+ appTemplate?: ETemplate;
38
+ }
39
+ interface IGeneratorOpts {
40
+ cwd: string;
41
+ args: IArgs;
42
+ defaultData?: IDefaultData;
43
+ }
44
+ declare const _default: ({ cwd, args, defaultData, }: IGeneratorOpts) => Promise<void>;
45
+ export default _default;
package/dist/index.js ADDED
@@ -0,0 +1,296 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
+ var __getOwnPropNames = Object.getOwnPropertyNames;
4
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
5
+ var __export = (target, all) => {
6
+ for (var name in all)
7
+ __defProp(target, name, { get: all[name], enumerable: true });
8
+ };
9
+ var __copyProps = (to, from, except, desc) => {
10
+ if (from && typeof from === "object" || typeof from === "function") {
11
+ for (let key of __getOwnPropNames(from))
12
+ if (!__hasOwnProp.call(to, key) && key !== except)
13
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
14
+ }
15
+ return to;
16
+ };
17
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
18
+
19
+ // src/index.ts
20
+ var src_exports = {};
21
+ __export(src_exports, {
22
+ default: () => src_default
23
+ });
24
+ module.exports = __toCommonJS(src_exports);
25
+ var import_utils = require("@umijs/utils");
26
+ var import_fs = require("fs");
27
+ var import_path = require("path");
28
+ var import_template = require("./template");
29
+ var pkg = require("../package");
30
+ var DEFAULT_DATA = {
31
+ pluginName: "umi-plugin-demo",
32
+ email: "i@domain.com",
33
+ author: "umijs",
34
+ version: pkg.version,
35
+ npmClient: "pnpm" /* pnpm */,
36
+ registry: import_template.ERegistry.npm,
37
+ withHusky: false,
38
+ extraNpmrc: "",
39
+ appTemplate: "app" /* app */
40
+ };
41
+ var src_default = async ({
42
+ cwd,
43
+ args,
44
+ defaultData = DEFAULT_DATA
45
+ }) => {
46
+ let [name] = args._;
47
+ let npmClient = "pnpm" /* pnpm */;
48
+ let registry = import_template.ERegistry.npm;
49
+ let appTemplate = (defaultData == null ? void 0 : defaultData.appTemplate) || "app" /* app */;
50
+ const { username, email } = await (0, import_utils.getGitInfo)();
51
+ const author = email && username ? `${username} <${email}>` : "";
52
+ let pluginName = `umi-plugin-${name || "demo"}`;
53
+ const target = name ? (0, import_path.join)(cwd, name) : cwd;
54
+ const { isCancel, text, select, intro, outro } = import_utils.clackPrompts;
55
+ const exitPrompt = () => {
56
+ outro(import_utils.chalk.red("Exit create-umi"));
57
+ process.exit(1);
58
+ };
59
+ const selectAppTemplate = async () => {
60
+ appTemplate = await select({
61
+ message: "Pick Umi App Template",
62
+ options: [
63
+ { label: "Simple App", value: "app" /* app */ },
64
+ {
65
+ label: "Ant Design Pro",
66
+ value: "max" /* max */,
67
+ hint: "more plugins and ready to use features"
68
+ },
69
+ { label: "Vue Simple App", value: "vue-app" /* vueApp */ },
70
+ {
71
+ label: "Umi Plugin",
72
+ value: "plugin" /* plugin */,
73
+ hint: "for plugin development"
74
+ }
75
+ ],
76
+ initialValue: "app" /* app */
77
+ });
78
+ };
79
+ const selectNpmClient = async () => {
80
+ npmClient = await select({
81
+ message: "Pick Npm Client",
82
+ options: [
83
+ { label: "npm" /* npm */, value: "npm" /* npm */ },
84
+ { label: "cnpm" /* cnpm */, value: "cnpm" /* cnpm */ },
85
+ { label: "tnpm" /* tnpm */, value: "tnpm" /* tnpm */ },
86
+ { label: "yarn" /* yarn */, value: "yarn" /* yarn */ },
87
+ { label: "pnpm" /* pnpm */, value: "pnpm" /* pnpm */, hint: "recommended" }
88
+ ],
89
+ initialValue: "pnpm" /* pnpm */
90
+ });
91
+ };
92
+ const selectRegistry = async () => {
93
+ registry = await select({
94
+ message: "Pick Npm Registry",
95
+ options: [
96
+ {
97
+ label: "npm",
98
+ value: import_template.ERegistry.npm
99
+ },
100
+ {
101
+ label: "taobao",
102
+ value: import_template.ERegistry.taobao,
103
+ hint: "recommended for China"
104
+ }
105
+ ],
106
+ initialValue: import_template.ERegistry.npm
107
+ });
108
+ };
109
+ const internalTemplatePrompts = async () => {
110
+ intro(import_utils.chalk.bgHex("#19BDD2")(" create-umi "));
111
+ await selectAppTemplate();
112
+ if (isCancel(appTemplate)) {
113
+ exitPrompt();
114
+ }
115
+ await selectNpmClient();
116
+ if (isCancel(npmClient)) {
117
+ exitPrompt();
118
+ }
119
+ await selectRegistry();
120
+ if (isCancel(registry)) {
121
+ exitPrompt();
122
+ }
123
+ const isPlugin = appTemplate === "plugin" /* plugin */;
124
+ if (isPlugin) {
125
+ pluginName = await text({
126
+ message: `What's the plugin name?`,
127
+ placeholder: pluginName,
128
+ validate: (value) => {
129
+ if (!(value == null ? void 0 : value.length)) {
130
+ return "Please input plugin name";
131
+ }
132
+ }
133
+ });
134
+ if (isCancel(pluginName)) {
135
+ exitPrompt();
136
+ }
137
+ }
138
+ outro(import_utils.chalk.green(`You're all set!`));
139
+ };
140
+ const useDefaultData = !!args.default;
141
+ const useExternalTemplate = !!args.template;
142
+ switch (true) {
143
+ case useExternalTemplate:
144
+ await selectNpmClient();
145
+ if (isCancel(npmClient)) {
146
+ exitPrompt();
147
+ }
148
+ await selectRegistry();
149
+ if (isCancel(registry)) {
150
+ exitPrompt();
151
+ }
152
+ await (0, import_template.unpackTemplate)({
153
+ template: args.template,
154
+ dest: target,
155
+ registry
156
+ });
157
+ break;
158
+ default:
159
+ if (!useDefaultData) {
160
+ await internalTemplatePrompts();
161
+ }
162
+ }
163
+ const version = pkg.version;
164
+ const monorepoRoot = await detectMonorepoRoot({ target });
165
+ const inMonorepo = !!monorepoRoot;
166
+ const projectRoot = inMonorepo ? monorepoRoot : target;
167
+ const shouldInitGit = args.git !== false;
168
+ const withHusky = shouldInitGit && !inMonorepo;
169
+ let pnpmExtraNpmrc = "";
170
+ const isPnpm = npmClient === "pnpm" /* pnpm */;
171
+ let pnpmMajorVersion;
172
+ let pnpmVersion;
173
+ if (isPnpm) {
174
+ pnpmVersion = await getPnpmVersion();
175
+ pnpmMajorVersion = parseInt(pnpmVersion.split(".")[0], 10);
176
+ import_utils.logger.debug(`pnpm version: ${pnpmVersion}`);
177
+ if (pnpmMajorVersion === 7) {
178
+ pnpmExtraNpmrc = `strict-peer-dependencies=false`;
179
+ }
180
+ }
181
+ const injectInternalTemplateFiles = async () => {
182
+ const generator = new import_utils.BaseGenerator({
183
+ path: (0, import_path.join)(__dirname, "..", "templates", appTemplate),
184
+ target,
185
+ slient: true,
186
+ data: useDefaultData ? defaultData : {
187
+ version: version.includes("-canary.") ? version : `^${version}`,
188
+ npmClient,
189
+ registry,
190
+ author,
191
+ email,
192
+ withHusky,
193
+ extraNpmrc: isPnpm ? pnpmExtraNpmrc : "",
194
+ pluginName
195
+ }
196
+ });
197
+ await generator.run();
198
+ };
199
+ if (!useExternalTemplate) {
200
+ await injectInternalTemplateFiles();
201
+ }
202
+ const context = {
203
+ inMonorepo,
204
+ target,
205
+ projectRoot
206
+ };
207
+ if (!withHusky) {
208
+ await removeHusky(context);
209
+ }
210
+ if (inMonorepo) {
211
+ await moveNpmrc(context);
212
+ }
213
+ if (shouldInitGit) {
214
+ await initGit(context);
215
+ } else {
216
+ import_utils.logger.info(`Skip Git init`);
217
+ }
218
+ const isPnpm8 = pnpmMajorVersion === 8;
219
+ const pnpmHighestResolutionMinVersion = "8.7.0";
220
+ const isPnpmHighestResolution = isPnpm8 && import_utils.semver.gte(pnpmVersion, pnpmHighestResolutionMinVersion);
221
+ if (!useDefaultData && args.install !== false) {
222
+ if (isPnpm8 && !isPnpmHighestResolution) {
223
+ await installAndUpdateWithPnpm(target);
224
+ } else {
225
+ (0, import_utils.installWithNpmClient)({ npmClient, cwd: target });
226
+ }
227
+ } else {
228
+ import_utils.logger.info(`Skip install deps`);
229
+ if (isPnpm8) {
230
+ import_utils.logger.warn(
231
+ import_utils.chalk.yellow(
232
+ `You current using pnpm v8, it will install minimal version of dependencies`
233
+ )
234
+ );
235
+ import_utils.logger.warn(
236
+ import_utils.chalk.green(
237
+ `Recommended that you run ${import_utils.chalk.bold.cyan(
238
+ "pnpm up -L"
239
+ )} to install latest version of dependencies`
240
+ )
241
+ );
242
+ }
243
+ }
244
+ };
245
+ async function detectMonorepoRoot(opts) {
246
+ const { target } = opts;
247
+ const rootPkg = await import_utils.pkgUp.pkgUp({ cwd: (0, import_path.dirname)(target) });
248
+ if (!rootPkg) {
249
+ return null;
250
+ }
251
+ const rootDir = (0, import_path.dirname)(rootPkg);
252
+ if ((0, import_utils.tryPaths)([
253
+ (0, import_path.join)(rootDir, "lerna.json"),
254
+ (0, import_path.join)(rootDir, "pnpm-workspace.yaml")
255
+ ])) {
256
+ return rootDir;
257
+ }
258
+ return null;
259
+ }
260
+ async function moveNpmrc(opts) {
261
+ const { target, projectRoot } = opts;
262
+ const sourceNpmrc = (0, import_path.join)(target, "./.npmrc");
263
+ const targetNpmrc = (0, import_path.join)(projectRoot, "./.npmrc");
264
+ if (!(0, import_fs.existsSync)(targetNpmrc)) {
265
+ await import_utils.fsExtra.copyFile(sourceNpmrc, targetNpmrc);
266
+ }
267
+ await import_utils.fsExtra.remove(sourceNpmrc);
268
+ }
269
+ async function initGit(opts) {
270
+ const { projectRoot } = opts;
271
+ const isGit = (0, import_fs.existsSync)((0, import_path.join)(projectRoot, ".git"));
272
+ if (isGit)
273
+ return;
274
+ try {
275
+ await import_utils.execa.execa("git", ["init"], { cwd: projectRoot });
276
+ } catch {
277
+ import_utils.logger.error(`Initial the git repo failed`);
278
+ }
279
+ }
280
+ async function removeHusky(opts) {
281
+ const dir = (0, import_path.join)(opts.target, "./.husky");
282
+ if ((0, import_fs.existsSync)(dir)) {
283
+ await import_utils.fsExtra.remove(dir);
284
+ }
285
+ }
286
+ async function installAndUpdateWithPnpm(cwd) {
287
+ await import_utils.execa.execa("pnpm", ["up", "-L"], { cwd, stdio: "inherit" });
288
+ }
289
+ async function getPnpmVersion() {
290
+ try {
291
+ const { stdout } = await import_utils.execa.execa("pnpm", ["--version"]);
292
+ return stdout.trim();
293
+ } catch (e) {
294
+ throw new Error("Please install pnpm first", { cause: e });
295
+ }
296
+ }
@@ -0,0 +1,12 @@
1
+ export declare enum ERegistry {
2
+ npm = "https://registry.npmjs.com/",
3
+ taobao = "https://registry.npmmirror.com/"
4
+ }
5
+ export declare type UmiTemplate = `@umijs/${string}-template`;
6
+ interface IUnpackTemplateOpts {
7
+ template: UmiTemplate;
8
+ dest: string;
9
+ registry: ERegistry;
10
+ }
11
+ export declare const unpackTemplate: (opts: IUnpackTemplateOpts) => Promise<string>;
12
+ export {};
@@ -0,0 +1,113 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
+ var __getOwnPropNames = Object.getOwnPropertyNames;
4
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
5
+ var __export = (target, all) => {
6
+ for (var name in all)
7
+ __defProp(target, name, { get: all[name], enumerable: true });
8
+ };
9
+ var __copyProps = (to, from, except, desc) => {
10
+ if (from && typeof from === "object" || typeof from === "function") {
11
+ for (let key of __getOwnPropNames(from))
12
+ if (!__hasOwnProp.call(to, key) && key !== except)
13
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
14
+ }
15
+ return to;
16
+ };
17
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
18
+
19
+ // src/template.ts
20
+ var template_exports = {};
21
+ __export(template_exports, {
22
+ ERegistry: () => ERegistry,
23
+ unpackTemplate: () => unpackTemplate
24
+ });
25
+ module.exports = __toCommonJS(template_exports);
26
+ var import_utils = require("@umijs/utils");
27
+ var import_tar = require("@umijs/utils/compiled/tar");
28
+ var ERegistry = /* @__PURE__ */ ((ERegistry2) => {
29
+ ERegistry2["npm"] = "https://registry.npmjs.com/";
30
+ ERegistry2["taobao"] = "https://registry.npmmirror.com/";
31
+ return ERegistry2;
32
+ })(ERegistry || {});
33
+ var unpackTemplate = async (opts) => {
34
+ const { template, dest, registry } = opts;
35
+ import_utils.logger.info(
36
+ `Init a new project with template ${import_utils.chalk.blue(template)} from npm ...`
37
+ );
38
+ const tryDownload = async (name) => {
39
+ const url = await getNpmPkgTarUrl({ registry, name });
40
+ if (!url) {
41
+ return;
42
+ }
43
+ try {
44
+ return await downloadTar({ dest, url });
45
+ } catch (e) {
46
+ throw new Error(`Download ${name} failed from ${registry}`, { cause: e });
47
+ }
48
+ };
49
+ const nameList = [];
50
+ const isStartWithUmi = template.startsWith("@umijs/");
51
+ if (template.endsWith("-template")) {
52
+ if (isStartWithUmi) {
53
+ nameList.push(template);
54
+ } else {
55
+ nameList.push(`@umijs/${template}`);
56
+ }
57
+ } else if (isStartWithUmi) {
58
+ nameList.push(`${template}-template`);
59
+ } else {
60
+ nameList.push(`@umijs/${template}-template`);
61
+ }
62
+ for await (const name of nameList) {
63
+ const success = await tryDownload(name);
64
+ if (success) {
65
+ import_utils.logger.ready(`Init ${import_utils.chalk.green(name)} success`);
66
+ return success;
67
+ }
68
+ }
69
+ throw new Error(
70
+ `Template ${nameList.map((i) => import_utils.chalk.yellow(i)).join(", ")} not found from ${registry}`
71
+ );
72
+ };
73
+ async function getNpmPkgTarUrl(opts) {
74
+ var _a;
75
+ const { registry, name } = opts;
76
+ const nameWithoutScope = name.startsWith("@") ? name.split("/")[1] : name;
77
+ const latestPkgInfoUrl = `${registry}${name}/latest`;
78
+ const res = await import_utils.axios.get(latestPkgInfoUrl, { validateStatus: () => true });
79
+ const latestVersion = (_a = res == null ? void 0 : res.data) == null ? void 0 : _a.version;
80
+ if (!latestVersion) {
81
+ return;
82
+ }
83
+ const latestTarUrl = `${registry}${name}/-/${nameWithoutScope}-${latestVersion}.tgz`;
84
+ return latestTarUrl;
85
+ }
86
+ async function downloadTar(opts) {
87
+ const { dest, url } = opts;
88
+ return new Promise(async (resolve, reject) => {
89
+ try {
90
+ const res = await import_utils.axios.get(url, {
91
+ responseType: "stream"
92
+ });
93
+ import_utils.fsExtra.mkdirpSync(dest);
94
+ res.data.pipe(
95
+ (0, import_tar.x)({
96
+ C: dest,
97
+ strip: 1
98
+ })
99
+ );
100
+ resolve(dest);
101
+ } catch (e) {
102
+ if (import_utils.fsExtra.existsSync(dest)) {
103
+ import_utils.fsExtra.removeSync(dest);
104
+ }
105
+ reject(e);
106
+ }
107
+ });
108
+ }
109
+ // Annotate the CommonJS export names for ESM import in node:
110
+ 0 && (module.exports = {
111
+ ERegistry,
112
+ unpackTemplate
113
+ });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "create-umi",
3
- "version": "4.2.6-alpha.6",
3
+ "version": "4.2.6",
4
4
  "description": "create-umi",
5
5
  "homepage": "https://github.com/umijs/umi/tree/master/packages/create-umi#readme",
6
6
  "bugs": "https://github.com/umijs/umi/issues",
@@ -19,7 +19,7 @@
19
19
  "templates"
20
20
  ],
21
21
  "dependencies": {
22
- "@umijs/utils": "4.2.6-alpha.6"
22
+ "@umijs/utils": "4.2.6"
23
23
  },
24
24
  "publishConfig": {
25
25
  "access": "public"