Skip to content

Commit

Permalink
Copy sync for node 14
Browse files Browse the repository at this point in the history
Signed-off-by: macdonst <[email protected]>
  • Loading branch information
macdonst committed Jul 31, 2023
1 parent 6d9ce87 commit 33a734e
Showing 1 changed file with 32 additions and 4 deletions.
36 changes: 32 additions & 4 deletions create-project.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { randomUUID } from 'crypto'
import { createWriteStream, existsSync, mkdirSync, readFileSync, renameSync, unlinkSync, writeFileSync, cpSync, rmSync } from 'fs'
import { accessSync, createWriteStream, existsSync, mkdirSync, readdirSync, readFileSync, renameSync, statSync, unlinkSync, writeFileSync, rmSync } from 'fs'
import { createRequire } from 'module'
import { tmpdir } from 'os'
import { isAbsolute, join, resolve, parse } from 'path'
Expand Down Expand Up @@ -43,9 +43,9 @@ export async function createProject ({ dest, path, name }) {

// Extract starter project
tar.x({ C: temp, file: starterProjectArchive, sync: true })

// Move starter project to final destination

// Check if the temp and projectDir are on the same file system
const tempRoot = parse(temp).root
const projectRoot = parse(projectDir).root
Expand All @@ -54,7 +54,7 @@ export async function createProject ({ dest, path, name }) {

if (!isSameFileSystemRoot) {
// if not, we need to copy the files instead of moving them
cpSync(packageDir, projectDir, { recursive: true })
copySync(packageDir, projectDir)
rmSync(packageDir, { recursive: true })
} else {
renameSync(packageDir, projectDir)
Expand Down Expand Up @@ -136,3 +136,31 @@ async function computeTarballUrl() {
)
return body.versions[version].dist.tarball
}

// When node 14 support is dropped switch to using `cpSync`
function copySync(src, dest) {
const copy = (copySrc, copyDest) => {
const list = readdirSync(copySrc)
list.forEach((item) => {
const ss = resolve(copySrc, item)
const stat = statSync(ss)
const curSrc = resolve(copySrc, item)
const curDest = resolve(copyDest, item)

if (stat.isFile()) {
const fileData = readFileSync(curSrc)
writeFileSync(curDest, fileData)
} else if (stat.isDirectory()) {
mkdirSync(curDest, { recursive: true })
copy(curSrc, curDest)
}
})
}

try {
accessSync(dest)
} catch (err) {
mkdirSync(dest, { recursive: true })
}
copy(src, dest)
}

0 comments on commit 33a734e

Please sign in to comment.