@justeattakeaway/eslint-plugin-snacks-pie-migration 0.1.1 → 0.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md
CHANGED
|
@@ -154,7 +154,9 @@ This is particularly useful during migration to PIE components, as it allows you
|
|
|
154
154
|
- Focus on preventing new deprecated component usage
|
|
155
155
|
- Gradually refactor existing code at your own pace
|
|
156
156
|
|
|
157
|
-
ℹ️
|
|
157
|
+
> ℹ️ When running locally, the branch changes will be compared against the merge base ref. It assumes the `main` branch exists. Otherwise the `master` branch will be used. In any case, no extra configuration is needed.
|
|
158
|
+
|
|
159
|
+
> ℹ️ When running on Github CI, the plugin will use the PR base branch SHA since the merge base can't be easily inferred. In this case, ensure to pass the `BASE_SHA` environment variable. Its value should be set to `github.event.pull_request.base.sha`.
|
|
158
160
|
|
|
159
161
|
To use the processor, add the following line to your ESLint configuration:
|
|
160
162
|
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
const { readFileSync } = require('fs');
|
|
2
|
-
const { isFileNew,
|
|
2
|
+
const { isFileNew, getFileStateFromRef } = require('../util/git-utils');
|
|
3
3
|
const { addedComponentsPostprocessor } = require('../util/added-components-postprocessor');
|
|
4
4
|
|
|
5
5
|
/**
|
|
@@ -9,6 +9,6 @@ const { addedComponentsPostprocessor } = require('../util/added-components-postp
|
|
|
9
9
|
*/
|
|
10
10
|
module.exports = {
|
|
11
11
|
postprocess (messages, filePath) {
|
|
12
|
-
return addedComponentsPostprocessor(messages, filePath, { readFileSync, isFileNew,
|
|
12
|
+
return addedComponentsPostprocessor(messages, filePath, { readFileSync, isFileNew, getFileStateFromRef });
|
|
13
13
|
},
|
|
14
14
|
};
|
|
@@ -10,7 +10,7 @@ const { getDefaultBranchName } = require('./git-utils');
|
|
|
10
10
|
* @param filePath - the file path
|
|
11
11
|
* @returns A filtered array of messages based on certain conditions
|
|
12
12
|
*/
|
|
13
|
-
function addedComponentsPostprocessor (messages, filePath, { readFileSync, isFileNew,
|
|
13
|
+
function addedComponentsPostprocessor (messages, filePath, { readFileSync, isFileNew, getFileStateFromRef }) {
|
|
14
14
|
// Get file relative path to the repo
|
|
15
15
|
const relativeFilePath = path.relative(process.cwd(), filePath)
|
|
16
16
|
.replace(/\\/g, '/'); // Fix path handling on Windows
|
|
@@ -20,8 +20,10 @@ function addedComponentsPostprocessor (messages, filePath, { readFileSync, isFil
|
|
|
20
20
|
const isNewFile = isFileNew(relativeFilePath);
|
|
21
21
|
|
|
22
22
|
// Get file content before and after
|
|
23
|
-
|
|
24
|
-
|
|
23
|
+
// In CI is preferred to use the base SHA as it is easily available
|
|
24
|
+
// In local environment it's easier to pull the info with git merge-base from the main branch
|
|
25
|
+
const ref = 'BASE_SHA' in process.env && process.env.BASE_SHA;
|
|
26
|
+
const filePreviousState = isNewFile ? '' : getFileStateFromRef(relativeFilePath, ref, getDefaultBranchName());
|
|
25
27
|
const fileCurrentState = readFileSync(relativeFilePath, 'utf8');
|
|
26
28
|
|
|
27
29
|
// Get list of added components
|
package/lib/util/git-utils.js
CHANGED
|
@@ -16,17 +16,18 @@ function isFileNew (relativeFilePath) {
|
|
|
16
16
|
}
|
|
17
17
|
|
|
18
18
|
/**
|
|
19
|
-
* Get file content from a
|
|
19
|
+
* Get file content from a ref
|
|
20
20
|
* @param {string} relativeFilePath - The relative path to the file from the repo root
|
|
21
|
-
* @param {string}
|
|
21
|
+
* @param {string} ref - SHA ref to check the file content from, give preference to this option when running on CI
|
|
22
|
+
* @param {string} branch - Alternatively, the branch name to check the file content from, give preference to this option when running on local environment
|
|
22
23
|
* @returns
|
|
23
24
|
*/
|
|
24
|
-
function
|
|
25
|
+
function getFileStateFromRef (relativeFilePath, ref, branch = 'main') {
|
|
25
26
|
try {
|
|
26
|
-
//
|
|
27
|
-
const
|
|
28
|
-
return execSync(`git show ${
|
|
29
|
-
} catch
|
|
27
|
+
// If the base sha wasn't provided, try to guess with merge-base
|
|
28
|
+
const sha = ref || execSync(`git merge-base HEAD ${branch}`, { encoding: 'utf8' }).trim();
|
|
29
|
+
return execSync(`git show ${sha}:"${relativeFilePath}"`, { encoding: 'utf8', stdio: 'pipe' });
|
|
30
|
+
} catch {
|
|
30
31
|
// If the file was added in this branch, this might fail, return empty string
|
|
31
32
|
return '';
|
|
32
33
|
}
|
|
@@ -47,6 +48,6 @@ function getDefaultBranchName () {
|
|
|
47
48
|
|
|
48
49
|
module.exports = {
|
|
49
50
|
isFileNew,
|
|
50
|
-
|
|
51
|
+
getFileStateFromRef,
|
|
51
52
|
getDefaultBranchName,
|
|
52
53
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@justeattakeaway/eslint-plugin-snacks-pie-migration",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.2",
|
|
4
4
|
"description": "This plugin helps developers to identify deprecated Snacks components and provides suggestions of replacement PIE components",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|