Skip to content

Commit

Permalink
feat(cli): add newly created components created using pharos-cli to i…
Browse files Browse the repository at this point in the history
…nitComponents files (#630)

* feat(cli): add newly created components created using pharos-cli to initComponents files

* feat(cli): use forEach to iterate files and also add new console log message

* feat(cli): update log color from green to yellow

* feat(cli): add new script that uses ts-morph to update initComponents files

* feat(cli): empty commit to trigger actions

* feat(cli): find nodes dynamically using syntax kind instead of index

* feat(cli): update artifactory link for ts-morph package

* feat(cli): update artifactory link for ts-morph package
  • Loading branch information
satya-achanta-venkata authored Oct 27, 2023
1 parent 908b475 commit ff057f1
Show file tree
Hide file tree
Showing 6 changed files with 117 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/loud-deers-hear.md
Original file line number Diff line number Diff line change
@@ -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
3 changes: 2 additions & 1 deletion packages/pharos-cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
6 changes: 6 additions & 0 deletions packages/pharos-cli/src/cmds/pharos-component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
generateFile,
camelCaseText,
addComponentToIndexFile,
addComponentsToInitFile,
} from '../utils/generation-utils';
import type {
ComponentNameOptions,
Expand Down Expand Up @@ -72,6 +73,11 @@ const createPharosComponent = async (componentName: ComponentName): Promise<void
console.log(colors.yellow(`\nAdding Pharos${titleCaseName} component export`));
addComponentToIndexFile(currentDirectory, nameOptions);
console.log('\nComponent creation complete! \nHappy developing!'.cyan);
addComponentsToInitFile(currentDirectory, nameOptions);
console.log(
`\nAdded new components to initComponents files as well. You should be able to use your component without making any changes`
.yellow
);
} else {
console.log(
'\n@ithaka/pharos-cli encountered an error attempting to create your component\n'.red
Expand Down
16 changes: 16 additions & 0 deletions packages/pharos-cli/src/utils/generation-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import type {
TemplatePath,
FilePath,
} from '../types';
import { addNewComponentToInitComponents } from './initComponentsUtil';

declare function require(name: string);

Expand Down Expand Up @@ -68,3 +69,18 @@ export const addComponentToIndexFile = (

console.log(colors.green(`\nPharos${nameOptions.titleCaseName} export added to ${filePath}`));
};

export const addComponentsToInitFile = (
currentDirectory: FilePath,
nameOptions: ComponentNameOptions
): void => {
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);
});
};
37 changes: 37 additions & 0 deletions packages/pharos-cli/src/utils/initComponentsUtil.ts
Original file line number Diff line number Diff line change
@@ -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();
}
51 changes: 51 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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"

[email protected], 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"
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down

0 comments on commit ff057f1

Please sign in to comment.