-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #109 from UrbanInstitute/feat-cli-init-component
CLI command for initializing component boilerplate
- Loading branch information
Showing
8 changed files
with
533 additions
and
4 deletions.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
#!/usr/bin/env node | ||
import { input } from "@inquirer/prompts"; | ||
import fs from "fs"; | ||
import path from "path"; | ||
import process from "process"; | ||
import { createStory } from "./createStory.js"; | ||
import { createDocs } from "./createDocs.js"; | ||
|
||
// check if PascalCase https://www.w3resource.com/javascript-exercises/javascript-string-exercise-56.php | ||
function isPascalCase(str) { | ||
return /^[A-Z][A-Za-z]*$/.test(str); | ||
} | ||
|
||
// check if name is valid | ||
function validateName(componentName) { | ||
if (!componentName) { | ||
return "Please enter a component name"; | ||
} else if (!isPascalCase(componentName)) { | ||
return "Component name must be in PascalCase"; | ||
} else { | ||
return true; | ||
} | ||
} | ||
|
||
function createFiles(componentName) { | ||
// validate file name | ||
const validated = validateName(componentName); | ||
if (validated !== true) { | ||
return console.error(validated); | ||
} | ||
|
||
// declare dir | ||
const dir = path.join(process.cwd(), "src", "lib", componentName); | ||
|
||
// if directory exists show error, else create files | ||
if (fs.existsSync(dir)) { | ||
console.error("Directory already exists"); | ||
} else { | ||
// create directory | ||
fs.mkdirSync(dir, { recursive: true }); | ||
|
||
// create Component.svelte (blank) | ||
const svelteFilePath = path.join(dir, `${componentName}.svelte`); | ||
fs.writeFileSync(svelteFilePath, "", "utf8"); | ||
|
||
// create Component.docs.md | ||
const docsFilePath = path.join(dir, `${componentName}.docs.md`); | ||
fs.writeFileSync(docsFilePath, createDocs(componentName), "utf8"); | ||
|
||
// create Component.stories.svelte | ||
const storyFilePath = path.join(dir, `${componentName}.stories.svelte`); | ||
fs.writeFileSync(storyFilePath, createStory(componentName), "utf8"); | ||
|
||
// Confirmation text | ||
console.log(`Boilerplate files created in ${dir}`); | ||
} | ||
} | ||
|
||
async function main() { | ||
// if there's an argument in the command, use it (but no more) | ||
if (process.argv[2] && !process.argv[3]) { | ||
createFiles(process.argv[2]); | ||
} else { | ||
// show input | ||
await input({ | ||
message: "What is the name of your component? (ie: MyComponent)", | ||
validate: validateName | ||
}).then(createFiles); | ||
} | ||
} | ||
|
||
main().catch(console.error); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
export function createDocs(componentName) { | ||
return `Esse ipsum deserunt dolor minim dolore sunt cillum. | ||
\`\`\`js | ||
import { ${componentName} } from "@urbaninstitute/dataviz-components"; | ||
\`\`\` | ||
`; | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
export function createStory(componentName) { | ||
return `<script context="module"> | ||
import TestingTree from "./TestingTree.svelte"; | ||
import docs from "./TestingTree.docs.md?raw"; | ||
export const meta = { | ||
title: "Components/TestingTree", | ||
component: TestingTree, | ||
tags: ["autodocs"], | ||
parameters: { | ||
docs: { | ||
description: { | ||
component: docs | ||
} | ||
}, | ||
githubLink: { | ||
url: "/TestingTree/TestingTree.svelte" | ||
} | ||
} | ||
}; | ||
</script> | ||
<script> | ||
import { Story, Template } from "@storybook/addon-svelte-csf"; | ||
</script> | ||
<Template let:args> | ||
<TestingTree {...args} /> | ||
</Template> | ||
<Story name="Default" />`; | ||
} |
Oops, something went wrong.