create-catalyst-app-internal 0.0.1-beta.33 → 0.0.1-beta.34

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.
@@ -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:",
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 = path.join(__dirname, "../../", '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 = path.join(__dirname, "../../", "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/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "create-catalyst-app-internal",
3
3
  "bin": "scripts/cli.cjs",
4
- "version": "0.0.1-beta.33",
4
+ "version": "0.0.1-beta.34",
5
5
  "description": "cli package to scaffold Catalyst application",
6
6
  "dependencies": {
7
7
  "commander": "^8.2.0",
package/scripts/cli.cjs CHANGED
@@ -28,7 +28,7 @@ const program = new Commander.Command()
28
28
  .action(async (folderName = null, cmd) => {
29
29
  try {
30
30
  if (process.argv.includes("new-route")) {
31
- const createRoutePath = path.join(__dirname, "../codemod/createNewRoute.js")
31
+ const createRoutePath = path.join(__dirname, "../codemod/new-route/index.js")
32
32
  execSync(`node ${createRoutePath}`, { stdio: "inherit" })
33
33
  return
34
34
  }
@@ -1,110 +0,0 @@
1
- const fs = require("fs");
2
- const path = require('path');
3
- const prompts = require('prompts');
4
- const pc = require("picocolors")
5
-
6
- const { program } = require('commander');
7
- const { execSync } = require('child_process');
8
-
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
- const { path: routePath, component } = program;
19
-
20
- // If both options are not provided, prompt the user for input
21
- if (!routePath || !component) {
22
- const response = await prompts([
23
- {
24
- type: routePath ? null : 'text',
25
- name: 'path',
26
- message: 'Enter the route path',
27
- validate: value => (value ? true : 'Route path is required')
28
- },
29
- {
30
- type: component ? null : 'text',
31
- name: 'component',
32
- message: 'Enter the component name for new route',
33
- validate: value => {
34
- const componentNameRegex = /^[A-Z][a-zA-Z0-9]*$/;
35
-
36
- return componentNameRegex.test(value) || "Component name should start with an uppercase letter and may contain alphanumeric characters";
37
- }
38
- },
39
- ]);
40
-
41
-
42
- routePathName = routePath || response.path;
43
- componentName = component || response.component;
44
- }
45
-
46
- if (componentName) {
47
- // Add new component
48
- const isCreated = addNewComponent(componentName)
49
-
50
- // Add the new route
51
- if (isCreated) addNewRoute(componentName, routePathName);
52
- }
53
- }
54
-
55
- // Function to create component using given inputs
56
- function addNewComponent(componentName) {
57
-
58
- const componentDir = "src/js/containers"
59
- const componentPath = path.join(componentDir, `${componentName}/${componentName}.js`);
60
-
61
- // Create the new component file
62
- const componentTemplate = `import React from 'react';
63
-
64
- const ${componentName} = () => {
65
- return (
66
- <div>
67
- <h1>${componentName}</h1>
68
- <p>This is the ${componentName} component.</p>
69
- </div>
70
- );
71
- };
72
-
73
- export default ${componentName};
74
- `;
75
-
76
- try {
77
- // directory check
78
- if (fs.existsSync(`${componentDir}/${componentName}`)) {
79
- throw new Error("directory with similar name already exist");
80
- }
81
-
82
- // component check
83
- if (fs.existsSync(componentPath)) {
84
- throw new Error("component with similar name already exist");
85
- }
86
-
87
- fs.mkdirSync(`${componentDir}/${componentName}`)
88
- fs.writeFileSync(componentPath, componentTemplate, 'utf8');
89
- console.log(`\nComponent ${componentName} created at ${componentPath}`);
90
-
91
- return true
92
-
93
- } catch (e) {
94
- console.log(pc.red(`\nError: ${e}`))
95
- }
96
- }
97
-
98
- // Function to run the codemod with the provided inputs
99
- function addNewRoute(componentName, routePath) {
100
- const codemodPath = path.join(__dirname, 'createRoute.js');
101
- const command = `jscodeshift --silent -t ${codemodPath} --routePath ${routePath} --componentName ${componentName} src/js/routes/index.js`;
102
-
103
- try {
104
- execSync(command, { stdio: 'inherit' });
105
- } catch (error) {
106
- console.log(pc.red(`\nError running codemod: ${e}`))
107
- }
108
- }
109
-
110
- main();
@@ -1,43 +0,0 @@
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
-
19
- const result = root.find(jscodeshift.ImportDeclaration, { source: { value: `@containers/${componentName}/${componentName}` } })
20
-
21
- if (result.__paths.length > 0) {
22
- console.log(pc.red("\nError: Route with similar name already exist!"))
23
- return
24
- } else {
25
-
26
- // Insert the node at the top of the file
27
- root.find(jscodeshift.Program).get('body', 0).insertBefore(`import ${componentName} from '@containers/${componentName}/${componentName}'`);
28
-
29
- root.find(jscodeshift.VariableDeclarator, { id: { name: 'routes' } })
30
- .forEach(path => {
31
- // Ensure we are modifying the top-level array expression
32
- if (path.value.init.type === 'ArrayExpression') {
33
- path.value.init.elements.push(newObject);
34
- }
35
- });
36
-
37
-
38
- console.log(pc.green('\nNew route added successfully.'));
39
- return root.toSource();
40
- }
41
-
42
- };
43
-