-
Notifications
You must be signed in to change notification settings - Fork 156
Expand file tree
/
Copy pathadd-to-index.js
More file actions
38 lines (30 loc) · 1.22 KB
/
Copy pathadd-to-index.js
File metadata and controls
38 lines (30 loc) · 1.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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");
};