Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(action): reuse args from package.json #106

Merged
merged 2 commits into from
Nov 29, 2024
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
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ npx sherif@latest

We recommend running Sherif in your CI once [all errors are fixed](#autofix). Run it by **specifying a version instead of latest**. This is useful to prevent regressions (e.g. when adding a library to a package but forgetting to update the version in other packages of the monorepo).

When using the GitHub Action, it will search for a `sherif` script in the root `package.json` and use the same arguments automatically to avoid repeating them twice. You can override this behaviour with the `args` parameter.

<details>

<summary>GitHub Actions example</summary>
Expand Down Expand Up @@ -82,7 +84,7 @@ sherif --fix

### No-install mode

If you don't want Sherif to run your packager manager's `install` command after running autofix, you can use the `--no-install` flag:
If you don't want Sherif to run your packager manager's `install` command after running autofix, you can use the `--no-install` flag:

```bash
sherif --fix --no-install
Expand Down Expand Up @@ -168,4 +170,3 @@ Dependencies should be ordered alphabetically to prevent complex diffs when inst
## License

[MIT](./LICENSE)

24 changes: 23 additions & 1 deletion action/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -32590,7 +32590,7 @@ function run() {
// Get inputs
const version = core.getInput('version');
const token = core.getInput('github-token');
const additionalArgs = core.getInput('args');
let additionalArgs = core.getInput('args');
// Initialize octokit
const octokit = github.getOctokit(token);
// Determine release to download
Expand Down Expand Up @@ -32655,6 +32655,9 @@ function run() {
core.setOutput('sherif-path', binaryPath);
core.info('Sherif has been installed successfully');
// Prepare arguments
if (!additionalArgs) {
additionalArgs = (yield getArgsFromPackageJson()) || '';
}
const args = additionalArgs.split(' ').filter(arg => arg !== '');
// Configure output options to preserve colors
const options = {
Expand All @@ -32679,6 +32682,25 @@ function run() {
}
});
}
function getArgsFromPackageJson() {
return __awaiter(this, void 0, void 0, function* () {
try {
const packageJsonFile = yield fsp.readFile(path.resolve(process.cwd(), 'package.json'));
const packageJson = JSON.parse(packageJsonFile.toString());
// Extract args from the `sherif` script in package.json, starting after
// `sherif ` and ending before the next `&&` or end of line
const regexResult = /sherif\s([a-zA-Z\s\.-]*)(?=\s&&|$)/g.exec(packageJson.scripts.sherif);
if (regexResult && regexResult.length > 1) {
const args = regexResult[1];
core.info(`Using the arguments "${args}" from the root package.json`);
return args;
}
}
catch (_a) {
core.info('Failed to extract args from package.json');
}
});
}
run();


Expand Down
27 changes: 26 additions & 1 deletion action/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ async function run(): Promise<void> {
// Get inputs
const version = core.getInput('version');
const token = core.getInput('github-token');
const additionalArgs = core.getInput('args');
let additionalArgs = core.getInput('args');

// Initialize octokit
const octokit = github.getOctokit(token);
Expand Down Expand Up @@ -91,6 +91,9 @@ async function run(): Promise<void> {
core.info('Sherif has been installed successfully');

// Prepare arguments
if (!additionalArgs) {
additionalArgs = (await getArgsFromPackageJson()) || '';
}
const args = additionalArgs.split(' ').filter(arg => arg !== '');

// Configure output options to preserve colors
Expand Down Expand Up @@ -119,4 +122,26 @@ async function run(): Promise<void> {
}
}

async function getArgsFromPackageJson() {
try {
const packageJsonFile = await fsp.readFile(
path.resolve(process.cwd(), 'package.json')
);
const packageJson = JSON.parse(packageJsonFile.toString());

// Extract args from the `sherif` script in package.json, starting after
// `sherif ` and ending before the next `&&` or end of line
const regexResult = /sherif\s([a-zA-Z\s\.-]*)(?=\s&&|$)/g.exec(
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add a comment to explain what this regex does?

packageJson.scripts.sherif
);
if (regexResult && regexResult.length > 1) {
const args = regexResult[1];
core.info(`Using the arguments "${args}" from the root package.json`);
return args;
}
} catch {
core.info('Failed to extract args from package.json');
}
}

run();