Skip to content

Commit 3e5a2be

Browse files
Implement script for fixing import paths
1 parent 2e0ad66 commit 3e5a2be

File tree

12 files changed

+237
-55
lines changed

12 files changed

+237
-55
lines changed

.eslintrc.json

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,16 @@
3434
"react-native",
3535
"react-hooks",
3636
"prettier",
37-
"import"
37+
"import",
38+
"simple-import-sort"
3839
],
3940
"rules": {
4041
"import/no-named-as-default-member": "off",
4142
"import/no-named-as-default": "off",
43+
"import/first": "error",
44+
"import/consistent-type-specifier-style": ["error", "prefer-top-level"],
45+
"import/newline-after-import": "error",
46+
"import/no-duplicates": "error",
4247
"import/extensions": [
4348
"error",
4449
"never",
@@ -72,7 +77,6 @@
7277
"disallowTypeAnnotations": true
7378
}
7479
],
75-
"import/consistent-type-specifier-style": ["error", "prefer-top-level"],
7680
"no-console": 0,
7781
"indent": "off",
7882
"quotes": [
@@ -108,21 +112,24 @@
108112
"bracketSpacing": true,
109113
"arrowParens": "avoid",
110114
"bracketSameLine": false,
111-
"endOfLine": "lf",
112-
"importOrder": [
113-
"^react",
114-
"^react-native$",
115-
"^@/slices",
116-
"^@/types",
117-
"^@/components",
118-
"^@/hooks",
119-
"^@/utils",
120-
"^@/assets",
121-
"^[./]",
122-
"^@/"
123-
],
124-
"importOrderSeparation": true
115+
"endOfLine": "lf"
125116
}
126-
]
117+
],
118+
"simple-import-sort/imports": ["error", {
119+
"groups": [
120+
["^react"],
121+
["^react-native$"],
122+
["^\\w"],
123+
["^@/slices"],
124+
["^@/types"],
125+
["^@/components"],
126+
["^@/hooks"],
127+
["^@/utils"],
128+
["^@/assets"],
129+
["^@/"],
130+
["^[./]"]
131+
]
132+
}],
133+
"simple-import-sort/exports": "error"
127134
}
128135
}

