63 lines
1.8 KiB
JavaScript
63 lines
1.8 KiB
JavaScript
import path from "path";
|
|
import fs from "fs";
|
|
import { selectProject } from "./lib/select-project.js";
|
|
import { importSourceFactory } from "./lib/import-source.js";
|
|
|
|
|
|
const PROJECT_DIR = process.env.npm_config_local_prefix;
|
|
const HOMEBREW_DIR = path.join(PROJECT_DIR, "homebrew");
|
|
|
|
const projects = fs.readdirSync(HOMEBREW_DIR, {
|
|
withFileTypes: true,
|
|
})
|
|
.filter((dir) => dir.isDirectory())
|
|
.map((dir) => dir.name);
|
|
|
|
if (projects.length === 0) {
|
|
console.error("No homebrew projects");
|
|
process.exit(1);
|
|
}
|
|
|
|
const project = selectProject(projects);
|
|
const SOURCE_DIR = path.join(HOMEBREW_DIR, project);
|
|
const importSource = importSourceFactory(SOURCE_DIR);
|
|
|
|
const meta = importSource("meta.json");
|
|
meta._meta.dateLastModified = parseInt(Date.now()/1000);
|
|
|
|
const sources = fs.readdirSync(SOURCE_DIR, {
|
|
withFileTypes: true,
|
|
})
|
|
.filter((source) => source.isFile() && source.name.endsWith(".json") && source.name !== "meta.json")
|
|
.map((source) => source.name);
|
|
|
|
|
|
const homebrew = sources.reduce((result, source) => {
|
|
const sourceData = importSource(source);
|
|
delete sourceData._meta;
|
|
|
|
Object.entries(sourceData).forEach(([sourceKey, sourceValue]) => {
|
|
if (!(sourceKey in result)) {
|
|
|
|
result[sourceKey] = sourceValue;
|
|
return;
|
|
}
|
|
if (Array.isArray(result[sourceKey])) {
|
|
result[sourceKey] = result[sourceKey].concat(sourceValue);
|
|
return;
|
|
}
|
|
throw new Error(`Could not merge non-array key ${sourceKey}`);
|
|
});
|
|
|
|
return result;
|
|
}, meta);
|
|
|
|
|
|
console.log(homebrew.subclass);
|
|
|
|
const outFile = path.join(HOMEBREW_DIR, `${project}.json`);
|
|
if (fs.existsSync(outFile)) {
|
|
fs.unlinkSync(outFile);
|
|
}
|
|
|
|
fs.writeFileSync(outFile, JSON.stringify(homebrew, null, 4), { encoding: "utf-8"}); |