create-catalyst-app-internal 0.0.1-beta.7 → 0.0.1-beta.71

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (66) hide show
  1. package/Readme.md +56 -0
  2. package/codemod/new-route/index.js +98 -0
  3. package/codemod/new-route/template.js +119 -0
  4. package/codemod/new-route/transformers/createReducer.js +38 -0
  5. package/codemod/new-route/transformers/createRoute.js +33 -0
  6. package/codemod/new-route/utils.js +90 -0
  7. package/license +10 -0
  8. package/package.json +6 -7
  9. package/scripts/cli.cjs +163 -42
  10. package/templates/common/.eslintignore +1 -0
  11. package/templates/common/.eslintrc +28 -0
  12. package/templates/common/README.md +2 -2
  13. package/templates/common/api.js +12 -23
  14. package/templates/common/client/styles.js +1 -12
  15. package/templates/common/config/config.json +2 -23
  16. package/templates/common/public/favicon.ico +0 -0
  17. package/templates/common/server/document.js +3 -5
  18. package/templates/common/server/index.js +1 -0
  19. package/templates/common/server/server.js +8 -1
  20. package/templates/common/src/js/routes/index.js +0 -6
  21. package/templates/common/webpackConfig.js +1 -0
  22. package/templates/mcp-root/mcp/context.md +674 -0
  23. package/templates/mcp-root/mcp/mcp.js +36 -0
  24. package/templates/none-js/client/index.js +5 -7
  25. package/templates/none-js/package.json +12 -5
  26. package/templates/none-js/src/js/containers/App/index.js +1 -1
  27. package/templates/none-js/src/js/routes/utils.js +42 -0
  28. package/templates/none-ts/client/index.js +23 -0
  29. package/templates/none-ts/package.json +34 -0
  30. package/templates/none-ts/src/js/containers/App/index.js +16 -0
  31. package/templates/none-ts/src/js/routes/utils.js +42 -0
  32. package/templates/none-ts/tsconfig.json +13 -0
  33. package/templates/none-ts/types.d.ts +4 -0
  34. package/templates/redux-js/client/index.js +1 -1
  35. package/templates/redux-js/package.json +11 -3
  36. package/templates/redux-js/src/js/containers/App/index.js +1 -1
  37. package/templates/redux-js/src/js/containers/App/reducer.js +0 -1
  38. package/templates/redux-js/src/js/routes/utils.js +42 -0
  39. package/templates/redux-js/src/js/store/index.js +2 -2
  40. package/templates/redux-ts/client/index.js +28 -0
  41. package/templates/redux-ts/package.json +37 -0
  42. package/templates/redux-ts/src/js/containers/App/actions.js +17 -0
  43. package/templates/redux-ts/src/js/containers/App/index.js +16 -0
  44. package/templates/redux-ts/src/js/containers/App/reducer.js +19 -0
  45. package/templates/redux-ts/src/js/routes/utils.js +42 -0
  46. package/templates/redux-ts/src/js/store/index.js +28 -0
  47. package/templates/redux-ts/tsconfig.json +13 -0
  48. package/templates/redux-ts/types.d.ts +4 -0
  49. package/templates/rtk-js/client/index.js +1 -1
  50. package/templates/rtk-js/package.json +11 -3
  51. package/templates/rtk-js/src/js/containers/App/index.js +1 -1
  52. package/templates/rtk-js/src/js/containers/App/reducer.js +1 -1
  53. package/templates/rtk-js/src/js/routes/utils.js +42 -0
  54. package/templates/rtk-js/src/js/store/index.js +2 -2
  55. package/templates/rtk-ts/client/index.js +28 -0
  56. package/templates/rtk-ts/package.json +37 -0
  57. package/templates/rtk-ts/src/js/containers/App/index.js +16 -0
  58. package/templates/rtk-ts/src/js/containers/App/reducer.js +18 -0
  59. package/templates/rtk-ts/src/js/routes/utils.js +42 -0
  60. package/templates/rtk-ts/src/js/store/index.js +28 -0
  61. package/templates/rtk-ts/tsconfig.json +13 -0
  62. package/templates/rtk-ts/types.d.ts +4 -0
  63. package/templates/tailwind/postcss.config.js +5 -0
  64. package/templates/tailwind/src/static/css/base/index.scss +2 -0
  65. package/templates/common/.babelrc +0 -26
  66. package/templates/common/src/js/routes/utils.js +0 -29