.githooks/pre-commit

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,27 @@
11
#!/bin/bash
2-
# Get latest git tag
3-
latest_tag=$(git describe --tags --abbrev=0)
4-
latest_tag_without_v=${latest_tag#"v"}
5-
version_from_tag=$(echo "$latest_tag_without_v" | grep -P '^(?P<major>0|[1-9]\d*)\.(?P<minor>0|[1-9]\d*)\.(?P<patch>0|[1-9]\d*)(?:-(?P<prerelease>(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+(?P<buildmetadata>[0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?$')
2+
# This script is ./pre-commit
3+
# Execute all scripts in ./pre-commit.d in order (0-foo before 10-bar)
64

7-
# Get the version from package.json
8-
version=$(jq -r '.version' package.json)
5+
# Get the directory of this script
6+
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
7+
error=0
98

10-
echo "Latest tag: $latest_tag_without_v"
11-
echo "Version from tag: $version_from_tag"
12-
echo "Version from package.json: $version"
9+
echo "Running pre-commit hooks in $DIR/pre-commit.d"
1310

14-
# Check if the version from the tag and package.json are the same
15-
if [ "$version" != "$version_from_tag" ]; then
16-
echo "Version in package.json and tag are not the same"
17-
exit 1
18-
fi
11+
# Run all scripts in ./pre-commit.d
12+
for script in $DIR/pre-commit.d/*; do
13+
if [ -x $script ]; then
14+
echo "> Running $script"
15+
$script
16+
err=$?
17+
if [ $err -ne 0 ]; then
18+
echo "Error in $script: $err"
19+
if [ $error -eq 0 ]; then
20+
error=$err
21+
fi
22+
fi
23+
printf "\n"
24+
fi
25+
done
1926

20-
echo "Version in package.json and tag are the same"
27+
exit $error
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#!/bin/bash
2+
conflicts=$(git diff --cached --name-only -G"<<<<<|=====|>>>>>")
3+
4+
if [[ -n "$conflicts" ]]; then
5+
echo "Unresolved merge conflicts in these files:"
6+
7+
for conflict in $conflicts; do
8+
echo "$conflict"
9+
done;
10+
11+
exit 1;
12+
fi
13+
14+
exit 0
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#!/bin/bash
2+
# Get latest git tag
3+
latest_tag=$(git describe --tags --abbrev=0)
4+
latest_tag_without_v=${latest_tag#"v"}
5+
version_from_tag=$(echo "$latest_tag_without_v" | grep -P '^(?P<major>0|[1-9]\d*)\.(?P<minor>0|[1-9]\d*)\.(?P<patch>0|[1-9]\d*)(?:-(?P<prerelease>(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+(?P<buildmetadata>[0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?$')
6+
7+
# Get the version from package.json
8+
version=$(jq -r '.version' package.json)
9+
10+
echo "Latest tag: $latest_tag_without_v"
11+
echo "Version from tag: $version_from_tag"
12+
echo "Version from package.json: $version"
13+
14+
# Check if the version from the tag and package.json are the same
15+
if [ "$version" != "$version_from_tag" ]; then
16+
echo "Version in package.json and tag are not the same"
17+
exit 1
18+
fi
19+
20+
echo "Version in package.json and tag are the same"
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
diff --git a/install-git-hooks.js b/install-git-hooks.js
2+
index 62723526d054b2da4ec7fa418f9125bdc59c14be..a21d9dde0b0d78cb327787fabc6ac40b1aeaa871 100644
3+
--- a/install-git-hooks.js
4+
+++ b/install-git-hooks.js
5+
@@ -54,7 +54,7 @@ function copyDir(src, dest) {
6+
};
7+
8+
function copy(src, dest) {
9+
- fs.copyFileSync(src, dest);
10+
+ fs.cpSync(src, dest);
11+
};
12+
13+
module.exports = installGitHooks;

__tests__/App.test.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22
* @format
33
*/
44
// Note: import explicitly to use the types shipped with jest.
5-
import { it } from '@jest/globals';
6-
75
import React from 'react';
8-
import 'react-native';
96
// Note: test renderer must be required after react-native.
107
import renderer from 'react-test-renderer';
118

129
import App from '@/App';
1310

11+
import 'react-native';
12+
import { it } from '@jest/globals';
13+
1414
it('renders correctly', () => {
1515
renderer.create(<App />);
1616
});

app.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
11
{
2-
"name": "opendtu-react-native",
3-
"displayName": "opendtu-react-native"
2+
"name": "opendtu-react-native"
43
}

fix-imports.mjs

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
#!/usr/bin/env node
2+
// check all imports in ./src and filter out file extensions from the import paths for .ts and .tsx.
3+
import fs from 'fs';
4+
5+
const srcDir = './src';
6+
7+
// check if dry run
8+
if (process.argv.includes('--dry')) {
9+
console.log('Dry run enabled');
10+
process.env.DRY_RUN = 'true';
11+
}
12+
13+
const dryRun = process.env.DRY_RUN === 'true';
14+
15+
const fixImports = path => {
16+
const content = fs.readFileSync(path, 'utf8');
17+
const imports = content.match(/import (?:type )?{?(?:\s*[a-zA-Z0-9_]*,?\s)*}? ?from ['"]([^'"]*)['"];/gs) || [];
18+
19+
const newImports = imports.map(imp => {
20+
const match = imp.match(/from ['"](.*)['"]/);
21+
const oldPath = match[1];
22+
// remove .ts and .tsx
23+
const newPath = oldPath.replace(/\.tsx?/g, '');
24+
25+
return [imp, imp.replace(oldPath, newPath), oldPath === newPath];
26+
}).filter(([oldPath, newImp, same]) => !same);
27+
28+
if (!newImports.length) {
29+
return;
30+
}
31+
32+
const newContent = content.replace(
33+
new RegExp(newImports.map(([oldPath]) => oldPath).join('|'), 'g'),
34+
match => {
35+
const newImport = newImports.find(([oldPath]) => oldPath === match);
36+
return newImport[1];
37+
}
38+
);
39+
40+
const changesMade = content !== newContent;
41+
42+
const newImportsStr = newImports.map(([oldPath, newPath]) => `Old: \n${oldPath} \nNew: \n${newPath}`).join('\n');
43+
44+
console.log(`\nChanges in ${path}:\n${newImportsStr}\n`);
45+
46+
if (!changesMade) {
47+
return;
48+
}
49+
50+
if (dryRun) {
51+
console.log(`Dry run enabled, skipping writing to ${path}`);
52+
return;
53+
}
54+
55+
if (content !== newContent) {
56+
fs.writeFileSync(path, newContent);
57+
console.log(`Fixed imports in ${path}`);
58+
}
59+
};
60+
61+
const walk = dir => {
62+
const files = fs.readdirSync(dir);
63+
files.forEach(file => {
64+
const path = `${dir}/${file}`;
65+
if (fs.statSync(path).isDirectory()) {
66+
walk(path);
67+
} else {
68+
if (path.endsWith('.ts') || path.endsWith('.tsx')) {
69+
fixImports(path);
70+
}
71+
}
72+
});
73+
74+
if (dir === srcDir) {
75+
console.log('All imports fixed');
76+
}
77+
};
78+
79+
walk(srcDir);

index.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
/**
22
* @format
33
*/
4-
import { name as appName } from '@root/app.json';
4+
import { AppRegistry } from 'react-native';
5+
56
import { decode, encode } from 'base-64';
7+
8+
import App from '@/App';
9+
610
import 'moment/locale/de';
711
import 'moment/locale/en-gb';
8-
9-
import { AppRegistry } from 'react-native';
1012
import 'react-native-get-random-values';
1113
import 'react-native-url-polyfill/auto';
12-
1314
import '@/utils/rsplit';
1415
import '@/translations';
15-
16-
import App from '@/App';
16+
import { name as appName } from '@root/app.json';
1717

1818
if (!global.btoa) {
1919
global.btoa = encode;

licenses.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,12 @@
1717
"licenseUrl": "https://github.com/react-native-async-storage/async-storage/raw/master/LICENSE",
1818
"parents": "opendtu-react-native"
1919
},
20+
"@react-native-clipboard/[email protected]": {
21+
"licenses": "MIT",
22+
"repository": "https://github.com/react-native-clipboard/clipboard",
23+
"licenseUrl": "https://github.com/react-native-clipboard/clipboard/raw/master/LICENSE",
24+
"parents": "opendtu-react-native"
25+
},
2026
"@react-native-community/[email protected]": {
2127
"licenses": "MIT",
2228
"repository": "https://github.com/react-native-netinfo/react-native-netinfo",

0 commit comments

Comments
 (0)