yo.node.conf.js 3.72 KB
const shelljs = require('shelljs');
const yargs = require('yargs');
const path = require('path');
const cssnano = require('cssnano');
const request = require('request');
const fs = require('fs');
const pkg = require('../package.json');

const distDir = path.join(__dirname, '../dist/node');
const needBuild = yargs.argv.build;

shelljs.rm('-rf', distDir);
shelljs.mkdir('-p', distDir);

const cpPaths = [
    'favicon.ico',
    '.nvmrc',
    '.npmrc',
    'process.json',
    'Dockerfile',
    'yarn.lock',
    'package.json',
    '*.js',
    'config',
    'apps',
    'doraemon',
    'utils',
    ['public/static', 'public'],
];

new Promise((resolve, reject) => { // 加载manifest.json文件
    if (needBuild === 'YES') {
        cpPaths.push(`dist/statics/yohobuywap-node/${pkg.version}/manifest.json`);
        resolve();
    } else {
        request({
            url: `https://cdn.yoho.cn/yohobuywap-node/${pkg.version}/manifest.json`,
            gzip: true,
            json: true
        }, (error, response, data) => {
            if (error || !data || data.error) {
                return reject(`download manifest.json faild:${error || !data || data.error}`);
            }
            fs.writeFileSync(path.join(distDir, 'manifest.json'), JSON.stringify(data), {
                flag: 'w'
            });
            resolve();
        });
    }
}).then(() => { // 拷贝node代码
    cpPaths.forEach(p => {
        let dist = distDir;
        let file = p;

        if (typeof p === 'object') {
            dist = path.join(dist, p[1]);
            file = p[0];

            if (!shelljs.test('-e', dist)) {
                shelljs.mkdir('-p', dist);
            }
        }
        shelljs.cp('-R', path.join(__dirname, '../', file), dist);
    });
}).then(() => { // 验证文件正确性
    if (!shelljs.test('-e', path.join(distDir, 'manifest.json'))) {
        console.error('error:check manifest.json faild');
        return process.exit(1); //eslint-disable-line
    }
}).then(() => { // 编译mipcss
    return new Promise((resolve, reject) => {
        // 编译mip的css
        const csspath = path.join(distDir, 'apps/mip/css');
        const outputcsspath = path.join(distDir, 'apps/mip/min-css');

        shelljs.ls('-R', csspath).forEach(filename => {
            if (/\.css$/.test(filename)) {
                const cssfile = fs.readFileSync(path.join(csspath, filename)).toString();

                cssnano.process(cssfile, {
                    safe: true,
                    autoprefixer: {
                        add: true,
                        browsers: ['> 1%', 'android >=4', 'ios >=8']
                    },
                    from: path.join(csspath, filename),
                    to: path.join(outputcsspath, filename),
                }).then(function(result) {
                    if (result && result.css) {
                        const dir = path.dirname(result.opts.to);

                        if (!shelljs.test('-e', dir)) {
                            shelljs.mkdir('-p', dir);
                        }
                        fs.writeFileSync(result.opts.to, result.css, {
                            flag: 'w'
                        });
                        resolve();
                    }
                    reject(`mip css faild ${result.opts.to} result: ${result.css}`);
                }).catch(err => {
                    reject(`error:node build faild! mip css cssnano:${err}`);
                });
            }
        });
    });
}).then(() => { // 安装依赖和清理node_modules
    shelljs.cd(distDir);
    if (shelljs.exec('yarn --production=true').code !== 0) {
        throw 'yarn install faild';
    }
}).catch(error => {
    console.error(`error:${error}`);
    return process.exit(1); //eslint-disable-line
});