misc: make generate-sources script independent of zx

This commit is contained in:
Abdullah Atta
2023-06-27 05:57:59 +05:00
parent 4843d1c39e
commit 221767ed4d

View File

@@ -17,26 +17,27 @@ You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
import { $, fs, path, which } from "zx"; import fs from "fs/promises";
import fsSync from "fs";
import path from "path";
import { spawnSync } from "child_process";
import { fileURLToPath } from "url";
function uniqBy(a, key) { async function move(from, to) {
let seen = new Set(); try {
return a.filter((item) => { await fs.rename(from, to);
let k = key(item); } catch {
if (!k) return true; await fs.cp(from, to, { overwrite: true, force: true, recursive: true });
return seen.has(k) ? false : seen.add(k); await fs.rm(from, { force: true, recursive: true });
}); }
} }
$.env = process.env; function execute(cmd, args) {
$.quote = (s) => s; spawnSync(cmd, args, { env: process.env, stdio: "inherit" });
}
const generators = await which("flatpak-node-generator"); const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
if (generators.length <= 0)
throw new Error(
"flatpak-node-generator not found. Please install flatpak-node-generator from https://github.com/flatpak/flatpak-builder-tools."
);
const lockfiles = [ const lockfiles = [
{ {
@@ -60,7 +61,7 @@ const lockfiles = [
let allSources = []; let allSources = [];
for (const lockfile of lockfiles) { for (const lockfile of lockfiles) {
if (!fs.existsSync(lockfile.path)) { if (!fsSync.existsSync(lockfile.path)) {
console.error("skipping. lockfile does not exist at", lockfile.path); console.error("skipping. lockfile does not exist at", lockfile.path);
continue; continue;
} }
@@ -71,23 +72,30 @@ for (const lockfile of lockfiles) {
"node_modules" "node_modules"
); );
console.log("backing up node_modules at", nodeModulesPath); console.log("backing up node_modules at", nodeModulesPath);
fs.moveSync( await move(
nodeModulesPath, nodeModulesPath,
path.join(path.dirname(lockfile.path), "node_modules.backup") path.join(path.dirname(lockfile.path), "node_modules.backup")
); );
const output = `${lockfile.name}-sources.json`; const output = `${lockfile.name}-sources.json`;
await $`flatpak-node-generator${ execute(
lockfile.ignoreDev ? " --no-devel" : "" `flatpak-node-generator`,
} npm ${lockfile.path} -o ${output}`; [
lockfile.ignoreDev ? "--no-devel" : false,
"npm",
lockfile.path,
"-o",
output
].filter(Boolean)
);
const sources = JSON.parse(fs.readFileSync(output, "utf-8")); const sources = JSON.parse(await fs.readFile(output, "utf-8"));
allSources = [...allSources, ...sources]; allSources = [...allSources, ...sources];
fs.rmSync(output, { force: true }); await fs.rm(output, { force: true });
console.log("recovering node_modules to", nodeModulesPath); console.log("recovering node_modules to", nodeModulesPath);
fs.moveSync( await move(
path.join(path.dirname(lockfile.path), "node_modules.backup"), path.join(path.dirname(lockfile.path), "node_modules.backup"),
nodeModulesPath nodeModulesPath
); );
@@ -95,9 +103,9 @@ for (const lockfile of lockfiles) {
console.log("writing sources"); console.log("writing sources");
fs.writeFileSync( await fs.writeFile(
"generated-sources.json", "generated-sources.json",
JSON.stringify(uniqBy(allSources, (i) => i.url)) JSON.stringify(allSources, undefined, 4)
); );
console.log("done"); console.log("done");