Skip to content
索引

询问是否创建文件夹

js
const lolcat = require("@darkobits/lolcatjs")
const inquirer = require("inquirer")
const fs = require("fs")
const clearDir = require("../utils/deleteDir")
const download = require("../utils/download")

const create = async () => {
  const { projectName = "my-vite" } = await inquirer.prompt([
    {
      type: "input",
      name: "projectName",
      message: "Please input the project name"
    }
  ])
  fs.access(projectName, fs.constants.F_OK, async noExist => {
    if (noExist) {
      download(projectName)
    } else {
      console.log(lolcat.fromString("There is already a folder with that name"))
      const { intention } = await inquirer.prompt([
        {
          type: "list",
          name: "intention",
          message: lolcat.fromString("Overwrite folder of this name?"),
          choices: ["Yes", "No"]
        }
      ])
      if (intention == "No") return
      clearDir(projectName)
      download(projectName)
    }
  })
}
module.exports = create
const lolcat = require("@darkobits/lolcatjs")
const inquirer = require("inquirer")
const fs = require("fs")
const clearDir = require("../utils/deleteDir")
const download = require("../utils/download")

const create = async () => {
  const { projectName = "my-vite" } = await inquirer.prompt([
    {
      type: "input",
      name: "projectName",
      message: "Please input the project name"
    }
  ])
  fs.access(projectName, fs.constants.F_OK, async noExist => {
    if (noExist) {
      download(projectName)
    } else {
      console.log(lolcat.fromString("There is already a folder with that name"))
      const { intention } = await inquirer.prompt([
        {
          type: "list",
          name: "intention",
          message: lolcat.fromString("Overwrite folder of this name?"),
          choices: ["Yes", "No"]
        }
      ])
      if (intention == "No") return
      clearDir(projectName)
      download(projectName)
    }
  })
}
module.exports = create

删除文件夹

js
const fs = require("fs")

function emptyDir(path) {
  const files = fs.readdirSync(path)
  files.forEach(file => {
    const filePath = `${path}/${file}`
    const stats = fs.statSync(filePath)
    stats.isDirectory() ? emptyDir(filePath) : fs.unlinkSync(filePath)
  })
}

function rmEmptyDir(path) {
  const files = fs.readdirSync(path)
  if (files.length > 0) {
    let tempFile = 0
    files.forEach(file => {
      tempFile++
      rmEmptyDir(`${path}/${file}`, 1)
    })
    if (tempFile === files.length) {
      fs.rmdirSync(path)
    }
  } else {
    fs.rmdirSync(path)
  }
}

function clearDir(path) {
  emptyDir(path)
  rmEmptyDir(path)
}

module.exports = clearDir
const fs = require("fs")

function emptyDir(path) {
  const files = fs.readdirSync(path)
  files.forEach(file => {
    const filePath = `${path}/${file}`
    const stats = fs.statSync(filePath)
    stats.isDirectory() ? emptyDir(filePath) : fs.unlinkSync(filePath)
  })
}

function rmEmptyDir(path) {
  const files = fs.readdirSync(path)
  if (files.length > 0) {
    let tempFile = 0
    files.forEach(file => {
      tempFile++
      rmEmptyDir(`${path}/${file}`, 1)
    })
    if (tempFile === files.length) {
      fs.rmdirSync(path)
    }
  } else {
    fs.rmdirSync(path)
  }
}

function clearDir(path) {
  emptyDir(path)
  rmEmptyDir(path)
}

module.exports = clearDir

克隆项目

js
const ora = require("ora")
const shell = require("shelljs")
const gitClone = require("download-git-repo")
const handlebars = require("handlebars")
const lolcat = require("@darkobits/lolcatjs")
const fs = require("fs")

/**
 * @param  projectName name of the created project
 */
function download(projectName) {
  const loading = ora("downloading...").start()
  gitClone(
    "https://gitee.com:zqylzcwcxy/vite-vue3-tempalte#master",
    projectName,
    { clone: true },
    err => {
      if (err) {
        console.log(err)
        loading.fail()
      } else {
        loading.succeed()
        const packagePath = `${projectName}/package.json`
        const packageContent = fs.readFileSync(packagePath, "utf8")
        const packageResult = handlebars.compile(packageContent)({ name: projectName })
        fs.writeFileSync(packagePath, packageResult)
        console.log(lolcat.fromString("init successfully"))
        shell.cd(projectName)
        if (!shell.which("code")) {
          shell.echo("need vscode to run script")
          shell.exit(1)
        } else {
          shell.exec("code ./")
        }
        if (!shell.which("yarn")) {
          shell.echo("need yarn to run auto script")
          shell.exit(1)
        } else {
          shell.exec("yarn")
          shell.exec("yarn dev")
        }
      }
    }
  )
}

module.exports = download
const ora = require("ora")
const shell = require("shelljs")
const gitClone = require("download-git-repo")
const handlebars = require("handlebars")
const lolcat = require("@darkobits/lolcatjs")
const fs = require("fs")

/**
 * @param  projectName name of the created project
 */
function download(projectName) {
  const loading = ora("downloading...").start()
  gitClone(
    "https://gitee.com:zqylzcwcxy/vite-vue3-tempalte#master",
    projectName,
    { clone: true },
    err => {
      if (err) {
        console.log(err)
        loading.fail()
      } else {
        loading.succeed()
        const packagePath = `${projectName}/package.json`
        const packageContent = fs.readFileSync(packagePath, "utf8")
        const packageResult = handlebars.compile(packageContent)({ name: projectName })
        fs.writeFileSync(packagePath, packageResult)
        console.log(lolcat.fromString("init successfully"))
        shell.cd(projectName)
        if (!shell.which("code")) {
          shell.echo("need vscode to run script")
          shell.exit(1)
        } else {
          shell.exec("code ./")
        }
        if (!shell.which("yarn")) {
          shell.echo("need yarn to run auto script")
          shell.exit(1)
        } else {
          shell.exec("yarn")
          shell.exec("yarn dev")
        }
      }
    }
  )
}

module.exports = download

判断文件存在

js
fs.access(path.resolve(__dirname, ".prettierignore"), fs.constants.F_OK, (err) => {
  console.log(`${err ? '不存在' : '存在'}`);
})
fs.access(path.resolve(__dirname, ".prettierignore"), fs.constants.F_OK, (err) => {
  console.log(`${err ? '不存在' : '存在'}`);
})

fs-extra

https://juejin.cn/post/6844903641594216455#comment

Released under the MIT License.