package/Readme.md ADDED
@@ -0,0 +1,56 @@
1
+ # Creating a Catalyst App
2
+
3
+ Scaffold your Catalyst app swiftly with `create-catalyst-app`. This tool expedites the process by initializing your project with predefined configurations. To kickstart your project, execute the following command:
4
+
5
+ ```bash
6
+ npx create-catalyst-app@latest
7
+ ```
8
+
9
+ Upon execution, you'll be prompted to name your project. Once named, a template will be cloned into the specified directory.
10
+
11
+ ```bash
12
+ ✔ What is your project named? my-app
13
+ ```
14
+
15
+ Next, select your preferred state management tool from the provided options.
16
+
17
+ ```bash
18
+ ? Choose state management: › - Use arrow-keys. Return to submit .
19
+ ❯ Redux
20
+ Redux Toolkit (RTK)
21
+ None
22
+ ```
23
+
24
+ Following your selection, a default template will be cloned into the directory, and all necessary packages will be installed.
25
+
26
+ ## Getting Started
27
+
28
+ Commence development by initiating the application in development mode with the following commands:
29
+
30
+ Navigate into the directory
31
+
32
+ ```bash
33
+ cd my-app
34
+ ```
35
+
36
+ For running the application in development mode, run:
37
+
38
+ ```bash
39
+ npm run start
40
+ ```
41
+
42
+ For a production build, change NODE_ENV to "production" in config/config.json, and then run :
43
+
44
+ ```bash
45
+ npm run build
46
+ ```
47
+
48
+ To serve the production build, execute:
49
+
50
+ ```bash
51
+ npm run serve
52
+ ```
53
+
54
+ ## Documentation
55
+
56
+ Explore the complete documentation at [https://catalyst.1mg.com](https://catalyst.1mg.com).
@@ -0,0 +1,98 @@
1
+ const fs = require("fs")
2
+ const path = require("path")
3
+ const pc = require("picocolors")
4
+ const prompts = require('prompts');
5
+ const { program } = require('commander');
6
+
7
+ const { createNewComponent, createRTKReducerFile, createReduxActionFile, createReduxReducerFile } = require("./template")
8
+ const { validateComponentName, validatePath, discardChanges, createDirectory, createReducerEntryInStore, createNewRoute, toCamelCase, checkIfRoutePathExist } = require("./utils")
9
+
10
+ // Configure commander to accept CLI options
11
+ program
12
+ .option('-p, --path <path>', 'Path for the new route')
13
+ .option('-c, --component <component>', 'Component name for the new route')
14
+ .parse(process.argv);
15
+
16
+
17
+ async function main() {
18
+ let { routePath, templateType, componentName } = program;
19
+
20
+ // If all options are not provided, prompt the user for input
21
+ if (!routePath || !componentName || !templateType) {
22
+ const response = await prompts([
23
+ {
24
+ type: "select",
25
+ name: "templateType",
26
+ message: "Choose state management template being used:",
27
+ choices: [
28
+ { title: "Redux", value: "redux" },
29
+ { title: "Redux Toolkit (RTK)", value: "rtk" },
30
+ { title: "None", value: "none" },
31
+ ],
32
+ },
33
+ {
34
+ type: routePath ? null : 'text',
35
+ name: 'routePath',
36
+ message: "Enter the route path",
37
+ validate: validatePath
38
+ },
39
+ {
40
+ type: componentName ? null : 'text',
41
+ name: 'componentName',
42
+ message: 'Enter the component name for new route',
43
+ validate: validateComponentName
44
+ }
45
+ ]);
46
+
47
+ routePath = routePath || response.routePath;
48
+ templateType = templateType || response.templateType
49
+ componentName = componentName || response.componentName;
50
+ }
51
+
52
+
53
+ // Code transformations
54
+ if (componentName && routePath && templateType) {
55
+
56
+ const reducerName = toCamelCase(componentName)
57
+ const containersDir = 'src/js/containers';
58
+ const routeTransformerPath = path.join(__dirname, "./", 'transformers/createRoute.js')
59
+ const reducerTransformerPath = path.join(__dirname, "./", 'transformers/createReducer.js');
60
+
61
+ const funcArgs = { containersDir, componentName, routePath, reducerName, templateType, reducerTransformerPath, routeTransformerPath }
62
+
63
+ // create directory
64
+ if (createDirectory(funcArgs)) {
65
+
66
+ try {
67
+
68
+ checkIfRoutePathExist(funcArgs)
69
+
70
+ if (templateType === "none") {
71
+ createNewComponent(funcArgs)
72
+ }
73
+
74
+ if (templateType === "redux") {
75
+ createNewComponent(funcArgs)
76
+ createReduxActionFile(funcArgs)
77
+ createReduxReducerFile(funcArgs)
78
+ createReducerEntryInStore(funcArgs)
79
+ }
80
+
81
+ if (templateType === "rtk") {
82
+ createNewComponent(funcArgs)
83
+ createRTKReducerFile(funcArgs)
84
+ createReducerEntryInStore(funcArgs)
85
+ }
86
+
87
+ createNewRoute(funcArgs);
88
+
89
+ } catch (e) {
90
+ discardChanges(funcArgs)
91
+ console.log(pc.red(`\n${e}`))
92
+ }
93
+ }
94
+
95
+ }
96
+ }
97
+
98
+ main();
@@ -0,0 +1,119 @@
1
+ const fs = require("fs")
2
+ const path = require("path")
3
+ const pc = require("picocolors")
4
+
5
+ // Function to create component using given input
6
+ function createNewComponent({ componentName, containersDir }) {
7
+
8
+ const componentPath = path.join(containersDir, `${componentName}/${componentName}.js`);
9
+
10
+ // Component template
11
+ const componentTemplate = `import React from 'react';
12
+
13
+ const ${componentName} = () => {
14
+ return (
15
+ <div>
16
+ <h1>${componentName}</h1>
17
+ <p>This is the ${componentName} component.</p>
18
+ </div>
19
+ );
20
+ };
21
+
22
+ export default ${componentName};
23
+ `;
24
+
25
+ fs.writeFileSync(componentPath, componentTemplate, 'utf8');
26
+ console.log(`\n${pc.cyan(componentName)} component created at ${pc.gray(componentPath)}`);
27
+ }
28
+
29
+
30
+ // Function to create rtk reducer file using given input
31
+ function createRTKReducerFile({ componentName, containersDir, reducerName }) {
32
+ const reducerPath = path.join(containersDir, `${componentName}/reducer.js`);
33
+
34
+ // rtk reducer template
35
+ const reducerTemplate = `import { createSlice } from "@reduxjs/toolkit"
36
+
37
+ const initialState = {
38
+ testActionDispatched: false,
39
+ }
40
+
41
+ export const appSlice = createSlice({
42
+ name: ${reducerName}Reducer,
43
+ initialState: initialState,
44
+ reducers: {
45
+ reduxTest: (state) => {
46
+ state.testActionDispatched = true
47
+ },
48
+ },
49
+ })
50
+
51
+ export const { reduxTest } = appSlice.actions
52
+ export const ${reducerName}Reducer = appSlice.reducer
53
+ `;
54
+
55
+ fs.writeFileSync(reducerPath, reducerTemplate, 'utf8');
56
+ console.log(`\n${pc.cyan("reducer.js")} file created at ${pc.gray(reducerPath)} `);
57
+ }
58
+
59
+
60
+ // Function to create redux action file using given input
61
+ function createReduxActionFile({ componentName, containersDir, reducerName }) {
62
+ const actionsPath = path.join(containersDir, `${componentName}/actions.js`);
63
+
64
+ // redux action template
65
+ const actionTemplate = `const createActionTypes = (prefix, actionTypeList = []) => {
66
+ const actionTypesObject = {}
67
+ actionTypeList.forEach((item) => {
68
+ actionTypesObject[item] = prefix + "/" + item
69
+ })
70
+
71
+ return actionTypesObject
72
+ }
73
+
74
+ export default createActionTypes
75
+
76
+ export const ${reducerName}Actions = createActionTypes(${reducerName}Actions, ["REDUX_TEST"])
77
+ export const reduxTest = () => {
78
+ return {
79
+ type: ${reducerName}Actions.REDUX_TEST,
80
+ }
81
+ }
82
+ `;
83
+
84
+ fs.writeFileSync(actionsPath, actionTemplate, 'utf8');
85
+ console.log(`\n${pc.cyan("action.js")} file created at ${pc.gray(actionsPath)} `);
86
+
87
+ }
88
+
89
+ // Function to create redux reducer file using given input
90
+ function createReduxReducerFile({ componentName, containersDir, reducerName }) {
91
+ const reducerPath = path.join(containersDir, `${componentName}/reducer.js`);
92
+
93
+ // redux reducer template
94
+ const reducerTemplate = `import { ${reducerName}Actions } from "./actions"
95
+
96
+ export const defaultState = {
97
+ testActionDispatched: false,
98
+ }
99
+
100
+ export const ${reducerName}Reducer = (state = defaultState, action) => {
101
+ switch (action.type) {
102
+ case ${reducerName}Actions.REDUX_TEST: {
103
+ return {
104
+ ...state,
105
+ testActionDispatched: true,
106
+ }
107
+ }
108
+
109
+ default:
110
+ return state
111
+ }
112
+ }
113
+ `;
114
+
115
+ fs.writeFileSync(reducerPath, reducerTemplate, 'utf8');
116
+ console.log(`\n${pc.cyan("reducer.js")} file created at ${pc.gray(reducerPath)} `);
117
+ }
118
+
119
+ module.exports = { createNewComponent, createRTKReducerFile, createReduxActionFile, createReduxReducerFile }
@@ -0,0 +1,38 @@
1
+ const pc = require("picocolors")
2
+
3
+ module.exports = function (fileInfo, api, options) {
4
+ const jscodeshift = api.jscodeshift;
5
+ const root = jscodeshift(fileInfo.source);
6
+
7
+ const { componentName, reducerName } = options;
8
+
9
+ // Insert the node at the top of the file
10
+ root.find(jscodeshift.Program).get('body', 0).insertBefore(`import { ${reducerName}Reducer } from '@containers/${componentName}/reducer.js'`);
11
+
12
+ root.find(jscodeshift.CallExpression, {
13
+ callee: {
14
+ name: 'combineReducers'
15
+ }
16
+ }).forEach(path => {
17
+ const args = path.value.arguments;
18
+ if (args.length > 0 && args[0].type === 'ObjectExpression') {
19
+ args[0].properties.push(
20
+ jscodeshift.property(
21
+ 'init',
22
+ jscodeshift.identifier(`${reducerName}Reducer`),
23
+ jscodeshift.identifier(`${reducerName}Reducer`)
24
+ )
25
+ );
26
+ args[0].properties.forEach(property => {
27
+ if (property.key.name === property.value.name) {
28
+ property.shorthand = true;
29
+ }
30
+ });
31
+ }
32
+ })
33
+
34
+ console.log(`\nReducer added in ${pc.gray("src/js/store/index.js")}`);
35
+ return root.toSource();
36
+
37
+ };
38
+
@@ -0,0 +1,33 @@
1
+ const pc = require("picocolors")
2
+
3
+ module.exports = function (fileInfo, api, options) {
4
+ const jscodeshift = api.jscodeshift;
5
+ const root = jscodeshift(fileInfo.source);
6
+
7
+ const { routePath, componentName } = options;
8
+
9
+ // Create a new route object using a template expression
10
+ const newObject = jscodeshift.template.expression`
11
+ {
12
+ path: ${JSON.stringify(routePath)},
13
+ end: true,
14
+ component: ${componentName}
15
+ }
16
+ `;
17
+
18
+ // Insert the node at the top of the file
19
+ root.find(jscodeshift.Program).get('body', 0).insertBefore(`import ${componentName} from '@containers/${componentName}/${componentName}'`);
20
+
21
+ root.find(jscodeshift.VariableDeclarator, { id: { name: 'routes' } })
22
+ .forEach(path => {
23
+ // Ensure we are modifying the top-level array expression
24
+ if (path.value.init.type === 'ArrayExpression') {
25
+ path.value.init.elements.push(newObject);
26
+ }
27
+ });
28
+
29
+ console.log(pc.green('\nNew route added successfully.'));
30
+ return root.toSource();
31
+
32
+ };
33
+
@@ -0,0 +1,90 @@
1
+ const fs = require("fs")
2
+ const path = require("path")
3
+ const pc = require("picocolors")
4
+ const { execSync } = require('child_process');
5
+
6
+ // validates router path
7
+ function validatePath(path) {
8
+ const pathPattern = /^\/[a-zA-Z0-9-_]+(?:\/:[a-zA-Z0-9-_]+)?(?:\?[a-zA-Z0-9-_=]*)?$/;
9
+
10
+ if (!path.startsWith('/')) {
11
+ return "The path must start with a forward slash (`/`).";
12
+ }
13
+ if (!pathPattern.test(path)) {
14
+ if (!/^[\/a-zA-Z0-9-_]+/.test(path)) {
15
+ s
16
+ return "Only letters, digits, hyphens, and underscores are allowed after the initial slash.";
17
+ }
18
+ if (/\/[^:][a-zA-Z0-9-_]+/.test(path)) {
19
+ return "Parameters must start with a slash and a colon, followed by valid characters.";
20
+ }
21
+ if (/\?[^a-zA-Z0-9-_=]*$/.test(path)) {
22
+ return "Query strings must start with a question mark and contain valid characters.";
23
+ }
24
+ return "The path is incorrectly formatted.";
25
+ }
26
+ return true;
27
+ }
28
+
29
+ // validates component name
30
+ function validateComponentName(name) {
31
+ return /^[A-Z][a-zA-Z0-9]*$/.test(name) || "Component name should start with an uppercase letter and may contain alphanumeric characters";
32
+ }
33
+
34
+
35
+ // discard changes if anything breaks
36
+ function discardChanges({ componentName, containersDir }) {
37
+ if (fs.existsSync(`${containersDir}/${componentName}`)) {
38
+ fs.rmSync(`${containersDir}/${componentName}`, { recursive: true, force: true });
39
+ }
40
+ }
41
+
42
+ // creates directory using given input
43
+ function createDirectory({ componentName, containersDir }) {
44
+ try {
45
+ // directory check
46
+ if (fs.existsSync(`${containersDir}/${componentName}`)) {
47
+ throw new Error("directory with similar name already exist");
48
+ }
49
+
50
+ fs.mkdirSync(`${containersDir}/${componentName}`)
51
+ console.log(`\n${pc.cyan(componentName)} directory created at ${pc.gray(containersDir)}`);
52
+
53
+ return true
54
+
55
+ } catch (e) {
56
+ console.log(pc.red(`\nError: ${e}`))
57
+ }
58
+ }
59
+
60
+ // adds route object inside src/js/route/index.js file
61
+ function createNewRoute({ componentName, routePath, routeTransformerPath }) {
62
+ const command = `jscodeshift --silent -t ${routeTransformerPath} --routePath ${routePath} --componentName ${componentName} src/js/routes/index.js`;
63
+ execSync(command, { stdio: 'inherit' });
64
+ }
65
+
66
+ // adds newly created reducer into src/js/store/index.js file
67
+ function createReducerEntryInStore({ componentName, routePath, reducerTransformerPath, reducerName }) {
68
+ const command = `jscodeshift --silent -t ${reducerTransformerPath} --reducerName ${reducerName} --routePath ${routePath} --componentName ${componentName} src/js/store/index.js`;
69
+ execSync(command, { stdio: 'inherit' });
70
+ }
71
+
72
+ // converts string to camel case
73
+ function toCamelCase(str) {
74
+ return str.toLowerCase().replace(/[^a-zA-Z0-9]+(.)/g, (match, chr) => chr.toUpperCase());
75
+ }
76
+
77
+ function checkIfRoutePathExist({routePath, componentName}) {
78
+ const routeIndexPath = "src/js/routes/index.js";
79
+ const data = fs.readFileSync(routeIndexPath, 'utf8')
80
+ // Check if the file content includes routePath
81
+ if (data.includes(routePath)) {
82
+ throw new Error(`${routePath} path already exist in src/js/routes/index.js`)
83
+ }
84
+
85
+ if (data.includes(`@containers/${componentName}/${componentName}`)) {
86
+ throw new Error(`${componentName} component path already exist in src/js/routes/index.js`)
87
+ }
88
+ }
89
+
90
+ module.exports = { validatePath, validateComponentName, discardChanges, createReducerEntryInStore, createNewRoute, createDirectory, toCamelCase, checkIfRoutePathExist }
package/license ADDED
@@ -0,0 +1,10 @@
1
+ MIT License
2
+
3
+ Copyright (c) 1mg Healthcare Solutions Private Limited.
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
6
+
7
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8
+
9
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
10
+
package/package.json CHANGED
@@ -1,21 +1,20 @@
1
1
  {
2
2
  "name": "create-catalyst-app-internal",
3
3
  "bin": "scripts/cli.cjs",
4
- "version": "0.0.1-beta.7",
4
+ "version": "0.0.1-beta.71",
5
5
  "description": "cli package to scaffold Catalyst application",
6
- "main": "index.js",
7
- "scripts": {
8
- "start": "node index.js"
9
- },
10
6
  "dependencies": {
11
7
  "commander": "^8.2.0",
12
8
  "fs": "^0.0.1-security",
13
9
  "https": "^1.0.0",
14
10
  "picocolors": "^0.1.0",
15
11
  "prompts": "^2.4.0",
16
- "tar": "6.1.15"
12
+ "tar": "^6.2.1",
13
+ "validate-npm-package-name": "^5.0.0",
14
+ "jscodeshift": "^0.16.0"
17
15
  },
18
16
  "devDependencies": {
19
17
  "prettier": "^3.2.5"
20
- }
18
+ },
19
+ "license": "MIT"
21
20
  }