-
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
152 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
const fs = require('fs-extra'); | ||
const path = require('path'); | ||
|
||
const save = (destinationPath, data, opts, cb) => { | ||
// @todo check if the filepath has an .ext | ||
let callback = cb; | ||
let options = opts; | ||
|
||
// set cb if options are not set | ||
if (typeof options === 'function') { | ||
callback = options; | ||
options = {}; | ||
} | ||
|
||
if (!options.overwrite && fs.existsSync(destinationPath)) { | ||
return callback({ | ||
message: 'File exist and cannot be overwritten. Set the option overwrite to true to overwrite files.', | ||
}); | ||
} | ||
|
||
return fs.mkdirs(path.dirname(destinationPath), () => { | ||
fs.writeFile(destinationPath, data, (err) => { | ||
if (err) { | ||
return callback(err); | ||
} | ||
|
||
return callback(null, `Successfully wrote ${destinationPath}`); | ||
}); | ||
}); | ||
}; // /save | ||
|
||
module.exports = save; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
const fs = require('fs-extra'); | ||
const path = require('path'); | ||
|
||
const saveSync = (destinationPath, data, options = {}) => { | ||
if (!options.overwrite && fs.existsSync(destinationPath)) { | ||
throw new Error('File exist and cannot be overwritten. Set the option overwrite to true to overwrite files.'); | ||
} | ||
|
||
try { | ||
fs.mkdirsSync(path.dirname(destinationPath)); | ||
fs.writeFileSync(destinationPath, data); | ||
} catch (err) { | ||
throw err; | ||
} | ||
}; // /saveSync | ||
|
||
module.exports = saveSync; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
const fs = require('fs-extra'); | ||
const path = require('path'); | ||
const expect = require('chai').expect; | ||
|
||
const save = require('../save'); | ||
|
||
const testCwd = 'test/files/testCache'; | ||
|
||
describe('save', () => { | ||
afterEach(() => { | ||
fs.removeSync(testCwd); | ||
}); | ||
|
||
it('should create a file within a non existing dir', (done) => { | ||
const filePath = path.join(testCwd, '/a/non/existing/path/test.txt'); | ||
|
||
save(filePath, 'test content', (err) => { | ||
expect(err).not.to.equal(undefined); | ||
|
||
expect(fs.existsSync(filePath)).to.equal(true); | ||
expect(fs.readFileSync(filePath, 'utf8')).to.equal('test content'); | ||
|
||
done(); | ||
}); | ||
}); | ||
|
||
it('should not overwrite the same file', (done) => { | ||
const filePath = path.join(testCwd, '/../config.json'); | ||
const oldFile = fs.readFileSync(filePath, 'utf8'); | ||
|
||
save(filePath, 'test content', (err) => { | ||
expect(err.message).to.equal('File exist and cannot be overwritten. Set the option overwrite to true to overwrite files.'); | ||
expect(fs.readFileSync(filePath, 'utf8')).to.equal(oldFile); | ||
|
||
done(); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
const fs = require('fs-extra'); | ||
const path = require('path'); | ||
const expect = require('chai').expect; | ||
|
||
const saveSync = require('../saveSync'); | ||
|
||
const testCwd = 'test/files/testCache'; | ||
|
||
describe('saveSync', () => { | ||
afterEach(() => { | ||
fs.removeSync(testCwd); | ||
}); | ||
|
||
it('saveSync | should save', () => { | ||
const filePath = path.join(testCwd, '/config.txt'); | ||
|
||
saveSync(filePath, 'test content'); | ||
|
||
expect(fs.readFileSync(filePath, 'utf8')).to.equal('test content'); | ||
}); | ||
|
||
it('saveSync | should not overwrite the same file', () => { | ||
const filePath = path.join(testCwd, '/../config.json'); | ||
const oldFile = fs.readFileSync(filePath, 'utf8'); | ||
let failed = false; | ||
|
||
try { | ||
saveSync(filePath, 'test content'); | ||
|
||
// if no error is thrown before it should fail here | ||
failed = true; | ||
} catch (e) { | ||
expect(e.message).to.equal('File exist and cannot be overwritten. Set the option overwrite to true to overwrite files.'); | ||
} | ||
|
||
expect(failed).to.equal(false); | ||
expect(fs.readFileSync(filePath, 'utf8')).to.equal(oldFile); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters