Skip to content

Commit e9d39e9

Browse files
committed
fix: 修复一些问题,优化代码
1 parent 4285c95 commit e9d39e9

File tree

15 files changed

+16046
-10023
lines changed

15 files changed

+16046
-10023
lines changed

build/index.ts

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,28 @@
1-
const {run} = require('runjs');
1+
'use strict';
2+
3+
const { sh, cli } = require('tasksfile');
24
const chalk = require('chalk');
35
const rawArgv = process.argv.slice(2);
46
const args = rawArgv.join(' ');
57

68
// 便于捕捉 build 之后的错误,然后进行自定义处理
79
// 配合 jenkins 执行 job
8-
run(`vue-cli-service build ${args}`, {
9-
async: true,
10-
stdio: 'inherit'
11-
}).then((output) => {
12-
console.log(chalk.cyan(output));
13-
}).catch((err) => {
14-
console.error('\n');
15-
console.error(chalk.magenta('编译打包出错了 ~~~~(>_<)~~~~ \n'));
16-
console.error(chalk.magenta('具体错误信息如下 \n'));
17-
console.error(chalk.red(`${err}.\n`));
18-
console.log(chalk.red(' Build failed with errors.\n'));
19-
process.exit(1);
20-
});
10+
function command() {
11+
sh(`vue-cli-service build ${args}`, {
12+
async: true,
13+
stdio: 'inherit',
14+
})
15+
.then((output) => {
16+
console.log(chalk.cyan(output || ''));
17+
})
18+
.catch((err) => {
19+
console.error('\n');
20+
console.error(chalk.magenta('编译打包出错了 ~~~~(>_<)~~~~ \n'));
21+
console.error(chalk.magenta('具体错误信息如下 \n'));
22+
console.error(chalk.red(`${err}.\n`));
23+
console.log(chalk.red(' Build failed with errors.\n'));
24+
process.exit(1);
25+
});
26+
}
27+
28+
command();

build/utils.js

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,11 @@ exports.cssLoaders = function (options) {
1010
const cssLoader = {
1111
loader: 'css-loader',
1212
options: {
13-
minimize: process.env.NODE_ENV === 'production' || process.env.NODE_ENV === 'dll',
1413
sourceMap: options.sourceMap
1514
}
1615
}
1716

18-
var postcssLoader = {
17+
const postcssLoader = {
1918
loader: 'postcss-loader',
2019
options: {
2120
sourceMap: options.sourceMap
@@ -35,7 +34,7 @@ exports.cssLoaders = function (options) {
3534
// you can specify a publicPath here
3635
// by default it use publicPath in webpackOptions.output
3736
// 解决图片作为背景引入时,路径不对的问题
38-
publicPath: '../../'
37+
publicPath: './'
3938
}
4039
})
4140
} else {
@@ -109,5 +108,3 @@ exports.createNotifierCallback = function () {
109108
exports.resolve = function (dir) {
110109
return path.join(__dirname, '..', dir)
111110
}
112-
113-

build/zip.ts

Lines changed: 50 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,57 @@
1-
'use strict';
2-
const path = require('path');
1+
#!/usr/bin/env node
2+
33
const fs = require('fs');
4+
const path = require('path');
45
const archiver = require('archiver');
5-
const utils = require('@liwb/cloud-utils');
6-
const pkg = require('../package.json');
7-
const outDir = `${path.resolve(__dirname, '../')}/${pkg.name}_${utils.formatDate(new Date(), 'yyyy-MM-dd_HH:mm:ss')}.zip`;
8-
9-
// create a file to stream archive data to.
10-
const output = fs.createWriteStream(outDir);
11-
const archive = archiver('zip', {
12-
zlib: { level: 9 } // Sets the compression level.
13-
});
14-
15-
// listen for all archive data to be written
16-
// 'close' event is fired only when a file descriptor is involved
17-
output.on('close', () => {
18-
console.log(archive.pointer() + ' total bytes');
19-
console.log('archiver has been finalized and the output file descriptor has closed.');
20-
});
21-
22-
// This event is fired when the data source is drained no matter what was the data source.
23-
// It is not part of this library but rather from the NodeJS Stream API.
24-
// @see: https://nodejs.org/api/stream.html#stream_event_end
25-
output.on('end', () => {
26-
console.log('Data has been drained');
27-
});
28-
29-
// good practice to catch warnings (ie stat failures and other non-blocking errors)
30-
archive.on('warning', (err) => {
31-
if (err.code === 'ENOENT') {
32-
// log warning
33-
} else {
34-
// throw error
35-
throw err;
6+
const { formatDate } = require('@liwb/cloud-utils');
7+
8+
const DEST_DIR = path.join(__dirname, '../dist');
9+
const DEST_ZIP_DIR = path.join(__dirname, '../dist-zip');
10+
11+
const extractExtensionData = () => {
12+
const extPackageJson = require('../package.json');
13+
14+
return {
15+
name: extPackageJson.name,
16+
version: extPackageJson.version,
17+
};
18+
};
19+
20+
const makeDestZipDirIfNotExists = () => {
21+
if (!fs.existsSync(DEST_ZIP_DIR)) {
22+
fs.mkdirSync(DEST_ZIP_DIR);
3623
}
37-
});
24+
};
25+
26+
const buildZip = (src, dist, zipFilename) => {
27+
console.info(`Building ${zipFilename}...`);
28+
29+
const archive = archiver('zip', { zlib: { level: 9 } });
30+
const stream = fs.createWriteStream(path.join(dist, zipFilename));
31+
32+
return new Promise((resolve, reject) => {
33+
archive
34+
.directory(src, false)
35+
.on('error', (err) => reject(err))
36+
.pipe(stream);
37+
38+
stream.on('close', () => resolve());
39+
archive.finalize();
40+
});
41+
};
3842

39-
// good practice to catch this error explicitly
40-
archive.on('error', (err) => {
41-
throw err;
42-
});
43+
const main = () => {
44+
const { name, version } = extractExtensionData();
45+
const zipFilename = `${name}-v${version}_${formatDate(
46+
new Date(),
47+
'yyyy-MM-dd_HH-mm-ss'
48+
)}.zip`;
4349

44-
// pipe archive data to the file
45-
archive.pipe(output);
50+
makeDestZipDirIfNotExists();
4651

47-
archive.directory('./dist/', false);
52+
buildZip(DEST_DIR, DEST_ZIP_DIR, zipFilename)
53+
.then(() => console.info('OK'))
54+
.catch(console.err);
55+
};
4856

49-
// finalize the archive (ie we are done appending files but streams have to finish yet)
50-
// 'close', 'end' or 'finish' may be fired right after calling this method so register to them beforehand
51-
archive.finalize();
57+
main();

0 commit comments

Comments
 (0)