forked from enb/enb
-
Notifications
You must be signed in to change notification settings - Fork 0
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
1 parent
3598c91
commit 4812715
Showing
2 changed files
with
91 additions
and
0 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,58 @@ | ||
/** | ||
* write-file | ||
* ============= | ||
* | ||
* Записывает целевой файл в файловую систему. | ||
* | ||
* **Опции** | ||
* | ||
* * *String* **target** — имя генерируемого файла. Обязательная опция. | ||
* * *String | Buffer* **content** - контент генерируемого файла, если он есть файл будет сгенерирован | ||
* * *Object | String* **fileOptions** - [аттрибуты генерируемого файла]{@link https://goo.gl/ZZzrdr} (default: 'utf8') | ||
* | ||
* **Пример** | ||
* | ||
* Генерируем текстовый-файл | ||
* | ||
* ```javascript | ||
* nodeConfig.addTech([ require('enb/techs/write-file'), { | ||
* content: 'bla bla bla', | ||
* fileOptions: { | ||
* encoding: 'utf8', // default | ||
* mode: '0o666', // default | ||
* flag: 'w' //default | ||
* }, | ||
* target: '?.bla.txt' | ||
* } ]); | ||
* ``` | ||
* | ||
* Генерируем bemdecl-файл | ||
* | ||
* ```javascript | ||
* nodeConfig.addTech([ require('enb/techs/write-file'), { | ||
* content: 'exports.blocks = {name: "bla"}', | ||
* fileOptions: { | ||
* encoding: 'utf8', // default | ||
* mode: '0o666', // default | ||
* flag: 'w' //default | ||
* }, | ||
* target: '?.bemdecl.js' | ||
* } ]); | ||
* ``` | ||
*/ | ||
var enb = require('../lib/api'), | ||
vfs = enb.asyncFs; | ||
|
||
module.exports = enb.buildFlow.create() | ||
.name('write-file') | ||
.target('target', '?.target') | ||
.defineRequiredOption('target') | ||
.defineRequiredOption('content') | ||
.defineOption('fileOptions', { encoding: 'utf8' }) | ||
.builder(function () { | ||
return this._content; | ||
}) | ||
.saver(function (filename, content) { | ||
return vfs.write(filename, content, this._fileOptions); | ||
}) | ||
.createTech(); |
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,33 @@ | ||
var FS = require('fs'), | ||
mockFS = require('mock-fs'), | ||
MockNode = require('mock-enb/lib/mock-node'), | ||
WriteFileTech = require('../../techs/write-file'); | ||
|
||
describe('techs/write-file', function () { | ||
var bundle; | ||
|
||
beforeEach(function () { | ||
mockFS({ | ||
bundle: mockFS.directory({ | ||
mode: 0755 | ||
}) | ||
}); | ||
|
||
bundle = new MockNode('bundle'); | ||
}); | ||
|
||
afterEach(function () { | ||
mockFS.restore(); | ||
}); | ||
|
||
it('should write target file to file system', function () { | ||
return bundle.runTech(WriteFileTech, { | ||
target: '?.txt', | ||
content: 'I\'m here' | ||
}) | ||
.should.be.fulfilled | ||
.then(function () { | ||
FS.readFileSync('bundle/bundle.txt').toString().should.be.eql('I\'m here'); | ||
}); | ||
}); | ||
}); |