Skip to content

Commit

Permalink
fix(init): use templates/ directory (#126)
Browse files Browse the repository at this point in the history
  • Loading branch information
ybiquitous committed Feb 19, 2018
1 parent d8fc94e commit 7e55ab5
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 25 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
"bin",
"lib",
"src",
".editorconfig",
".eslintrc.js"
"templates",
".editorconfig"
],
"engines": {
"node": ">=6"
Expand Down
34 changes: 15 additions & 19 deletions src/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@ const stdout = str => process.stdout.write(`${str}\n`)
const packagePath = (...pathElements) =>
path.join(...[__dirname, '..', ...pathElements])

const copyFile = async (src, dest) => {
await fs.copy(src, dest)
stdout(`${dest} was updated.`)
}

const template = name => path.join(__dirname, '..', 'templates', name)

class Init {
constructor(baseDir) {
this.baseDir = baseDir
Expand Down Expand Up @@ -59,37 +66,26 @@ class Init {
await this.writeFile('package.json', JSON.stringify(packageInfo, null, 2))
}

async copyEditorConfig() {
const source = packagePath('.editorconfig')
const target = this.currentPath('.editorconfig')
await fs.copy(source, target)
stdout(`${target} was updated.`)
async writeEditorConfig() {
const name = '.editorconfig'
await copyFile(packagePath(name), this.currentPath(name))
}

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

async writeCommitlintConfig() {
await this.writeFile(
'.commitlintrc.js',
`module.exports = {
extends: ['@commitlint/config-conventional'],
}`
)
const name = '.commitlintrc.js'
await copyFile(template(name), this.currentPath(name))
}
}

module.exports = async function init() {
const cmd = new Init(process.cwd())
await cmd.updatePackageFile()
await cmd.copyEditorConfig()
await cmd.writeEditorConfig()
await cmd.writeESLintConfig()
await cmd.writeCommitlintConfig()
}
Expand Down
3 changes: 3 additions & 0 deletions templates/.commitlintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
extends: ['@commitlint/config-conventional'],
}
4 changes: 4 additions & 0 deletions templates/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module.exports = {
root: true,
extends: ['ybiquitous'],
}
14 changes: 10 additions & 4 deletions test/init.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,11 @@ suite('init', () => {
assert(actual === expected)
})

test('copy ".editorconfig"', async () => {
test('write ".editorconfig"', async () => {
await fixture('package-normal.json')
await exec('init')
const { stdout, stderr } = await exec('init')
assert(stdout.includes('package.json was updated.'))
assert(stderr === '')

const original = await readFile(path.join(originalDir, '.editorconfig'))
const copy = await readFile(path.join(workDir, '.editorconfig'))
Expand All @@ -63,7 +65,9 @@ suite('init', () => {

test('write ".eslintrc.js"', async () => {
await fixture('package-normal.json')
await exec('init')
const { stdout, stderr } = await exec('init')
assert(stdout.includes('.eslintrc.js was updated.'))
assert(stderr === '')

const actual = await readFile(path.join(workDir, '.eslintrc.js'))
const expected = await readFile(fixturePath('.eslintrc_expected.js'))
Expand All @@ -72,7 +76,9 @@ suite('init', () => {

test('write ".commitlintrc.js"', async () => {
await fixture('package-normal.json')
await exec('init')
const { stdout, stderr } = await exec('init')
assert(stdout.includes('.commitlintrc.js was updated.'))
assert(stderr === '')

const actual = await readFile(path.join(workDir, '.commitlintrc.js'))
const expected = await readFile(fixturePath('.commitlintrc_expected.js'))
Expand Down

0 comments on commit 7e55ab5

Please sign in to comment.