Skip to content

Commit

Permalink
feat: copy .editorconfig file on init command (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
ybiquitous committed Aug 30, 2017
1 parent 6ecc070 commit c03020c
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 20 deletions.
5 changes: 1 addition & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"@commitlint/config-angular": "^3.1.1",
"eslint": "^4.5.0",
"eslint-config-ybiquitous": "^2.0.2",
"fs-extra": "^4.0.1",
"husky": "^0.14.3",
"lint-staged": "^4.0.4",
"markdownlint-cli": "^0.3.1",
Expand All @@ -34,7 +35,6 @@
"babel-polyfill": "^6.26.0",
"babel-preset-env": "^1.6.0",
"babel-register": "^6.26.0",
"fs-extra": "^4.0.1",
"mocha": "^3.5.0"
},
"scripts": {
Expand Down
27 changes: 19 additions & 8 deletions src/init.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,33 @@
const path = require('path')
const fs = require('fs')
const { promisify } = require('util')
const fs = require('fs-extra')
const originalPackage = require('../package.json')

const readFile = promisify(fs.readFile)
const writeFile = promisify(fs.writeFile)
const targetProps = ['scripts', 'lint-staged']

module.exports = async function init(basedir = process.cwd()) {
const packageFile = path.join(basedir, 'package.json')
const packageInfo = JSON.parse(await readFile(packageFile, 'utf8'))
async function updatePackageFile(baseDir) {
const packageFile = path.join(baseDir, 'package.json')
const packageInfo = JSON.parse(await fs.readFile(packageFile, 'utf8'))

targetProps.forEach((prop) => {
packageInfo[prop] = { ...originalPackage[prop], ...packageInfo[prop] }
})
packageInfo.scripts['test:watch'] = `${packageInfo.scripts.test} --watch`

await writeFile(packageFile, `${JSON.stringify(packageInfo, null, 2)}\n`)
await fs.writeFile(packageFile, `${JSON.stringify(packageInfo, null, 2)}\n`)

process.stdout.write(`${packageFile} was updated.\n`)
}

async function copyEditorConfig(baseDir) {
const source = path.join(__dirname, '..', '.editorconfig')
const target = path.join(baseDir, '.editorconfig')
await fs.copy(source, target)

process.stdout.write(`${target} was updated.\n`)
}

module.exports = async function init(baseDir = process.cwd()) {
await updatePackageFile(baseDir)

await copyEditorConfig(baseDir)
}
22 changes: 15 additions & 7 deletions src/init.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,28 @@ import assert from 'assert'
import init from './init'

suite('init', () => {
let tmpdir
let workDir
let packageJson

setup('work directory', async () => {
tmpdir = path.join(os.tmpdir(), `cli-${Date.now()}`)
await fs.mkdirs(tmpdir)
workDir = path.join(os.tmpdir(), `cli-${Date.now()}`)
await fs.mkdirs(workDir)
})

setup('package.json', async () => {
packageJson = path.join(tmpdir, 'package.json')
packageJson = path.join(workDir, 'package.json')
await fs.writeJson(packageJson, {
scripts: { test: 'abc' },
'lint-staged': { '*.css': 'xyz' },
})
})

teardown(async () => {
await fs.remove(tmpdir)
await fs.remove(workDir)
})

test('success', async () => {
await init(tmpdir)
test('write package.json', async () => {
await init(workDir)
const pkg = await fs.readJson(packageJson)

assert.deepStrictEqual(pkg.scripts, {
Expand All @@ -51,4 +51,12 @@ suite('init', () => {
'*.css': 'xyz',
})
})

test('write .editorconfig', async () => {
await init(workDir)

const original = await fs.readFile(path.join(__dirname, '..', '.editorconfig'), 'utf8')
const wrote = await fs.readFile(path.join(workDir, '.editorconfig'), 'utf8')
assert(original === wrote)
})
})

0 comments on commit c03020c

Please sign in to comment.