Skip to content

Commit 288d857

Browse files
authored
Merge pull request #9 from pagesource/bugfix/relative-path-fixes
remove relative path hardcoding
2 parents b4dc5d8 + e112f14 commit 288d857

File tree

12 files changed

+73
-44
lines changed

12 files changed

+73
-44
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ the teams to maintain consistency across project and saves a lot of development
1010

1111
Generate-plop is by default configured to generate files in [TypeScript](https://www.typescriptlang.org/) format. However, you can generate javascript files as well by passing --js flag in script command.
1212

13-
![Demo](https://s10.gifyu.com/images/generate-plop.gif)
13+
![Demo](https://s10.gifyu.com/images/generate-plopd707158878537f9a.gif)
1414

1515
## Features
1616

@@ -75,7 +75,7 @@ Its only dependent on two packages:
7575

7676
## Documentation
7777

78-
[Documentation coming soon](https://linktodocumentation)
78+
[Documentation coming soon](https://github.com/pagesource/universal-react-docs/blob/master/docs/generate-plop.md)
7979
## Contributors
8080

8181
1. [Khushboo Kumari](https://github.com/khus29)

constants.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,5 @@ export default {
77
STORES_PATH: 'src/stores/',
88
APPS_PATH: 'apps/',
99
PACKAGES_PATH: 'packages/',
10-
SERVICES: 'packages/services',
11-
TYPES_SRC:'types'
10+
SERVICES: 'packages/services'
1211
};

index.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,17 @@ const args = process.argv.slice(2);
33
import { Plop, run} from "plop";
44
import minimist from "minimist";
55
const argv = minimist(args);
6+
import { dirname } from 'path';
7+
import { fileURLToPath } from 'url';
8+
9+
const __filename = fileURLToPath(import.meta.url);
10+
const __dirname = dirname(__filename);
611

712
Plop.prepare(
813
{
914
cwd: argv.cwd,
1015
preload: argv.preload || [],
11-
configPath: `${process.cwd()}/node_modules/component-generator/plopfile.js`,
16+
configPath: `${__dirname}/plopfile.js`,
1217
completion: argv.completion,
1318
},
1419
function (env) {

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@xt-pagesource/generate-plop",
3-
"version": "0.1.1",
3+
"version": "0.1.2",
44
"description": "Generate new components fast and consistently.",
55
"main": "index.js",
66
"type": "module",

pages/index.js

100644100755
Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import componentExists from '../utils/componentExists.js'
1010
import monorepoQues from '../utils/monorepoHelpers.js'
1111
import config from '../constants.js'
12-
import {getComputedFolderPath, getFileExtension} from '../utils/common.js'
12+
import {getComputedFolderPath, getFileExtension,getRootDirectoryPath} from '../utils/common.js'
1313

1414
const fileExtension = getFileExtension()
1515

@@ -40,10 +40,12 @@ export default {
4040
],
4141
actions: (data) => {
4242
// Generate pagename/index.js and pagename/test.js
43+
const rootPath = getRootDirectoryPath()
44+
4345
const pagePath =
4446
data.route.trim() !== ''
45-
? `../../${getComputedFolderPath(data.monorepoPath, config.PAGES_PATH)}/${data.route}`
46-
: `../../${getComputedFolderPath(data.monorepoPath, config.PAGES_PATH)}`;
47+
? `${rootPath}/${getComputedFolderPath(data.monorepoPath, config.PAGES_PATH)}/${data.route}`
48+
: `${rootPath}/${getComputedFolderPath(data.monorepoPath, config.PAGES_PATH)}`;
4749

4850
const actions = [
4951
{
@@ -54,7 +56,7 @@ export default {
5456
},
5557
{
5658
type: 'modify',
57-
path: `../../${getComputedFolderPath(data.monorepoPath, config.ROUTES_PATH)}/paths.${fileExtension}`,
59+
path: `${rootPath}/${getComputedFolderPath(data.monorepoPath, config.ROUTES_PATH)}/paths.${fileExtension}`,
5860
transform: (fileContents, data) => {
5961
let routeVarName =
6062
data.route.trim() !== ''

react-component/index.js

100644100755
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import componentExists from '../utils/componentExists.js'
1010
import monorepoQues from '../utils/monorepoHelpers.js'
1111
import config from '../constants.js'
12-
import {isTypescript, getComputedFolderPath, getFileExtension} from '../utils/common.js'
12+
import {isTypescript, getComputedFolderPath, getFileExtension, getRootDirectoryPath} from '../utils/common.js'
1313

1414
const fileExtension = getFileExtension()
1515

@@ -57,8 +57,9 @@ export default {
5757
],
5858
actions: (data) => {
5959
// Generate index.ts/js and index.test.tsx/js
60+
const rootPath = getRootDirectoryPath()
6061
let componentTemplate;
61-
let folderPath = `../../${getComputedFolderPath(data.monorepoPath, config.COMPONENT_PATH)}${data.folder}`;
62+
let folderPath = `${rootPath}/${getComputedFolderPath(data.monorepoPath, config.COMPONENT_PATH)}${data.folder}`;
6263

6364
switch (data.type) {
6465
case 'Class': {

react-hooks/index.js

100644100755
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import componentExists from '../utils/componentExists.js'
1010
import monorepoQues from '../utils/monorepoHelpers.js'
1111
import config from '../constants.js'
12-
import {getFileExtension, getComputedFolderPath} from '../utils/common.js'
12+
import {getFileExtension, getComputedFolderPath, getRootDirectoryPath} from '../utils/common.js'
1313

1414
const fileExtension = getFileExtension()
1515

@@ -36,7 +36,8 @@ export default {
3636
],
3737
actions: (data) => {
3838
// Generate useHookName/index.js and useHookName/test.js
39-
const folderPath = `../../${getComputedFolderPath(data.monorepoPath, config.HOOKS_PATH)}`;
39+
const rootPath = getRootDirectoryPath()
40+
const folderPath = `${rootPath}/${getComputedFolderPath(data.monorepoPath, config.HOOKS_PATH)}`;
4041

4142
const actions = [
4243
{

services/index.js

100644100755
Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
import componentExists from '../utils/componentExists.js'
1010
import config from '../constants.js'
11-
import {getComputedFolderPath, getFileExtension, isTypescript} from '../utils/common.js'
11+
import {getComputedFolderPath, getFileExtension, getRootDirectoryPath, isTypescript} from '../utils/common.js'
1212

1313
const fileExtension = getFileExtension()
1414

@@ -27,30 +27,42 @@ export default {
2727
name: 'name',
2828
message: 'What should it be called?',
2929
default: 'ChangeTitle',
30-
30+
validate: (value) => {
31+
value = `${value}Service`;
32+
if (/.+/.test(value)) {
33+
return componentExists(value, config.API_SRC, config.SERVICES)
34+
? 'A service with this name already exists '
35+
: true;
36+
}
37+
38+
return 'The name is required';
39+
}
3140
}
3241
],
3342
actions: (data) => {
3443
// Generate serviceName.js
35-
const folderPath = `../../${getComputedFolderPath(config.SERVICES, config.API_SRC)}`
36-
44+
const rootPath = getRootDirectoryPath()
45+
46+
const folderPath = `${rootPath}/${getComputedFolderPath(config.SERVICES, config.API_SRC)}`
47+
3748
const actions = [
3849
{
3950
type: 'add',
4051
path: `${folderPath}/{{properCase name}}Service.${fileExtension}`,
4152
templateFile: data.isGraphql == 'yes'? `./services/${fileExtension}-templates/graphql.${fileExtension}.hbs` : `./services/${fileExtension}-templates/index.${fileExtension}.hbs`,
4253
abortOnFail: true
4354
}
55+
4456
];
4557
if(isTypescript()) {
46-
actions.push({
47-
type: 'add',
48-
path: `${folderPath}/types/common.ts`,
49-
templateFile: `./services/${fileExtension}-templates/types.ts.hbs`,
50-
abortOnFail: true,
51-
});
52-
}
53-
54-
return actions;
58+
actions.push({
59+
type: 'add',
60+
path: `${folderPath}/types/common.ts`,
61+
templateFile: `./services/${fileExtension}-templates/types.ts.hbs`,
62+
abortOnFail: true,
63+
});
64+
}
65+
return actions;
66+
5567
}
5668
};

stores/index.js

100644100755
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import componentExists from '../utils/componentExists.js'
1010
import monorepoQues from '../utils/monorepoHelpers.js'
1111
import config from '../constants.js'
12-
import {isTypescript, getComputedFolderPath, getFileExtension} from '../utils/common.js'
12+
import {isTypescript, getComputedFolderPath, getFileExtension, getRootDirectoryPath} from '../utils/common.js'
1313

1414
const fileExtension = getFileExtension()
1515

@@ -35,7 +35,8 @@ export default {
3535
}
3636
],
3737
actions: (data) => {
38-
const folderPath = `../../${getComputedFolderPath(data.monorepoPath, config.STORES_PATH)}`;
38+
const rootPath = getRootDirectoryPath()
39+
const folderPath = `${rootPath}/${getComputedFolderPath(data.monorepoPath, config.STORES_PATH)}`;
3940

4041
const actions = [
4142
{

utils/common.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
/**
22
* common
33
*/
4-
4+
import { dirname } from 'path';
5+
import { fileURLToPath } from 'url';
6+
7+
58
/**
69
* isTypescript - Based on script args, it returns if it is Typescript or javascript generate action
710
* @returns {boolean}
@@ -28,4 +31,13 @@ export function getFileExtension() {
2831

2932
export function getComputedFolderPath(monoropePath, resourcePath) {
3033
return monoropePath ? `${monoropePath}/${resourcePath}` : resourcePath
34+
}
35+
36+
/**
37+
* getRootDirectoryPath - This will return root folder path, parallel to node_modules
38+
*/
39+
export function getRootDirectoryPath() {
40+
const __filename = fileURLToPath(import.meta.url);
41+
const __dirname = dirname(__filename);
42+
return __dirname.substring(0, (__dirname.indexOf('node_modules')-1))
3143
}

0 commit comments

Comments
 (0)