Skip to content

Instantly share code, notes, and snippets.

@si458
Last active December 28, 2023 10:18
Show Gist options
  • Save si458/8571e8435f0bf6ece31c118e459ab780 to your computer and use it in GitHub Desktop.
Save si458/8571e8435f0bf6ece31c118e459ab780 to your computer and use it in GitHub Desktop.
build script for pkg with icon and metainfo
if (process.argv[2] == undefined) process.exit(0);
if (process.argv[2] == "") process.exit(0);
const { pkg_fetch_version, node_version, pkg_cache_path, icon, version, description, company, name, copyright, file } = require(`./${process.argv[2]}.json`);
const ResEdit = require('resedit');
const path = require("path");
const fs = require('fs');
const https = require('https');
function download(url, dest) {
return new Promise((resolve, reject) => {
fs.access(dest, fs.constants.F_OK, (err) => {
if (err === null) reject('File already exists');
const request = https.get(url, response => {
if (response.statusCode === 200) {
const file = fs.createWriteStream(dest, { flags: 'wx' });
file.on('finish', () => resolve());
file.on('error', err => {
file.close();
if (err.code === 'EEXIST') reject('File already exists');
else fs.unlink(dest, () => reject(err.message)); // Delete temp file
});
response.pipe(file);
} else if (response.statusCode === 302 || response.statusCode === 301) {
download(response.headers.location, dest).then(() => resolve());
} else {
reject(`Server responded with ${response.statusCode}: ${response.statusMessage}`);
}
});
request.on('error', err => {
reject(err.message);
});
});
});
}
process.env['PKG_CACHE_PATH'] = path.join(__dirname, pkg_cache_path);
const pkg_fetch = path.join(process.env['PKG_CACHE_PATH'], `v${pkg_fetch_version}`);
const fetched = path.join(pkg_fetch, `fetched-v${node_version}-win-x64`);
const built = path.join(pkg_fetch, `built-v${node_version}-win-x64`);
const url = `https://github.com/vercel/pkg-fetch/releases/download/v${pkg_fetch_version}/node-v${node_version}-win-x64`
const { exec } = require('pkg');
(async () => {
if (!fs.existsSync(fetched)) {
console.log('Downloading File');
try { await fs.mkdirSync(pkg_fetch, { recursive: true }); } catch (e) { console.error(e); }
try { await download(url,fetched); } catch (e) { console.error(e); process.exit(1); }
console.log('Downloaded File');
} else {
console.log('Using Existing File');
}
console.log('Reading EXE');
let data = fs.readFileSync(fetched);
let exe = ResEdit.NtExecutable.from(data);
let res = ResEdit.NtExecutableResource.from(exe);
let viList = ResEdit.Resource.VersionInfo.fromEntries(res.entries);
console.log(viList[0].data.strings);
let vi = viList[0];
const theversion = `${version}.0`.split(".");
console.log('Removing OriginalFilename');
vi.removeStringValue({ lang: 1033, codepage: 1200 }, 'OriginalFilename');
console.log('Removing InternalName');
vi.removeStringValue({ lang: 1033, codepage: 1200 }, 'InternalName');
console.log('Setting Product Version');
vi.setProductVersion(theversion[0], theversion[1], theversion[2], theversion[3], 1033);
console.log('Setting File Version');
vi.setFileVersion(theversion[0], theversion[1], theversion[2], theversion[3], 1033);
console.log('Setting File Info');
vi.setStringValues(
{ lang: 1033, codepage: 1200 },
{
FileDescription: description,
ProductName: name,
CompanyName: company,
LegalCopyright: copyright
}
);
console.log(vi.data.strings);
vi.outputToResourceEntries(res.entries);
console.log('Replacing Icon');
let iconFile = ResEdit.Data.IconFile.from(fs.readFileSync(path.join(__dirname, icon)));
ResEdit.Resource.IconGroupEntry.replaceIconsForResource(
res.entries,
1,
1033,
iconFile.icons.map((item) => item.data)
);
res.outputResource(exe);
console.log('Generating EXE');
let newBinary = exe.generate();
console.log('Saving EXE');
fs.writeFileSync(built, Buffer.from(newBinary));
console.log('Bundling App');
await exec(['--build', '--debug', '--config', `${process.argv[2]}.json`, `${file}`]);
})();
{
"pkg": {
"targets": [
"node14-win-x64"
],
"outputPath": "dist",
"assets": [
"node_modules/printer/lib/node_printer.node"
]
},
"pkg_fetch_version": "3.2",
"node_version": "14.17.2",
"pkg_cache_path": ".pkg-cache",
"icon": "icon.ico",
"name": "myapp",
"description": "myapp description",
"company": "mycompany",
"version": "1.0.0",
"copyright": "mycopyright Ltd",
"file": "index.js"
}
npm install resedit pkg --save-dev
node build.js myapp
@jyj-code
Copy link

Error Sir,
PS D:\my-node-project> node build.js myapp
file:///D:/my-node-project/build.js:3
const { pkg_fetch_version, node_version, pkg_cache_path, icon, version, description, company, name, copyright, file } = require(./${process.argv[2]}.json);
^

ReferenceError: require is not defined in ES module scope, you can use import instead
This file is being treated as an ES module because it has a '.js' file extension and 'D:\my-node-project\package.json' contains "type": "module". To treat it as a CommonJS script, rename it to use the '.cjs' file extension.
at file:///D:/my-node-project/build.js:3:121
at ModuleJob.run (internal/modules/esm/module_job.js:183:25)
at async Loader.import (internal/modules/esm/loader.js:178:24)
at async Object.loadESM (internal/process/esm_loader.js:68:5)
at async handleMainPromise (internal/modules/run_main.js:59:12)

@si458
Copy link
Author

si458 commented Dec 28, 2023

same reply, so you would need to convert it yourself sadly

your error looks like you are using 'es-module'
the script i wrote isnt a module but a standalone file using commonjs

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment