stencil-cli generate.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Command for generating a new stencil component within any stencil project, example 'stencil generate my-component' | |
if (process.argv[2] === 'generate') { | |
if (!process.argv[3]) { | |
shell.echo('Please state the component name after the "generate" command.'); | |
shell.exit(1); | |
} | |
// The uppercase version of the component alias | |
var componentName = capitalizeFirstLetter(process.argv[3]); | |
// The tag name of the component alias | |
var componentTag = process.argv[3].toLowerCase(); | |
var templatePath = path.join(cliPath, 'templates', 'component'); | |
// Create the right formats for the given component name | |
if (componentName.includes('-')) { | |
var componentParts = componentName.split("-"); | |
componentName = ''; | |
for(var part in componentParts) { | |
componentName += capitalizeFirstLetter(componentParts[part]); | |
} | |
} else { | |
var componentParts = componentName.split(/(?=[A-Z])/); | |
componentTag = ''; | |
var first = true; | |
for(var part in componentParts) { | |
if (first) { | |
componentTag += componentParts[part].toLowerCase(); | |
} else { | |
componentTag += '-' + componentParts[part].toLowerCase(); | |
} | |
first = false; | |
} | |
} | |
// Create component folder | |
shell.mkdir('src/components/' + componentTag); | |
// Copy template files to the new component folder | |
shell.cp(path.join(templatePath, 'component.css'), 'src/components/' + componentTag + '/' + componentTag + '.css'); | |
shell.cp(path.join(templatePath, 'component.spec.ts'), 'src/components/' + componentTag + '/' + componentTag + '.spec.ts'); | |
shell.cp(path.join(templatePath, 'component.tsx'), 'src/components/' + componentTag + '/' + componentTag + '.tsx'); | |
// Replace the placeholders with the component name and tag name | |
shell.ls('src/components/' + componentTag + '/' + componentTag + '.*').forEach(function (file) { | |
shell.sed('-i', 'COMPONENT_NAME', componentName, file); | |
shell.sed('-i', 'COMPONENT_TAG', componentTag, file); | |
}); | |
shell.echo('Generated stencil component "' + componentName + '".'); | |
shell.exit(0); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment