diff --git a/README.md b/README.md index fb648d6..33ba2fb 100644 --- a/README.md +++ b/README.md @@ -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: diff --git a/util/add-to-index.js b/util/add-to-index.js new file mode 100644 index 0000000..c52aac8 --- /dev/null +++ b/util/add-to-index.js @@ -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"); +}; \ No newline at end of file diff --git a/util/create-component.js b/util/create-component.js index fb35646..e750d47 100644 --- a/util/create-component.js +++ b/util/create-component.js @@ -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]; @@ -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 +);