Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions pgpm/cli/src/commands/package.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,13 @@ Options:
--plan Include deployment plan (default: true)
--pretty Pretty-print output (default: true)
--functionDelimiter <delimiter> Function delimiter (default: $EOFCODE$)
--outputDiff Export AST diff files when round-trip mismatch detected (default: false)
--cwd <directory> Working directory (default: current directory)

Examples:
pgpm package Package with defaults
pgpm package --no-plan Package without plan
pgpm package --outputDiff Package and export AST diff files if mismatch detected
`;

export default async (
Expand Down Expand Up @@ -51,10 +53,17 @@ export default async (
default: '$EOFCODE$',
useDefault: true,
required: false
},
{
type: 'confirm',
name: 'outputDiff',
default: false,
useDefault: true,
required: false
}
];

let { cwd, plan, pretty, functionDelimiter } = await prompter.prompt(argv, questions);
let { cwd, plan, pretty, functionDelimiter, outputDiff } = await prompter.prompt(argv, questions);

const project = new PgpmPackage(cwd);

Expand All @@ -69,7 +78,8 @@ export default async (
usePlan: plan,
packageDir: project.modulePath,
pretty,
functionDelimiter
functionDelimiter,
outputDiff
});

return argv;
Expand Down
19 changes: 13 additions & 6 deletions pgpm/core/src/packaging/package.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ interface PackageModuleOptions {
extension?: boolean;
pretty?: boolean;
functionDelimiter?: string;
outputDiff?: boolean;
}

interface WritePackageOptions extends PackageModuleOptions {
Expand All @@ -44,7 +45,7 @@ const filterStatements = (stmts: RawStmt[], extension: boolean): RawStmt[] => {

export const packageModule = async (
packageDir: string,
{ usePlan = true, extension = true, pretty = true, functionDelimiter = '$EOFCODE$' }: PackageModuleOptions = {}
{ usePlan = true, extension = true, pretty = true, functionDelimiter = '$EOFCODE$', outputDiff = false }: PackageModuleOptions = {}
): Promise<{ sql: string; diff?: boolean; tree1?: string; tree2?: string }> => {
const resolveFn = usePlan ? resolveWithPlan : resolve;
const sql = resolveFn(packageDir);
Expand All @@ -70,7 +71,8 @@ export const packageModule = async (
});

const tree1 = parsed.stmts;
const tree2 = await parse(finalSql);
const reparsed = await parse(finalSql);
const tree2 = filterStatements(reparsed.stmts as any, extension);

const results: {
sql: string;
Expand Down Expand Up @@ -102,6 +104,7 @@ export const writePackage = async ({
extension = true,
usePlan = true,
packageDir,
outputDiff = false,
}: WritePackageOptions): Promise<void> => {
const pkgPath = `${packageDir}/package.json`;
const pkg = require(pkgPath);
Expand All @@ -116,6 +119,7 @@ export const writePackage = async ({
const { sql, diff, tree1, tree2 } = await packageModule(packageDir, {
extension,
usePlan,
outputDiff,
});

const outPath = extension ? `${packageDir}/sql` : `${packageDir}/out`;
Expand All @@ -140,10 +144,13 @@ export const writePackage = async ({
}

if (diff) {
log.warn(`⚠️ SQL diff exists! Review the ${relative(packageDir, outPath)}/ folder.`);
// Uncomment if needed:
// writeFileSync(`${outPath}/orig.${sqlFileName}.tree.json`, tree1);
// writeFileSync(`${outPath}/parsed.${sqlFileName}.tree.json`, tree2);
if (outputDiff) {
writeFileSync(`${outPath}/orig.${sqlFileName}.tree.json`, tree1!);
writeFileSync(`${outPath}/parsed.${sqlFileName}.tree.json`, tree2!);
log.warn(`⚠️ AST round-trip diff detected! Diff files written to ${relative(packageDir, outPath)}/`);
} else {
log.warn(`⚠️ AST round-trip diff detected! Run with --outputDiff to export the AST diff files.`);
}
}

const writePath = `${outPath}/${sqlFileName}`;
Expand Down