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 1 commit
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).

By default, it will search for a `sherif` script in the root `package.json` and try to use the same arguments, so you can avoid repeating yourself. But you can override this behaviour with the `args` param.
Copy link
Owner

Choose a reason for hiding this comment

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

Suggested change
By default, it will search for a `sherif` script in the root `package.json` and try to use the same arguments, so you can avoid repeating yourself. But you can override this behaviour with the `args` param.
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)

31 changes: 30 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,32 @@ 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());
if (!('scripts' in packageJson)) {
core.info('No scripts found in package.json');
return;
}
if (!('sherif' in packageJson.scripts)) {
core.info('No sherif script found in package.json');
return;
}
// Select the args of the sherif script
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(`Found args "${args}" package.json`);
return args;
}
}
catch (_a) {
core.info('Failed to extract args from package.json');
}
});
}
run();


Expand Down
36 changes: 35 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,35 @@ 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());

if (!('scripts' in packageJson)) {
core.info('No scripts found in package.json');
return;
}

if (!('sherif' in packageJson.scripts)) {
core.info('No sherif script found in package.json');
Copy link
Owner

Choose a reason for hiding this comment

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

I don't think we need to log here, it could get noisy for nothing.

return;
}

// Select the args of the sherif script
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(`Found args "${args}" package.json`);
Copy link
Owner

Choose a reason for hiding this comment

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

Suggested change
core.info(`Found args "${args}" package.json`);
core.info(`Using the arguments "${args}" from the root package.json`);

return args;
}
} catch {
core.info('Failed to extract args from package.json');
}
}

run();