Skip to content

Commit db8177c

Browse files
committed
resolve path issue
1 parent 7e0b74e commit db8177c

File tree

4 files changed

+25
-13
lines changed

4 files changed

+25
-13
lines changed

README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,21 @@ Imagine working on a project with more than 100s of react components. how good w
1111

1212
The way I see it is when you work with multiple different teams on different projects, it is always useful to have isolated project standers that can be easily adjustable for the whole team.
1313

14+
####Install
15+
16+
`npm i react-react-native-component-node-module-templates`
17+
18+
####Setup on first time run
19+
20+
`node node_modules/react-react-native-component-node-module-templates/init.js`
21+
22+
You will notice this will crate an entry called `gen` in your package.json and copy a folder to your project root called
23+
`developer-templates`. Fell free to edit the as you see needed.
24+
25+
####To run the generator
26+
27+
`npm run gen`
28+
1429
See [NPM script to automate tasks in React and other JS Projects](https://roshan.digital/npm-script-to-save-time-react-project/) post for more information.
1530

1631
To Add a new Template

index.js

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ const getFolderList = (path) => {
1919

2020
// recursively pick the path to project package.json
2121
const locationSelector = async (path)=>{
22-
let cPath= path ? path:__dirname;
22+
let cPath= path ? path:__dirname+"/../../";
2323
let folderList = getFolderList(cPath);
2424
folderList.push({title:"ADD_HERE"},{title:"GO_BACK"});
2525
const response = await prompts({
@@ -42,11 +42,9 @@ const locationSelector = async (path)=>{
4242

4343
//prompt user to select tech stack and save it to a config file
4444
const stackTypeSelector = async (path)=> {
45-
path= path ? path:__dirname+"/developer-templates";
46-
console.log("stackTypeSelector",path);
47-
console.log(__dirname)
45+
path= path ? path:__dirname+"/../../developer-templates";
4846
try {
49-
return JSON.parse(fs.readFileSync(__dirname+"developer-templates/config.json")).techStack;
47+
return JSON.parse(fs.readFileSync(__dirname+"/../../developer-templates/config.json")).techStack;
5048
}catch (e) {
5149
let folderList = getFolderList(path);
5250
const response = await prompts({
@@ -55,7 +53,7 @@ const stackTypeSelector = async (path)=> {
5553
message: 'Select your Project stack',
5654
choices: folderList
5755
});
58-
fs.writeFile ("developer-templates/config.json", JSON.stringify(response), function(err) {
56+
fs.writeFile (__dirname+"/../../developer-templates/config.json", JSON.stringify(response), function(err) {
5957
if (err) throw err;
6058
console.log('Response was saved to /developer-templates/config.json. Delete this file if you want to change it later.');
6159
}
@@ -68,7 +66,7 @@ const stackTypeSelector = async (path)=> {
6866
const templateSelector = async (path)=> {
6967
const techStack = await stackTypeSelector();
7068
console.log ("Available templates for " +techStack);
71-
path = path ? path:__dirname+"/developer-templates/"+techStack;
69+
path = path ? path:__dirname+"/../../developer-templates/"+techStack;
7270
console.log("templateSelector",path)
7371
let folderList = getFolderList(path);
7472
const response = await prompts({
@@ -77,7 +75,7 @@ const templateSelector = async (path)=> {
7775
message: 'Select a template',
7876
choices: folderList
7977
});
80-
const selectedPath = __dirname+"/developer-templates/"+techStack+"/"+response.templateType;
78+
const selectedPath = __dirname+"/../../developer-templates/"+techStack+"/"+response.templateType;
8179
const templateName = getFolderList(selectedPath)[0].title;
8280
return [selectedPath,selectedPath+"/"+templateName,templateName];
8381
}
@@ -119,12 +117,12 @@ const generateTaskList = (userInputRequests,responses) => {
119117

120118
const folderNameReplaceHandler = (originalValue,newValue,path) =>{
121119
console.log("Folder Names => ",originalValue," to", newValue);
122-
shell.exec('cd '+path+' && pwd &&'+__dirname+'/node_modules/.bin/renamer --find /'+originalValue+'/i --replace "'+newValue+'" "**/"');
120+
shell.exec('cd '+path+' && pwd &&'+__dirname+'/../../node_modules/.bin/renamer --find /'+originalValue+'/i --replace "'+newValue+'" "**/"');
123121
}
124122

125123
const fileNameReplaceHandler = (originalValue,newValue,path) =>{
126124
console.log("File Names => ",originalValue," to", newValue);
127-
shell.exec('cd '+path+' && pwd &&'+__dirname+'/node_modules/.bin/renamer --find /'+originalValue+'/i --replace "'+newValue+'" "**"');
125+
shell.exec('cd '+path+' && pwd &&'+__dirname+'/../../node_modules/.bin/renamer --find /'+originalValue+'/i --replace "'+newValue+'" "**"');
128126
}
129127

130128
const fileContentReplaceHandler = async (originalValue,newValue,path) =>{

init.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@ const ncp = require('ncp').ncp;
22
const npmAddScript = require('npm-add-script')
33

44
ncp("node_modules/react-react-native-component-node-module-templates/developer-templates","developer-templates")
5-
npmAddScript({key: "gen" , value: "node node_modules/react-react-native-component-node-module-templates/index.js" , force: true})
5+
npmAddScript({key: "gen" , value: "npm explore react-react-native-component-node-module-templates npm run gen" , force: true})

package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
"shelljs": "^0.8.4"
1616
},
1717
"scripts": {
18-
"gen": "node index.js",
19-
"init": "node init.js"
18+
"gen": "node index.js"
2019
}
2120
}

0 commit comments

Comments
 (0)