microfox
Version:
Universal CLI tool for creating modern TypeScript packages with npm availability checking
113 lines (111 loc) • 4.99 kB
JavaScript
import {
glob
} from "./chunk-755TRFLZ.mjs";
// src/commands/track-ci.ts
import { Command } from "commander";
import chalk from "chalk";
import path from "path";
import fs from "fs/promises";
import { pathToFileURL } from "url";
import yaml from "js-yaml";
import { findUp } from "find-up";
var trackCi = new Command("track-ci").description("Generate GitHub Actions workflows from tracker scripts.").action(async () => {
var _a, _b, _c, _d, _e;
console.log(chalk.cyan("Generating GitHub Actions workflow..."));
const projectRoot = process.cwd();
const trackerFiles = await glob("**/__track__/**/*.tracker.ts", {
cwd: projectRoot,
ignore: "**/node_modules/**"
});
if (trackerFiles.length === 0) {
console.log(chalk.yellow("No tracker files found to generate workflows from."));
return;
}
const workflow = {
name: "Automated Trackers",
on: {},
jobs: {}
};
for (const trackerPath of trackerFiles) {
try {
const fullTrackerPath = path.resolve(projectRoot, trackerPath);
const trackerModule = await import(pathToFileURL(fullTrackerPath).href);
const tracker = trackerModule.default;
if (tracker && tracker.config && tracker.config.github) {
const jobName = tracker.config.github.name.replace(/\s+/g, "-").toLowerCase();
const { on } = tracker.config.github;
for (const key of Object.keys(on)) {
if (key === "schedule") {
workflow.on.schedule = [...workflow.on.schedule || [], ...on[key]];
} else {
if (!workflow.on[key]) workflow.on[key] = {};
if (on[key].branches) {
workflow.on[key].branches = [...workflow.on[key].branches || [], ...on[key].branches];
}
if (on[key].paths) {
workflow.on[key].paths = [...workflow.on[key].paths || [], ...on[key].paths];
}
}
}
const conditions = [];
if (on.push) {
conditions.push("github.event_name == 'push'");
}
if (on.pull_request) {
conditions.push("github.event_name == 'pull_request'");
}
if (on.schedule) {
on.schedule.forEach((s) => {
conditions.push(`github.event.schedule == '${s.cron}'`);
});
}
const ifCondition = conditions.join(" || ");
workflow.jobs[jobName] = {
"if": ifCondition,
"runs-on": "ubuntu-latest",
steps: [
{ name: "Checkout Code", uses: "actions/checkout@v4" },
{ name: "Setup Node.js", uses: "actions/setup-node@v4", with: { "node-version": "20" } },
{ name: "Install Dependencies", run: "npm install" },
{ name: "Run Tracker", run: `npx microfox track --file ${trackerPath}` }
// Assumes microfox is available
]
};
console.log(chalk.green(` Added job: ${tracker.config.github.name} with condition: "${ifCondition}"`));
}
} catch (error) {
console.error(chalk.red(` Failed to process tracker ${trackerPath}:`), error);
}
}
if (workflow.on.push) {
if (workflow.on.push.branches) workflow.on.push.branches = [...new Set(workflow.on.push.branches)];
if (workflow.on.push.paths) workflow.on.push.paths = [...new Set(workflow.on.push.paths)];
if (((_a = workflow.on.push.branches) == null ? void 0 : _a.length) === 0) delete workflow.on.push.branches;
if (((_b = workflow.on.push.paths) == null ? void 0 : _b.length) === 0) delete workflow.on.push.paths;
if (Object.keys(workflow.on.push).length === 0) delete workflow.on.push;
}
if (workflow.on.pull_request) {
if (workflow.on.pull_request.branches) workflow.on.pull_request.branches = [...new Set(workflow.on.pull_request.branches)];
if (workflow.on.pull_request.paths) workflow.on.pull_request.paths = [...new Set(workflow.on.pull_request.paths)];
if (((_c = workflow.on.pull_request.branches) == null ? void 0 : _c.length) === 0) delete workflow.on.pull_request.branches;
if (((_d = workflow.on.pull_request.paths) == null ? void 0 : _d.length) === 0) delete workflow.on.pull_request.paths;
if (Object.keys(workflow.on.pull_request).length === 0) delete workflow.on.pull_request;
}
if (((_e = workflow.on.schedule) == null ? void 0 : _e.length) === 0) delete workflow.on.schedule;
const workflowYaml = yaml.dump(workflow);
let githubDir = await findUp(".github", { type: "directory" });
if (!githubDir) {
githubDir = path.resolve(projectRoot, ".github");
console.log(chalk.yellow("Could not find .github directory. Creating one at project root."));
}
const outputPath = path.resolve(githubDir, "workflows", "trackers.yml");
await fs.mkdir(path.dirname(outputPath), { recursive: true });
await fs.writeFile(outputPath, workflowYaml);
console.log(chalk.cyan(`
Successfully generated workflow at ${outputPath}`));
});
export {
trackCi
};
//# sourceMappingURL=chunk-SG52XHNA.mjs.map