Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@
"unfetch": "^3.0.0",
"update-notifier": "^2.3.0",
"url-loader": "^0.5.8",
"validate-npm-package-name": "^3.0.0",
"webpack": "^3.7.0",
"webpack-dev-server": "^2.9.0",
"webpack-merge": "^4.1.0",
Expand Down
14 changes: 11 additions & 3 deletions src/commands/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import fs from 'fs.promised';
import { green } from 'chalk';
import { prompt } from 'inquirer';
import { resolve, dirname } from 'path';
import validateNpmPackageName from 'validate-npm-package-name';
import asyncCommand from '../lib/async-command';
import { info, isDir, hasCommand, error, trim, warn } from '../util';
import { install, initGit, addScripts, isMissing } from './../lib/setup';
Expand Down Expand Up @@ -89,6 +90,16 @@ export default asyncCommand({
info(`Assuming you meant ${repo}...`);
}

// Use `--name` value or `dest` dir's name
argv.name = argv.name || argv.dest;

const npmPackageNameErrors = validateNpmPackageName(argv.name).errors;

if (npmPackageNameErrors) {
const npmPackageNameMsg = npmPackageNameErrors.join(', ');
return error(`${argv.name} is not a valid package name. ${npmPackageNameMsg}`, 1);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you add a chalk message before this?

Copy link
Contributor Author

@milesthedisch milesthedisch Jan 21, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure what the chalk message would have in it. Doesn't the error method from the utils already display the message given?

i.e ✖ ERROR ./asdasd is not a valid package name. name cannot start with a period, name can only contain URL-friendly characters

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are right. Internally error method in utils uses chalk itself.

@prateekbh that is totally valid. 😄

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

gosh I need to do my revision for this codebase

}

// Attempt to fetch the `template`
let archive = await gittar.fetch(repo).catch(err => {
err = err || { message:'An error occured while fetching template.' };
Expand Down Expand Up @@ -153,9 +164,6 @@ export default asyncCommand({
warn('Could not locate `package.json` file!');
}

// Use `--name` value or `dest` dir's name
argv.name = argv.name || argv.dest;

// Update `package.json` key
if (pkgData) {
spinner.text = 'Updating `name` within `package.json` file';
Expand Down
10 changes: 10 additions & 0 deletions tests/create.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,14 @@ describe('preact create', () => {
expect(output).toEqual(snapshots[key]);
});
});

it('should fail given an invalid name', async () => {
const INVALID_NAME = '*()@!#!$-invalid-name';

try {
await create('default', INVALID_NAME);
} catch (e) {
expect(e);
}
});
});
10 changes: 10 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1304,6 +1304,10 @@ builtin-status-codes@^3.0.0:
version "3.0.0"
resolved "https://registry.yarnpkg.com/builtin-status-codes/-/builtin-status-codes-3.0.0.tgz#85982878e21b98e1c66425e03d0174788f569ee8"

builtins@^1.0.3:
version "1.0.3"
resolved "https://registry.yarnpkg.com/builtins/-/builtins-1.0.3.tgz#cb94faeb61c8696451db36534e1422f94f0aee88"

[email protected]:
version "3.0.0"
resolved "https://registry.yarnpkg.com/bytes/-/bytes-3.0.0.tgz#d32815404d689699f85a4ea4fa8755dd13a96048"
Expand Down Expand Up @@ -7352,6 +7356,12 @@ validate-npm-package-license@^3.0.1:
spdx-correct "~1.0.0"
spdx-expression-parse "~1.0.0"

validate-npm-package-name@^3.0.0:
version "3.0.0"
resolved "https://registry.yarnpkg.com/validate-npm-package-name/-/validate-npm-package-name-3.0.0.tgz#5fa912d81eb7d0c74afc140de7317f0ca7df437e"
dependencies:
builtins "^1.0.3"

vary@~1.1.2:
version "1.1.2"
resolved "https://registry.yarnpkg.com/vary/-/vary-1.1.2.tgz#2299f02c6ded30d4a5961b0b9f74524a18f634fc"
Expand Down