Skip to content

Commit 220a4b9

Browse files
committed
refactor: De-nesting
1 parent 5e8142d commit 220a4b9

15 files changed

Lines changed: 56 additions & 97 deletions

bin/pangolin.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ program
1010
.command('create <name>')
1111
.description('Create a new Pangolin.js project')
1212
.action(name => {
13-
require('../lib/commands/create')(name)
13+
require('../commands/create')(name)
1414
})
1515

1616
program
Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,18 @@ const fs = require('fs')
33
const path = require('path')
44
const prompts = require('prompts')
55

6-
const copyDir = require('../utils/copy-dir')
7-
const generateGitignore = require('../utils/generate-gitignore')
8-
const generatePackage = require('../utils/generate-package')
9-
const generatePangolinConfig = require('../utils/generate-pangolin-config')
10-
const installDependencies = require('../utils/install-dependencies')
11-
const version = require('../../package.json').version
6+
const copyDir = require('../lib/copy-dir.js')
7+
const generateGitignore = require('../lib/generate-gitignore.js')
8+
const generatePackage = require('../lib/generate-package.js')
9+
const generatePangolinConfig = require('../lib/generate-pangolin-config.js')
10+
const installDependencies = require('../lib/install-dependencies.js')
11+
const version = require('../package.json').version
1212

