-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
build: 出力されたmjsの内部参照で拡張子を指定するようにアップデート (#1178)
* build: 出力されたmjsの内部参照で拡張子を指定するようにアップデート * ci: extensionReplaceをbuildのworkflowへ追加 * test: outputExtensionReplaceのテストコードを追加 * refactor: getMjsFilesの削除 * chore: main関数内でのreplaceImportsInFile実行方法を修正 * refactor: try/catch部分をPromise.allSettledを利用した形に置き換え * feat: jscodeshiftを利用した置き換えに修正 * chore: 実行コマンドの軽微な修正 * feat: ターゲットとなったファイルと処理を出力するように修正 * refactor: checkFileExistsのタイミングを変更 * test: test.eachを利用したテスト方法へ変更 * chore: lintのルールをアップデート * chore: lockファイルの修正 --------- Co-authored-by: re-taro <[email protected]>
- Loading branch information
1 parent
b85a9e4
commit 0eef78d
Showing
6 changed files
with
441 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,7 +17,8 @@ | |
}, | ||
"env": { | ||
"browser": true, | ||
"node": true | ||
"node": true, | ||
"jest": true | ||
}, | ||
"overrides": [ | ||
{ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
// eslint-disable-next-line @typescript-eslint/no-var-requires | ||
const { access } = require('fs').promises; | ||
const { dirname, join, resolve } = require('path'); | ||
|
||
async function checkFileExists(filePath) { | ||
try { | ||
await access(filePath); | ||
return true; | ||
} catch { | ||
return false; | ||
} | ||
} | ||
|
||
const shouldReplaceExtension = (value) => | ||
(value.startsWith('./') || value.startsWith('../')) && | ||
!value.endsWith('.mjs'); | ||
|
||
/** | ||
* | ||
* @param {import('jscodeshift').FileInfo} file | ||
* @param {import('jscodeshift').API} api | ||
*/ | ||
module.exports = async function transformer(file, api) { | ||
const j = api.jscodeshift; | ||
const root = j(file.source); | ||
await Promise.all( | ||
[ | ||
j.ImportDeclaration, | ||
j.ExportNamedDeclaration, | ||
j.ExportAllDeclaration, | ||
j.ExportDefaultDeclaration, | ||
] | ||
.flatMap((declaration) => root.find(declaration).nodes()) | ||
.flatMap(async (node) => { | ||
const source = node.source; | ||
if (!source) { | ||
return; | ||
} | ||
const value = source.value; | ||
|
||
if (shouldReplaceExtension(value)) { | ||
const importPath = resolve(dirname(file.path), value); | ||
const mjsPath = `${importPath}.mjs`; | ||
const indexPath = join(importPath, 'index.mjs'); | ||
|
||
if (await checkFileExists(mjsPath)) { | ||
node.source.value = `${value}.mjs`; | ||
} else if (await checkFileExists(indexPath)) { | ||
node.source.value = `${value}/index.mjs`; | ||
} else { | ||
console.error( | ||
`missing ${mjsPath} and ${indexPath} for ${file.path}`, | ||
); | ||
return; | ||
} | ||
console.info( | ||
`change ${value} to ${node.source.value} in ${file.path}`, | ||
); | ||
} | ||
}), | ||
); | ||
|
||
return root.toSource(); | ||
}; | ||
|
||
module.exports.checkFileExists = checkFileExists; | ||
module.exports.shouldReplaceExtension = shouldReplaceExtension; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
const { | ||
checkFileExists, | ||
shouldReplaceExtension, | ||
// eslint-disable-next-line @typescript-eslint/no-var-requires | ||
} = require('./outputExtensionReplace'); | ||
|
||
describe('checkFileExists', () => { | ||
it('should return true if the file exists', async () => { | ||
const result = await checkFileExists(__filename); | ||
expect(result).toBe(true); | ||
}); | ||
|
||
it('should return false if the file does not exist', async () => { | ||
const filePath = 'non_existent_file.dummy'; | ||
const result = await checkFileExists(filePath); | ||
expect(result).toBe(false); | ||
}); | ||
}); | ||
|
||
describe('shouldReplaceExtension', () => { | ||
test.each([ | ||
['./foo', true], | ||
['../foo', true], | ||
['./foo/bar', true], | ||
['../foo/bar', true], | ||
['./foo.mjs', false], | ||
['../foo.mjs', false], | ||
['./foo/bar.mjs', false], | ||
['../foo/bar.mjs', false], | ||
['react', false], | ||
])('should return %p if the value is %p', (value, expected) => { | ||
const result = shouldReplaceExtension(value); | ||
expect(result).toBe(expected); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.