Skip to content

Commit f51a6ab

Browse files
committed
Validation for paths using homedir
1 parent 6bf5a47 commit f51a6ab

File tree

5 files changed

+57
-41
lines changed

5 files changed

+57
-41
lines changed

src/auth/index.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
const fs = require('fs');
2-
const path = require('path');
32

43
const settings = require('../settings');
4+
const utils = require('../utils');
55

66
/**
77
* Get the Github token from the auth file
@@ -11,7 +11,7 @@ const settings = require('../settings');
1111
async function getToken(jsonPath = '') {
1212
let auth = {};
1313
const authPath = jsonPath || settings.authPath;
14-
const authFile = path.resolve(authPath);
14+
const authFile = utils.resolvePath(authPath);
1515

1616
try {
1717
auth = JSON.parse(fs.readFileSync(authFile, 'utf8'));
@@ -35,7 +35,7 @@ async function getToken(jsonPath = '') {
3535
async function updateToken(token, jsonPath = '') {
3636
let auth = {};
3737
const authPath = jsonPath || settings.authPath;
38-
const authFile = path.resolve(authPath);
38+
const authFile = utils.resolvePath(authPath);
3939

4040
try {
4141
auth = JSON.parse(fs.readFileSync(authFile, 'utf8'));

src/index.js

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,14 @@ const gitHandler = require('./gitHandler');
1010
const githubHandler = require('./githubHandler');
1111
const questionnaire = require('./questionnaire');
1212

13-
async function myPackage() {
13+
async function run() {
1414
// First arg = path
15-
const destPath = path.resolve(process.argv[2]);
15+
let destPath = process.argv[2];
16+
if (!destPath) {
17+
throw new Error('A path for the new project is required');
18+
}
19+
20+
destPath = utils.resolvePath(destPath);
1621
const projectFolder = utils.normalizeName(destPath);
1722
const templatePath = path.join(__dirname, '..', 'template');
1823

@@ -23,14 +28,6 @@ async function myPackage() {
2328
throw new Error(`The project folder '${destPath} already exists. You need to specify a different path.`);
2429
}
2530

26-
// Create folder
27-
try {
28-
fs.mkdirSync(destPath);
29-
} catch (error) {
30-
console.error('The folder project was not created. See details on the log');
31-
throw error;
32-
}
33-
3431
// Questionnaire for the options
3532
const answers = await questionnaire.run(projectFolder);
3633

@@ -39,6 +36,14 @@ async function myPackage() {
3936
path: destPath,
4037
});
4138

39+
// Create folder
40+
try {
41+
fs.mkdirSync(destPath);
42+
} catch (error) {
43+
console.error('The folder project was not created');
44+
throw error;
45+
}
46+
4247
// Create project object
4348
const project = new Project(answers);
4449

@@ -126,4 +131,4 @@ async function myPackage() {
126131
}
127132
}
128133

129-
myPackage();
134+
run();

src/questionnaire/index.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,11 @@ async function run(name) {
1010
resp.hasRemote = !!resp.git.url;
1111
} else {
1212
Object.assign(resp, await questions.getAuthFile());
13+
1314
const token = await auth.getToken(resp.authPath);
15+
1416
Object.assign(resp, await questions.getAuthToken(token));
17+
1518
if (resp.token) {
1619
if (resp.token !== token && await auth.confirmUpdateToken()) {
1720
auth.updateToken(resp.token, settings.authPath);
@@ -21,10 +24,6 @@ async function run(name) {
2124
}
2225
}
2326

24-
if (resp.useTesting) {
25-
Object.assign(resp, await questions.getTestingDetails());
26-
}
27-
2827
return resp;
2928
}
3029

src/questionnaire/questions.js

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
const inquirer = require('inquirer');
2+
const fs = require('fs');
23

34
const settings = require('../settings');
5+
const utils = require('../utils');
46

57
async function getProjectDetails(name) {
68
return inquirer.prompt([
@@ -71,15 +73,16 @@ async function getProjectDetails(name) {
7173
},
7274

7375
{
74-
type: 'confirm',
75-
name: 'useGithub',
76-
message: 'Would you like to create a GitHub repository?',
76+
type: 'checkbox',
77+
name: 'testPackages',
78+
message: 'Which test packages do you want to include?',
79+
choices: settings.testingPkgs,
7780
},
7881

7982
{
8083
type: 'confirm',
81-
name: 'useTesting',
82-
message: 'Would you like to include testing?',
84+
name: 'useGithub',
85+
message: 'Would you like to create a GitHub repository?',
8386
},
8487
]);
8588
}
@@ -102,24 +105,20 @@ async function getGitRemoteDetails() {
102105
]);
103106
}
104107

105-
async function getTestingDetails() {
106-
return inquirer.prompt([
107-
{
108-
type: 'checkbox',
109-
name: 'testPackages',
110-
message: 'Which test packages do you want to include?',
111-
choices: settings.testingPkgs,
112-
},
113-
]);
114-
}
115-
116108
async function getAuthFile() {
117109
return inquirer.prompt([
118110
{
119111
type: 'input',
120112
name: 'authPath',
121113
message: 'What is the path for the auth.json file?',
122114
default: settings.authPath,
115+
validate: (ans) => {
116+
const path = utils.resolvePath(ans);
117+
if (path && fs.existsSync(path)) {
118+
return true;
119+
}
120+
return 'You should introduce a real path for the auth.json';
121+
},
123122
},
124123
]);
125124
}
@@ -148,7 +147,6 @@ async function confirmUpdateToken() {
148147
module.exports = {
149148
getProjectDetails,
150149
getGitRemoteDetails,
151-
getTestingDetails,
152150
getAuthFile,
153151
getAuthToken,
154152
confirmUpdateToken,

src/utils/index.js

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,19 @@
11
const { exec, spawn } = require('child_process');
22
const fs = require('fs');
33
const path = require('path');
4+
const os = require('os');
5+
6+
/**
7+
* Resolve a path even if is using shell specific for home
8+
* @param {String} oPath The path to resolve
9+
* @return {String} The resolved path
10+
*/
11+
function resolvePath(oPath) {
12+
let fPath = '';
13+
fPath = oPath.replace('~', os.homedir());
14+
fPath = path.resolve(fPath);
15+
return fPath;
16+
}
417

