Skip to content

Commit

Permalink
feat(action): reuse args from package.json (#106)
Browse files Browse the repository at this point in the history
* feat(action): reuse args from package.json

* review
  • Loading branch information
colinlienard authored Nov 29, 2024
1 parent dec1df6 commit ec03fc5
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 4 deletions.
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(
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();

0 comments on commit ec03fc5

Please sign in to comment.