Skip to content

Commit

Permalink
Merge pull request #848 from bitpredator/dev
Browse files Browse the repository at this point in the history
chore: [system]\runcode 🎨 Run formatter
  • Loading branch information
bitpredator authored Sep 26, 2024
2 parents 89bf2c2 + 6684373 commit 9f090a5
Show file tree
Hide file tree
Showing 12 changed files with 604 additions and 582 deletions.
16 changes: 8 additions & 8 deletions server-data/resources/[system]/[builders]/webpack/fxmanifest.lua
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
-- This resource is part of the default Cfx.re asset pack (cfx-server-data)
-- Altering or recreating for local use only is strongly discouraged.

version '1.0.0'
author 'Cfx.re <[email protected]>'
description 'Builds resources with webpack. To learn more: https://webpack.js.org'
repository 'https://github.com/citizenfx/cfx-server-data'
version("1.0.0")
author("Cfx.re <[email protected]>")
description("Builds resources with webpack. To learn more: https://webpack.js.org")
repository("https://github.com/citizenfx/cfx-server-data")

dependency 'yarn'
server_script 'webpack_builder.js'
dependency("yarn")
server_script("webpack_builder.js")

fx_version 'adamant'
game 'common'
fx_version("adamant")
game("common")
296 changes: 153 additions & 143 deletions server-data/resources/[system]/[builders]/webpack/webpack_builder.js
Original file line number Diff line number Diff line change
@@ -1,173 +1,183 @@
const fs = require('fs');
const path = require('path');
const workerFarm = require('worker-farm');
// eslint-disable-next-line no-unused-vars
const async = require('async');
let buildingInProgress = false;
// eslint-disable-next-line no-unused-vars
let currentBuildingModule = '';

// some modules will not like the custom stack trace logic
// eslint-disable-next-line no-unused-vars
const ops = Error.prepareStackTrace;
Error.prepareStackTrace = undefined;

