Blame view

build/yo.node.conf.js 4.02 KB
陈峰 authored
1
const shelljs = require('shelljs');
陈峰 authored
2
const yargs = require('yargs');
陈峰 authored
3
const path = require('path');
陈峰 authored
4
const cssnano = require('cssnano');
陈峰 authored
5
const rp = require('request-promise');
陈峰 authored
6
const fs = require('fs');
陈峰 authored
7 8 9
const pkg = require('../package.json');

const distDir = path.join(__dirname, '../dist/node');
陈峰 authored
10
const needBuild = yargs.argv.build;
陈峰 authored
11
陈峰 authored
12
shelljs.rm('-rf', distDir);
陈峰 authored
13
shelljs.mkdir('-p', distDir);
陈峰 authored
14 15 16 17 18 19

const cpPaths = [
    'favicon.ico',
    '.nvmrc',
    '.npmrc',
    'process.json',
陈峰 authored
20
    'Dockerfile',
陈峰 authored
21 22 23 24 25 26 27 28 29 30
    'yarn.lock',
    'package.json',
    '*.js',
    'config',
    'apps',
    'doraemon',
    'utils',
    ['public/static', 'public'],
];
陈峰 authored
31 32 33 34 35
new Promise((resolve, reject) => { // 加载manifest.json文件
    if (needBuild === 'YES') {
        cpPaths.push(`dist/statics/yohobuywap-node/${pkg.version}/manifest.json`);
        resolve();
    } else {
陈峰 authored
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
        return Promise.all([
            rp({
                url: `https://cdn.yoho.cn/yohobuywap-node/${pkg.version}/manifest.json`,
                gzip: true,
                json: true
            }),
            rp({
                url: 'https://m.yohobuy.com/sw.js',
                gzip: true
            }),
        ]).then(results => {
            const [manifest, sw] = results;

            if (!manifest || !sw) {
                throw 'manifest.json or sw.js download faild';
陈峰 authored
51
            }
陈峰 authored
52 53 54 55
            fs.writeFileSync(path.join(distDir, 'manifest.json'), JSON.stringify(manifest), {
                flag: 'w'
            });
            fs.writeFileSync(path.join(__dirname, '../public/static/sw.js'), sw, {
陈峰 authored
56 57 58
                flag: 'w'
            });
            resolve();
陈峰 authored
59
        }).catch(reject);
陈峰 authored
60 61 62 63 64
    }
}).then(() => { // 拷贝node代码
    cpPaths.forEach(p => {
        let dist = distDir;
        let file = p;
陈峰 authored
65
陈峰 authored
66 67 68
        if (typeof p === 'object') {
            dist = path.join(dist, p[1]);
            file = p[0];
陈峰 authored
69
陈峰 authored
70 71 72
            if (!shelljs.test('-e', dist)) {
                shelljs.mkdir('-p', dist);
            }
陈峰 authored
73
        }
陈峰 authored
74 75 76 77 78 79
        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
陈峰 authored
80
    }
陈峰 authored
81 82 83 84 85
}).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');
陈峰 authored
86
陈峰 authored
87 88 89
        shelljs.ls('-R', csspath).forEach(filename => {
            if (/\.css$/.test(filename)) {
                const cssfile = fs.readFileSync(path.join(csspath, filename)).toString();
陈峰 authored
90
陈峰 authored
91 92 93 94 95 96 97 98 99 100 101
                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);
陈峰 authored
102
陈峰 authored
103 104 105 106 107 108 109 110 111 112 113
                        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}`);
陈峰 authored
114 115 116
                });
            }
        });
陈峰 authored
117 118 119 120 121
    });
}).then(() => { // 安装依赖和清理node_modules
    shelljs.cd(distDir);
    if (shelljs.exec('yarn --production=true').code !== 0) {
        throw 'yarn install faild';
陈峰 authored
122
    }
陈峰 authored
123 124 125
}).catch(error => {
    console.error(`error:${error}`);
    return process.exit(1); //eslint-disable-line
陈峰 authored
126 127
});
陈峰 authored
128 129 130 131 132