Skip to content

Commit

Permalink
Always delete git folders
Browse files Browse the repository at this point in the history
Signed-off-by: macdonst <[email protected]>
  • Loading branch information
macdonst committed Mar 1, 2024
1 parent d8411f4 commit 4664387
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 17 deletions.
33 changes: 17 additions & 16 deletions create-project.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import { randomUUID } from 'crypto'
import { createRequire } from 'module'
import { tmpdir } from 'os'
import { isAbsolute, join, resolve } from 'path'
import fs from 'fs'
import git from 'isomorphic-git'

const { existsSync, lstatSync, mkdirSync, readFileSync, renameSync, rmSync, unlinkSync, writeFileSync } = fs
const { existsSync, lstatSync, readFileSync, renameSync, rmSync, unlinkSync, writeFileSync } = fs

const require = createRequire(import.meta.url)

Expand Down Expand Up @@ -33,10 +31,6 @@ export async function createProject ({ dest, path, name, template = STARTER_PROJ
}
}

// Download folder
const temp = join(tmpdir(), randomUUID())
mkdirSync(temp)

// Project folder
const projectDir = isAbsolute(dest) ? dest : resolve(dest)
if (existsSync(projectDir)) {
Expand All @@ -52,16 +46,13 @@ async function createFromTemplate(projectDir, dest, appName, template) {
// Clone the template project
await git.clone({ fs, http, dir: projectDir, url: template })

// Remove git folders
remove(join(projectDir, '.git'))
remove(join(projectDir, '.github'))

// Clean up miscellaneous starter project files
if (template === STARTER_PROJECT) {
renameSync(join(projectDir, 'template.gitignore'), join(projectDir, '.gitignore'))
remove(join(projectDir, '.git'))
remove(join(projectDir, '.github'))
remove(join(projectDir, '.npmignore'))
remove(join(projectDir, '.npmrc'))
remove(join(projectDir, 'LICENSE'))
remove(join(projectDir, 'manifest.json'))
remove(join(projectDir, 'readme.md'))
remove(join(projectDir, 'scripts'))
cleanStarterProject(projectDir)
}

updatePackageJson(dest, appName)
Expand All @@ -72,6 +63,16 @@ async function createFromTemplate(projectDir, dest, appName, template) {
}
}

function cleanStarterProject(projectDir) {
renameSync(join(projectDir, 'template.gitignore'), join(projectDir, '.gitignore'))
remove(join(projectDir, '.npmignore'))
remove(join(projectDir, '.npmrc'))
remove(join(projectDir, 'LICENSE'))
remove(join(projectDir, 'manifest.json'))
remove(join(projectDir, 'readme.md'))
remove(join(projectDir, 'scripts'))
}

function remove(filePath) {
if (existsSync(filePath)) {
if (lstatSync(filePath).isDirectory()) {
Expand Down
17 changes: 16 additions & 1 deletion test/index-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,22 @@ test('index.js', (t) => {
try {
execSync(`node index.js test/${TEST_APP_PATH} heck-nah`).toString()
} catch (error) {
t.ok('bad template')
t.ok(true, 'bad template')
}

t.teardown(() => {
cleanProj()
})
})

// run index.js in subprocess with path and invalid template repo
test('index.js', (t) => {
t.plan(1)

try {
execSync(`node index.js test/${TEST_APP_PATH} https://github.com/macdonst/read-it-to-me`).toString()
} catch (error) {
t.ok(true, 'bad template repo')
}

t.teardown(() => {
Expand Down

0 comments on commit 4664387

Please sign in to comment.