Skip to content

Commit

Permalink
Share name validation
Browse files Browse the repository at this point in the history
  • Loading branch information
JoeStanton committed May 14, 2015
1 parent f3bba8b commit a5e2b6a
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 12 deletions.
14 changes: 13 additions & 1 deletion local-cli/generator-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,19 @@ function walk(current) {
return [].concat.apply([current], files);
}

function validatePackageName(name) {
if (!name.match(/^[$A-Z_][0-9A-Z_$]*$/i)) {
console.error(
'"%s" is not a valid name for a project. Please use a valid identifier ' +
'name (alphanumeric).',
name
);
process.exit(1);
}
}

module.exports = {
copyAndReplace: copyAndReplace,
walk: walk
walk: walk,
validatePackageName: validatePackageName
};
8 changes: 5 additions & 3 deletions local-cli/new-library.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,10 @@ function writePackageJSON(libraryName, dest) {

module.exports = {
init: function(args) {
var name = args[1];
if (!name) { showHelp(); }
newLibrary(name);
var libraryName = args[1];
if (!libraryName) { showHelp(); }

utils.validatePackageName(libraryName);
newLibrary(libraryName);
}
}
11 changes: 3 additions & 8 deletions react-native-cli/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
var fs = require('fs');
var path = require('path');
var spawn = require('child_process').spawn;
var utils = require('../local-cli/generator-utils');

var CLI_MODULE_PATH = function() {
//return path.resolve('/Users/joe/Git/react-native-js/cli.js');
Expand Down Expand Up @@ -57,14 +58,7 @@ if (cli) {
}

function init(name) {
if (!name.match(/^[$A-Z_][0-9A-Z_$]*$/i)) {
console.error(
'"%s" is not a valid name for a project. Please use a valid identifier ' +
'name (alphanumeric).',
name
);
process.exit(1);
}
utils.validatePackageName(name);

var root = path.resolve(name);
var projectName = path.basename(root);
Expand Down Expand Up @@ -101,6 +95,7 @@ function init(name) {
}

function run(command, cb) {
utils.validatePackageName()
var parts = command.split(/\s+/);
var cmd = parts[0];
var args = parts.slice(1);
Expand Down

0 comments on commit a5e2b6a

Please sign in to comment.