Skip to content

Commit

Permalink
[core] Allow running prettier from material-ui-x (#22071)
Browse files Browse the repository at this point in the history
  • Loading branch information
oliviertassinari committed Aug 6, 2020
1 parent 574fd61 commit f434177
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 12 deletions.
4 changes: 2 additions & 2 deletions scripts/listChangedFiles.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ async function execGitCmd(args) {
return gitResults.trim().toString().split('\n');
}

async function listChangedFiles() {
const comparedBranch = process.env.CIRCLECI ? 'origin/next' : 'next';
async function listChangedFiles({ branch }) {
const comparedBranch = process.env.CIRCLECI ? `origin/${branch}` : branch;
const mergeBase = await execGitCmd(['rev-parse', comparedBranch]);
const gitDiff = await execGitCmd(['diff', '--name-only', mergeBase]);
const gitLs = await execGitCmd(['ls-files', '--others', '--exclude-standard']);
Expand Down
26 changes: 16 additions & 10 deletions scripts/prettier.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ function runPrettier(options) {

const warnedFiles = [];
const ignoredFiles = fs
.readFileSync('.eslintignore', 'utf-8')
.readFileSync(path.join(process.cwd(), '.eslintignore'), 'utf-8')
.split(/\r*\n/)
.filter((notEmpty) => notEmpty);

Expand All @@ -51,7 +51,7 @@ function runPrettier(options) {
return;
}

const prettierConfigPath = path.join(__dirname, '../prettier.config.js');
const prettierConfigPath = path.join(process.cwd(), 'prettier.config.js');

files.forEach((file) => {
const prettierOptions = prettier.resolveConfig.sync(file, {
Expand Down Expand Up @@ -95,28 +95,34 @@ function runPrettier(options) {
}

async function run(argv) {
const { mode } = argv;
const { mode, branch } = argv;
const shouldWrite = mode === 'write' || mode === 'write-changed';
const onlyChanged = mode === 'check-changed' || mode === 'write-changed';

let changedFiles;
if (onlyChanged) {
changedFiles = await listChangedFiles();
changedFiles = await listChangedFiles({ branch });
}

runPrettier({ changedFiles, shouldWrite });
runPrettier({ changedFiles, shouldWrite, branch });
}

yargs
.command({
command: '$0 [mode]',
description: 'formats codebase',
builder: (command) => {
return command.positional('mode', {
description: '"write" | "check-changed" | "write-changed"',
type: 'string',
default: 'write-changed',
});
return command
.positional('mode', {
description: '"write" | "check-changed" | "write-changed"',
type: 'string',
default: 'write-changed',
})
.option('branch', {
default: 'next',
describe: 'The branch to diff against',
type: 'string',
});
},
handler: run,
})
Expand Down

0 comments on commit f434177

Please sign in to comment.