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

fix(init): use templates/ directory #126

Merged
merged 1 commit into from
Feb 19, 2018
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
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