From f054c7f5fa86b2a74bd52cc4f7fd48be686b8ccc Mon Sep 17 00:00:00 2001 From: Satya Achanta Date: Tue, 10 Oct 2023 18:00:14 -0400 Subject: [PATCH 1/8] feat(cli): add newly created components created using pharos-cli to initComponents files --- .changeset/loud-deers-hear.md | 5 ++++ .../pharos-cli/src/cmds/pharos-component.ts | 2 ++ .../pharos-cli/src/utils/generation-utils.ts | 26 +++++++++++++++++++ 3 files changed, 33 insertions(+) create mode 100644 .changeset/loud-deers-hear.md 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/src/cmds/pharos-component.ts b/packages/pharos-cli/src/cmds/pharos-component.ts index c47fb9015..be8c83eb4 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, @@ -71,6 +72,7 @@ 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].map((filePath: FilePath) => { + fs.readFile(filePath, 'utf8', function (_, data) { + const existingFileContent: string[] = data.split('} from'); + const newImportStatement = + existingFileContent[0] + ` ${componentName}` + ',' + '\n' + '} from'; + const updatedFileContent = newImportStatement + existingFileContent[1]; + fs.writeFileSync(filePath, updatedFileContent); + + const existingRegisterComponents: string[] = updatedFileContent.split(']);'); + const newlyRegisteredComponent = + existingRegisterComponents[0] + ` ${componentName},` + '\n' + ']);'; + fs.writeFileSync(filePath, newlyRegisteredComponent); + }); + }); +}; From 5984efe593d19903f016b02e7d59071651964338 Mon Sep 17 00:00:00 2001 From: Satya Achanta Date: Sat, 21 Oct 2023 21:10:20 -0400 Subject: [PATCH 2/8] feat(cli): use forEach to iterate files and also add new console log message --- packages/pharos-cli/src/cmds/pharos-component.ts | 6 +++++- packages/pharos-cli/src/utils/generation-utils.ts | 2 +- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/packages/pharos-cli/src/cmds/pharos-component.ts b/packages/pharos-cli/src/cmds/pharos-component.ts index be8c83eb4..8e860c5d7 100644 --- a/packages/pharos-cli/src/cmds/pharos-component.ts +++ b/packages/pharos-cli/src/cmds/pharos-component.ts @@ -72,8 +72,12 @@ const createPharosComponent = async (componentName: ComponentName): Promise { + [unitTestsFilePath, storybookFilePath].forEach((filePath: FilePath) => { fs.readFile(filePath, 'utf8', function (_, data) { const existingFileContent: string[] = data.split('} from'); const newImportStatement = From 401699340039090a673202801fc689481635cd06 Mon Sep 17 00:00:00 2001 From: Satya Achanta Date: Mon, 23 Oct 2023 18:34:46 -0400 Subject: [PATCH 3/8] feat(cli): update log color from green to yellow --- packages/pharos-cli/src/cmds/pharos-component.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/pharos-cli/src/cmds/pharos-component.ts b/packages/pharos-cli/src/cmds/pharos-component.ts index 8e860c5d7..5b714dc57 100644 --- a/packages/pharos-cli/src/cmds/pharos-component.ts +++ b/packages/pharos-cli/src/cmds/pharos-component.ts @@ -76,7 +76,7 @@ const createPharosComponent = async (componentName: ComponentName): Promise Date: Wed, 25 Oct 2023 22:38:02 -0400 Subject: [PATCH 4/8] feat(cli): add new script that uses ts-morph to update initComponents files --- packages/pharos-cli/package.json | 3 +- .../pharos-cli/src/utils/generation-utils.ts | 16 ++---- .../src/utils/initComponentsUtil.ts | 28 ++++++++++ yarn.lock | 51 +++++++++++++++++++ 4 files changed, 84 insertions(+), 14 deletions(-) create mode 100644 packages/pharos-cli/src/utils/initComponentsUtil.ts 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/utils/generation-utils.ts b/packages/pharos-cli/src/utils/generation-utils.ts index 915402954..6295afe2f 100644 --- a/packages/pharos-cli/src/utils/generation-utils.ts +++ b/packages/pharos-cli/src/utils/generation-utils.ts @@ -7,6 +7,7 @@ import type { TemplatePath, FilePath, } from '../types'; +import { addNewComponentToInitComponents } from './initComponentsUtil'; declare function require(name: string); @@ -79,18 +80,7 @@ export const addComponentsToInitFile = ( // import new component in list of imports // and then register new component - [unitTestsFilePath, storybookFilePath].forEach((filePath: FilePath) => { - fs.readFile(filePath, 'utf8', function (_, data) { - const existingFileContent: string[] = data.split('} from'); - const newImportStatement = - existingFileContent[0] + ` ${componentName}` + ',' + '\n' + '} from'; - const updatedFileContent = newImportStatement + existingFileContent[1]; - fs.writeFileSync(filePath, updatedFileContent); - - const existingRegisterComponents: string[] = updatedFileContent.split(']);'); - const newlyRegisteredComponent = - existingRegisterComponents[0] + ` ${componentName},` + '\n' + ']);'; - fs.writeFileSync(filePath, newlyRegisteredComponent); - }); + [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..b59153a3a --- /dev/null +++ b/packages/pharos-cli/src/utils/initComponentsUtil.ts @@ -0,0 +1,28 @@ +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[0].addNamedImport(componentName); + + const registerComponentsExpressions = sourceFile.getDescendantsOfKind( + SyntaxKind.ArrayLiteralExpression + ); + + registerComponentsExpressions[0].addElement(componentName); + + await sourceFile.save(); +} diff --git a/yarn.lock b/yarn.lock index cdaab3cc3..9f3805a52 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://artifactory.acorn.cirrostratus.org/artifactory/api/npm/npms/@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://artifactory.acorn.cirrostratus.org/artifactory/api/npm/npms/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://artifactory.acorn.cirrostratus.org/artifactory/api/npm/npms/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://artifactory.acorn.cirrostratus.org/artifactory/api/npm/npms/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://artifactory.acorn.cirrostratus.org/artifactory/api/npm/npms/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://artifactory.acorn.cirrostratus.org/artifactory/api/npm/npms/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://artifactory.acorn.cirrostratus.org/artifactory/api/npm/npms/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" From c0616c7cd165eb87e3fbadbcebfbb5c93c739bad Mon Sep 17 00:00:00 2001 From: Satya Achanta Date: Wed, 25 Oct 2023 22:52:28 -0400 Subject: [PATCH 5/8] feat(cli): empty commit to trigger actions From e848da76b1a38b623b523bb4e42be6b7a33db361 Mon Sep 17 00:00:00 2001 From: Satya Achanta Date: Thu, 26 Oct 2023 21:22:31 -0400 Subject: [PATCH 6/8] feat(cli): find nodes dynamically using syntax kind instead of index --- .../src/utils/initComponentsUtil.ts | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/packages/pharos-cli/src/utils/initComponentsUtil.ts b/packages/pharos-cli/src/utils/initComponentsUtil.ts index b59153a3a..5025b2bb4 100644 --- a/packages/pharos-cli/src/utils/initComponentsUtil.ts +++ b/packages/pharos-cli/src/utils/initComponentsUtil.ts @@ -16,13 +16,22 @@ export async function addNewComponentToInitComponents( SyntaxKind.ImportDeclaration ); - importStatementsDeclarations[0].addNamedImport(componentName); + importStatementsDeclarations.forEach((dec) => { + const importClause = dec.getImportClause(); + if (importClause && importClause.getNamedImports().length > 0) { + dec.addNamedImport(componentName); + } + }); - const registerComponentsExpressions = sourceFile.getDescendantsOfKind( - SyntaxKind.ArrayLiteralExpression - ); + const registerComponentsExpressions = sourceFile.getDescendantsOfKind(SyntaxKind.CallExpression); - registerComponentsExpressions[0].addElement(componentName); + registerComponentsExpressions.forEach((callExp) => { + callExp.forEachChild((expChild) => { + if (expChild.isKind(SyntaxKind.ArrayLiteralExpression)) { + expChild.addElement(componentName); + } + }); + }); await sourceFile.save(); } From 8e27b759c8076f1f8b849b63e5765dfa5eb872de Mon Sep 17 00:00:00 2001 From: Satya Achanta Date: Fri, 27 Oct 2023 15:54:34 -0400 Subject: [PATCH 7/8] feat(cli): update artifactory link for ts-morph package --- yarn.lock | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/yarn.lock b/yarn.lock index 9f3805a52..a8d87d772 100644 --- a/yarn.lock +++ b/yarn.lock @@ -5743,7 +5743,7 @@ "@ts-morph/common@~0.21.0": version "0.21.0" - resolved "https://artifactory.acorn.cirrostratus.org/artifactory/api/npm/npms/@ts-morph/common/-/common-0.21.0.tgz#30272bde654127326d8b73643b9a8de280135fb4" + resolved "https://registry.yarnpkg.com/@ts-morph/common/-/common-0.21.0.tgz#30272bde654127326d8b73643b9a8de280135fb4" integrity sha512-ES110Mmne5Vi4ypUKrtVQfXFDtCsDXiUiGxF6ILVlE90dDD4fdpC1LSjydl/ml7xJWKSDZwUYD2zkOePMSrPBA== dependencies: fast-glob "^3.2.12" From eeb04200d0c52e7f34554f43323eee093a081af3 Mon Sep 17 00:00:00 2001 From: Satya Achanta Date: Fri, 27 Oct 2023 16:00:01 -0400 Subject: [PATCH 8/8] feat(cli): update artifactory link for ts-morph package --- yarn.lock | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/yarn.lock b/yarn.lock index a8d87d772..74ce8e2c9 100644 --- a/yarn.lock +++ b/yarn.lock @@ -9122,7 +9122,7 @@ co@^4.6.0: code-block-writer@^12.0.0: version "12.0.0" - resolved "https://artifactory.acorn.cirrostratus.org/artifactory/api/npm/npms/code-block-writer/-/code-block-writer-12.0.0.tgz#4dd58946eb4234105aff7f0035977b2afdc2a770" + 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: @@ -11813,7 +11813,7 @@ fast-glob@^3.1.1, fast-glob@^3.2.2, fast-glob@^3.2.7, fast-glob@^3.2.9, fast-glo fast-glob@^3.2.12: version "3.3.1" - resolved "https://artifactory.acorn.cirrostratus.org/artifactory/api/npm/npms/fast-glob/-/fast-glob-3.3.1.tgz#784b4e897340f3dbbef17413b3f11acf03c874c4" + 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" @@ -16709,7 +16709,7 @@ minimatch@^5.0.1: minimatch@^7.4.3: version "7.4.6" - resolved "https://artifactory.acorn.cirrostratus.org/artifactory/api/npm/npms/minimatch/-/minimatch-7.4.6.tgz#845d6f254d8f4a5e4fd6baf44d5f10c8448365fb" + 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" @@ -16802,7 +16802,7 @@ mkdirp@^1.0.3, mkdirp@^1.0.4: mkdirp@^2.1.6: version "2.1.6" - resolved "https://artifactory.acorn.cirrostratus.org/artifactory/api/npm/npms/mkdirp/-/mkdirp-2.1.6.tgz#964fbcb12b2d8c5d6fbc62a963ac95a273e2cc19" + resolved "https://registry.yarnpkg.com/mkdirp/-/mkdirp-2.1.6.tgz#964fbcb12b2d8c5d6fbc62a963ac95a273e2cc19" integrity sha512-+hEnITedc8LAtIP9u3HJDFIdcLV2vXP33sqLLIzkv1Db1zO/1OxbvYf0Y1OC/S/Qo5dxHXepofhmxL02PsKe+A== moment@^2.29.1: @@ -17756,7 +17756,7 @@ password-prompt@^1.0.4: path-browserify@^1.0.1: version "1.0.1" - resolved "https://artifactory.acorn.cirrostratus.org/artifactory/api/npm/npms/path-browserify/-/path-browserify-1.0.1.tgz#d98454a9c3753d5790860f16f68867b9e46be1fd" + resolved "https://registry.yarnpkg.com/path-browserify/-/path-browserify-1.0.1.tgz#d98454a9c3753d5790860f16f68867b9e46be1fd" integrity sha512-b7uo2UCUOYZcnF/3ID0lulOJi/bafxa1xPe7ZPsammBSpjSWQkjNxlt635YGS2MiR9GjvuXCtz2emr3jbsz98g== path-case@^2.1.0: @@ -21840,7 +21840,7 @@ ts-lit-plugin@^1.2.1: ts-morph@^20.0.0: version "20.0.0" - resolved "https://artifactory.acorn.cirrostratus.org/artifactory/api/npm/npms/ts-morph/-/ts-morph-20.0.0.tgz#c46b4c231dfc93347091901f1f9a3e13413230fd" + 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"