Skip to content
This repository has been archived by the owner on Aug 1, 2020. It is now read-only.

Commit

Permalink
fix: Create valid package names, package scopes or component routes /…
Browse files Browse the repository at this point in the history
… context-route from invalid inputs
  • Loading branch information
about-code committed Oct 5, 2017
1 parent fca633b commit 6d1bbcd
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 16 deletions.
17 changes: 13 additions & 4 deletions config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
const changeCase = require("change-case");

const packageNameRule = (val) => {
let segments = val
.split("/")
.map((segment) => changeCase.lowerCase(changeCase.paramCase(segment)));
if (segments.length > 1 && segments[0][0] !== '@') {
segments[0] = '@' + segments[0];
}
return segments.join("/");
}

module.exports = {
defaults: {
// Some Defaults
Expand All @@ -8,13 +18,12 @@ module.exports = {
packageName: "foo-feature"
},
conventions: {
routeRule: (val) => changeCase.lowerCase(val.replace(/ /, '-')),
pathRule: (val) => changeCase.lowerCase(val),
classNameRule: (val) => changeCase.upperCaseFirst(changeCase.camelCase(val)),
classFileNameRule: (val) => changeCase.upperCaseFirst(changeCase.camelCase(val)),
componentSelectorRule: (val) => changeCase.lowerCase(changeCase.paramCase(val)),
packageNameRule: (val) => val
.split("/")
.map((segment) => (segment[0] === '@' ? '@': '') + changeCase.lowerCase(changeCase.paramCase(segment)))
.join("/")
packageScopeRule: (val) => '@' + packageNameRule(val.split('/')[0]),
packageNameRule: packageNameRule
}
};
4 changes: 2 additions & 2 deletions generator-tasks/class-ng-component/generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ let process = require('process')
,prompt = require("../../generator-steps/prompt");

const KEY = 'class-ng-component'
const {packageNameRule, classNameRule, componentSelectorRule, pathRule} = config.conventions;
const {packageNameRule, classNameRule, componentSelectorRule, routeRule} = config.conventions;
const {packageName, packageScope} = config.defaults;

module.exports = function package() {
return prompt(KEY, [
{type: "input", name: "comp_name", required: true, message: "Component class name (CamelCase):", filter: classNameRule},
{type: "input", name: "comp_selector", required: true, message: "Component Selector (kebab-case):", filter: componentSelectorRule},
{type: "input", name: "comp_route", required: true, message: "Component route (all lowercase):", filter: pathRule},
{type: "input", name: "comp_route", required: true, message: "Component route (all lowercase):", filter: routeRule},
{type: "input", name: "pkg_fullname", required: true, message: "Target package (Feature Package):", default: "@foo/foo-feature", filter: packageNameRule},
{type: "confirm", name: "confirm", required: true, message: "Ready?"}
])
Expand Down
13 changes: 7 additions & 6 deletions generator-tasks/project-app/generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,18 @@ let process = require('process')
,prompt = require("../../generator-steps/prompt");

const KEY = 'project-app'
const {packageNameRule} = config.conventions;
const {packageNameRule, packageScopeRule, routeRule} = config.conventions;
const {packageScope} = config.defaults;
const contextRootRule = (val) => routeRule(path.join("/", val, "/").replace(/\\/, "/"))

module.exports = function project() {
return prompt(KEY, [
{type: "input", name: "proj_name", required: true, message: "Project name (kebab-case):", filter: packageNameRule},
{type: "input", name: "app_ctx_root", required: true, message: "App context root (use leading slash):", default: "/", filter: (val) => path.join("/", val, "/").replace(/\\/, "/")},
{type: "input", name: "app_title", required: false, message: "Title to use in browser tabs:"},
{type: "input", name: "pkg_scope", required: true, message: `NPM Package Scope e.g. ${packageScope} (kebab-case):`, filter: packageNameRule},
{type: "input", name: "proj_name", required: true, message: "Project name (kebab-case):", default: "foo", filter: packageNameRule},
{type: "input", name: "app_ctx_root", required: true, message: "App context root (use leading slash):", default: "/", filter: contextRootRule},
{type: "input", name: "app_title", required: false, message: "Title to use in browser tabs:", default: (answers) => answers.proj_name},
{type: "input", name: "pkg_scope", required: true, message: `NPM Package Scope e.g. ${packageScope} (kebab-case):`, filter: packageScopeRule},
{type: "input", name: "pkg_description", required: false, message: "Short project description (max. one sentence):"},
{type: "input", name: "pkg_version", required: true, message: "Initial version:", default: "1.0.0"},
{type: "input", name: "pkg_version", required: true, message: "Initial version:", default: "0.0.0"},
{type: "input", name: "pkg_author", required: false, message: "Authorship:", default: "anonymous"},
{type: "confirm", name: "bool_default_pkgs", required: false, message: "Generate app and theme package?", default: "n"},
{type: "confirm", name: "bool_install_deps", required: false, message: "Immediately install dependencies?", default: "y"},
Expand Down
8 changes: 4 additions & 4 deletions slushfile.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
let gulp = require("gulp")
,generatorProject = require("./generator-generator-steps/project-app/generator")
,generatorPackage = require("./generator-generator-steps/package-feature/generator")
,generatorModule = require("./generator-generator-steps/class-ng-module/generator")
,generatorComponent = require("./generator-generator-steps/class-ng-component/generator")
,generatorProject = require("./generator-tasks/project-app/generator")
,generatorPackage = require("./generator-tasks/package-feature/generator")
,generatorModule = require("./generator-tasks/class-ng-module/generator")
,generatorComponent = require("./generator-tasks/class-ng-component/generator")

gulp.task("default", function(done) {
return inquirer.prompt([
Expand Down

0 comments on commit 6d1bbcd

Please sign in to comment.