-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
37 lines (30 loc) · 943 Bytes
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
// @flow
'use strict'
const os = require('os')
const fs = require('fs')
const path = require('path')
const cpFile = require('cp-file')
const makeDir = require('make-dir')
const pathExists = require('path-exists')
module.exports = (input /* : string */, opts /* : Object */) => {
if (typeof input !== 'string') {
throw new TypeError(`Expected a string, got ${typeof input}`)
}
opts = Object.assign({ add: false, overwrite: false }, opts)
const configPath = opts.dirPath || path.join(os.homedir(), '.touch-alt')
if (!pathExists.sync(configPath)) {
makeDir.sync(configPath)
}
const target = path.resolve(configPath, input)
if (opts.add) {
cpFile.sync(input, target)
return
}
if (pathExists.sync(target)) {
cpFile.sync(target, input, { overwrite: opts.overwrite })
} else if (pathExists.sync(input)) {
throw new Error(`already exist: ${input}`)
} else {
fs.writeFileSync(input, '')
}
}