create-pnpm-cli 0.0.12 → 0.0.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.
Files changed (2) hide show
  1. package/lib/cli.cjs +46 -29
  2. package/package.json +1 -1
package/lib/cli.cjs CHANGED
@@ -35,7 +35,7 @@ __export(cli_exports, {
35
35
  module.exports = __toCommonJS(cli_exports);
36
36
 
37
37
  // package.json
38
- var version = "0.0.11";
38
+ var version = "0.0.13";
39
39
 
40
40
  // src/constants.ts
41
41
  var import_path = __toESM(require("path"), 1);
@@ -54,6 +54,7 @@ var PACKAGE_MAP = {
54
54
 
55
55
  // src/utils.ts
56
56
  var import_child_process = require("child_process");
57
+ var import_fast_glob = __toESM(require("fast-glob"), 1);
57
58
  var import_fs_extra = __toESM(require("fs-extra"), 1);
58
59
  var import_os = __toESM(require("os"), 1);
59
60
  var import_path2 = __toESM(require("path"), 1);
@@ -74,47 +75,73 @@ function cloneRepository(repoUrl, targetPath) {
74
75
  }
75
76
  );
76
77
  }
77
- async function fetchTemplateFromRepo(repoUrl, templateName) {
78
+ function getRepoTempPath(repoUrl) {
78
79
  const tempDir = getTempDir();
79
80
  const [gitRepoUrl] = repoUrl.split("#");
80
- const repoTempPath = import_path2.default.join(
81
+ return import_path2.default.join(
81
82
  tempDir,
82
83
  gitRepoUrl.split("/").pop()?.replace(".git", "") || "unknown"
83
84
  );
84
- const templateTempPath = import_path2.default.join(repoTempPath, "templates", templateName);
85
- await import_fs_extra.default.ensureDir(tempDir);
85
+ }
86
+ async function fetchRepository(repoUrl) {
87
+ const repoTempPath = getRepoTempPath(repoUrl);
88
+ await import_fs_extra.default.ensureDir(import_path2.default.dirname(repoTempPath));
86
89
  if (await import_fs_extra.default.pathExists(repoTempPath)) {
87
90
  await import_fs_extra.default.remove(repoTempPath);
88
91
  }
89
92
  try {
93
+ const [gitRepoUrl] = repoUrl.split("#");
90
94
  cloneRepository(gitRepoUrl, repoTempPath);
91
- if (!await import_fs_extra.default.pathExists(templateTempPath)) {
92
- throw new Error(
93
- `\u6A21\u677F\u76EE\u5F55\u4E0D\u5B58\u5728: templates/${templateName}\uFF0C\u8BF7\u68C0\u67E5\u4ED3\u5E93\u4E2D\u662F\u5426\u5B58\u5728\u8BE5\u6A21\u677F`
94
- );
95
- }
96
- return templateTempPath;
95
+ return repoTempPath;
97
96
  } catch (err) {
98
97
  const errorMessage = err instanceof Error ? err.message : String(err);
99
- throw new Error(`\u4ECE\u4ED3\u5E93\u62C9\u53D6\u6A21\u677F\u5931\u8D25: ${repoUrl}\u3002\u9519\u8BEF\u4FE1\u606F: ${errorMessage}`);
98
+ throw new Error(`\u4ECE\u4ED3\u5E93\u62C9\u53D6\u5931\u8D25: ${repoUrl}\u3002\u9519\u8BEF\u4FE1\u606F: ${errorMessage}`);
99
+ }
100
+ }
101
+ async function getTemplateNames(repoTempPath) {
102
+ const templatesPath = import_path2.default.join(repoTempPath, "templates");
103
+ if (!await import_fs_extra.default.pathExists(templatesPath)) {
104
+ throw new Error("\u4ED3\u5E93\u4E2D\u4E0D\u5B58\u5728 templates \u76EE\u5F55");
100
105
  }
106
+ const names = await (0, import_fast_glob.default)(`**`, {
107
+ deep: 1,
108
+ cwd: templatesPath,
109
+ onlyFiles: false
110
+ });
111
+ return names;
112
+ }
113
+ function getTemplatePath(repoTempPath, templateName) {
114
+ return import_path2.default.join(repoTempPath, "templates", templateName);
101
115
  }
102
116
 
103
117
  // src/cli.ts
104
118
  var import_prompts = require("@inquirer/prompts");
105
119
  var import_commander = require("commander");
106
- var import_fast_glob = __toESM(require("fast-glob"), 1);
107
120
  var import_fs_extra2 = __toESM(require("fs-extra"), 1);
108
121
  var import_package_manager_install = __toESM(require("package-manager-install"), 1);
109
122
  var import_path3 = __toESM(require("path"), 1);
110
123
  var cli = import_commander.program;
111
124
  cli.name("create-pnpm-cli").version(version);
112
125
  cli.command("create", { isDefault: true }).description("create pnpm cli").argument("[path]", "path to mkdir", "./").action(async (targetPath) => {
113
- const names = await (0, import_fast_glob.default)(`**`, {
114
- deep: 1,
115
- cwd: TEMPLATE_PATH,
116
- onlyFiles: false
117
- });
126
+ console.log("\u6B63\u5728\u4ECE\u4ED3\u5E93\u62C9\u53D6\u6A21\u677F...");
127
+ let repoTempPath;
128
+ try {
129
+ repoTempPath = await fetchRepository(TEMPLATE_REPO_URL);
130
+ } catch (err) {
131
+ handleError(err, "\u62C9\u53D6\u4ED3\u5E93\u5931\u8D25");
132
+ return;
133
+ }
134
+ let names;
135
+ try {
136
+ names = await getTemplateNames(repoTempPath);
137
+ } catch (err) {
138
+ handleError(err, "\u8BFB\u53D6\u6A21\u677F\u5217\u8868\u5931\u8D25");
139
+ return;
140
+ }
141
+ if (names.length === 0) {
142
+ handleError(new Error("\u672A\u627E\u5230\u4EFB\u4F55\u6A21\u677F"), "\u6A21\u677F\u5217\u8868\u4E3A\u7A7A");
143
+ return;
144
+ }
118
145
  const template = await (0, import_prompts.select)({
119
146
  message: "\u8BF7\u9009\u62E9\u6A21\u677F",
120
147
  choices: names.map((item) => ({ name: item, value: item }))
@@ -123,18 +150,8 @@ cli.command("create", { isDefault: true }).description("create pnpm cli").argume
123
150
  message: "\u8BF7\u8F93\u5165\u5305\u540D",
124
151
  default: template
125
152
  });
126
- console.log("\u6B63\u5728\u4ECE\u4ED3\u5E93\u62C9\u53D6\u6A21\u677F...");
127
- let templateTempPath;
128
- try {
129
- templateTempPath = await fetchTemplateFromRepo(
130
- TEMPLATE_REPO_URL,
131
- template
132
- );
133
- } catch (err) {
134
- handleError(err, "\u62C9\u53D6\u6A21\u677F\u5931\u8D25");
135
- return;
136
- }
137
153
  const target = import_path3.default.join(targetPath, name);
154
+ const templateTempPath = getTemplatePath(repoTempPath, template);
138
155
  await import_fs_extra2.default.ensureDir(target).catch((err) => handleError(err, "\u521B\u5EFA\u6587\u4EF6\u5939\u5931\u8D25"));
139
156
  console.log("\u6B63\u5728\u62F7\u8D1D\u6587\u4EF6...");
140
157
  await import_fs_extra2.default.copy(templateTempPath, target).catch((err) => handleError(err, "\u62F7\u8D1D\u6587\u4EF6\u9519\u8BEF"));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "create-pnpm-cli",
3
- "version": "0.0.12",
3
+ "version": "0.0.13",
4
4
  "description": "create pnpm cli",
5
5
  "scripts": {
6
6
  "dev": "tsup ./src/cli.ts --dts --clean --shims --format=cjs --platform=node --outDir=./lib --watch",