create-vite package

44 阅读1分钟

主要的两个库

1. mi'nimist 获取 node 运行参数

var argv = require('minimist')(process.argv.slice(2));
console.log(argv);
$ node example/parse.js -a beep -b boop
{ _: [], a: 'beep', b: 'boop' }
$ node example/parse.js -x 3 -y 4 -n5 -abc --beep=boop foo bar baz
{ _: [ 'foo', 'bar', 'baz' ],
  x: 3,
  y: 4,
  n: 5,
  a: true,
  b: true,
  c: true,
  beep: 'boop' }

2. prompts 弹出框获取值

在简单的 npx create-vite 时,弹出弹框让用户输入,特定的名称/模板等信息

运行函数

init().catch((e) => {
  console.error(e)
})

通过 init 来运行函数,通过 prompts 来获取,通过我们的运行参数来决定弹出的弹框内容。 在 prompts 中,通过 object type 是否 null, 来决定是否正常弹出。

在 root 文件夹下,有各框架的模板文件:template-lit, template-react, template-vue, ... 等

然后通过 fs.copyFileSync 来拷贝文件,通过 fs.mkdirSync 来创建文件夹。

function copy(src: string, dest: string) {
  const stat = fs.statSync(src)
  if (stat.isDirectory()) {
    copyDir(src, dest)
  } else {
    fs.copyFileSync(src, dest)
  }
}
function copyDir(srcDir: string, destDir: string) {
  fs.mkdirSync(destDir, { recursive: true })
  for (const file of fs.readdirSync(srcDir)) {
    const srcFile = path.resolve(srcDir, file)
    const destFile = path.resolve(destDir, file)
    copy(srcFile, destFile)
  }
}