Skip to content

Commit

Permalink
✨ Ask for package manager when is JS project
Browse files Browse the repository at this point in the history
  • Loading branch information
kefranabg committed Dec 3, 2019
1 parent 81e3873 commit 29986ce
Show file tree
Hide file tree
Showing 14 changed files with 376 additions and 63 deletions.
19 changes: 11 additions & 8 deletions src/project-infos.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ const { execSync } = require('child_process')
const {
getPackageJson,
getProjectName,
getAuthorWebsiteFromGithubAPI
getAuthorWebsiteFromGithubAPI,
getPackageManagerFromLockFile
} = require('./utils')

const GITHUB_URL = 'https://github.com/'
Expand Down Expand Up @@ -128,17 +129,18 @@ const getProjectInfos = async () => {

const packageJson = await getPackageJson()
const isJSProject = !!packageJson
const packageManager = isJSProject
? getPackageManagerFromLockFile()
: undefined
const name = getProjectName(packageJson)
const description = get(packageJson, 'description', undefined)
const engines = get(packageJson, 'engines', undefined)
const author = getAuthorName(packageJson)
const version = get(packageJson, 'version', undefined)
const licenseName = get(packageJson, 'license', undefined)
const homepage = get(packageJson, 'homepage', undefined)
const usage = has(packageJson, 'scripts.start') ? 'npm run start' : undefined
const testCommand = has(packageJson, 'scripts.test')
? 'npm run test'
: undefined
const hasStartCommand = has(packageJson, 'scripts.start')
const hasTestCommand = has(packageJson, 'scripts.test')
const repositoryUrl = await getReposUrl(packageJson)
const issuesUrl = await getReposIssuesUrl(packageJson)
const isGithubRepos = isGithubRepository(repositoryUrl)
Expand Down Expand Up @@ -176,9 +178,10 @@ const getProjectInfos = async () => {
licenseUrl,
documentationUrl,
isGithubRepos,
usage,
testCommand,
isJSProject
hasStartCommand,
hasTestCommand,
isJSProject,
packageManager
}
}

Expand Down
99 changes: 84 additions & 15 deletions src/project-infos.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ jest.mock('child_process', () => ({
jest.mock('./utils', () => ({
getPackageJson: jest.fn(),
getProjectName: jest.fn(() => 'readme-md-generator'),
getAuthorWebsiteFromGithubAPI: jest.fn(() => 'https://www.franck-abgrall.me/')
getAuthorWebsiteFromGithubAPI: jest.fn(
() => 'https://www.franck-abgrall.me/'
),
getPackageManagerFromLockFile: jest.fn(() => 'yarn')
}))

const succeed = jest.fn()
Expand Down Expand Up @@ -85,8 +88,9 @@ describe('projectInfos', () => {
isGithubRepos: true,
isJSProject: true,
issuesUrl: 'https://github.com/kefranabg/readme-md-generator/issues',
usage: undefined,
testCommand: undefined
hasStartCommand: false,
hasTestCommand: false,
packageManager: 'yarn'
})
})

Expand Down Expand Up @@ -138,8 +142,9 @@ describe('projectInfos', () => {
isGithubRepos: false,
isJSProject: true,
issuesUrl: 'https://gitlab.com/kefranabg/readme-md-generator/issues',
usage: undefined,
testCommand: undefined
hasStartCommand: false,
hasTestCommand: false,
packageManager: 'yarn'
})
})

Expand Down Expand Up @@ -171,8 +176,8 @@ describe('projectInfos', () => {
isGithubRepos: true,
isJSProject: false,
issuesUrl: 'https://github.com/kefranabg/readme-md-generator/issues',
usage: undefined,
testCommand: undefined
hasStartCommand: false,
hasTestCommand: false
})
})

Expand Down Expand Up @@ -202,8 +207,8 @@ describe('projectInfos', () => {
isGithubRepos: false,
isJSProject: false,
issuesUrl: 'https://gitlab.com/kefranabg/readme-md-generator/issues',
usage: undefined,
testCommand: undefined
hasStartCommand: false,
hasTestCommand: false
})
})

