Skip to content

Commit 873898d

Browse files
committed
Refactor to make main method more readable
1 parent 1111b0c commit 873898d

File tree

3 files changed

+35
-66
lines changed

3 files changed

+35
-66
lines changed

src/githubHandler/index.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,12 @@ const auth = require('../auth');
1212
* github api response, otherwise, return false.
1313
* @throws If the token is not present
1414
*/
15-
async function create(name, isPrivate = false, description = '', url = '') {
15+
async function create({
16+
name,
17+
isPrivate = false,
18+
description = '',
19+
url = '',
20+
}) {
1621
let token = '';
1722
let result;
1823

src/index.js

Lines changed: 21 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -9,27 +9,27 @@ const settings = require('./settings');
99
const gitHandler = require('./gitHandler');
1010
const githubHandler = require('./githubHandler');
1111
const questionnaire = require('./questionnaire');
12+
const template = require('./template');
1213

1314
async function run() {
15+
const TEMPLATE_PATH = path.join(__dirname, '..', 'template');
16+
1417
// First arg = path
15-
let destPath = process.argv[2];
18+
const destPath = utils.fs.resolvePath(process.argv[2]);
1619
if (!destPath) {
1720
throw new Error('A path for the new project is required');
1821
}
1922

20-
destPath = utils.fs.resolvePath(destPath);
21-
const projectFolder = utils.string.normalizeName(destPath);
22-
const templatePath = path.join(__dirname, '..', 'template');
23-
2423
// TODO Include here a way to get "options" for the other args
2524

2625
// Do not continue if the project folder already exists.
2726
if (fs.existsSync(destPath)) {
2827
throw new Error(`The project folder '${destPath} already exists. You need to specify a different path.`);
2928
}
3029

30+
// Setup the defaults
3131
const defaults = {
32-
projectName: projectFolder,
32+
projectName: utils.string.normalizeName(destPath),
3333
gitUserName: await gitHandler.userValue('name'),
3434
gitUserEmail: await gitHandler.userValue('email'),
3535
license: settings.default.license,
@@ -44,83 +44,38 @@ async function run() {
4444
path: destPath,
4545
});
4646

47-
// Create folder
48-
try {
49-
fs.mkdirSync(destPath);
50-
} catch (error) {
51-
console.error('The folder project was not created');
52-
throw error;
53-
}
54-
5547
// Create project object
5648
const project = new Project(answers);
5749

58-
// Setup git configuration values for the project object
50+
// Create folder
5951
try {
60-
await project.initGitConfig();
52+
fs.mkdirSync(project.path);
6153
} catch (error) {
62-
console.error(error);
54+
console.error('The folder project was not created');
55+
throw error;
6356
}
6457

65-
// Initialize git
66-
await gitHandler.init(destPath);
58+
// Initialize git in the dest folder
59+
await gitHandler.init(project.path);
6760

6861
// Create github repository and include properties to the project object
6962
if (project.useGithub) {
70-
const resp = await githubHandler.create(
71-
project.name,
72-
project.isPrivate,
73-
project.description,
74-
project.url
75-
);
63+
const resp = await githubHandler.create(project);
7664
if (resp !== false) {
77-
project.git.httpUrl = resp.html_url;
78-
project.git.name = resp.name;
79-
project.git.sshUrl = resp.ssh_url;
80-
project.issueTracker = `${project.git.httpUrl}/issues`;
81-
gitHandler.addRemote(destPath, project.git.sshUrl);
82-
project.hasRemote = true;
65+
project.setGithubValues(resp);
66+
gitHandler.addRemote(project.path, project.git.sshUrl);
8367
}
8468
}
8569

8670
// Copy template files
87-
utils.fs.copyDirRecursive(templatePath, destPath);
71+
template.copy(TEMPLATE_PATH, project.path);
8872

8973
// TODO Copy license and update with project data
9074

9175

9276
// Update readme with project data
93-
let originalReadmeFile;
94-
try {
95-
originalReadmeFile = fs.readFileSync(path.join(templatePath, 'README.md'), 'utf8');
96-
} catch (error) {
97-
throw error;
98-
}
99-
100-
const generatedReadmeFile = utils.string.replaceByDictionary(originalReadmeFile, project.dictionary);
101-
fs.writeFile(`${destPath}/README.md`, generatedReadmeFile, (err) => {
102-
if (err) {
103-
throw err;
104-
}
105-
console.log('File updated: README.md');
106-
});
107-
108-
// Update package.json with project data
109-
let originalPackageFile;
110-
try {
111-
originalPackageFile = fs.readFileSync(path.join(templatePath, 'package.json'), 'utf8');
112-
} catch (error) {
113-
throw error;
114-
}
115-
116-
const generatedPackageFile = utils.string.replaceByDictionary(originalPackageFile, project.dictionary);
117-
// TODO Change this to sync because dependencies depends on it
118-
fs.writeFile(`${destPath}/package.json`, generatedPackageFile, 'utf8', (err) => {
119-
if (err) {
120-
throw err;
121-
}
122-
console.log('File updated: package.json');
123-
});
77+
template.updateFile(path.join(project.path, 'README.md'), project.dictionary);
78+
template.updateFile(path.join(project.path, 'package.json'), project.dictionary);
12479

12580
// Install devDependencies
12681
console.log('Installing dev dependencies...');
@@ -132,10 +87,11 @@ async function run() {
13287
);
13388

13489
// Commit and push
135-
await gitHandler.commit(destPath);
90+
console.log(await gitHandler.commit(project.path));
13691

13792
if (project.hasRemote) {
138-
gitHandler.push(destPath);
93+
await gitHandler.push(project.path);
94+
console.log(`Code pushed to ${project.git.sshUrl}`);
13995
}
14096
}
14197

src/project/index.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,14 @@ class Project {
127127
async setAuthorEmail() {
128128
this.author.email = this.author.email || await gitHandler.userValue('email');
129129
}
130+
131+
setGithubValues(data) {
132+
this.git.httpUrl = data.html_url;
133+
this.git.name = data.name;
134+
this.git.sshUrl = data.ssh_url;
135+
this.issueTracker = `${this.git.httpUrl}/issues`;
136+
this.hasRemote = true;
137+
}
130138
}
131139

132140
module.exports = Project;

0 commit comments

Comments
 (0)