Beispiel #1
0
exports.file = function (file, extname) {
    var binary;

    // 文件
    if (path.isFile(file)) {
        extname = extname || path.extname(file);
        try {
            binary = reader(file, 'binary');
        } catch (err) {
            debug.error('base64 file', file);
            return process.exit(1);
        }
    } else {
        debug.error('base64 file', file + ' 不是一个本地文件');
        return process.exit(1);
    }

    // data:image/png;base64,
    var prefix = 'data:' + mime.get(extname) + ';base64,';
    var base64;

    try {
        base64 = Buffer.from(binary, 'binary').toString('base64');
    } catch (err) {
        debug.error('base64 file', file);
        debug.error('base64 error', err.message);
        return process.exit(1);
    }

    return prefix + base64;
};
Beispiel #2
0
            collection.each(files, function (index, file) {
                var basename = path.basename(file);
                var destPath = path.join(options.destDirname, basename);

                try {
                    fse.copySync(file, destPath);
                    debug.success(name + ' file', destPath);
                } catch (err) {
                    debug.error(name + ' file', destPath);
                    debug.error('copy error', err.message);
                    return process.exit(1);
                }
            });
Beispiel #3
0
module.exports = function (next) {
    log.manage({
        dirname: path.join(configs.root, 'logs')
    });

    next();
};
Beispiel #4
0
    }).on('close', function () {
        console.loadingEnd();
        debug.ignore('unzip ' + name, tempFile);

        var zip = new AdmZip(tempFile);
        var unzipError = null;

        try {
            zip.extractAllTo(unzipDirname, true);
        } catch (err) {
            unzipError = err;
            debug.error('unzip ' + name, unzipDirname);
            debug.error('unzip ' + name, err.message);
        }

        if (unzipError) {
            return process.exit(1);
        }

        if (options.type === 'file') {
            // 目标目录 + 临时目录 + 所有文件
            var globFile = path.join(options.destDirname, tempName, './**/*');
            var files = glob.sync(globFile);

            collection.each(files, function (index, file) {
                var basename = path.basename(file);
                var destPath = path.join(options.destDirname, basename);

                try {
                    fse.copySync(file, destPath);
                    debug.success(name + ' file', destPath);
                } catch (err) {
                    debug.error(name + ' file', destPath);
                    debug.error('copy error', err.message);
                    return process.exit(1);
                }
            });

            try {
                fse.removeSync(unzipDirname);
            } catch (err) {
                // ignore
            }
        } else {
            debug.success(name + ' directory', unzipDirname);
        }

        try {
            fse.removeSync(tempFile);
        } catch (err) {
            // ignore
        }

        callback(null, unzipDirname);
    });
var writePackageJSON = function (json, destDirname) {
    var file = path.join(destDirname, 'package.json');

    json.createBy = 'coolie@' + pkg.version + ' ' + Date.now();

    try {
        fse.outputFileSync(file, JSON.stringify(json, null, 2), 'utf8');
        debug.success('create', file);
    } catch (err) {
        debug.danger('create', file);
        debug.danger('error', err.message);
    }
};
Beispiel #6
0
/**
 * 读取 convert
 * @param template
 * @returns {*}
 */
