Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(init): add more details to init command help #27

Merged
merged 1 commit into from
Oct 10, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ ybiq --help

### `init`

Setup Node.js project.
Setup npm project.

```sh
ybiq init
Expand Down
6 changes: 1 addition & 5 deletions src/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,7 @@ module.exports = function cli() {
// eslint-disable-next-line no-unused-expressions
yargs
.usage('ybiq <command>')
.command({
command: 'init',
desc: 'Setup npm project',
handler: () => init(),
})
.command('init', init.desc, {}, init)
.demandCommand(1)
.argv
}
90 changes: 54 additions & 36 deletions src/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,54 +4,72 @@ const originalPackage = require('../package.json')

const targetProps = ['scripts', 'lint-staged']

async function updatePackageFile(baseDir) {
const packageFile = path.join(baseDir, 'package.json')
const packageInfo = JSON.parse(await fs.readFile(packageFile, 'utf8'))
function stdout(str) {
process.stdout.write(`${str}\n`)
}

targetProps.forEach((prop) => {
packageInfo[prop] = { ...originalPackage[prop], ...packageInfo[prop] }
})
packageInfo.scripts['test:watch'] = `${packageInfo.scripts.test} --watch`
packageInfo.scripts['test:coverage'] = 'echo "unsupported." && exit 1'
class Init {
constructor(baseDir) {
this.baseDir = baseDir
}

await fs.writeFile(packageFile, `${JSON.stringify(packageInfo, null, 2)}\n`)
filePath(fileName) {
return path.join(this.baseDir, fileName)
}

process.stdout.write(`${packageFile} was updated.\n`)
}
async writeFile(fileName, fileContent) {
const file = this.filePath(fileName)
await fs.writeFile(file, `${fileContent}\n`)
stdout(`${file} was updated.`)
}

async function copyEditorConfig(baseDir) {
const source = path.join(__dirname, '..', '.editorconfig')
const target = path.join(baseDir, '.editorconfig')
await fs.copy(source, target)
async readFile(fileName) {
return fs.readFile(path.join(this.baseDir, fileName), 'utf8')
}

process.stdout.write(`${target} was updated.\n`)
}
async updatePackageFile() {
const packageInfo = JSON.parse(await this.readFile('package.json'))

async function writeConfigFile(baseDir, fileName, fileContent) {
const target = path.join(baseDir, fileName)
await fs.writeFile(target, fileContent)
process.stdout.write(`${target} was wrote.\n`)
}
targetProps.forEach((prop) => {
packageInfo[prop] = { ...originalPackage[prop], ...packageInfo[prop] }
})
packageInfo.scripts['test:watch'] = `${packageInfo.scripts.test} --watch`
packageInfo.scripts['test:coverage'] = 'echo "unsupported." && exit 1'

await this.writeFile('package.json', JSON.stringify(packageInfo, null, 2))
}

async function writeESLintConfig(baseDir) {
writeConfigFile(baseDir, '.eslintrc.js', `module.exports = {
async copyEditorConfig() {
const source = path.join(__dirname, '..', '.editorconfig')
const target = this.filePath('.editorconfig')
await fs.copy(source, target)
stdout(`${target} was updated.`)
}

async writeESLintConfig() {
await this.writeFile('.eslintrc.js', `module.exports = {
root: true,
extends: ['ybiquitous'],
}
`)
}
}`)
}

async function writeCommitlintConfig(baseDir) {
writeConfigFile(baseDir, 'commitlint.config.js', `module.exports = {
async writeCommitlintConfig() {
await this.writeFile('commitlint.config.js', `module.exports = {
extends: ['@commitlint/config-angular'],
}
`)
}`)
}
}

module.exports = async function init() {
const baseDir = process.cwd()
await updatePackageFile(baseDir)
await copyEditorConfig(baseDir)
await writeESLintConfig(baseDir)
await writeCommitlintConfig(baseDir)
const cmd = new Init(process.cwd())
await cmd.updatePackageFile()
await cmd.copyEditorConfig()
await cmd.writeESLintConfig()
await cmd.writeCommitlintConfig()
}

module.exports.desc = `Setup npm project:
- Update 'package.json'
- Create '.editorconfig'
- Create '.eslintrc.js'
- Create 'commitlint.config.js'`
8 changes: 6 additions & 2 deletions test/help.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@ const HELP = `
ybiq <command>

Commands:
init Setup npm project
init Setup npm project:
- Update 'package.json'
- Create '.editorconfig'
- Create '.eslintrc.js'
- Create 'commitlint.config.js'

Options:
--help Show help [boolean]
Expand All @@ -25,7 +29,7 @@ suite('help', () => {

test('with `--help` option', async () => {
const { stdout, stderr } = await exec('--help')
assert(stdout.includes(HELP))
assert(stdout.includes(HELP), stdout)
assert(stderr === '')
})
})