Commit 35c3d3e3 authored by Daniel Flohrer's avatar Daniel Flohrer
Browse files

initial commit

parents
{
"extends": "standard",
"plugins": [
"standard",
"promise"
]
}
\ No newline at end of file
# OSX
#
.DS_Store
# node.js
#
node_modules/
npm-debug.log
.eslintrc.json
const TGDFile = require('./src/TGDFile')
module.exports = TGDFile
{
"name": "tgd-file",
"version": "0.0.1",
"description": "extended file operations",
"main": "index.js",
"author": "Daniel Flohrer",
"license": "ISC",
"repository": {
"type": "git",
"url": "git@git.1000grad.com:daniel/tgd-file.git"
},
"scripts": {},
"preferGlobal": false,
"bin": {},
"dependencies": {
"tgd-shell": "git+ssh://git@git.1000grad.com:daniel/tgd-shell.git#0.0.2"
},
"devDependencies": {
"eslint": "^3.13.1",
"eslint-config-standard": "^6.2.1",
"eslint-plugin-promise": "^3.4.0",
"eslint-plugin-standard": "^2.0.1"
}
}
const fs = require('fs')
const TGDShell = require('tgd-shell')
// manipulating file with sed: http://www.thegeekstuff.com/2009/11/unix-sed-tutorial-append-insert-replace-and-count-file-lines
module.exports = class TGDFile {
static exists (fsPath, verboseLog = false) {
const exists = fs.existsSync(fsPath)
if (!exists) {
if (verboseLog) {
console.log(`... ${fsPath} doesn't exist ...`)
}
}
return exists
}
static copy (srcPath, destPath, verbose = false) {
return TGDShell.exec(`cp ${srcPath} ${destPath}`, verbose)
}
static write (path, dataString) {
return new Promise((resolve, reject) => {
fs.writeFile(path, dataString, (err) => {
err ? reject(err) : resolve()
})
})
}
static append (path, dataString) {
return new Promise((resolve, reject) => {
fs.appendFile(path, dataString, 'utf8', (err) => {
err ? reject(err) : resolve()
})
})
}
static replace (path, data) {
return new Promise((resolve, reject) => {
const concatenated = Object.keys(data).map(key => {
const tmpFile = `${path}_${key}.tmp`
return `sed 's/${key}.*/${key} "${data[key]}"/' ${path} > ${tmpFile} && mv ${tmpFile} ${path}`
}).join(' && ')
TGDShell.exec(concatenated).then(stdout => resolve(stdout)).catch(stderr => reject(stderr))
})
}
static insertBefore (path, dataString, pattern) {
return TGDFile._edit(path, dataString, pattern, 'i')
}
static appendAfter (path, dataString, pattern) {
return TGDFile._edit(path, dataString, pattern, 'a')
}
static replaceLine (path, dataString, pattern) {
return TGDFile._edit(path, dataString, pattern, 'c')
}
static _edit (path, dataString, pattern, sedAction) {
const tmpFile = `${path}_${sedAction}.tmp`
const command = `sed '/${pattern}/${sedAction} \\
${dataString}\n' ${path} > ${tmpFile} && mv ${tmpFile} ${path}`
return TGDShell.exec(command)
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment