yo.node.conf.js
3.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
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',
'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';
}
shelljs.exec('../../bin/node-prune ./node_modules');
}).catch(error => {
console.error(`error:${error}`);
return process.exit(1); //eslint-disable-line
});