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
13 changes: 12 additions & 1 deletion cli/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,15 @@ const icebergOption: INamedOption<yargs.Options> = {
},
};

const fmtIgnoreJsOption: INamedOption<yargs.Options> = {
name: "ignore-js-files",
option: {
describe: "If set, the formatter will not consider javascript files (.js)",
type: "boolean",
default: false,
},
};

const testConnectionOptionName = "test-connection";

const watchOptionName = "watch";
Expand Down Expand Up @@ -708,6 +717,7 @@ export function runCli() {
positionalOptions: [projectDirMustExistOption],
options: [
actionsOption,
fmtIgnoreJsOption,
{
name: checkOptionName,
option: {
Expand All @@ -718,7 +728,8 @@ export function runCli() {
}
],
processFn: async argv => {
let actions = ["{definitions,includes}/**/*.{js,sqlx}"];
const extensions = argv[fmtIgnoreJsOption.name] ? "*.sqlx" : "*.{js,sqlx}";
let actions = [`{definitions,includes}/**/${extensions}`];
if (actionsOption.name in argv && argv[actionsOption.name].length > 0) {
actions = argv[actionsOption.name];
}
Expand Down
2 changes: 2 additions & 0 deletions cli/index_help_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,5 +92,7 @@ suite("help command", () => {
expect(output).to.include("--check");
expect(output).to.include("Check if files are formatted correctly without modifying them.");
expect(output).to.include("--actions");
expect(output).to.include("--ignore-js-files");
expect(output).to.include("If set, the formatter will not consider javascript files (.js)");
});
});
75 changes: 75 additions & 0 deletions cli/index_project_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,5 +125,80 @@ SELECT 1 as test
expect(afterFormatCheckResult.exitCode).equals(0);
expect(afterFormatCheckResult.stdout).contains("All files are formatted correctly");
});

test("test for format command ignore js files", async () => {
const projectDir = tmpDirFixture.createNewTmpDir();
const npmCacheDir = tmpDirFixture.createNewTmpDir();
const workflowSettingsPath = path.join(projectDir, "workflow_settings.yaml");
const packageJsonPath = path.join(projectDir, "package.json");

// Initialize a project using the CLI, don't install packages.
await getProcessResult(
execFile(nodePath, [cliEntryPointPath, "init", projectDir, DEFAULT_DATABASE, DEFAULT_LOCATION])
);

// Install packages manually to get around bazel read-only sandbox issues.
const workflowSettings = dataform.WorkflowSettings.create(
loadYaml(fs.readFileSync(workflowSettingsPath, "utf8"))
);
delete workflowSettings.dataformCoreVersion;
fs.writeFileSync(workflowSettingsPath, dumpYaml(workflowSettings));
fs.writeFileSync(
packageJsonPath,
`{
"dependencies":{
"@dataform/core": "${version}"
}
}`
);
await getProcessResult(
execFile(npmPath, [
"install",
"--prefix",
projectDir,
"--cache",
npmCacheDir,
corePackageTarPath
])
);

// Create files that need formatting and ensure that the js file is not modified
const unformattedFilePath = path.join(projectDir, "definitions", "unformatted.sqlx");
fs.ensureFileSync(unformattedFilePath);
fs.writeFileSync(
unformattedFilePath,
`
config { type: "table" }
SELECT 1 as test
`
);


const jsContents = `
function myCoolFn() {
return true; }

modules.exports = {
myCoolFn, }
`
const unformattedJsFilePath = path.join(projectDir, "includes", "someMod.js");
fs.ensureFileSync(unformattedJsFilePath);
fs.writeFileSync(
unformattedJsFilePath,
jsContents,
);

// Run formatter
const formatCmdRun = await getProcessResult(
execFile(nodePath, [cliEntryPointPath, "format", "--ignore-js-files", projectDir])
);

expect(formatCmdRun.exitCode).equals(0);

// Ensure the js file didn't change
const bufFromFile = fs.readFileSync(unformattedJsFilePath);
const bufFromContents = Buffer.from(jsContents, 'utf-8');
expect(bufFromContents.equals(bufFromFile)).equals(true)
});
});
});
Loading