fix binary file

This commit is contained in:
fxy060608 2018-11-18 18:43:39 +08:00
parent 30772c715c
commit 3218e32a7b
6 changed files with 190 additions and 6 deletions

View File

@ -1,6 +1,8 @@
const fs = require('fs')
const path = require('path')
const isBinary = require('isbinaryfile')
async function generate(dir, files, base = '') {
@ -10,7 +12,12 @@ async function generate(dir, files, base = '') {
cwd: dir,
nodir: true
}).forEach(rawPath => {
files[path.join(base, rawPath)] = fs.readFileSync(path.resolve(dir, rawPath), 'utf-8')
const sourcePath = path.resolve(dir, rawPath)
if (isBinary.sync(sourcePath)) {
files[path.join(base, rawPath)] = fs.readFileSync(sourcePath) // return buffer
} else {
files[path.join(base, rawPath)] = fs.readFileSync(sourcePath, 'utf-8')
}
})
}
@ -21,10 +28,11 @@ module.exports = (api, options, rootOptions) => {
api.extendPackage({
dependencies: {
'@dcloudio/uni-h5': 'latest'
'vuex': '^3.0.1',
'@dcloudio/uni-h5': '*'
},
devDependencies: {
'@dcloudio/vue-cli-plugin-uni': 'latest'
'@dcloudio/vue-cli-plugin-uni': '*'
},
babel: {
presets: [
@ -71,7 +79,11 @@ module.exports = (api, options, rootOptions) => {
const tmp = path.join(home, '.uni-app/templates', template.replace(/[\/:]/g, '-'), 'src')
if (fs.existsSync(tmp)) {
require('rimraf').sync.rm(tmp)
try {
require('rimraf').sync.rm(tmp)
} catch (e) {
console.error(e)
}
}
await new Promise((resolve, reject) => {

129
node_modules/isbinaryfile/index.js generated vendored Normal file
View File

@ -0,0 +1,129 @@
var fs = require('fs');
var path = require('path');
var alloc = require('buffer-alloc');
var MAX_BYTES = 512;
module.exports = function(bytes, size, cb) {
// Only two args
if (cb === undefined) {
var file = bytes;
cb = size;
fs.stat(file, function(err, stat) {
if (err || !stat.isFile()) return cb(err, false);
fs.open(file, 'r', function(r_err, descriptor){
if (r_err) return cb(r_err);
bytes = alloc(MAX_BYTES);
// Read the file with no encoding for raw buffer access.
fs.read(descriptor, bytes, 0, bytes.length, 0, function(err, size, bytes){
fs.close(descriptor, function(c_err){
if (c_err) return cb(c_err, false);
return cb(null, isBinaryCheck(bytes, size));
});
});
});
});
}
else
return cb(null, isBinaryCheck(bytes, size));
};
function isBinaryCheck(bytes, size) {
if (size === 0)
return false;
var suspicious_bytes = 0;
var total_bytes = Math.min(size, MAX_BYTES);
// UTF-8 BOM
if (size >= 3 && bytes[0] == 0xEF && bytes[1] == 0xBB && bytes[2] == 0xBF) {
return false;
}
// UTF-32 BOM
if (size >= 4 && bytes[0] === 0x00 && bytes[1] === 0x00 && bytes[2] == 0xFE && bytes[3] == 0xFF) {
return false;
}
// UTF-32 LE BOM
if (size >= 4 && bytes[0] == 0xFF && bytes[1] == 0xFE && bytes[2] === 0x00 && bytes[3] === 0x00) {
return false;
}
// GB BOM
if (size >= 4 && bytes[0] == 0x84 && bytes[1] == 0x31 && bytes[2] == 0x95 && bytes[3] == 0x33) {
return false;
}
if (total_bytes >= 5 && bytes.slice(0, 5) == "%PDF-") {
/* PDF. This is binary. */
return true;
}
// UTF-16 BE BOM
if (size >= 2 && bytes[0] == 0xFE && bytes[1] == 0xFF) {
return false;
}
// UTF-16 LE BOM
if (size >= 2 && bytes[0] == 0xFF && bytes[1] == 0xFE) {
return false;
}
for (var i = 0; i < total_bytes; i++) {
if (bytes[i] === 0) { // NULL byte--it's binary!
return true;
}
else if ((bytes[i] < 7 || bytes[i] > 14) && (bytes[i] < 32 || bytes[i] > 127)) {
// UTF-8 detection
if (bytes[i] > 193 && bytes[i] < 224 && i + 1 < total_bytes) {
i++;
if (bytes[i] > 127 && bytes[i] < 192) {
continue;
}
}
else if (bytes[i] > 223 && bytes[i] < 240 && i + 2 < total_bytes) {
i++;
if (bytes[i] > 127 && bytes[i] < 192 && bytes[i + 1] > 127 && bytes[i + 1] < 192) {
i++;
continue;
}
}
suspicious_bytes++;
// Read at least 32 bytes before making a decision
if (i > 32 && (suspicious_bytes * 100) / total_bytes > 10) {
return true;
}
}
}
if ((suspicious_bytes * 100) / total_bytes > 10) {
return true;
}
return false;
}
module.exports.sync = function(bytes, size) {
// Only one arg
if (size === undefined) {
var file = bytes;
try {
if(!fs.statSync(file).isFile()) return false;
} catch (err) {
// otherwise continue on
}
var descriptor = fs.openSync(file, 'r');
try {
// Read the file with no encoding for raw buffer access.
bytes = alloc(MAX_BYTES);
size = fs.readSync(descriptor, bytes, 0, bytes.length, 0);
} finally {
fs.closeSync(descriptor);
}
return isBinaryCheck(bytes, size);
}
else
return isBinaryCheck(bytes, size);
}

36
node_modules/isbinaryfile/package.json generated vendored Normal file
View File

@ -0,0 +1,36 @@
{
"name": "isbinaryfile",
"description": "Detects if a file is binary in Node.js. Similar to Perl's -B.",
"version": "3.0.3",
"dependencies": {
"buffer-alloc": "^1.2.0"
},
"devDependencies": {
"mocha": "^2.2.4",
"grunt": "~0.4.1",
"grunt-release": "~0.6.0",
"grunt-exec": "0.4.3",
"grunt-cli": "~0.1.13"
},
"engines": {
"node": ">=0.6.0"
},
"files": [
"index.js"
],
"license": "MIT",
"main": "./index.js",
"maintainers": [
{
"name": "Garen J. Torikian",
"email": "gjtorikian@gmail.com"
}
],
"repository": {
"type": "git",
"url": "https://github.com/gjtorikian/isBinaryFile"
},
"scripts": {
"test": "mocha"
}
}

View File

@ -11,6 +11,7 @@
"dependencies": {
"download-git-repo": "^1.1.0",
"glob": "^7.1.3",
"isbinaryfile": "^3.0.3",
"ora": "^3.0.0",
"rimraf": "^2.6.2",
"user-home": "^2.0.0"

View File

@ -12,11 +12,11 @@ module.exports = [{
},
{
name: '登录模板',
value: 'hello uni-app'
value: 'dcloudio/uni-template-login'
},
{
name: '看图模板',
value: 'hello uni-app'
value: 'dcloudio/uni-template-picture'
},
{
name: '自定义模板',

View File

@ -372,6 +372,12 @@ isarray@^1.0.0, isarray@~1.0.0:
version "1.0.0"
resolved "http://registry.npm.taobao.org/isarray/download/isarray-1.0.0.tgz#bb935d48582cba168c06834957a54a3e07124f11"
isbinaryfile@^3.0.3:
version "3.0.3"
resolved "http://registry.npm.taobao.org/isbinaryfile/download/isbinaryfile-3.0.3.tgz#5d6def3edebf6e8ca8cae9c30183a804b5f8be80"
dependencies:
buffer-alloc "^1.2.0"
isurl@^1.0.0-alpha5:
version "1.0.0"
resolved "http://registry.npm.taobao.org/isurl/download/isurl-1.0.0.tgz#b27f4f49f3cdaa3ea44a0a5b7f3462e6edc39d67"