function readConvert(template) {
    var convert;

    try {
        convert = JSON.parse(fs.readFileSync(
            path.join(template.path, '.convert.json'),
            'utf8'
        ));
    } catch (err) {
        // ignore
    }

    return convert || {};
}
Beispiel #7
0
var createReadmeMD = function (meta, options) {
    var destDirname = options.destDirname;
    var destName = path.basename(destDirname);
    var srcName = 'readme.md';
    var destFile = path.join(destDirname, srcName);
    var readmeMDTemplatePath = path.join(meta.root, 'readme.md');
    var readmeMDTemplateData = fse.readFileSync(readmeMDTemplatePath, 'utf8');
    var tpl = new Template(readmeMDTemplateData, {
        compress: false
    });
    var data = tpl.render({
        name: destName,
        pkg: pkg,
        now: date.format('YYYY-MM-DD HH:mm:ss.SSS')
    });

    try {
        fse.outputFileSync(destFile, data, 'utf8');
    } catch (err) {
        // ignore
    }

    debug.success('create', path.join(destName, srcName));
};
Beispiel #8
0
var parseRequire = exports.parseRequire = function (file, options) {
    options = object.assign({
        deep: true,
        nodeModulesDirname: path.join(process.cwd(), NODE_MODUELS),
        code: null
    }, options);
    options.code = options.code || fse.readFileSync(file, 'utf-8');
    options.srcCoolieConfigNodeModulesDirname = options.nodeModulesDirname;
    options.coolieConfigs = {};
    options.async = false;

    var requireList = requireParser(file, options);

    if (!options.deep) {
        return requireList;
    }

    var parsedMap = {};
    var parsedList = [];

    options.deep = false;
    options.code = null;

    function deep(fileList) {
        fileList.forEach(function (fileItem) {
            var file = fileItem.file;

            if (parsedMap[fileItem.file]) {
                return;
            }

            parsedList.push(fileItem);
            parsedMap[fileItem.file] = true;
            deep(parseRequire(file, options));
        });
    }

    deep(requireList);
    return parsedList;
};
Beispiel #9
0
module.exports = function (next) {
    var app = express();

    app.set('env', configs.env);
    app.set('port', configs.port);
    app.set('views', path.join(configs.webroot, './.views/'));
    app.engine('html', Template.express({
        compress: true
    }));
    app.set('view engine', 'html');

    // 路由区分大小写,默认 disabled
    app.set('case sensitive routing', true);

    // 严格路由,即 /a/b !== /a/b/
    app.set('strict routing', false);

    app.set('jsonp callback name', 'callback');
    app.set('json spaces', 'local' === configs.env ? 4 : 0);
    app.set('view cache', 'local' !== configs.env);

    next(null, app);
};
Beispiel #10
0
module.exports = function (file, encoding, embedFile) {
    encoding = encoding || 'binary';
    bufferMap[encoding] = bufferMap[encoding] || {};
    var bf = bufferMap[encoding][file];

    if (!bf) {
        if (!path.isFile(file)) {
            console.log();
            debug.error('read file', file);
            debug.error('read file', '该文件不存在');

            if (embedFile) {
                debug.error('embed file', embedFile);
            }

            debug.warn('warning', '模块路径指南 <' + bookURL('/introduction/module-path/') + '>');
            process.exit(1);
        }

        try {
            var ret = fs.readFileSync(file, encoding);

            bf = bufferMap[encoding][file] =  Buffer.from(ret, encoding);
        } catch (err) {
            debug.error('read file', file);
            debug.error('read file', err.message);

            if (embedFile) {
                debug.error('embed file', embedFile);
            }

            process.exit(1);
        }
    }

    return encoding === 'binary' ? bf : bf.toString(encoding);
};
Beispiel #11
0
/**
 * 文件描述
 * @author ydr.me
 * @create 2015-10-22 17:21
 */


'use strict';

var path = require('blear.node.path');
var fs = require('fs');
var assert = require('assert');

var minifyHTML = require('../../src/minify/html.js');

var srcDirname = path.join(__dirname, 'src/');
var file = path.join(srcDirname, 'html.html');
var srcCoolieConfigMainModulesDirname = path.join(srcDirname, './');
var srcMainPath = path.join(srcCoolieConfigMainModulesDirname, 'html-main.js');
var destDirname = path.join(__dirname, 'dest/');
var destCoolieConfigJSPath = path.join(destDirname, 'static/js/dwdqwdqwdqwdqwdqwdqw312.js');
var destResourceDirname = path.join(destDirname, 'static/res/');
var destJSDirname = path.join(destDirname, 'static/js/');
var destCSSDirname = path.join(destDirname, 'static/css/');
var mainVersionMap = {};