Expand Down Expand Up @@ -231,8 +236,9 @@ describe('projectInfos', () => {
documentationUrl: undefined,
isGithubRepos: false,
isJSProject: false,
usage: undefined,
testCommand: undefined
testCommand: undefined,
hasStartCommand: false,
hasTestCommand: false
})
})

Expand Down Expand Up @@ -286,8 +292,9 @@ describe('projectInfos', () => {
isGithubRepos: true,
isJSProject: true,
issuesUrl: 'https://github.com/kefranabg/readme-md-generator/issues',
usage: undefined,
testCommand: undefined
hasStartCommand: false,
hasTestCommand: false,
packageManager: 'yarn'
})
})

Expand Down Expand Up @@ -345,8 +352,70 @@ describe('projectInfos', () => {
isGithubRepos: true,
isJSProject: true,
issuesUrl: 'https://github.com/kefranabg/readme-md-generator/issues',
usage: undefined,
testCommand: undefined
hasStartCommand: false,
hasTestCommand: false,
packageManager: 'yarn'
})
})

it('should return correct infos when lock file is found', async () => {
const packgeJsonInfos = {
name: 'readme-md-generator',
version: '0.1.3',
description: 'CLI that generates beautiful README.md files.',
author: 'Franck Abgrall',
license: 'MIT',
homepage: 'https://github.com/kefranabg/readme-md-generator',
repository: {
type: 'git',
url: 'git+https://github.com/kefranabg/readme-md-generator.git'
},
bugs: {
url: 'https://github.com/kefranabg/readme-md-generator/issues'
},
engines: {
npm: '>=5.5.0',
node: '>=9.3.0'
},
scripts: {
start: 'node src/index.js',
test: 'jest'
}
}
utils.getPackageJson.mockReturnValueOnce(Promise.resolve(packgeJsonInfos))
utils.getPackageManagerFromLockFile.mockReturnValueOnce('yarn')
childProcess.execSync.mockReturnValue(
'https://github.com/kefranabg/readme-md-generator.git'
)

const projectInfos = await getProjectInfos()

expect(projectInfos).toEqual({
name: 'readme-md-generator',
description: 'CLI that generates beautiful README.md files.',
version: '0.1.3',
author: 'Franck Abgrall',
repositoryUrl: 'https://github.com/kefranabg/readme-md-generator',
homepage: 'https://github.com/kefranabg/readme-md-generator',
contributingUrl:
'https://github.com/kefranabg/readme-md-generator/blob/master/CONTRIBUTING.md',
authorWebsite: 'https://www.franck-abgrall.me/',
githubUsername: 'kefranabg',
engines: {
npm: '>=5.5.0',
node: '>=9.3.0'
},
licenseName: 'MIT',
licenseUrl:
'https://github.com/kefranabg/readme-md-generator/blob/master/LICENSE',
documentationUrl:
'https://github.com/kefranabg/readme-md-generator#readme',
isGithubRepos: true,
isJSProject: true,
issuesUrl: 'https://github.com/kefranabg/readme-md-generator/issues',
hasStartCommand: true,
hasTestCommand: true,
packageManager: 'yarn'
})
})
})
Expand Down
1 change: 1 addition & 0 deletions src/questions/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module.exports = {
askProjectName: require('./project-name'),
askProjectVersion: require('./project-version'),
askProjectDescription: require('./project-description'),
askPackageManager: require('./package-manager'),
askProjectHomepage: require('./project-homepage'),
askProjectDemoUrl: require('./project-demo-url'),
askProjectDocumentationUrl: require('./project-documentation-url'),
Expand Down
1 change: 1 addition & 0 deletions src/questions/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ describe('questions', () => {
'askProjectName',
'askProjectVersion',
'askProjectDescription',
'askPackageManager',
'askProjectHomepage',
'askProjectDemoUrl',
'askProjectDocumentationUrl',
Expand Down
7 changes: 6 additions & 1 deletion src/questions/install-command.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
const isNil = require('lodash/isNil')

module.exports = projectInfos => ({
type: 'input',
message: '📦 Install command (use empty value to skip)',
name: 'installCommand',
default: projectInfos.isJSProject ? 'npm install' : undefined
default: answers => {
const packageManager = answers.packageManager || projectInfos.packageManager
return isNil(packageManager) ? undefined : `${packageManager} install`
}
})
39 changes: 23 additions & 16 deletions src/questions/install-command.spec.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,34 @@
const askInstallCommand = require('./install-command')

describe('askInstallCommand', () => {
it('should return correct question format when project lang is js', () => {
const projectInfos = { isJSProject: true }
const result = askInstallCommand(projectInfos)
it('should return correct question format', () => {
const result = askInstallCommand()
expect(result).toEqual(
expect.objectContaining({
type: 'input',
message: '📦 Install command (use empty value to skip)',
name: 'installCommand'
})
)
})

it('should return undefined default answer when package manager is not defined', () => {
const projectInfos = {}

expect(result).toEqual({
type: 'input',
message: '📦 Install command (use empty value to skip)',
name: 'installCommand',
default: 'npm install'
const result = askInstallCommand(projectInfos).default({
packageManager: undefined
})

expect(result).toBeUndefined()
})

it('should return correct question format when project lang is not js', () => {
const projectInfos = { isJSProject: false }
const result = askInstallCommand(projectInfos)
it('should return correct default answer when package manager is defined', () => {
const projectInfos = {}

expect(result).toEqual({
type: 'input',
message: '📦 Install command (use empty value to skip)',
name: 'installCommand',
default: undefined
const result = askInstallCommand(projectInfos).default({
packageManager: 'yarn'
})

expect(result).toEqual('yarn install')
})
})
18 changes: 18 additions & 0 deletions src/questions/package-manager.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
const isEmpty = require('lodash/isEmpty')

module.exports = projectInfos => ({
type: 'list',
message: '📦 Choose Package Manager ',
name: 'packageManager',
choices: [
{
name: 'npm',
value: 'npm'
},
{
name: 'yarn',
value: 'yarn'
}
],
when: () => projectInfos.isJSProject && isEmpty(projectInfos.packageManager)
})
47 changes: 47 additions & 0 deletions src/questions/package-manager.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
const askPackageManager = require('./package-manager')

const expectedQuestion = {
type: 'list',
message: '📦 Choose Package Manager ',
name: 'packageManager',
choices: [
{
name: 'npm',
value: 'npm'
},
{
name: 'yarn',
value: 'yarn'
}
]
}

describe('askPackageManager', () => {
it('should return correct question format when package manager is undefined', () => {
const projectInfos = { packageManager: undefined }
const result = askPackageManager(projectInfos)

expect(result).toEqual(expect.objectContaining(expectedQuestion))
})

it('should not show question for a non JS Project', () => {
const projectInfos = { isJSProject: false, packageManager: undefined }
const result = askPackageManager(projectInfos).when(projectInfos)

expect(result).toBe(false)
})

it('should not show question when package manager has already been detected', () => {
const projectInfos = { isJSProject: true, packageManager: 'yarn' }
const result = askPackageManager(projectInfos).when(projectInfos)

expect(result).toBe(false)
})

it('should show question when package manager is undefined and if project is JS', () => {
const projectInfos = { isJSProject: true, packageManager: undefined }
const result = askPackageManager(projectInfos).when(projectInfos)

expect(result).toBe(true)
})
})
9 changes: 8 additions & 1 deletion src/questions/test-command.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
const isNil = require('lodash/isNil')

module.exports = projectInfos => ({
type: 'input',
message: '✅ Test command (use empty value to skip)',
name: 'testCommand',
default: projectInfos.testCommand
default: answers => {
const packageManager = answers.packageManager || projectInfos.packageManager
return projectInfos.hasTestCommand && !isNil(packageManager)
? `${packageManager} run test`
: undefined
}
})

0 comments on commit 29986ce

Please sign in to comment.