const webpackBuildTask = {
shouldBuild(resourceName) {
const numMetaData = GetNumResourceMetadata(resourceName, 'webpack_config');
shouldBuild(resourceName) {
const numMetaData = GetNumResourceMetadata(resourceName, 'webpack_config');

if (numMetaData > 0) {
for (let i = 0; i < numMetaData; i++) {
const configName = GetResourceMetadata(resourceName, 'webpack_config');
if (numMetaData > 0) {
for (let i = 0; i < numMetaData; i++) {
const configName = GetResourceMetadata(resourceName, 'webpack_config');

if (shouldBuild(configName)) {
return true;
}
}
}
if (shouldBuild(configName)) {
return true;
}
}
}

return false;
return false;

function loadCache(config) {
const cachePath = `cache/${resourceName}/${config.replace(/\//g, '_')}.json`;
function loadCache(config) {
const cachePath = `cache/${resourceName}/${config.replace(/\//g, '_')}.json`;

try {
return JSON.parse(fs.readFileSync(cachePath, {encoding: 'utf8'}));
} catch {
return null;
}
}
try {
return JSON.parse(fs.readFileSync(cachePath, { encoding: 'utf8' }));
}
catch {
return null;
}
}

function shouldBuild(config) {
const cache = loadCache(config);
function shouldBuild(config) {
const cache = loadCache(config);

if (!cache) {
return true;
}
if (!cache) {
return true;
}

for (const file of cache) {
const stats = getStat(file.name);
for (const file of cache) {
const stats = getStat(file.name);

if (!stats ||
if (!stats ||
stats.mtime !== file.stats.mtime ||
stats.size !== file.stats.size ||
stats.inode !== file.stats.inode) {
return true;
}
}

return false;
}

function getStat(path) {
try {
const stat = fs.statSync(path);

return stat ? {
mtime: stat.mtimeMs,
size: stat.size,
inode: stat.ino,
} : null;
} catch {
return null;
}
}
},

build(resourceName, cb) {
let buildWebpack = async () => {
let error = null;
const configs = [];
const promises = [];
const numMetaData = GetNumResourceMetadata(resourceName, 'webpack_config');

for (let i = 0; i < numMetaData; i++) {
configs.push(GetResourceMetadata(resourceName, 'webpack_config', i));
}

for (const configName of configs) {
const configPath = GetResourcePath(resourceName) + '/' + configName;

const cachePath = `cache/${resourceName}/${configName.replace(/\//g, '_')}.json`;

try {
fs.mkdirSync(path.dirname(cachePath));
} catch {
}

const config = require(configPath);

const workers = workerFarm(require.resolve('./webpack_runner'));

if (config) {
const resourcePath = path.resolve(GetResourcePath(resourceName));

while (buildingInProgress) {
console.log(`webpack is busy: we are waiting to compile ${resourceName} (${configName})`);
await sleep(3000);
}

console.log(`${resourceName}: started building ${configName}`);

buildingInProgress = true;
currentBuildingModule = resourceName;

promises.push(new Promise((resolve, reject) => {
workers({
configPath,
resourcePath,
cachePath
}, (err, outp) => {
workerFarm.end(workers);

if (err) {
console.error(err.stack || err);
if (err.details) {
console.error(err.details);
}

buildingInProgress = false;
currentBuildingModule = '';
currentBuildingScript = '';
reject("worker farm webpack errored out");
return;
}

if (outp.errors) {
for (const error of outp.errors) {
console.log(error);
}
buildingInProgress = false;
currentBuildingModule = '';
currentBuildingScript = '';
reject("webpack got an error");
return;
}

console.log(`${resourceName}: built ${configName}`);
buildingInProgress = false;
resolve();
});
}));
}
}

try {
await Promise.all(promises);
} catch (e) {
error = e.toString();
}

buildingInProgress = false;
currentBuildingModule = '';

if (error) {
cb(false, error);
} else cb(true);
};
buildWebpack().then();
}
return true;
}
}

return false;
}

// eslint-disable-next-line no-shadow
function getStat(path) {
try {
const stat = fs.statSync(path);

return stat ? {
mtime: stat.mtimeMs,
size: stat.size,
inode: stat.ino,
} : null;
}
catch {
return null;
}
}
},

build(resourceName, cb) {
const buildWebpack = async () => {
let error = null;
const configs = [];
const promises = [];
const numMetaData = GetNumResourceMetadata(resourceName, 'webpack_config');

for (let i = 0; i < numMetaData; i++) {
configs.push(GetResourceMetadata(resourceName, 'webpack_config', i));
}

for (const configName of configs) {
const configPath = GetResourcePath(resourceName) + '/' + configName;

const cachePath = `cache/${resourceName}/${configName.replace(/\//g, '_')}.json`;

try {
fs.mkdirSync(path.dirname(cachePath));
}
// eslint-disable-next-line no-inline-comments
catch { /* empty */ }

const config = require(configPath);

const workers = workerFarm(require.resolve('./webpack_runner'));

if (config) {
const resourcePath = path.resolve(GetResourcePath(resourceName));

while (buildingInProgress) {
console.log(`webpack is busy: we are waiting to compile ${resourceName} (${configName})`);
await sleep(3000);
}

console.log(`${resourceName}: started building ${configName}`);

buildingInProgress = true;
currentBuildingModule = resourceName;

promises.push(new Promise((resolve, reject) => {
workers({
configPath,
resourcePath,
cachePath,
}, (err, outp) => {
workerFarm.end(workers);

if (err) {
console.error(err.stack || err);
if (err.details) {
console.error(err.details);
}

buildingInProgress = false;
currentBuildingModule = '';
currentBuildingScript = '';
reject('worker farm webpack errored out');
return;
}

if (outp.errors) {
// eslint-disable-next-line no-shadow
for (const error of outp.errors) {
console.log(error);
}
buildingInProgress = false;
currentBuildingModule = '';
currentBuildingScript = '';
reject('webpack got an error');
return;
}

console.log(`${resourceName}: built ${configName}`);
buildingInProgress = false;
resolve();
});
}));
}
}

try {
await Promise.all(promises);
}
catch (e) {
error = e.toString();
}

buildingInProgress = false;
currentBuildingModule = '';

if (error) {
cb(false, error);
}
else {cb(true);}
};
buildWebpack().then();
},
};

function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
return new Promise(resolve => setTimeout(resolve, ms));
}

RegisterResourceBuildTaskFactory('z_webpack', () => webpackBuildTask);
Loading

0 comments on commit 9f090a5

Please sign in to comment.