diff --git a/.changeset/loud-deers-hear.md b/.changeset/loud-deers-hear.md new file mode 100644 index 000000000..2a6a7bc33 --- /dev/null +++ b/.changeset/loud-deers-hear.md @@ -0,0 +1,5 @@ +--- +'@ithaka/pharos-cli': minor +--- + +Add newly created components to init components files so that developer can start using the components without any manual changes diff --git a/packages/pharos-cli/package.json b/packages/pharos-cli/package.json index 0359607d0..38b9e117e 100644 --- a/packages/pharos-cli/package.json +++ b/packages/pharos-cli/package.json @@ -20,7 +20,8 @@ "jest": "^27.0.1", "ts-jest": "^27.0.1", "ts-node": "^10.0.0", - "typescript": "^4.5.2" + "typescript": "^4.5.2", + "ts-morph": "^20.0.0" }, "scripts": { "test": "jest", diff --git a/packages/pharos-cli/src/cmds/pharos-component.ts b/packages/pharos-cli/src/cmds/pharos-component.ts index c47fb9015..5b714dc57 100644 --- a/packages/pharos-cli/src/cmds/pharos-component.ts +++ b/packages/pharos-cli/src/cmds/pharos-component.ts @@ -4,6 +4,7 @@ import { generateFile, camelCaseText, addComponentToIndexFile, + addComponentsToInitFile, } from '../utils/generation-utils'; import type { ComponentNameOptions, @@ -72,6 +73,11 @@ const createPharosComponent = async (componentName: ComponentName): Promise { + const componentName = `Pharos${nameOptions.titleCaseName}`; + const unitTestsFilePath: FilePath = `${currentDirectory}/packages/pharos/src/test/initComponents.ts`; + const storybookFilePath: FilePath = `${currentDirectory}/.storybook/initComponents.js`; + + // import new component in list of imports + // and then register new component + [unitTestsFilePath, storybookFilePath].forEach(async (filePath: FilePath) => { + await addNewComponentToInitComponents(filePath, componentName); + }); +}; diff --git a/packages/pharos-cli/src/utils/initComponentsUtil.ts b/packages/pharos-cli/src/utils/initComponentsUtil.ts new file mode 100644 index 000000000..5025b2bb4 --- /dev/null +++ b/packages/pharos-cli/src/utils/initComponentsUtil.ts @@ -0,0 +1,37 @@ +import * as fs from 'fs'; +import { Project, SyntaxKind } from 'ts-morph'; + +export async function addNewComponentToInitComponents( + inputFilePath: string, + componentName: string +) { + const code = await fs.promises.readFile(inputFilePath, 'utf8'); + + const project = new Project(); + const sourceFile = project.createSourceFile(inputFilePath, code, { + overwrite: true, + }); + + const importStatementsDeclarations = sourceFile.getDescendantsOfKind( + SyntaxKind.ImportDeclaration + ); + + importStatementsDeclarations.forEach((dec) => { + const importClause = dec.getImportClause(); + if (importClause && importClause.getNamedImports().length > 0) { + dec.addNamedImport(componentName); + } + }); + + const registerComponentsExpressions = sourceFile.getDescendantsOfKind(SyntaxKind.CallExpression); + + registerComponentsExpressions.forEach((callExp) => { + callExp.forEachChild((expChild) => { + if (expChild.isKind(SyntaxKind.ArrayLiteralExpression)) { + expChild.addElement(componentName); + } + }); + }); + + await sourceFile.save(); +} diff --git a/yarn.lock b/yarn.lock index cdaab3cc3..74ce8e2c9 100644 --- a/yarn.lock +++ b/yarn.lock @@ -5741,6 +5741,16 @@ resolved "https://registry.yarnpkg.com/@trysound/sax/-/sax-0.2.0.tgz#cccaab758af56761eb7bf37af6f03f326dd798ad" integrity sha512-L7z9BgrNEcYyUYtF+HaEfiS5ebkh9jXqbszz7pC0hRBPaatV0XjSD3+eHrpqFemQfgwiFF0QPIarnIihIDn7OA== +"@ts-morph/common@~0.21.0": + version "0.21.0" + resolved "https://registry.yarnpkg.com/@ts-morph/common/-/common-0.21.0.tgz#30272bde654127326d8b73643b9a8de280135fb4" + integrity sha512-ES110Mmne5Vi4ypUKrtVQfXFDtCsDXiUiGxF6ILVlE90dDD4fdpC1LSjydl/ml7xJWKSDZwUYD2zkOePMSrPBA== + dependencies: + fast-glob "^3.2.12" + minimatch "^7.4.3" + mkdirp "^2.1.6" + path-browserify "^1.0.1" + "@tsconfig/node10@^1.0.7": version "1.0.9" resolved "https://registry.yarnpkg.com/@tsconfig/node10/-/node10-1.0.9.tgz#df4907fc07a886922637b15e02d4cebc4c0021b2" @@ -9110,6 +9120,11 @@ co@^4.6.0: resolved "https://registry.yarnpkg.com/co/-/co-4.6.0.tgz#6ea6bdf3d853ae54ccb8e47bfa0bf3f9031fb184" integrity sha512-QVb0dM5HvG+uaxitm8wONl7jltx8dqhfU33DcqtOZcLSVIKSDDLDi7+0LbAKiyI8hD9u42m2YxXSkMGWThaecQ== +code-block-writer@^12.0.0: + version "12.0.0" + resolved "https://registry.yarnpkg.com/code-block-writer/-/code-block-writer-12.0.0.tgz#4dd58946eb4234105aff7f0035977b2afdc2a770" + integrity sha512-q4dMFMlXtKR3XNBHyMHt/3pwYNA69EDk00lloMOaaUMKPUXBw6lpXtbu3MMVG6/uOihGnRDOlkyqsONEUj60+w== + coffee-script@^1.12.4: version "1.12.7" resolved "https://registry.yarnpkg.com/coffee-script/-/coffee-script-1.12.7.tgz#c05dae0cb79591d05b3070a8433a98c9a89ccc53" @@ -11796,6 +11811,17 @@ fast-glob@^3.1.1, fast-glob@^3.2.2, fast-glob@^3.2.7, fast-glob@^3.2.9, fast-glo merge2 "^1.3.0" micromatch "^4.0.4" +fast-glob@^3.2.12: + version "3.3.1" + resolved "https://registry.yarnpkg.com/fast-glob/-/fast-glob-3.3.1.tgz#784b4e897340f3dbbef17413b3f11acf03c874c4" + integrity sha512-kNFPyjhh5cKjrUltxs+wFx+ZkbRaxxmZ+X0ZU31SOsxCEtP9VPgtq2teZw1DebupL5GmDaNQ6yKMMVcM41iqDg== + dependencies: + "@nodelib/fs.stat" "^2.0.2" + "@nodelib/fs.walk" "^1.2.3" + glob-parent "^5.1.2" + merge2 "^1.3.0" + micromatch "^4.0.4" + fast-json-stable-stringify@2.x, fast-json-stable-stringify@^2.0.0, fast-json-stable-stringify@^2.1.0: version "2.1.0" resolved "https://registry.yarnpkg.com/fast-json-stable-stringify/-/fast-json-stable-stringify-2.1.0.tgz#874bf69c6f404c2b5d99c481341399fd55892633" @@ -16681,6 +16707,13 @@ minimatch@^5.0.1: dependencies: brace-expansion "^2.0.1" +minimatch@^7.4.3: + version "7.4.6" + resolved "https://registry.yarnpkg.com/minimatch/-/minimatch-7.4.6.tgz#845d6f254d8f4a5e4fd6baf44d5f10c8448365fb" + integrity sha512-sBz8G/YjVniEz6lKPNpKxXwazJe4c19fEfV2GDMX6AjFz+MX9uDWIZW8XreVhkFW3fkIdTv/gxWr/Kks5FFAVw== + dependencies: + brace-expansion "^2.0.1" + minimatch@^9.0.1: version "9.0.3" resolved "https://registry.npmjs.org/minimatch/-/minimatch-9.0.3.tgz#a6e00c3de44c3a542bfaae70abfc22420a6da825" @@ -16767,6 +16800,11 @@ mkdirp@^1.0.3, mkdirp@^1.0.4: resolved "https://registry.yarnpkg.com/mkdirp/-/mkdirp-1.0.4.tgz#3eb5ed62622756d79a5f0e2a221dfebad75c2f7e" integrity sha512-vVqVZQyf3WLx2Shd0qJ9xuvqgAyKPLAiqITEtqW0oIUjzo3PePDd6fW9iFz30ef7Ysp/oiWqbhszeGWW2T6Gzw== +mkdirp@^2.1.6: + version "2.1.6" + resolved "https://registry.yarnpkg.com/mkdirp/-/mkdirp-2.1.6.tgz#964fbcb12b2d8c5d6fbc62a963ac95a273e2cc19" + integrity sha512-+hEnITedc8LAtIP9u3HJDFIdcLV2vXP33sqLLIzkv1Db1zO/1OxbvYf0Y1OC/S/Qo5dxHXepofhmxL02PsKe+A== + moment@^2.29.1: version "2.29.4" resolved "https://registry.yarnpkg.com/moment/-/moment-2.29.4.tgz#3dbe052889fe7c1b2ed966fcb3a77328964ef108" @@ -17716,6 +17754,11 @@ password-prompt@^1.0.4: ansi-escapes "^3.1.0" cross-spawn "^6.0.5" +path-browserify@^1.0.1: + version "1.0.1" + resolved "https://registry.yarnpkg.com/path-browserify/-/path-browserify-1.0.1.tgz#d98454a9c3753d5790860f16f68867b9e46be1fd" + integrity sha512-b7uo2UCUOYZcnF/3ID0lulOJi/bafxa1xPe7ZPsammBSpjSWQkjNxlt635YGS2MiR9GjvuXCtz2emr3jbsz98g== + path-case@^2.1.0: version "2.1.1" resolved "https://registry.yarnpkg.com/path-case/-/path-case-2.1.1.tgz#94b8037c372d3fe2906e465bb45e25d226e8eea5" @@ -21795,6 +21838,14 @@ ts-lit-plugin@^1.2.1: dependencies: lit-analyzer "1.2.1" +ts-morph@^20.0.0: + version "20.0.0" + resolved "https://registry.yarnpkg.com/ts-morph/-/ts-morph-20.0.0.tgz#c46b4c231dfc93347091901f1f9a3e13413230fd" + integrity sha512-JVmEJy2Wow5n/84I3igthL9sudQ8qzjh/6i4tmYCm6IqYyKFlNbJZi7oBdjyqcWSWYRu3CtL0xbT6fS03ESZIg== + dependencies: + "@ts-morph/common" "~0.21.0" + code-block-writer "^12.0.0" + ts-node@^10.0.0: version "10.9.1" resolved "https://registry.yarnpkg.com/ts-node/-/ts-node-10.9.1.tgz#e73de9102958af9e1f0b168a6ff320e25adcff4b"