build.js 1.86 KB
Newer Older
范雪寒's avatar
范雪寒 committed
1 2 3 4 5 6 7 8 9 10 11
let fs = require('fs');
const compressing = require('compressing');

function fix2(num) {
    if (num >= 10) {
        return '' + num;
    } else {
        return '0' + num;
    }
}

李维's avatar
李维 committed
12 13 14 15 16 17 18
async function initFiles() {
    const formExists = await fs.existsSync('./publish/form');
    if (formExists) {
        await fs.rmdirSync('./publish/form');
    }
}

范雪寒's avatar
范雪寒 committed
19
async function copyDir(src, dst) {
李维's avatar
李维 committed
20
    // fs.fileex
范雪寒's avatar
范雪寒 committed
21 22 23 24 25 26
    const exists = await fs.existsSync(dst);
    if (!exists) {
        await fs.mkdirSync(dst);
    }
    //读取目录
    const paths = await fs.readdirSync(src);
李维's avatar
李维 committed
27
    paths.forEach(async function (path) {
范雪寒's avatar
范雪寒 committed
28 29 30 31
        const newSrc = `${src}/${path}`;
        const newDst = `${dst}/${path}`;
        const st = await fs.statSync(newSrc);
        if (st.isFile()) {
李维's avatar
李维 committed
32
            console.log('copy ' + newDst);
范雪寒's avatar
范雪寒 committed
33 34 35 36 37
            const data = await fs.readFileSync(newSrc);
            await fs.writeFileSync(newDst, data);
        } else if (st.isDirectory()) {
            copyDir(newSrc, newDst);
        }
李维's avatar
李维 committed
38
    });
39 40
}

范雪寒's avatar
范雪寒 committed
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
async function main() {
    let date = new Date();
    let fileName = `Release_${date.getFullYear()}${fix2(date.getMonth() + 1)}${fix2(date.getDate())} `;
    fileName += `${fix2(date.getHours())}-${fix2(date.getMinutes())}-${fix2(date.getSeconds())}`;

    await copyDir('../play/build/web-desktop', './publish/play');

    const data = await fs.readFileSync('./index.html');
    await fs.writeFileSync('./publish/play/index.html', data);

    compressing.zip.uncompress('../form/publish/form.zip', './publish/form')
        .then(() => {
            const tarStream = new compressing.zip.Stream();
            tarStream.addEntry('./publish/play');
            tarStream.addEntry('./publish/form');

            const destStream = fs.createWriteStream(`publish/${fileName}.zip`);

            tarStream.pipe(destStream);

            console.log('打包完成!');
        });
}

main();