Skip to content

Commit

Permalink
♻️ Setup command refactoring (#180)
Browse files Browse the repository at this point in the history
* ♻️ Improve setup code

Improve setup code
  • Loading branch information
tbetous authored Jun 6, 2019
1 parent 755786f commit dc0e280
Show file tree
Hide file tree
Showing 9 changed files with 420 additions and 327 deletions.
16 changes: 16 additions & 0 deletions internals/helpers/clean-readme-content.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
const util = require('util')
const writeFile = util.promisify(require('fs').writeFile)

const newReadmeContent = `# My bento-starter project
## Documentation
Documentation available :point_right: [here](https://bento-starter.netlify.com/)`

/**
* Replace README.md content
*
* @returns {Promise<any>}
*/
module.exports = async () =>
await writeFile(`${__dirname}/../../README.md`, newReadmeContent)
12 changes: 12 additions & 0 deletions internals/helpers/clean-useless-dependencies.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
const util = require('util')
const exec = util.promisify(require('child_process').exec)
const shell = require('shelljs')

/**
* Remove npm dependencies which are only used by this script
* @returns {Promise<any>}
*/
module.exports = async () =>
await exec(
'npm uninstall rimraf compare-versions chalk shelljs read-pkg write-pkg inquirer ora boxen --save-dev'
)
20 changes: 20 additions & 0 deletions internals/helpers/clean-useless-resources.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
const path = require('path')
const util = require('util')
const rimraf = util.promisify(require('rimraf'))

const rootDirname = path.resolve(`${__dirname}/../../`)

const resourcesPathsToDelete = [
`${rootDirname}/internals`,
`${rootDirname}/docs`,
`${rootDirname}/.github`,
`${rootDirname}/resources`,
`${rootDirname}/.env.example`,
`${rootDirname}/LICENSE`
]

/**
* Delete useless resources
* */
module.exports = async () =>
await Promise.all(resourcesPathsToDelete.map(path => rimraf(path)))
19 changes: 19 additions & 0 deletions internals/helpers/clean-useless-scripts.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
const readPkg = require('read-pkg')
const writePkg = require('write-pkg')

/**
* Remove the "setup" script from package.json
* @returns {Promise<any>}
*/
module.exports = async () => {
const pkg = await readPkg()

if (!pkg.scripts) {
pkg.scripts = {}
}

delete pkg.scripts.setup
delete pkg.scripts.presetup

return writePkg(pkg)
}
21 changes: 12 additions & 9 deletions internals/helpers/git-helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,21 @@ async function checkIfRepositoryIsAClone() {
return !!isClonedRepo
}

/**
* Checks if we are under Git version control and if this is a clone of our repository.
* @returns {Promise<boolean>}
*/
async function checkIfRepositoryIsCleanable() {
const hasGitRepo = await hasGitRepository()
return hasGitRepo && (await checkIfRepositoryIsAClone())
}

/**
* Remove the current Git repository
* @returns {Promise<any>}
*/
function removeGitRepository() {
return new Promise((resolve, reject) => {
try {
shell.rm('-rf', '.git/')
resolve()
} catch (err) {
reject(err)
}
})
async function removeGitRepository() {
return exec('rm -Rf .git')
}

/**
Expand Down Expand Up @@ -74,6 +76,7 @@ module.exports = {
initGitRepository,
hasGitRepository,
checkIfRepositoryIsAClone,
checkIfRepositoryIsCleanable,
removeGitRepository,
doInitalCommit,
changeOrigin
Expand Down
13 changes: 13 additions & 0 deletions internals/helpers/set-local-env-file.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const util = require('util')
const copyFile = util.promisify(require('fs').copyFile)

/**
* Create '.env.local' file by copying '.env.example' file
*
* @returns {Promise<any>}
*/
module.exports = async () =>
await copyFile(
`${__dirname}/../../.env.example`,
`${__dirname}/../../.env.local`
)
Loading

0 comments on commit dc0e280

Please sign in to comment.