Primeiro Commit v0.beta1
This commit is contained in:
92
scripts/run-sales-intelligence-sql.mjs
Normal file
92
scripts/run-sales-intelligence-sql.mjs
Normal file
@@ -0,0 +1,92 @@
|
||||
#!/usr/bin/env node
|
||||
|
||||
import fs from "node:fs/promises";
|
||||
import { execFileSync } from "node:child_process";
|
||||
import path from "node:path";
|
||||
import { fileURLToPath } from "node:url";
|
||||
|
||||
const scriptDir = path.dirname(fileURLToPath(import.meta.url));
|
||||
const fetchScriptPath = path.join(scriptDir, "fetch-sales-intelligence.mjs");
|
||||
|
||||
function printUsage() {
|
||||
console.log(`Usage:
|
||||
node "${path.resolve(process.argv[1] || "run-sales-intelligence-sql.mjs")}" --sql-file <file> [fetch args...]
|
||||
node "${path.resolve(process.argv[1] || "run-sales-intelligence-sql.mjs")}" --stdin [fetch args...]
|
||||
|
||||
Examples:
|
||||
node "${path.resolve(process.argv[1] || "run-sales-intelligence-sql.mjs")}" --sql-file query.sql --limit 200
|
||||
Get-Content query.sql | node "${path.resolve(process.argv[1] || "run-sales-intelligence-sql.mjs")}" --stdin --render-sql
|
||||
|
||||
Notes:
|
||||
- This helper preserves complex logical SQL that is awkward to pass through PowerShell quoting.
|
||||
- All flags other than --sql-file / --stdin / --help are passed through to fetch-sales-intelligence.mjs.
|
||||
`);
|
||||
}
|
||||
|
||||
async function readStdin() {
|
||||
const chunks = [];
|
||||
for await (const chunk of process.stdin) {
|
||||
chunks.push(chunk);
|
||||
}
|
||||
return Buffer.concat(chunks).toString("utf8");
|
||||
}
|
||||
|
||||
const args = process.argv.slice(2);
|
||||
let sqlFilePath = null;
|
||||
let useStdin = false;
|
||||
const passthroughArgs = [];
|
||||
|
||||
for (let index = 0; index < args.length; index += 1) {
|
||||
const arg = args[index];
|
||||
if (arg === "--help") {
|
||||
printUsage();
|
||||
process.exit(0);
|
||||
}
|
||||
if (arg === "--sql-file") {
|
||||
sqlFilePath = args[index + 1];
|
||||
index += 1;
|
||||
if (!sqlFilePath) {
|
||||
console.error("Missing value for --sql-file");
|
||||
process.exit(1);
|
||||
}
|
||||
continue;
|
||||
}
|
||||
if (arg === "--stdin") {
|
||||
useStdin = true;
|
||||
continue;
|
||||
}
|
||||
passthroughArgs.push(arg);
|
||||
}
|
||||
|
||||
if (sqlFilePath && useStdin) {
|
||||
console.error("Use either --sql-file or --stdin, not both.");
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
if (!sqlFilePath && !useStdin) {
|
||||
console.error("One of --sql-file or --stdin is required.");
|
||||
printUsage();
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
const sql = useStdin ? await readStdin() : await fs.readFile(path.resolve(sqlFilePath), "utf8");
|
||||
if (!sql.trim()) {
|
||||
console.error("SQL input is empty.");
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
try {
|
||||
const stdout = execFileSync(process.execPath, [fetchScriptPath, "--sql", sql, ...passthroughArgs], {
|
||||
encoding: "utf8",
|
||||
stdio: ["ignore", "pipe", "pipe"],
|
||||
});
|
||||
process.stdout.write(stdout);
|
||||
} catch (error) {
|
||||
if (typeof error.stdout === "string" && error.stdout.length > 0) {
|
||||
process.stdout.write(error.stdout);
|
||||
}
|
||||
if (typeof error.stderr === "string" && error.stderr.length > 0) {
|
||||
process.stderr.write(error.stderr);
|
||||
}
|
||||
process.exit(typeof error.status === "number" ? error.status : 1);
|
||||
}
|
||||
Reference in New Issue
Block a user