Skip to content

Commit 13c4619

Browse files
committed
[Enhance] No app name restrictions
1 parent 2cd93c6 commit 13c4619

File tree

4 files changed

+36
-54
lines changed

4 files changed

+36
-54
lines changed

bin/init.ts

Lines changed: 32 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import chalk from "chalk"
55
import inquirer from "inquirer"
66
import selectShell from "select-shell"
77
import ora from "ora"
8-
import { camelCaseToDash } from "../utils/functions"
8+
import { AppNameToNodePackageName } from "../utils/functions"
99
import fs from "fs"
1010

1111
enum TYPE_OF_APP {
@@ -45,8 +45,8 @@ function isValidateComponentNaming(name: string) {
4545
if (!name) {
4646
shell.echo(chalk.redBright("Please provide name of your app."))
4747
return false
48-
} else if (!/^[a-z0-9]+$/i.test(name)) {
49-
shell.echo(chalk.redBright("App name should be alphaNumeric."))
48+
} else if (!/^[a-z0-9\-_]+$/i.test(name)) {
49+
shell.echo(chalk.redBright("App name cannot contain special symbols."))
5050
return false
5151
} else if (fs.existsSync(`./${name}`)) {
5252
shell.echo(chalk.redBright("App name with the same name already exists."))
@@ -59,7 +59,7 @@ async function nameTheApp() {
5959
return inquirer.prompt([
6060
{
6161
name: "value",
62-
message: "Name of your app (alphaNumeric): "
62+
message: "Name of your app:"
6363
}
6464
])
6565
}
@@ -89,33 +89,36 @@ async function isWantStyle() {
8989
])
9090
}
9191

92-
function createApp(gitURL: string, nameOfApp: string) {
92+
function createApp(gitURL: string, nameOfApp: string, defaultNodePackageName: string) {
9393
const gitDownSpinner = ora("Creating app: " + nameOfApp + "...\n")
9494
gitDownSpinner.start()
9595
shell.exec(`git clone ${gitURL} ${nameOfApp}`, (code: number, stdout: string, stderr: string) => {
9696
gitDownSpinner.stop()
97-
flowUp(code, stdout, stderr, nameOfApp)
97+
if (code !== 0) {
98+
shell.echo(chalk.cyanBright(`code: ${code}`))
99+
shell.echo(chalk.cyanBright(`Program output: ${stdout}`))
100+
shell.echo(chalk.cyanBright(`Program stderr: ${stderr}`))
101+
}
102+
flowUp(nameOfApp, defaultNodePackageName)
98103
})
99104
}
100105

101-
function flowUp(code: number, stdout: string, stderr: string, nameOfApp: string) {
106+
function flowUp(nameOfApp: string, defaultNodePackageName: string) {
102107
const projectCleanupSpinner = ora("Project Cleanup...\n")
103108
projectCleanupSpinner.start()
104-
if (code !== 0) {
105-
shell.echo(chalk.cyanBright(`code: ${code}`))
106-
shell.echo(chalk.cyanBright(`Program output: ${stdout}`))
107-
shell.echo(chalk.cyanBright(`Program stderr: ${stderr}`))
108-
}
109+
109110

110111
setTimeout(() => {
111-
shell.sed("-i", "flow-react-ts", camelCaseToDash(`${nameOfApp}`), `./${nameOfApp}/package.json`)
112-
112+
shell.sed("-i", defaultNodePackageName, AppNameToNodePackageName(`${nameOfApp}`), `./${nameOfApp}/package.json`)
113113
shell.rm("-rf", `${nameOfApp}/.git`)
114114
projectCleanupSpinner.stop()
115115

116-
shell.echo(chalk.greenBright(nameOfApp + " created."))
117-
shell.echo(chalk.greenBright("cd " + nameOfApp + " and npm install and npm run dev."))
116+
const npmInstallSpinner = ora("npm install...just a moment, please.\n").start()
117+
shell.cd(nameOfApp)
118+
shell.exec("npm install")
119+
npmInstallSpinner.stop()
118120

121+
shell.echo(chalk.greenBright(nameOfApp + " created."))
119122
process.exit(0)
120123
}, 2000)
121124
}
@@ -142,6 +145,17 @@ function getGitURL(selectValue: number, style: string): string {
142145
}
143146
}
144147

148+
function getDefaultNodePackageName(selectValue: number): string {
149+
switch (selectValue) {
150+
case TYPE_OF_APP.REACT:
151+
return "flow-react-ts"
152+
case TYPE_OF_APP.NEXT:
153+
return "flow-next-ts"
154+
default:
155+
invalidProgramInput()
156+
}
157+
}
158+
145159
function selectTheNameOfTheApp() {
146160
appList.on("select", async (options) => {
147161
const isStyle = await isWantStyle()
@@ -157,7 +171,8 @@ function selectTheNameOfTheApp() {
157171
return process.exit(0)
158172
}
159173
const gitURL = getGitURL(options[0].value, style)
160-
createApp(gitURL, nameOfApp)
174+
const defaultNodePackageName = getDefaultNodePackageName(options[0].value)
175+
createApp(gitURL, nameOfApp, defaultNodePackageName)
161176
})
162177
}
163178

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "flow-react-cli",
3-
"version": "1.0.1",
3+
"version": "1.0.2",
44
"description": "Create React app & boilerplate",
55
"bin": {
66
"flow": "dist/bin/main.js"

utils/functions.ts

Lines changed: 2 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
'use strict';
22

3-
import chalk from 'chalk';
43
import shelljs = require('shelljs');
5-
import fs = require('fs');
6-
74
import path = require('path');
85

96
export interface TemplateType {
@@ -49,36 +46,6 @@ export const resolveComponent = (
4946
};
5047
};
5148

52-
export const exitIfNotDoobooRepo = async (): Promise<void> => {
53-
const exists = fs.existsSync('.dooboo');
54-
55-
if (!exists) {
56-
shelljs.echo(
57-
chalk.redBright(
58-
'\nproject is not compatible with dooboo-cli v5. Are you sure you are in correct dir?',
59-
),
60-
);
61-
62-
process.exit(0);
63-
}
64-
};
65-
66-
export const exitIfNotV5 = async (): Promise<void> => {
67-
const exists = fs.existsSync('.dooboo/v5');
68-
69-
if (!exists) {
70-
shelljs.echo(
71-
chalk.redBright(
72-
`\nproject is not compatible with dooboo-cli v5.
73-
Maybe you are using older projects.
74-
Then please install version lower than dooboo-cli@5`,
75-
),
76-
);
77-
78-
process.exit(0);
79-
}
80-
};
81-
8249
export const toCamelCase = (str: string, cap1st: boolean): string => {
8350
return ((cap1st ? '-' : '') + str).replace(/-+([^-])/g, (a, b) => {
8451
return b.toUpperCase();
@@ -90,8 +57,8 @@ export const isCamelCase = (str: string): boolean => {
9057
else return false;
9158
};
9259

93-
export const camelCaseToDash = (str: string): string => {
94-
return str.replace(/([a-zA-Z])(?=[A-Z])/g, '$1-').toLowerCase();
60+
export const AppNameToNodePackageName = (str: string): string => {
61+
return str.replace(/([a-zA-Z])(?=[A-Z])/g, '$1-').replace('_', '-').toLowerCase();
9562
};
9663

9764
export const camelize = (str: string): string => {

0 commit comments

Comments
 (0)