-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Franck
committed
Jun 8, 2019
1 parent
1ae6c40
commit ead151b
Showing
7 changed files
with
250 additions
and
224 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
const getProjectName = require('project-name') | ||
const isNil = require('lodash/isNil') | ||
const get = require('lodash/get') | ||
const util = require('util') | ||
const ora = require('ora') | ||
const exec = util.promisify(require('child_process').exec) | ||
|
||
const { getPackageJson } = require('../utils') | ||
|
||
const GITHUB_URL = 'https://github.com/' | ||
|
||
/** | ||
* Clean repository url by removing '.git' and 'git+' | ||
* | ||
* @param {string} reposUrl | ||
*/ | ||
const cleanReposUrl = reposUrl => | ||
reposUrl | ||
.replace('\n', '') | ||
.replace('git+', '') | ||
.replace('.git', '') | ||
|
||
/** | ||
* Get repository url from pakage json | ||
* | ||
* @param {Object} reposUrl | ||
*/ | ||
const getReposUrlFromPackageJson = async packageJson => { | ||
const reposUrl = get(packageJson, 'repository.url', undefined) | ||
return isNil(reposUrl) ? undefined : cleanReposUrl(reposUrl) | ||
} | ||
|
||
/** | ||
* Get repository url from git | ||
*/ | ||
const getReposUrlFromGit = async () => { | ||
try { | ||
const result = await exec('git config --get remote.origin.url') | ||
return cleanReposUrl(result.stdout) | ||
} catch (err) { | ||
return undefined | ||
} | ||
} | ||
|
||
/** | ||
* Get repository url from package.json or git | ||
* | ||
* @param {Object} packageJson | ||
*/ | ||
const getReposUrl = async packageJson => | ||
(await getReposUrlFromPackageJson(packageJson)) || | ||
(await getReposUrlFromGit()) | ||
|
||
/** | ||
* Get repository issues url from package.json or git | ||
* | ||
* @param {Object} packageJson | ||
*/ | ||
const getReposIssuesUrl = async packageJson => { | ||
let reposIssuesUrl = get(packageJson, 'bugs.url', undefined) | ||
|
||
if (isNil(reposIssuesUrl)) { | ||
const reposUrl = await getReposUrl() | ||
|
||
if (!isNil(reposUrl)) { | ||
reposIssuesUrl = `${reposUrl}/issues` | ||
} | ||
} | ||
|
||
return reposIssuesUrl | ||
} | ||
|
||
/** | ||
* Check if repository is a Github repository | ||
* | ||
* @param {string} repositoryUrl | ||
*/ | ||
const isGithubRepository = repositoryUrl => | ||
!isNil(repositoryUrl) && repositoryUrl.includes(GITHUB_URL) | ||
|
||
/** | ||
* Get github username from repository url | ||
* | ||
* @param {string} repositoryUrl | ||
*/ | ||
const getGithubUsernameFromRepositoryUrl = repositoryUrl => | ||
repositoryUrl.replace(GITHUB_URL, '').split('/')[0] | ||
|
||
/** | ||
* Get license url from github repository url | ||
* | ||
* @param {string} repositoryUrl | ||
*/ | ||
const getLicenseUrlFromGithubRepositoryUrl = repositoryUrl => | ||
`${repositoryUrl}/blob/master/LICENSE` | ||
|
||
/** | ||
* Get project informations from git and package.json | ||
*/ | ||
const getProjectInfos = async () => { | ||
const spinner = ora('Gathering project infos').start() | ||
|
||
const packageJson = await getPackageJson() | ||
const name = getProjectName() || undefined | ||
const description = get(packageJson, 'description', undefined) | ||
const engines = get(packageJson, 'engines', undefined) | ||
const author = get(packageJson, 'author', undefined) | ||
const version = get(packageJson, 'version', undefined) | ||
const licenseName = get(packageJson, 'license', undefined) | ||
const documentationUrl = get(packageJson, 'homepage', undefined) | ||
const repositoryUrl = await getReposUrl(packageJson) | ||
const contributingUrl = await getReposIssuesUrl(packageJson) | ||
const isGithubRepos = isGithubRepository(repositoryUrl) | ||
const githubUsername = isGithubRepos | ||
? getGithubUsernameFromRepositoryUrl(repositoryUrl) | ||
: undefined | ||
const licenseUrl = isGithubRepos | ||
? getLicenseUrlFromGithubRepositoryUrl(repositoryUrl) | ||
: undefined | ||
|
||
spinner.succeed('Project infos gathered') | ||
|
||
return { | ||
name, | ||
description, | ||
version, | ||
author, | ||
repositoryUrl, | ||
contributingUrl, | ||
githubUsername, | ||
engines, | ||
licenseName, | ||
licenseUrl, | ||
documentationUrl, | ||
isGithubRepos | ||
} | ||
} | ||
|
||
module.exports = { | ||
getProjectInfos | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,13 @@ | ||
const askProjectName = require('./project-name') | ||
const askProjectDescription = require('./project-description') | ||
const askAuhtorName = require('./author-name') | ||
const askAuthorGithub = require('./author-github') | ||
const askAuthorTwitter = require('./author-twitter') | ||
const askLicenseName = require('./license-name') | ||
const askLicenseUrl = require('./license-url') | ||
const askContributing = require('./contributing') | ||
const askProjectVersion = require('./project-version') | ||
const askProjectPrerequisites = require('./project-prerequisites') | ||
const askProjectDocumentationUrl = require('./project-documentation-url') | ||
|
||
module.exports = { | ||
askProjectName, | ||
askProjectVersion, | ||
askProjectDescription, | ||
askProjectDocumentationUrl, | ||
askAuhtorName, | ||
askAuthorGithub, | ||
askAuthorTwitter, | ||
askProjectPrerequisites, | ||
askLicenseName, | ||
askLicenseUrl, | ||
askContributing | ||
askProjectName: require('./project-name'), | ||
askProjectVersion: require('./project-version'), | ||
askProjectDescription: require('./project-description'), | ||
askProjectDocumentationUrl: require('./project-documentation-url'), | ||
askAuhtorName: require('./author-name'), | ||
askAuthorGithub: require('./author-github'), | ||
askAuthorTwitter: require('./author-twitter'), | ||
askProjectPrerequisites: require('./project-prerequisites'), | ||
askLicenseName: require('./license-name'), | ||
askLicenseUrl: require('./license-url'), | ||
askContributing: require('./contributing') | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
const ejs = require('ejs') | ||
const path = require('path') | ||
const ora = require('ora') | ||
const util = require('util') | ||
const getYear = require('date-fns/get_year') | ||
const readFile = util.promisify(require('fs').readFile) | ||
const writeFile = util.promisify(require('fs').writeFile) | ||
|
||
/** | ||
* Create readme file from the given readmeContent | ||
* | ||
* @param {string} readmeContent | ||
*/ | ||
const writeReadme = async readmeContent => | ||
await writeFile('README.md', readmeContent) | ||
|
||
/** | ||
* Get README template content from the given templatePath | ||
* | ||
* @param {string} templatePath | ||
*/ | ||
const getReadmeTemplate = async templatePath => { | ||
const spinner = ora('Loading README template').start() | ||
|
||
try { | ||
const template = await readFile(templatePath, 'utf8') | ||
spinner.succeed('README template loaded') | ||
return template | ||
} catch (err) { | ||
spinner.fail('README template loading fail') | ||
throw err | ||
} | ||
} | ||
|
||
/** | ||
* Build README content with the given answersContext and templateName | ||
* | ||
* @param {Object} context | ||
* @param {string} templateName | ||
*/ | ||
const buildReadmeContent = async (context, templateName) => { | ||
const currentYear = getYear(new Date()) | ||
const templatePath = path.resolve( | ||
__dirname, | ||
`../../templates/${templateName}.md` | ||
) | ||
const template = await getReadmeTemplate(templatePath) | ||
|
||
return ejs.render(template, { | ||
filename: templatePath, | ||
currentYear, | ||
...context | ||
}) | ||
} | ||
|
||
module.exports = { | ||
getReadmeTemplate, | ||
writeReadme, | ||
buildReadmeContent | ||
} |
Oops, something went wrong.