Skip to content

Commit b8e356e

Browse files
committed
Prompt for target directory
1 parent b4dae9a commit b8e356e

File tree

1 file changed

+35
-19
lines changed

1 file changed

+35
-19
lines changed

src/index.ts

Lines changed: 35 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,13 @@ async function prompt(options: Omit<PromptObject, 'name'>) {
2727
}
2828

2929
async function textPrompt(message: string, initial?: string): Promise<string> {
30-
return prompt({
30+
const resp = await prompt({
3131
type: 'text',
3232
message,
3333
initial
3434
})
35+
36+
return resp.trim()
3537
}
3638

3739
async function togglePrompt(message: string, initial = false, active = 'Yes', inactive = 'No'): Promise<boolean> {
@@ -72,14 +74,39 @@ async function init() {
7274
const scopedPackageName = await textPrompt('Package name', '@skirtle/test-project')
7375

7476
// TODO: Tightening this check, e.g. for hyphen positions
75-
if (!/@[a-z0-9-]+\/[a-z0-9-]+/.test(scopedPackageName)) {
77+
if (!/^@[a-z0-9-]+\/[a-z0-9-]+$/.test(scopedPackageName)) {
7678
console.log('Invalid package name: ' + scopedPackageName)
7779
process.exit(1)
7880
}
7981

82+
const unscopedPackageName = scopedPackageName.replace(/.*\//, '')
83+
const shortUnscopedPackageName = unscopedPackageName.replace(/^vue-/, '')
84+
const projectName = unscopedPackageName.replace(/-+/g, ' ').trim().split(' ').map(s => s[0].toUpperCase() + s.slice(1)).join(' ')
85+
const globalVariableName = projectName.replace(/ /g, '')
86+
87+
const targetDirName = await textPrompt('Target directory', unscopedPackageName)
88+
89+
if (targetDirName !== '.' && !/^[\w-]+$/.test(targetDirName)) {
90+
console.log('Invalid directory name: ' + targetDirName)
91+
process.exit(1)
92+
}
93+
94+
const targetDirPath = path.join(cwd, targetDirName)
95+
96+
if (targetDirName === '.') {
97+
// TODO: Check files properly and prompt accordingly
98+
if (fs.existsSync(path.join(targetDirPath, 'package.json'))) {
99+
console.log('Target directory already contains package.json')
100+
}
101+
} else {
102+
if (fs.existsSync(targetDirPath)) {
103+
console.log('Target directory already exists')
104+
}
105+
}
106+
80107
const githubPath = await textPrompt('GitHub path, e.g. skirtles-code/test-project (optional)')
81108

82-
if (githubPath && !/[\w-]+\/[\w-]+/.test(githubPath)) {
109+
if (githubPath && !/^[\w-]+\/[\w-]+$/.test(githubPath)) {
83110
console.log('Invalid GitHub path: ' + githubPath)
84111
process.exit(1)
85112
}
@@ -89,12 +116,6 @@ async function init() {
89116
const includePlayground = await togglePrompt('Include playground application for development?', true)
90117
const includeExamples = await togglePrompt('Include example code?', true, 'Yes', 'No, just configs')
91118

92-
const unscopedPackageName = scopedPackageName.replace(/.*\//, '')
93-
const shortUnscopedPackageName = unscopedPackageName.replace(/^vue-/, '')
94-
const projectName = unscopedPackageName.replace(/-+/g, ' ').trim().split(' ').map(s => s[0].toUpperCase() + s.slice(1)).join(' ')
95-
const globalVariableName = projectName.replace(/ /g, '')
96-
const targetDirName = unscopedPackageName
97-
98119
const [githubUserName, githubRepoName] = (githubPath || '/').split('/')
99120
const githubUrl = githubPath ? `https://github.com/${githubPath}` : ''
100121
const githubIssues = githubPath ? `${githubUrl}/issues` : ''
@@ -103,15 +124,6 @@ async function init() {
103124
const docsBase = githubRepoName && includeGithubPages ? `/${githubRepoName}/` : '/'
104125
const homepageUrl = githubPagesOrigin && includeGithubPages ? `${githubPagesOrigin}${docsBase}` : githubUrl
105126

106-
const targetDirPath = path.join(cwd, targetDirName)
107-
108-
if (fs.existsSync(targetDirPath)) {
109-
console.log('Target directory already exists')
110-
} else {
111-
// TODO: Shouldn't need recursive once we're done
112-
fs.mkdirSync(targetDirPath, { recursive: true })
113-
}
114-
115127
const templateDirPath = path.resolve(__dirname, 'template')
116128

117129
const config: Config = {
@@ -153,7 +165,11 @@ async function init() {
153165
console.log('Project created')
154166
console.log('Note: pnpm must be used as the package manager')
155167
console.log()
156-
console.log('cd ' + targetDirName)
168+
169+
if (targetDirName !== '.') {
170+
console.log('cd ' + targetDirName)
171+
}
172+
157173
console.log('pnpm install')
158174
console.log()
159175
console.log(`You should add a suitable license at ${targetDirName}/packages/${config.shortUnscopedPackageName}/LICENSE`)

0 commit comments

Comments
 (0)