const webpackDevServer = require('webpack-dev-server')
const webpack = require('webpack')

const config = require('../webpack.dev.js')
const compiler = webpack(config)
const server = new webpackDevServer(config.devServer, compiler)

const path = require("path");
const runScript = require("./runScript");
const fs = require("fs");
// const skinUrl = "./resource/skin.json";
// fs.watchFile(skinUrl, { persistent: true, interval: 2000 }, (curr, prev) => {
//     const skinDataAll = fs.readFileSync(skinUrl)
//     var endFile = `export const SkinJson = ${skinDataAll}`
//     fs.writeFileSync("./src/SkinJson.ts", endFile);
//     console.log("SkinJson.ts文件已更新")
// })

// 修改resource文件夹任意内容自动刷新资源
fs.watch('./resource/', { recursive: true }, (event, filename) => {

    if (
        filename === ".DS_Store"
        || filename === "res.json"
        || filename === "skin.json"
    ) return;

    runScript(path.resolve('./scripts/flushRes.js'), (err) => {
        console.log(err);
    });

});

runScript(path.resolve('./scripts/flushRes.js'), (err) => {
    console.log(err);
});

const argv = process.argv;

const runServer = async () => {
    const ipV4 = await webpackDevServer.internalIP('v4')
    console.log('========== ipV4 ==========', ipV4)
    server.options.host = ipV4
    const argvPort = +argv[argv.indexOf("-p") + 1]
    argvPort && (server.options.port = +argv[argv.indexOf("-p") + 1])
    await server.start()
}

runServer()