Skip to content

Commit 9d970bb

Browse files
committed
1 parent 7511b42 commit 9d970bb

14 files changed

+149
-24
lines changed

.husky/.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
_

.husky/pre-commit

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#!/bin/sh
2+
. "$(dirname "$0")/_/husky.sh"
3+
4+
npm run format && npm test

.prettierignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
**/dist

.prettierrc

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{
22
"tabWidth": 2,
33
"useTabs": false,
4-
"singleQuote": true
4+
"singleQuote": true,
5+
"trailingComma" : "none"
56
}

package.json

+3-1
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,10 @@
1010
"tsc-alias": "dist/bin/index.js"
1111
},
1212
"scripts": {
13+
"prepare": "husky install",
1314
"build:dev": "tsc -w",
1415
"clean": "npm unlink && rimraf dist && tsc && npm link && rimraf package-lock.json",
15-
"release:patch": "npm test && npm version patch",
16+
"release:patch": "npm version patch",
1617
"postversion": "git push && git push --tags && npm publish",
1718
"format": "prettier --write \"**/*.{js,ts}\"",
1819
"test": "npm run clean && jest"
@@ -49,6 +50,7 @@
4950
"@types/jest": "^26.0.20",
5051
"@types/node": "^11.12.0",
5152
"@types/shelljs": "^0.8.8",
53+
"husky": "^5.1.3",
5254
"jest": "^26.6.3",
5355
"prettier": "^2.2.1",
5456
"rimraf": "^3.0.2",

pnpm-lock.yaml

+9
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

projects/project6/package-lock.json

+80
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

projects/project6/package.json

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"private": true,
3+
"scripts": {
4+
"build": "npm i && tsc && tsc-alias",
5+
"start": "npm run build && node ./dist/index.js"
6+
},
7+
"dependencies": {
8+
"@esfx/collections-hashmap": "^1.0.0-pre.17"
9+
}
10+
}

projects/project6/src/index.ts

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { HashMap } from '@esfx/collections-hashmap';
2+
3+
const map = new HashMap();

projects/project6/tsconfig.json

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"compilerOptions": {
3+
"outDir": "dist",
4+
"baseUrl": "./src",
5+
"paths": {
6+
"@/*": [
7+
"./*",
8+
],
9+
},
10+
},
11+
}

src/bin/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,5 @@ program
1818
replaceTscAliasPaths({
1919
configFile: program.project,
2020
watch: !!program.watch,
21-
outDir: program.directory,
21+
outDir: program.directory
2222
});

src/helpers.ts

+6-6
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ export const loadConfig = (file: string): ITSConfig => {
3636
compilerOptions: { baseUrl, outDir, paths } = {
3737
baseUrl: undefined,
3838
outDir: undefined,
39-
paths: undefined,
40-
},
39+
paths: undefined
40+
}
4141
} = FileUtils.toObject(file) as IRawTSConfig;
4242

4343
const config: ITSConfig = {};
@@ -65,7 +65,7 @@ export const loadConfig = (file: string): ITSConfig => {
6565
}
6666
return {
6767
...parentConfig,
68-
...config,
68+
...config
6969
};
7070
}
7171

@@ -80,11 +80,11 @@ export function getProjectDirPathInOutDir(
8080
[
8181
`${outDir}/**/${projectDir}`,
8282
`!${outDir}/**/${projectDir}/**/${projectDir}`,
83-
`!${outDir}/**/node_modules`,
83+
`!${outDir}/**/node_modules`
8484
],
8585
{
8686
dot: true,
87-
onlyDirectories: true,
87+
onlyDirectories: true
8888
}
8989
);
9090

@@ -101,7 +101,7 @@ export function existsResolvedAlias(path: string): boolean {
101101
const globPattern = [`${path}.{js,jsx}`];
102102
const files = sync(globPattern, {
103103
dot: true,
104-
onlyFiles: true,
104+
onlyFiles: true
105105
});
106106

107107
if (files.length) return true;

src/index.ts

+16-13
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ export function replaceTscAliasPaths(
2424
outDir?: string;
2525
watch?: boolean;
2626
} = {
27-
watch: false,
28-
}
27+
watch: false
28+
}
2929
) {
3030
Output.info('=== tsc-alias starting ===');
3131
if (!options.configFile) {
@@ -120,7 +120,7 @@ export function replaceTscAliasPaths(
120120
basePath,
121121
path,
122122
paths: _paths,
123-
isExtra,
123+
isExtra
124124
};
125125
})
126126
.filter(({ prefix }) => prefix);
@@ -130,9 +130,10 @@ export function replaceTscAliasPaths(
130130
if (normalize(alias.path).includes('..')) {
131131
const tempBasePath = normalizePath(
132132
normalize(
133-
`${configDir}/${outDir}/${hasExtraModule && relConfDirPathInOutPath
134-
? relConfDirPathInOutPath
135-
: ''
133+
`${configDir}/${outDir}/${
134+
hasExtraModule && relConfDirPathInOutPath
135+
? relConfDirPathInOutPath
136+
: ''
136137
}/${baseUrl}`
137138
)
138139
);
@@ -166,15 +167,17 @@ export function replaceTscAliasPaths(
166167
const replaceImportStatement = ({
167168
orig,
168169
file,
169-
alias,
170+
alias
170171
}: {
171172
orig: string;
172173
file: string;
173174
alias: typeof aliases[0];
174175
}): string => {
175176
const requiredModule = orig.split(/"|'/)[1];
176177
const index = orig.indexOf(alias.prefix);
177-
const isAlias = requiredModule.startsWith(alias.prefix);
178+
const isAlias = requiredModule.includes('/')
179+
? requiredModule.startsWith(alias.prefix + '/')
180+
: requiredModule.startsWith(alias.prefix);
178181
if (index > -1 && isAlias) {
179182
let absoluteAliasPath = getAbsoluteAliasPath(alias.basePath, alias.path);
180183
let relativeAliasPath: string = normalizePath(
@@ -201,19 +204,19 @@ export function replaceTscAliasPaths(
201204
for (const alias of aliases) {
202205
const replacementParams = {
203206
file,
204-
alias,
207+
alias
205208
};
206209
tempCode = tempCode
207210
.replace(requireRegex, (orig) =>
208211
replaceImportStatement({
209212
orig,
210-
...replacementParams,
213+
...replacementParams
211214
})
212215
)
213216
.replace(importRegex, (orig) =>
214217
replaceImportStatement({
215218
orig,
216-
...replacementParams,
219+
...replacementParams
217220
})
218221
);
219222
}
@@ -227,11 +230,11 @@ export function replaceTscAliasPaths(
227230
// Finding files and changing alias paths
228231
const globPattern = [
229232
`${outPath}/**/*.{js,jsx,ts,tsx}`,
230-
`!${outPath}/**/node_modules`,
233+
`!${outPath}/**/node_modules`
231234
];
232235
const files = sync(globPattern, {
233236
dot: true,
234-
onlyFiles: true,
237+
onlyFiles: true
235238
});
236239

237240
const flen = files.length;

tests/test.spec.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@ import * as shell from 'shelljs';
33

44
const projectsRoot = join(__dirname, '../projects');
55

6-
[1, 3, 4, 5].forEach((value) => {
6+
[1, 3, 4, 5, 6].forEach((value) => {
77
it(`Project ${value}`, () => {
88
const { code, stderr } = shell.exec('npm start', {
99
cwd: join(projectsRoot, `project${value}`),
10-
silent: true,
10+
silent: true
1111
});
1212
if (code !== 0) console.error(stderr);
1313
expect(code).toEqual(0);

0 commit comments

Comments
 (0)