zixulu 0.0.1 → 0.0.3

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 (3) hide show
  1. package/dist/index.js +71 -42
  2. package/package.json +1 -1
  3. package/src/index.ts +69 -38
package/dist/index.js CHANGED
@@ -2,9 +2,10 @@
2
2
  import { Command } from 'commander';
3
3
  import { createPromptModule } from 'inquirer';
4
4
  import { readdirSync, mkdirSync, writeFileSync, statSync, readFileSync } from 'fs';
5
+ import { execSync } from 'child_process';
5
6
 
6
- var name = "deep-sea";
7
- var version = "0.0.1";
7
+ var name = "zixulu";
8
+ var version = "0.0.2";
8
9
  var main = "index.js";
9
10
  var license = "MIT";
10
11
  var type = "module";
@@ -41,6 +42,11 @@ var pkg = {
41
42
  const prompt = createPromptModule();
42
43
  const packageNameReg = /^[a-z0-9]{1}[-_a-z0-9]{0,213}$/i;
43
44
  const JavaScriptTargets = ["ES2015", "ES2016", "ES2017", "ES2018", "ES2019", "ES2020", "ES2021", "ES2022", "ES2023", "ESNext"];
45
+ async function getPackageLatestVersion(name) {
46
+ const response = await fetch(`https://registry.npmjs.org/${name}`);
47
+ const data = await response.json();
48
+ return data["dist-tags"].latest;
49
+ }
44
50
  function getBuildJS(answer) {
45
51
  const { target, css } = answer;
46
52
  if (css) {
@@ -97,37 +103,41 @@ execSync("npx tsc --sourceMap -module ${target} -outDir dist/esm && npx tsc --so
97
103
  `;
98
104
  }
99
105
  async function getPackageJson(answer) {
100
- const { name } = answer;
101
- const response = await fetch("https://registry.npmjs.org/typescript");
102
- const data = await response.json();
103
- return `{
104
- "name": "${name}",
105
- "version": "0.0.1",
106
- "main": "dist/cjs/index.js",
107
- "module": "dist/esm/index.js",
108
- "types": "dist/index.d.ts",
109
- "license": "MIT",
110
- "type": "module",
111
- "exports": {
112
- ".": {
113
- "import": {
114
- "types": "./dist/index.d.ts",
115
- "default": "./dist/esm/index.js"
116
- },
117
- "require": {
118
- "types": "./dist/index.d.ts",
119
- "default": "./dist/cjs/index.js"
120
- }
121
- }
122
- },
123
- "scripts": {
124
- "build": "node ./build.js"
125
- },
126
- "devDependencies": {
127
- "typescript": "^${data["dist-tags"].latest}"
128
- }
129
- }
130
- `;
106
+ const { name, react, esm, cjs } = answer;
107
+ const info = {
108
+ name,
109
+ version: "0.0.1",
110
+ main: "dist/cjs/index.js",
111
+ module: "dist/esm/index.js",
112
+ types: "dist/index.d.ts",
113
+ license: "MIT",
114
+ type: "module",
115
+ exports: {
116
+ ".": {}
117
+ },
118
+ scripts: {
119
+ build: "node ./build.js"
120
+ },
121
+ devDependencies: {}
122
+ };
123
+ if (esm) {
124
+ info.exports["."]["import"] = {
125
+ types: "./dist/index.d.ts",
126
+ default: "./dist/esm/index.js"
127
+ };
128
+ }
129
+ if (cjs) {
130
+ info.exports["."]["require"] = {
131
+ types: "./dist/index.d.ts",
132
+ default: "./dist/cjs/index.js"
133
+ };
134
+ }
135
+ if (react) {
136
+ info.devDependencies["@types/react"] = `^${await getPackageLatestVersion("@types/react")}`;
137
+ info.devDependencies["@types/react-dom"] = `^${await getPackageLatestVersion("@types/react-dom")}`;
138
+ }
139
+ info.devDependencies.typescript = `^${await getPackageLatestVersion("typescript")}`;
140
+ return JSON.stringify(info, null, 4);
131
141
  }
132
142
  function getTSConfig(answer) {
133
143
  const { react, target } = answer;
@@ -277,7 +287,7 @@ function getProjectAnswer(...exclude) {
277
287
  type: "confirm",
278
288
  name: "css",
279
289
  message: "是否打包 CSS:",
280
- default: true
290
+ default: false
281
291
  },
282
292
  {
283
293
  type: "confirm",
@@ -290,6 +300,13 @@ function getProjectAnswer(...exclude) {
290
300
  name: "cjs",
291
301
  message: "是否打包 Common JS:",
292
302
  default: true
303
+ },
304
+ {
305
+ type: "list",
306
+ name: "install",
307
+ message: "现在就安装依赖:",
308
+ choices: ["Yarn", "Pnpm", "Npm", "No"],
309
+ default: "Yarn"
293
310
  }
294
311
  ].filter(it => exclude.every(i => i !== it.name)));
295
312
  }
@@ -303,8 +320,8 @@ program
303
320
  .description("初始化 npm 项目")
304
321
  .option("-n --name <name>", "指定项目名称")
305
322
  .option("-r --react", "指定为 react 项目")
323
+ .option("-c --css", "打包 CSS")
306
324
  .option("-t --target <target>", "指定 JavaScript 版本")
307
- .option("--no-css", "不打包 CSS")
308
325
  .option("--no-esm", "不打包 ES Module")
309
326
  .option("--no-cjs", "不打包 Common JS")
310
327
  .action(async (options) => {
@@ -319,15 +336,15 @@ program
319
336
  initAnswer.react = true;
320
337
  exclude.push("react");
321
338
  }
339
+ if ("css" in options) {
340
+ initAnswer.css = true;
341
+ exclude.push("css");
342
+ }
322
343
  if ("target" in options) {
323
344
  validateTarget(options.target);
324
345
  exclude.push("target");
325
346
  initAnswer.target = options.target.trim().toUpperCase();
326
347
  }
327
- if (options.css === false) {
328
- exclude.push("css");
329
- initAnswer.css = false;
330
- }
331
348
  if (options.esm === false) {
332
349
  exclude.push("esm");
333
350
  initAnswer.esm = false;
@@ -336,21 +353,32 @@ program
336
353
  exclude.push("cjs");
337
354
  initAnswer.cjs = false;
338
355
  }
356
+ if (options.esm === false && options.cjs === false) {
357
+ throw new Error("必须选择 ES Module 和 Common JS 至少一种");
358
+ }
339
359
  const answer = { ...initAnswer, ...(await getProjectAnswer(...exclude)) };
340
- const { name, react } = answer;
360
+ const { name, react, install, esm, cjs } = answer;
361
+ if (esm === false && cjs === false) {
362
+ throw new Error("必须选择 ES Module 和 Common JS 至少一种");
363
+ }
341
364
  const dir = readdirSync("./");
342
365
  if (!dir.includes(name)) {
343
366
  mkdirSync(`./${name}`);
344
367
  }
345
368
  mkdirSync(`./${name}/src`);
346
- writeFileSync(`./${name}/src/index.${react ? "tsx" : "ts"}`, "");
369
+ writeFileSync(`./${name}/src/index.${react ? "tsx" : "ts"}`, 'export default "Hello, world!"');
347
370
  writeFileSync(`./${name}/build.js`, getBuildJS(answer));
348
371
  writeFileSync(`./${name}/package.json`, await getPackageJson(answer));
349
372
  writeFileSync(`./${name}/tsconfig.json`, getTSConfig(answer));
373
+ writeFileSync(`./${name}/.gitignore`, "node_modules");
374
+ if (install !== "No") {
375
+ execSync(`${install.toLowerCase()} install`, { cwd: `./${name}` });
376
+ }
377
+ console.log("创建项目成功");
350
378
  });
351
379
  program
352
380
  .command("build")
353
- .description("更改打包命令")
381
+ .description("更新打包命令")
354
382
  .option("--no-css", "不打包 CSS")
355
383
  .action(options => {
356
384
  const { css } = options;
@@ -365,5 +393,6 @@ program
365
393
  const config = readFileSync("./tsconfig.json", "utf-8");
366
394
  const target = config.match(/"target":[ ]*?"(.+?)"/i)[1].toUpperCase();
367
395
  writeFileSync("./build.js", getBuildJS({ target, css }));
396
+ console.log("更新打包命令成功");
368
397
  });
369
398
  program.parse();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "zixulu",
3
- "version": "0.0.1",
3
+ "version": "0.0.3",
4
4
  "main": "index.js",
5
5
  "license": "MIT",
6
6
  "type": "module",
package/src/index.ts CHANGED
@@ -4,6 +4,7 @@ import { Command } from "commander"
4
4
  import pkg from "../package.json"
5
5
  import { createPromptModule } from "inquirer"
6
6
  import { mkdirSync, readFileSync, readdirSync, statSync, writeFileSync } from "fs"
7
+ import { execSync } from "child_process"
7
8
 
8
9
  const prompt = createPromptModule()
9
10
 
@@ -18,10 +19,17 @@ interface ProjectAnswer {
18
19
  css: boolean
19
20
  esm: boolean
20
21
  cjs: boolean
22
+ install: "No" | "Yarn" | "Pnpm" | "Npm"
21
23
  }
22
24
 
23
25
  const JavaScriptTargets: JavaScriptTarget[] = ["ES2015", "ES2016", "ES2017", "ES2018", "ES2019", "ES2020", "ES2021", "ES2022", "ES2023", "ESNext"]
24
26
 
27
+ async function getPackageLatestVersion(name: string) {
28
+ const response = await fetch(`https://registry.npmjs.org/${name}`)
29
+ const data = await response.json()
30
+ return data["dist-tags"].latest
31
+ }
32
+
25
33
  function getBuildJS(answer: Pick<ProjectAnswer, "target" | "css">) {
26
34
  const { target, css } = answer
27
35
  if (css) {
@@ -79,37 +87,41 @@ execSync("npx tsc --sourceMap -module ${target} -outDir dist/esm && npx tsc --so
79
87
  }
80
88
 
81
89
  async function getPackageJson(answer: ProjectAnswer) {
82
- const { name } = answer
83
- const response = await fetch("https://registry.npmjs.org/typescript")
84
- const data = await response.json()
85
- return `{
86
- "name": "${name}",
87
- "version": "0.0.1",
88
- "main": "dist/cjs/index.js",
89
- "module": "dist/esm/index.js",
90
- "types": "dist/index.d.ts",
91
- "license": "MIT",
92
- "type": "module",
93
- "exports": {
94
- ".": {
95
- "import": {
96
- "types": "./dist/index.d.ts",
97
- "default": "./dist/esm/index.js"
98
- },
99
- "require": {
100
- "types": "./dist/index.d.ts",
101
- "default": "./dist/cjs/index.js"
102
- }
90
+ const { name, react, esm, cjs } = answer
91
+ const info: any = {
92
+ name,
93
+ version: "0.0.1",
94
+ main: "dist/cjs/index.js",
95
+ module: "dist/esm/index.js",
96
+ types: "dist/index.d.ts",
97
+ license: "MIT",
98
+ type: "module",
99
+ exports: {
100
+ ".": {}
101
+ },
102
+ scripts: {
103
+ build: "node ./build.js"
104
+ },
105
+ devDependencies: {}
106
+ }
107
+ if (esm) {
108
+ info.exports["."]["import"] = {
109
+ types: "./dist/index.d.ts",
110
+ default: "./dist/esm/index.js"
103
111
  }
104
- },
105
- "scripts": {
106
- "build": "node ./build.js"
107
- },
108
- "devDependencies": {
109
- "typescript": "^${data["dist-tags"].latest}"
110
112
  }
111
- }
112
- `
113
+ if (cjs) {
114
+ info.exports["."]["require"] = {
115
+ types: "./dist/index.d.ts",
116
+ default: "./dist/cjs/index.js"
117
+ }
118
+ }
119
+ if (react) {
120
+ info.devDependencies["@types/react"] = `^${await getPackageLatestVersion("@types/react")}`
121
+ info.devDependencies["@types/react-dom"] = `^${await getPackageLatestVersion("@types/react-dom")}`
122
+ }
123
+ info.devDependencies.typescript = `^${await getPackageLatestVersion("typescript")}`
124
+ return JSON.stringify(info, null, 4)
113
125
  }
114
126
 
115
127
  function getTSConfig(answer: ProjectAnswer) {
@@ -263,7 +275,7 @@ function getProjectAnswer<T extends keyof ProjectAnswer>(...exclude: T[]): Promi
263
275
  type: "confirm",
264
276
  name: "css",
265
277
  message: "是否打包 CSS:",
266
- default: true
278
+ default: false
267
279
  },
268
280
  {
269
281
  type: "confirm",
@@ -276,6 +288,13 @@ function getProjectAnswer<T extends keyof ProjectAnswer>(...exclude: T[]): Promi
276
288
  name: "cjs",
277
289
  message: "是否打包 Common JS:",
278
290
  default: true
291
+ },
292
+ {
293
+ type: "list",
294
+ name: "install",
295
+ message: "现在就安装依赖:",
296
+ choices: ["Yarn", "Pnpm", "Npm", "No"],
297
+ default: "Yarn"
279
298
  }
280
299
  ].filter(it => exclude.every(i => i !== it.name))
281
300
  )
@@ -294,8 +313,8 @@ program
294
313
  .description("初始化 npm 项目")
295
314
  .option("-n --name <name>", "指定项目名称")
296
315
  .option("-r --react", "指定为 react 项目")
316
+ .option("-c --css", "打包 CSS")
297
317
  .option("-t --target <target>", "指定 JavaScript 版本")
298
- .option("--no-css", "不打包 CSS")
299
318
  .option("--no-esm", "不打包 ES Module")
300
319
  .option("--no-cjs", "不打包 Common JS")
301
320
  .action(async (options: Partial<ProjectAnswer>) => {
@@ -310,15 +329,15 @@ program
310
329
  initAnswer.react = true
311
330
  exclude.push("react")
312
331
  }
332
+ if ("css" in options) {
333
+ initAnswer.css = true
334
+ exclude.push("css")
335
+ }
313
336
  if ("target" in options) {
314
337
  validateTarget(options.target!)
315
338
  exclude.push("target")
316
339
  initAnswer.target = options.target!.trim().toUpperCase() as JavaScriptTarget
317
340
  }
318
- if (options.css === false) {
319
- exclude.push("css")
320
- initAnswer.css = false
321
- }
322
341
  if (options.esm === false) {
323
342
  exclude.push("esm")
324
343
  initAnswer.esm = false
@@ -327,22 +346,33 @@ program
327
346
  exclude.push("cjs")
328
347
  initAnswer.cjs = false
329
348
  }
349
+ if (options.esm === false && options.cjs === false) {
350
+ throw new Error("必须选择 ES Module 和 Common JS 至少一种")
351
+ }
330
352
  const answer = { ...initAnswer, ...(await getProjectAnswer(...exclude)) } as ProjectAnswer
331
- const { name, react } = answer
353
+ const { name, react, install, esm, cjs } = answer
354
+ if (esm === false && cjs === false) {
355
+ throw new Error("必须选择 ES Module 和 Common JS 至少一种")
356
+ }
332
357
  const dir = readdirSync("./")
333
358
  if (!dir.includes(name)) {
334
359
  mkdirSync(`./${name}`)
335
360
  }
336
361
  mkdirSync(`./${name}/src`)
337
- writeFileSync(`./${name}/src/index.${react ? "tsx" : "ts"}`, "")
362
+ writeFileSync(`./${name}/src/index.${react ? "tsx" : "ts"}`, 'export default "Hello, world!"')
338
363
  writeFileSync(`./${name}/build.js`, getBuildJS(answer))
339
364
  writeFileSync(`./${name}/package.json`, await getPackageJson(answer))
340
365
  writeFileSync(`./${name}/tsconfig.json`, getTSConfig(answer))
366
+ writeFileSync(`./${name}/.gitignore`, "node_modules")
367
+ if (install !== "No") {
368
+ execSync(`${install.toLowerCase()} install`, { cwd: `./${name}` })
369
+ }
370
+ console.log("创建项目成功")
341
371
  })
342
372
 
343
373
  program
344
374
  .command("build")
345
- .description("更改打包命令")
375
+ .description("更新打包命令")
346
376
  .option("--no-css", "不打包 CSS")
347
377
  .action(options => {
348
378
  const { css } = options
@@ -357,6 +387,7 @@ program
357
387
  const config = readFileSync("./tsconfig.json", "utf-8")
358
388
  const target = config.match(/"target":[ ]*?"(.+?)"/i)![1].toUpperCase() as JavaScriptTarget
359
389
  writeFileSync("./build.js", getBuildJS({ target, css }))
390
+ console.log("更新打包命令成功")
360
391
  })
361
392
 
362
393
  program.parse()