1313
/**
1414
* Create a new project
1515
* @param {string} name Project name
1616
*/
17-
async function create (name) {
17+
module.exports = async function create (name) {
1818
const context = process.cwd()
1919
const dir = path.join(context, name)
2020

@@ -40,13 +40,16 @@ async function create (name) {
4040
}
4141

4242
// Write package.json
43-
const packageName = path.basename(name) === '.'
43+
const defaultPackageName = path.basename(name) === '.'
4444
? path.basename(context)
4545
: path.basename(name)
46-
const packageData = await generatePackage(packageName)
46+
47+
const packageData = await generatePackage({ name: defaultPackageName })
48+
4749
if (!packageData.license) {
4850
return
4951
}
52+
5053
const packagePath = path.join(dir, 'package.json')
5154
fs.writeFileSync(packagePath, JSON.stringify(packageData, null, 2))
5255

@@ -56,16 +59,14 @@ async function create (name) {
5659
fs.writeFileSync(gitignorePath, gitignoreData)
5760

5861
// Write Pangolin.js config
59-
const pangolinConfigData = generatePangolinConfig(packageData.name)
62+
const pangolinConfigData = generatePangolinConfig({ name: packageData.name })
6063
const pangolinConfigPath = path.join(dir, 'pangolin.config.js')
6164
fs.writeFileSync(pangolinConfigPath, pangolinConfigData)
6265

6366
// Copy template files
64-
const templatePath = path.join(__dirname, '../../template')
67+
const templatePath = path.join(__dirname, '../template')
6568
copyDir(templatePath, dir)
6669

6770
// Install dependencies
68-
await installDependencies(dir)
71+
installDependencies(dir)
6972
}
70-
71-
module.exports = create
Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ const path = require('path')
66
* @param {string} src Source path
77
* @param {string} dest Destination path
88
*/
9-
function copyDir (src, dest) {
9+
module.exports = function copyDir (src, dest) {
1010
fs.readdirSync(src).forEach(file => {
1111
const filePath = path.join(src, file)
1212
const fileDest = path.join(dest, file)
@@ -21,5 +21,3 @@ function copyDir (src, dest) {
2121
}
2222
})
2323
}
24-
25-
module.exports = copyDir

lib/utils/exec.js renamed to lib/exec.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ const childProcess = require('child_process')
44
* Primisified child process execution
55
* @param {string} command Command to execute
66
*/
7-
function exec (command) {
7+
module.exports = function exec (command) {
88
return new Promise((resolve, reject) => {
99
childProcess.exec(command, (error, stdout, stderr) => {
1010
if (error) {
@@ -19,5 +19,3 @@ function exec (command) {
1919
})
2020
})
2121
}
22-
23-
module.exports = exec
Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* Generate .gitignore contents
33
* @returns {string} .gitignore contents
44
*/
5-
function generateGitignore () {
5+
module.exports = function generateGitignore () {
66
return `# OS specific stuff
77
Thumbs.db
88
Desktop.ini
@@ -17,10 +17,6 @@ npm-debug.log*
1717
yarn-debug.log*
1818
yarn-error.log*
1919
20-
# Cache
21-
.eslintcache
22-
.stylelintcache
23-
2420
# Dependencies
2521
node_modules
2622
@@ -30,5 +26,3 @@ node_modules
3026
/dist
3127
`
3228
}
33-
34-
module.exports = generateGitignore
Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
const { blue } = require('kleur')
2-
const getGitUser = require('../utils/get-git-user')
32
const prompts = require('prompts')
43

4+
const getGitUser = require('./get-git-user.js')
5+
56
/**
67
* Generate base data for package.json
7-
* @param {string} name Project name
8+
* @param {Object} options Options
9+
* @param {string} options.name Project name
810
* @returns {Object} package.json-style object
911
*/
10-
async function generatePackage (name) {
12+
module.exports = async function generatePackage ({ name }) {
1113
const gitUser = await getGitUser()
1214

1315
const response = await prompts([
@@ -114,10 +116,8 @@ async function generatePackage (name) {
114116
dev: 'pangolin-core dev',
115117
build: 'pangolin-core build',
116118
'build:dev': 'pangolin-core build --dev',
117-
'lint:css': 'pangolin-core lint css --cache',
118-
'lint:js': 'pangolin-core lint js --cache'
119+
'lint:css': 'pangolin-core lint css',
120+
'lint:js': 'pangolin-core lint js'
119121
}
120122
}
121123
}
122-
123-
module.exports = generatePackage

lib/generate-pangolin-config.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/**
2+
* Generate Pangolin.js config
3+
* @param {Object} options Options
4+
* @param {string} options.name Project name
5+
* @returns {string} Contents of the config file
6+
*/
7+
module.exports = function generatePangolinConfig ({ name }) {
8+
return `module.exports = {
9+
project: {
10+
name: '${name}'
11+
}
12+
}
13+
`
14+
}
Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,17 @@ const exec = require('./exec')
55
*
66
* Based on:
77
* https://github.com/vuejs/vue-cli/blob/230314c980fb901095b9e18656c018be514aa1bf/lib/git-user.js
8+
*
9+
* @returns {{name: string, email: string}} Git user config
810
*/
9-
function getGitUser () {
11+
module.exports = async function getGitUser () {
1012
const getName = exec('git config --get user.name')
1113
const getEmail = exec('git config --get user.email')
1214

13-
return Promise
14-
.all([getName, getEmail])
15-
.then(([name, email]) => {
16-
return {
17-
name: (name && name.trim()) || '',
18-
email: (email && email.trim()) || ''
19-
}
20-
})
21-
}
15+
const [name, email] = await Promise.all([getName, getEmail])
2216

23-
module.exports = getGitUser
17+
return {
18+
name: (name && name.trim()) || '',
19+
email: (email && email.trim()) || ''
20+
}
21+
}
Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const spawnSync = require('child_process').spawnSync
55
* Install dependencies
66
* @param {string} dir Install in this folder
77
*/
8-
async function installDependencies (dir) {
8+
module.exports = function installDependencies (dir) {
99
const oldDir = process.cwd()
1010
process.chdir(dir)
1111

@@ -42,5 +42,3 @@ async function installDependencies (dir) {
4242

4343
process.chdir(oldDir)
4444
}
45-
46-
module.exports = installDependencies

lib/utils/generate-pangolin-config.js

Lines changed: 0 additions & 18 deletions
This file was deleted.

0 commit comments

Comments
 (0)