describe('minify/html.js', function () {
    it('e', function () {
        mainVersionMap[srcMainPath] = 'abcdef123123kdgqoiwdqw';
        var code = fs.readFileSync(file, 'utf8');
        var ret = minifyHTML(file, {
/**
 * 文件描述
 * @author ydr.me
 * @create 2015-10-22 18:56
 */


'use strict';

var path = require('blear.node.path');
var fs = require('fs');
var assert = require('assert');

var replaceHTMLTagLink = require('../../src/replace/html-tag-link.js');

var srcDirname = path.join(__dirname, 'src/');
var file = path.join(srcDirname, 'html-tag-link.html');
var code = fs.readFileSync(file, 'utf8');
var destDirname = path.join(__dirname, '.dest/');
var destCSSDirname = path.join(destDirname, 'static/css');
var destResourceDirname = path.join(destDirname, 'static/res');

describe('replace/html-tag-link.js', function () {
    it('e', function () {
        var ret = replaceHTMLTagLink(file, {
            code: code,
            srcDirname: srcDirname,
            destDirname: destDirname,
            destCSSDirname: destCSSDirname,
            destResourceDirname: destResourceDirname
        });
Beispiel #13
0
var createTemplate = function (meta, options, callback) {
    var root = meta.root;
    var convert = meta.convert;
    var destDirname = options.destDirname;
    var files = path.glob('**/*', {
        srcDirname: root,
        globOptions: {
            dot: false,
            nodir: true
        },
        filter: function (indexGlob, indexFile, file) {
            return !/\/\.[^/]+$/.test(file);
        }
    });

    var convert2 = {};
    var converted = {};
    var ignore2 = {};

    collection.each(convert, function (rela, transi) {
        convert2[path.join(root, rela)] = transi;
    });

    collection.each(ignoreMap, function (rela) {
        ignore2[path.join(root, rela)] = true;
    });

    plan.each(files, function (index, file, next) {
        var dir = path.dirname(file);
        var basename = path.basename(file);
        var renameType = renameMap[basename];
        var srcName = path.relative(root, file);
        var transiType = convert2[file];
        var relName = '';
        var relFile = '';
        var findConvert = false;
        var isDynamic = REG_REPLACE.test(basename);

        if (ignoreMap[srcName]) {
            return next();
        }

        if (isDynamic) {
            relName = basename.replace(REG_REPLACE, '.');
            relFile = path.join(dir, relName);
        }

        if (relFile) {
            if (converted[relFile]) {
                return next();
            }

            converted[relFile] = true;
            transiType = convert2[relFile];

            if (transiType) {
                srcName = path.relative(root, relFile);
                collection.each(transiType, function (key, originFile) {
                    if (options[key]) {
                        file = originFile;
                        findConvert = true;
                        return false;
                    }
                });

                if (!findConvert) {
                    file = transiType.none;
                }

                file = path.join(root, file);
                //debug.warn('transiType', transiType);
                //debug.warn('relFile', relFile);
                //debug.warn('srcName', srcName);
                //debug.warn('options', options);
                //debug.warn('file', file);
            }
        }

        if (renameType) {
            srcName = path.join(path.dirname(srcName), renameType);
        }

        var destFile = path.join(destDirname, srcName);
        var destName = path.basename(destDirname);

        try {
            fse.copySync(file, destFile, {
                // 是否覆盖
                clobber: false
            });
        } catch (err) {
            // ignore
        }

        debug.success('create', path.join(destName, srcName));
        setTimeout(next, 45);
    }).serial(callback);
};
Beispiel #14
0
    plan.each(files, function (index, file, next) {
        var dir = path.dirname(file);
        var basename = path.basename(file);
        var renameType = renameMap[basename];
        var srcName = path.relative(root, file);
        var transiType = convert2[file];
        var relName = '';
        var relFile = '';
        var findConvert = false;
        var isDynamic = REG_REPLACE.test(basename);

        if (ignoreMap[srcName]) {
            return next();
        }

        if (isDynamic) {
            relName = basename.replace(REG_REPLACE, '.');
            relFile = path.join(dir, relName);
        }

        if (relFile) {
            if (converted[relFile]) {
                return next();
            }

            converted[relFile] = true;
            transiType = convert2[relFile];

            if (transiType) {
                srcName = path.relative(root, relFile);
                collection.each(transiType, function (key, originFile) {
                    if (options[key]) {
                        file = originFile;
                        findConvert = true;
                        return false;
                    }
                });

                if (!findConvert) {
                    file = transiType.none;
                }

                file = path.join(root, file);
                //debug.warn('transiType', transiType);
                //debug.warn('relFile', relFile);
                //debug.warn('srcName', srcName);
                //debug.warn('options', options);
                //debug.warn('file', file);
            }
        }

        if (renameType) {
            srcName = path.join(path.dirname(srcName), renameType);
        }

        var destFile = path.join(destDirname, srcName);
        var destName = path.basename(destDirname);

        try {
            fse.copySync(file, destFile, {
                // 是否覆盖
                clobber: false
            });
        } catch (err) {
            // ignore
        }

        debug.success('create', path.join(destName, srcName));
        setTimeout(next, 45);
    }).serial(callback);
Beispiel #15
0
module.exports = function (options, callback) {
    var destDirname = options.destDirname;
    var url = options.url;
    var name = options.name;
    var tempName = random.guid();
    var tempFile = path.join(os.tmpdir(), tempName + '.zip');
    var unzipName = options.type === 'file' ? tempName : name;
    var tempStream = fse.createWriteStream(tempFile);
    var unzipDirname = path.join(destDirname, './' + unzipName);

    if (!typeis.Function(callback)) {
        callback = function () {
            // ignore
        };
    }

    debug.ignore('install ' + name, url);
    console.loading();
    request.get({
        url: url,
        debug: false
    }).on('error', function (err) {
        debug.error('install ' + name, err.message);
        process.exit(1);
    }).pipe(tempStream).on('error', function (err) {
        debug.error('install ' + name, err.message);
        console.loadingEnd();
        process.exit(1);
    }).on('close', function () {
        console.loadingEnd();
        debug.ignore('unzip ' + name, tempFile);

        var zip = new AdmZip(tempFile);
        var unzipError = null;

        try {
            zip.extractAllTo(unzipDirname, true);
        } catch (err) {
            unzipError = err;
            debug.error('unzip ' + name, unzipDirname);
            debug.error('unzip ' + name, err.message);
        }

        if (unzipError) {
            return process.exit(1);
        }

        if (options.type === 'file') {
            // 目标目录 + 临时目录 + 所有文件
            var globFile = path.join(options.destDirname, tempName, './**/*');
            var files = glob.sync(globFile);

            collection.each(files, function (index, file) {
                var basename = path.basename(file);
                var destPath = path.join(options.destDirname, basename);

                try {
                    fse.copySync(file, destPath);
                    debug.success(name + ' file', destPath);
                } catch (err) {
                    debug.error(name + ' file', destPath);
                    debug.error('copy error', err.message);
                    return process.exit(1);
                }
            });

            try {
                fse.removeSync(unzipDirname);
            } catch (err) {
                // ignore
            }
        } else {
            debug.success(name + ' directory', unzipDirname);
        }

        try {
            fse.removeSync(tempFile);
        } catch (err) {
            // ignore
        }

        callback(null, unzipDirname);
    });
};
Beispiel #16
0
/**
 * 文件描述
 * @author ydr.me
 * @create 2015-10-28 16:06
 */


'use strict';

var assert = require('assert');
var path = require('blear.node.path');

var copy = require('../../src/build/copy.js');

var srcDirname = path.join(__dirname, 'src');
var destDirname = path.join(__dirname, 'dest');

describe('build/copy.js', function () {
    it('e', function () {
        var list = copy({
            srcDirname: srcDirname,
            destDirname: destDirname,
            copy: [
                '**'
            ]
        });

        assert.equal(list.length > 1, true);
    });
});
Beispiel #17
0
 collection.each(ignoreMap, function (rela) {
     ignore2[path.join(root, rela)] = true;
 });
Beispiel #18
0
module.exports = function (href, options) {
    options = object.assign({}, defaults, options);
    var pathRet = pathURI.parseURI2Path(href);

    if (!pathURI.isRelatived(pathRet.path)) {
        return false;
    }

    var srcPath = pathURI.toAbsoluteFile(href, options.file, options.srcDirname);
    var extname = path.extname(srcPath);
    var destPath = minifyPathmap[srcPath];
    var destURI = minifyCSSmap[srcPath];
    var resList = resourceMap[srcPath];

    if (!destURI) {
        var srcCode = reader(srcPath, 'utf8', options.file);
        var destCode = srcCode;

        if (options.minifyCSS) {
            var minifyCSSRet = minifyCSS(srcPath, {
                code: srcCode,
                cleanCSSOptions: options.cleanCSSOptions,
                versionLength: options.versionLength,
                srcDirname: options.srcDirname,
                destDirname: options.destDirname,
                destHost: options.destHost,
                destResourceDirname: options.destResourceDirname,
                minifyResource: options.minifyResource,
                replaceCSSResource: true,
                mute: options.mute,
                middleware: options.middleware
            });
            destCode = minifyCSSRet.code;
            resList = minifyCSSRet.resList;
        }

        var destVersion = encryption.md5(destCode).slice(0, options.versionLength);

        destPath = path.join(options.destCSSDirname, destVersion + extname);
        destURI = pathURI.toRootURL(destPath, options.destDirname);
        destURI = pathURI.joinHost('css', options.destHost, destURI);

        if (options.signCSS) {
            destCode = sign('css') + '\n' + destCode;
        }

        try {
            fse.outputFileSync(destPath, destCode, 'utf8');
            minifyPathmap[srcPath] = destPath;
            minifyCSSmap[srcPath] = destURI;
            resourceMap[srcPath] = resList;

            if (!options.mute) {
                debug.success('build css', pathURI.toRootURL(srcPath, options.srcDirname));
            }
        } catch (err) {
            debug.error('write file', path.normalize(destPath));
            debug.error('write file', err.message);
            return process.exit(1);
        }
    }

    return {
        srcURL: pathURI.toRootURL(srcPath, options.srcDirname),
        destURL: destURI,
        url: destURI,
        srcFile: srcPath,
        destFile: destPath,
        resList: resList
    };
};
    }, function (node) {
        if (!node.attrs.hasOwnProperty('src')) {
            return node;
        }

        var type = node.attrs.type || DEFAULT_JS_TYPE;
        var isJS = JS_TYPES[type];

        if (!isJS) {
            return node;
        }

        if (!node.attrs.hasOwnProperty(COOLIE)) {
            return node;
        }

        if (node.attrs.hasOwnProperty(COOLIE_IGNORE)) {
            node.attrs[COOLIE_IGNORE] = null;
            return node;
        }

        var src = node.attrs.src;
        if (options.middleware) {
            src = options.middleware.exec({
                file: file,
                progress: 'pre-static',
                type: 'js',
                path: src
            }).path || src;
        }

        // src 是否静态路径
        var isStaticSrc = pathURI.isURL(src);

        // 有 coolie 属性
        var dataMain = node.attrs[DATA_MAIN];
        var dataConfig = node.attrs[DATA_CONFIG];

        if (!dataMain) {
            debug.error('coolie script', file);
            debug.error('coolie script', '`' + DATA_MAIN + '` is empty');
            return process.exit(1);
        }

        if (!dataConfig) {
            debug.error('coolie script', file);
            debug.error('coolie script', '`' + DATA_CONFIG + '` is empty');
            return process.exit(1);
        }

        if (!options.srcCoolieConfigMainModulesDirname) {
            debug.error('coolie script', file);
            debug.error('coolie script', '`coolie-config.js` is NOT defined, but used');
            return process.exit(1);
        }

        if (options.middleware) {
            dataMain = options.middleware.exec({
                file: file,
                progress: 'pre-static',
                type: 'js',
                path: dataMain
            }).path || dataMain;
        }

        var mainPath = path.join(options.srcCoolieConfigMainModulesDirname, dataMain);
        var mainVersion = options.mainVersionMap[mainPath];

        mainList.push(mainPath);

        if (!path.isFile(mainPath)) {
            debug.error('coolie main', '`' + mainPath + '` is NOT a file');
            return process.exit(1);
        }

        if (!mainVersion) {
            debug.error('coolie script', 'can not found `data-main` version');
            debug.error('coolie `data-main`', dataMain);
            debug.error('main file', mainPath);
            debug.error('html file', file);
            return process.exit(1);
        }

        var coolieConfigURI = pathURI.toRootURL(options.destCoolieConfigJSPath, options.destDirname);

        coolieConfigURI = pathURI.joinHost('js', options.destHost, coolieConfigURI);

        // 本域并且 src 为静态
        if (!pathURI.isURL(coolieConfigURI) && isStaticSrc) {
            coolieConfigURI = '~' + coolieConfigURI;
        }

        if (options.progressKey) {
            progress.run(options.progressKey, dataConfig);
        }

        node.attrs[DATA_MAIN] = mainVersion + '.js';
        node.attrs[DATA_CONFIG] = coolieConfigURI;
        node.attrs[COOLIE] = null;

        return node;
    }).exec();
Beispiel #20
0
 transform: function (value, args, params) {
     return path.resolve(value || './');
 }
Beispiel #21
0
/**
 * 文件描述
 * @author ydr.me
 * @create 2015-10-22 09:46
 */


'use strict';

var path = require('blear.node.path');
var assert = require('assert');

var base64 = require('../../src/utils/base64.js');

var base64File = path.join(__dirname, './src/base64.js');

describe('utils/base64.js', function () {
    it('base64.string', function () {
        var ret1 = base64.string('1');
        var ret2 = base64.string('1', '.txt');

        assert.equal(ret1, 'MQ==');
        assert.equal(ret2, 'data:text/plain;base64,MQ==');
    });

    it('base64.file', function () {
        var ret1 = base64.file(base64File);
        var ret2 = base64.file(base64File, '.css');

        assert.equal(/data:text\/javascript;base64,/.test(ret1), true);
        assert.equal(/data:text\/css;base64,/.test(ret2), true);
Beispiel #22
0
/**
 * 文件描述
 * @author ydr.me
 * @create 2015-10-27 11:21
 */


'use strict';

var assert = require('assert');
var path = require('blear.node.path');

var parseMain = require('../../src/parse/main.js');

var srcDirname = path.join(__dirname, '../../example/src/');

describe('parse/main.js', function () {
    it('e', function () {
        var mainMap = parseMain({
            glob: [
                "./static/js/app/**"
            ],
            srcDirname: srcDirname
        });

        console.log(mainMap);
    });
});


Beispiel #23
0
 collection.each(convert, function (rela, transi) {
     convert2[path.join(root, rela)] = transi;
 });