Skip to content

Commit cf96e6b

Browse files
author
alexlee-dev
committed
✏️ Validation of Application Name
1 parent f940313 commit cf96e6b

6 files changed

Lines changed: 56 additions & 8 deletions

File tree

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
99

1010
### Added
1111

12-
- Interactive option. Use the flag `--interactive` to use this mode.
12+
- Interactive option. Use the flag `--interactive` to use this mode
1313
- Comments :)
1414
- Cleanup on global error
1515
- Option for Author Name
16+
- Validation of application name according to NPM conventions
1617

1718
### Changed
1819

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ Want support for an additional language? Feel free to open a [new issue](https:/
6868
- [ora](https://github.com/sindresorhus/ora) - Elegant terminal spinner.
6969
- [pickitt](https://pickitt.netlify.com/) - When you need a computer to just pick it, reach for Pickitt!
7070
- [TypeScript](https://www.typescriptlang.org/) - A typed superset of JavaScript that compiles to plain JavaScript.
71+
- [validate-npm-package-name](https://github.com/npm/validate-npm-package-name) - Is the given string an acceptable npm package name?
7172

7273
## ✍️ Authors <a name = "authors"></a>
7374

package-lock.json

Lines changed: 19 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
"@types/fs-extra": "^9.0.1",
3939
"@types/inquirer": "^6.5.0",
4040
"@types/node": "^14.0.5",
41+
"@types/validate-npm-package-name": "^3.0.0",
4142
"@types/yargs": "^15.0.5",
4243
"copyfiles": "^2.2.0",
4344
"typescript": "^3.9.3"
@@ -52,6 +53,7 @@
5253
"fs-extra": "^9.0.0",
5354
"inquirer": "^7.1.0",
5455
"ora": "^4.0.4",
55-
"pickitt": "^3.2.0"
56+
"pickitt": "^3.2.0",
57+
"validate-npm-package-name": "^3.0.0"
5658
}
5759
}

src/index.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import {
2222
replaceTemplateValues,
2323
} from "./init";
2424
import { handleIncorrectApplicationName } from "./program";
25-
import { cleanupError } from "./util";
25+
import { cleanupError, validateApplicationName } from "./util";
2626

2727
/**
2828
* Main CLI Program
@@ -70,12 +70,9 @@ const main = async (): Promise<void> => {
7070
})
7171
.parse(process.argv);
7272

73-
// TODO - Catch names like "my.app.name" or "my app name"
74-
7573
// * Application Name must exist, and not consist of illegal characters
76-
if (applicationName === "." || !applicationName) {
77-
return handleIncorrectApplicationName(program);
78-
}
74+
validateApplicationName(applicationName);
75+
if (!applicationName) return;
7976

8077
if (program.interactive) {
8178
// * Interactive walk-thru

src/util.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1+
import chalk from "chalk";
12
import { spawn } from "child_process";
23
import fs from "fs-extra";
34
import path from "path";
5+
import validateProjectName from "validate-npm-package-name";
46

57
/**
68
* Executes a command in a spawned process.
@@ -74,3 +76,29 @@ export const valueReplacer = (
7476
return;
7577
});
7678
};
79+
80+
/**
81+
* Validates the application name according to NPM naming conventions.
82+
* @param applicationName Name of application.
83+
*/
84+
export const validateApplicationName = (applicationName: any) => {
85+
const validation = validateProjectName(applicationName);
86+
if (!validation.validForNewPackages) {
87+
console.error(
88+
`Cannot create an application named ${chalk.red(
89+
`"${applicationName}"`
90+
)} because of npm naming restrictions:`
91+
);
92+
console.log("");
93+
94+
[...(validation.errors || []), ...(validation.warnings || [])].forEach(
95+
(error) => {
96+
console.error(chalk.red(` * ${error}`));
97+
}
98+
);
99+
100+
console.log("");
101+
console.error("Please choose a different application name.");
102+
process.exit(1);
103+
}
104+
};

0 commit comments

Comments
 (0)