Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add to index #10

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,6 @@ This will generate:

The default templates for each file can be modified under `util/templates`.

Don't forget to add the component to your `index.ts` exports if you want the library to export the component!

### Installing Component Library Locally

Let's say you have another project (`test-app`) on your machine that you want to try installing the component library into without having to first publish the component library. In the `test-app` directory, you can run:
Expand Down
38 changes: 38 additions & 0 deletions util/add-to-index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
const fs = require("fs");
require("colors");

module.exports = addToIndex = (componentName) => {
const indexFileLoc = "./src/index.ts"

let data = fs.readFileSync(indexFileLoc, "utf-8");

let index = data.indexOf("import");

let insertedImport = [
data.slice(0, index),
`import ${componentName} from "./${componentName}/${componentName}";\n`,
data.slice(index),
].join("");

let closeExportBracket = insertedImport.lastIndexOf("{");

let insertedExport = [
insertedImport.slice(0, closeExportBracket + 1),
` ${componentName},`,
insertedImport.slice(closeExportBracket + 1),
].join("");

// TODO: Add prettier, then uncomment:
// Reasoning: As the export gets longer, Prettier will split the export onto multiple lines.
// try {
// const prettier = require('prettier')
// insertedExport = prettier.format(insertedExport, { parser: 'typescript' })
// console.log("Formatted library index with" + " Prettier".rainbow + " ✓".green)
// }
// catch (e) {
// console.log(e)
// console.error("Could not format the index with Prettier".red)
// }

fs.writeFileSync(indexFileLoc, insertedExport, "utf8");
};
7 changes: 7 additions & 0 deletions util/create-component.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
require("colors");
const fs = require("fs");
const templates = require("./templates");
const addToIndex = require('./add-to-index')

const componentName = process.argv[2];

Expand Down Expand Up @@ -32,3 +33,9 @@ generatedTemplates.forEach((template) => {
console.log(
"Successfully created component under: " + componentDirectory.green
);

addToIndex(componentName)

console.log(
"Successfully added " + componentName.cyan + " to: " + "./src/index.ts".green
);