518
/**
619
* Replace a string using a given dictionary
@@ -31,8 +44,8 @@ function normalizeName(filepath) {
3144
* @param {String} [destPath='../new'] The destination path
3245
*/
3346
function copyDirRecursive(currentPath = './', destPath = '../new') {
34-
let dest = path.resolve(destPath);
35-
let current = path.resolve(currentPath);
47+
let dest = resolvePath(destPath);
48+
let current = resolvePath(currentPath);
3649

3750
// Create the dest folder
3851
if (!fs.existsSync(dest)) {
@@ -43,8 +56,8 @@ function copyDirRecursive(currentPath = './', destPath = '../new') {
4356
// Read files in folder
4457
let files = fs.readdirSync(current);
4558
for(file of files) {
46-
src = path.resolve(path.join(current, file));
47-
dest = path.resolve(path.join(destPath, file));
59+
src = resolvePath(path.join(current, file));
60+
dest = resolvePath(path.join(destPath, file));
4861

4962
if (fs.lstatSync(src).isDirectory()) {
5063
// Recursive copy for folders
@@ -58,10 +71,10 @@ function copyDirRecursive(currentPath = './', destPath = '../new') {
5871
}
5972

6073
function deleteDirRecursive(folderPath) {
61-
const dirPath = path.resolve(folderPath);
74+
const dirPath = resolvePath(folderPath);
6275
if (fs.existsSync(dirPath)) {
6376
fs.readdirSync(dirPath).forEach((file) => {
64-
const curPath = path.resolve(path.join(dirPath, file));
77+
const curPath = resolvePath(path.join(dirPath, file));
6578
if (fs.lstatSync(curPath).isDirectory()) { // recurse
6679
deleteDirRecursive(curPath);
6780
} else { // delete file
@@ -128,4 +141,5 @@ module.exports = {
128141
execp,
129142
spawnp,
130143
normalizeName,
144+
resolvePath,
131145
};

0 commit comments

Comments
 (0)