@@ -6,6 +6,10 @@ const shell = require('shelljs')
66const defined = require ( 'defined' )
77const prompt = require ( './lib/prompts' )
88const 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 * u r l = / ) ) {
160- u = conf [ i + 2 ]
161- }
162- if ( ! u . match ( / ^ \s * u r l = / ) ) {
163- u = null
164- } else {
165- u = u . replace ( / ^ \s * u r l = / , '' )
166- }
167- }
168-
169- // Replace github url
170- if ( u && u . match ( / ^ g i t @ g i t h u b .c o m : / ) ) {
171- u = u . replace ( / ^ g i t @ g i t h u b .c o m : / , '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- }
0 commit comments