Skip to content

Commit 12648dc

Browse files
milesthedischlukeed
authored andcommitted
Validate Project Names (#468)
* Add validation for package name, Add package validate-npm-package-name. * Update test to check for error given invalid name. * Remove extra spaces.
1 parent 7942483 commit 12648dc

File tree

4 files changed

+32
-3
lines changed

4 files changed

+32
-3
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@
156156
"unfetch": "^3.0.0",
157157
"update-notifier": "^2.3.0",
158158
"url-loader": "^0.5.8",
159+
"validate-npm-package-name": "^3.0.0",
159160
"webpack": "^3.7.0",
160161
"webpack-dev-server": "^2.9.0",
161162
"webpack-merge": "^4.1.0",

src/commands/create.js

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import fs from 'fs.promised';
55
import { green } from 'chalk';
66
import { prompt } from 'inquirer';
77
import { resolve, dirname } from 'path';
8+
import validateNpmPackageName from 'validate-npm-package-name';
89
import asyncCommand from '../lib/async-command';
910
import { info, isDir, hasCommand, error, trim, warn } from '../util';
1011
import { install, initGit, addScripts, isMissing } from './../lib/setup';
@@ -89,6 +90,16 @@ export default asyncCommand({
8990
info(`Assuming you meant ${repo}...`);
9091
}
9192

93+
// Use `--name` value or `dest` dir's name
94+
argv.name = argv.name || argv.dest;
95+
96+
const npmPackageNameErrors = validateNpmPackageName(argv.name).errors;
97+
98+
if (npmPackageNameErrors) {
99+
const npmPackageNameMsg = npmPackageNameErrors.join(', ');
100+
return error(`${argv.name} is not a valid package name. ${npmPackageNameMsg}`, 1);
101+
}
102+
92103
// Attempt to fetch the `template`
93104
let archive = await gittar.fetch(repo).catch(err => {
94105
err = err || { message:'An error occured while fetching template.' };
@@ -153,9 +164,6 @@ export default asyncCommand({
153164
warn('Could not locate `package.json` file!');
154165
}
155166

156-
// Use `--name` value or `dest` dir's name
157-
argv.name = argv.name || argv.dest;
158-
159167
// Update `package.json` key
160168
if (pkgData) {
161169
spinner.text = 'Updating `name` within `package.json` file';

tests/create.test.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,14 @@ describe('preact create', () => {
1818
expect(output).toEqual(snapshots[key]);
1919
});
2020
});
21+
22+
it('should fail given an invalid name', async () => {
23+
const INVALID_NAME = '*()@!#!$-invalid-name';
24+
25+
try {
26+
await create('default', INVALID_NAME);
27+
} catch (e) {
28+
expect(e);
29+
}
30+
});
2131
});

yarn.lock

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1304,6 +1304,10 @@ builtin-status-codes@^3.0.0:
13041304
version "3.0.0"
13051305
resolved "https://registry.yarnpkg.com/builtin-status-codes/-/builtin-status-codes-3.0.0.tgz#85982878e21b98e1c66425e03d0174788f569ee8"
13061306

1307+
builtins@^1.0.3:
1308+
version "1.0.3"
1309+
resolved "https://registry.yarnpkg.com/builtins/-/builtins-1.0.3.tgz#cb94faeb61c8696451db36534e1422f94f0aee88"
1310+
13071311
13081312
version "3.0.0"
13091313
resolved "https://registry.yarnpkg.com/bytes/-/bytes-3.0.0.tgz#d32815404d689699f85a4ea4fa8755dd13a96048"
@@ -7352,6 +7356,12 @@ validate-npm-package-license@^3.0.1:
73527356
spdx-correct "~1.0.0"
73537357
spdx-expression-parse "~1.0.0"
73547358

7359+
validate-npm-package-name@^3.0.0:
7360+
version "3.0.0"
7361+
resolved "https://registry.yarnpkg.com/validate-npm-package-name/-/validate-npm-package-name-3.0.0.tgz#5fa912d81eb7d0c74afc140de7317f0ca7df437e"
7362+
dependencies:
7363+
builtins "^1.0.3"
7364+
73557365
vary@~1.1.2:
73567366
version "1.1.2"
73577367
resolved "https://registry.yarnpkg.com/vary/-/vary-1.1.2.tgz#2299f02c6ded30d4a5961b0b9f74524a18f634fc"

0 commit comments

Comments
 (0)