Skip to content

Commit

Permalink
feat(create-vite): migrate to ESM (#8253)
Browse files Browse the repository at this point in the history
  • Loading branch information
antfu authored May 21, 2022
1 parent 76fdc27 commit 49478ae
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 16 deletions.
55 changes: 39 additions & 16 deletions packages/create-vite/index.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,25 @@
#!/usr/bin/env node

// @ts-check
const fs = require('fs')
const path = require('path')
// Avoids autoconversion to number of the project name by defining that the args
// non associated with an option ( _ ) needs to be parsed as a string. See #4606
const argv = require('minimist')(process.argv.slice(2), { string: ['_'] })
// eslint-disable-next-line node/no-restricted-require
const prompts = require('prompts')
const {
yellow,
green,
cyan,
import fs from 'fs'
import path from 'path'
import { fileURLToPath } from 'url'
import minimist from 'minimist'
import prompts from 'prompts'
import {
blue,
magenta,
cyan,
green,
lightRed,
magenta,
red,
reset
} = require('kolorist')
reset,
yellow
} from 'kolorist'

// Avoids autoconversion to number of the project name by defining that the args
// non associated with an option ( _ ) needs to be parsed as a string. See #4606
const argv = minimist(process.argv.slice(2), { string: ['_'] })
const cwd = process.cwd()

const FRAMEWORKS = [
Expand Down Expand Up @@ -238,7 +239,11 @@ async function init() {

console.log(`\nScaffolding project in ${root}...`)

const templateDir = path.join(__dirname, `template-${template}`)
const templateDir = path.resolve(
fileURLToPath(import.meta.url),
'..',
`template-${template}`
)

const write = (file, content) => {
const targetPath = renameFiles[file]
Expand All @@ -256,7 +261,9 @@ async function init() {
write(file)
}

const pkg = require(path.join(templateDir, `package.json`))
const pkg = JSON.parse(
fs.readFileSync(path.join(templateDir, `package.json`), 'utf-8')
)

pkg.name = packageName || targetDir

Expand Down Expand Up @@ -291,12 +298,18 @@ function copy(src, dest) {
}
}

/**
* @param {string} projectName
*/
function isValidPackageName(projectName) {
return /^(?:@[a-z0-9-*~][a-z0-9-*._~]*\/)?[a-z0-9-~][a-z0-9-._~]*$/.test(
projectName
)
}

/**
* @param {string} projectName
*/
function toValidPackageName(projectName) {
return projectName
.trim()
Expand All @@ -306,6 +319,10 @@ function toValidPackageName(projectName) {
.replace(/[^a-z0-9-~]+/g, '-')
}

/**
* @param {string} srcDir
* @param {string} destDir
*/
function copyDir(srcDir, destDir) {
fs.mkdirSync(destDir, { recursive: true })
for (const file of fs.readdirSync(srcDir)) {
Expand All @@ -315,11 +332,17 @@ function copyDir(srcDir, destDir) {
}
}

/**
* @param {string} path
*/
function isEmpty(path) {
const files = fs.readdirSync(path)
return files.length === 0 || (files.length === 1 && files[0] === '.git')
}

/**
* @param {string} dir
*/
function emptyDir(dir) {
if (!fs.existsSync(dir)) {
return
Expand Down
1 change: 1 addition & 0 deletions packages/create-vite/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"name": "create-vite",
"version": "2.9.3",
"type": "module",
"license": "MIT",
"author": "Evan You",
"bin": {
Expand Down

0 comments on commit 49478ae

Please sign in to comment.