yo.node.conf.js
4.04 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
122
123
124
125
126
127
128
129
130
131
132
const shelljs = require('shelljs');
const yargs = require('yargs');
const path = require('path');
const cssnano = require('cssnano');
const rp = require('request-promise');
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-activity/${pkg.version}/manifest.json`);
resolve();
} else {
return Promise.all([
rp({
url: `https://cdn.yoho.cn/yohobuywap-node-activity/${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';
}
fs.writeFileSync(path.join(distDir, 'manifest.json'), JSON.stringify(manifest), {
flag: 'w'
});
fs.writeFileSync(path.join(__dirname, '../public/static/sw.js'), sw, {
flag: 'w'
});
resolve();
}).catch(reject);
}
}).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
});