Skip to content

Commit b655080

Browse files
committed
move helper methods to separate files
1 parent 30d7457 commit b655080

6 files changed

Lines changed: 85 additions & 74 deletions

File tree

index.js

Lines changed: 7 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ const shell = require('shelljs')
66
const defined = require('defined')
77
const prompt = require('./lib/prompts')
88
const arrayOrUndefined = require('./lib/array-or-undefined')
9+
const getAuthor = require('./lib/get-author')
10+
const mapDepToVersionString = require('./lib/map-dep-to-version-string')
11+
const scopeAndName = require('./lib/scope-and-name')
12+
const gitRemote = require('./lib/git-remote')
913

1014
// @TODO https://docs.npmjs.com/files/package.json
1115
// man
@@ -72,8 +76,8 @@ async function buildPackageOptions (options = {}, pkg = {}) {
7276
// Set things from opts, package.json or defaults
7377
opts.version = options.version || pkg.version || '1.0.0'
7478
opts.description = options.description || pkg.description || ''
75-
opts.author = options.author || pkg.author || await getAuthor()
76-
opts.repository = options.repository || (pkg.repository && pkg.repository.url) || await readGitRemote(options.directory)
79+
opts.author = options.author || pkg.author || getAuthor()
80+
opts.repository = options.repository || (pkg.repository && pkg.repository.url) || await gitRemote(options.directory)
7781
opts.keywords = arrayOrUndefined(options.keywords) || pkg.keywords || []
7882
opts.license = options.license || pkg.license || 'ISC'
7983
opts.main = options.main || pkg.main || 'index.js'
@@ -85,7 +89,7 @@ async function buildPackageOptions (options = {}, pkg = {}) {
8589
opts.scripts = Object.assign({}, options.scripts || {}, pkg.scripts || {})
8690

8791
// Get name and scope, if not from options from cwd
88-
const {name, scope} = getScopeAndName(options.scope, options.name || pkg.name, options.directory)
92+
const {name, scope} = scopeAndName(options.scope, options.name || pkg.name, options.directory)
8993
opts.name = name
9094
opts.scope = scope
9195

@@ -138,73 +142,3 @@ async function write (opts, pkg) {
138142
// Read full package back to return
139143
return fs.readJSON(opts.pkgPath)
140144
}
141-
142-
//
143-
// Helper Functions
144-
//
145-
146-
function readGitRemote (dir) {
147-
// Taken from npm: https://github.com/npm/init-package-json/blob/latest/default-input.js#L188-L208
148-
return new Promise((resolve) => {
149-
fs.readFile(path.join(dir, '.git', 'config'), 'utf8', function (err, conf) {
150-
if (err || !conf) {
151-
return resolve()
152-
}
153-
conf = conf.split(/\r?\n/)
154-
const i = conf.indexOf('[remote "origin"]')
155-
let u
156-
if (i !== -1) {
157-
// Check if one of the next two lines is the remote url
158-
u = conf[i + 1]
159-
if (!u.match(/^\s*url =/)) {
160-
u = conf[i + 2]
161-
}
162-
if (!u.match(/^\s*url =/)) {
163-
u = null
164-
} else {
165-
u = u.replace(/^\s*url = /, '')
166-
}
167-
}
168-
169-
// Replace github url
170-
if (u && u.match(/^git@github.com:/)) {
171-
u = u.replace(/^git@github.com:/, 'https://github.com/')
172-
}
173-
174-
resolve(u)
175-
})
176-
})
177-
}
178-
179-
function getAuthor () {
180-
const name = shell.exec('git config --get user.name', { silent: true }).stdout.trim()
181-
const email = shell.exec('git config --get user.email', { silent: true }).stdout.trim()
182-
if (!name) {
183-
return
184-
}
185-
return `${name} <${email}>`
186-
}
187-
188-
function getScopeAndName (scope, name, cwd) {
189-
// If no package name, get cwd base name
190-
if (!name) {
191-
name = path.basename(cwd)
192-
}
193-
194-
// If no scope, see if name has a scope in it
195-
if (!scope && name && name.startsWith('@')) {
196-
[scope, name] = name.split('/')
197-
}
198-
199-
// If still no scope, see if one directory up starts with an @
200-
const dirname = path.basename(path.dirname(cwd))
201-
if (dirname.startsWith('@')) {
202-
scope = dirname
203-
}
204-
205-
return { name, scope }
206-
}
207-
208-
function mapDepToVersionString (deps) {
209-
return Array.isArray(deps) ? deps : Object.keys(deps).map((name) => `${name}@${deps[name]}`)
210-
}

lib/array-or-undefined.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
'use strict'
32
// vim: set ft=javascript ts=2 sw=2:
43

lib/get-author.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
'use strict'
2+
// vim: set ft=javascript ts=2 sw=2:
3+
const shell = require('shelljs')
4+
5+
module.exports = function getAuthor () {
6+
const name = shell.exec('git config --get user.name', { silent: true }).stdout.trim()
7+
const email = shell.exec('git config --get user.email', { silent: true }).stdout.trim()
8+
if (!name) {
9+
return
10+
}
11+
return `${name} <${email}>`
12+
}

lib/git-remote.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
'use strict'
2+
// vim: set ft=javascript ts=2 sw=2:
3+
const path = require('path')
4+
const fs = require('fs-extra')
5+
6+
module.exports = function gitRemote (dir) {
7+
// Taken from npm: https://github.com/npm/init-package-json/blob/latest/default-input.js#L188-L208
8+
return new Promise((resolve) => {
9+
fs.readFile(path.join(dir, '.git', 'config'), 'utf8', function (err, conf) {
10+
if (err || !conf) {
11+
return resolve()
12+
}
13+
conf = conf.split(/\r?\n/)
14+
const i = conf.indexOf('[remote "origin"]')
15+
let u
16+
if (i !== -1) {
17+
// Check if one of the next two lines is the remote url
18+
u = conf[i + 1]
19+
if (!u.match(/^\s*url =/)) {
20+
u = conf[i + 2]
21+
}
22+
if (!u.match(/^\s*url =/)) {
23+
u = null
24+
} else {
25+
u = u.replace(/^\s*url = /, '')
26+
}
27+
}
28+
29+
// Replace github url
30+
if (u && u.match(/^git@github.com:/)) {
31+
u = u.replace(/^git@github.com:/, 'https://github.com/')
32+
}
33+
34+
resolve(u)
35+
})
36+
})
37+
}

lib/map-dep-to-version-string.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
'use strict'
2+
// vim: set ft=javascript ts=2 sw=2:
3+
4+
module.exports = function mapDepToVersionString (deps) {
5+
return Array.isArray(deps) ? deps : Object.keys(deps).map((name) => `${name}@${deps[name]}`)
6+
}

lib/scope-and-name.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
'use strict'
2+
// vim: set ft=javascript ts=2 sw=2:
3+
const path = require('path')
4+
5+
module.exports = function scopeAndName (scope, name, cwd) {
6+
// If no package name, get cwd base name
7+
if (!name) {
8+
name = path.basename(cwd)
9+
}
10+
11+
// If no scope, see if name has a scope in it
12+
if (!scope && name && name.startsWith('@')) {
13+
[scope, name] = name.split('/')
14+
}
15+
16+
// If still no scope, see if one directory up starts with an @
17+
const dirname = path.basename(path.dirname(cwd))
18+
if (dirname.startsWith('@')) {
19+
scope = dirname
20+
}
21+
22+
return { name, scope }
23+
}

0 commit comments

Comments
 (0)