Skip to content

Commit

Permalink
update: output.file is optional now, because autoHash will return an
Browse files Browse the repository at this point in the history
object of hashes.
  • Loading branch information
gucheen committed Aug 11, 2017
1 parent 1b5880a commit 6e2a403
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 8 deletions.
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,12 @@ autoHash({

### output example

- return as an object when call `autoHash()`
```js
{ testIndex: '5745abcc' }
```

- output as a file when `output.file` is provided (the object will still be returned in `autoHash()`)
```js
module.exports = { testIndex: '5745abcc' };
```
Expand All @@ -79,7 +85,7 @@ configuration file is in `.json`
}
],
"output": {
"file": "src/auto-hash.js" // (required) path of output file
"file": "src/auto-hash.js" // (optional) path of output file
},
"len": 10, // (optional) the length of hash string to be used, default to full string.
"rename": false, // (optional, false) rename the original file to originalFilename.hash.ext
Expand Down
8 changes: 7 additions & 1 deletion README.zh_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,12 @@ autoHash({

### 输出样例

- 当调用 `autoHash()` 时,会返回包含 hash 信息的对象
```js
{ testIndex: '5745abcc' }
```

- 当设置了 `output.file` 时,会输出到这个文件中(如果是调用 autoHash() 的方式,一样会返回 hash 对象)
```js
module.exports = { testIndex: '5745abcc' };
```
Expand All @@ -78,7 +84,7 @@ module.exports = { testIndex: '5745abcc' };
}
],
"output": {
"file": "src/auto-hash.js" // 输出文件(必须
"file": "src/auto-hash.js" // 输出文件(可选
},
"len": 10, // hash 取值长度,默认全部(可选)
"rename": false, // 重命名原文件,把 hash 附加到文件名中 originalFilename.hash.ext (可选,默认为 false)
Expand Down
11 changes: 5 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,7 @@ function genHash(argv) {
loadConfig('./auto-hash.config.json');
}
if (!(Array.isArray(config.files) && config.files.length)) {
throw new Error('Missing files list');
}
if (!(config.output && config.output.file)) {
throw new Error('Missing output file path');
throw new Error('Missing file list');
}
const hashes = {};
config.files.forEach(fileObj => {
Expand Down Expand Up @@ -87,8 +84,10 @@ function genHash(argv) {
copyFile(filePath, fileHash);
}
});
const fileContent = `module.exports = ${util.inspect(hashes)};`;
fs.writeFileSync(config.output.file, fileContent);
if (config.output && config.output.file) {
const fileContent = `module.exports = ${util.inspect(hashes)};`;
fs.writeFileSync(config.output.file, fileContent);
}
return hashes;
}

Expand Down

0 comments on commit 6e2a403

Please sign in to comment.