Skip to content

Commit

Permalink
refactor: try/catch部分をPromise.allSettledを利用した形に置き換え
Browse files Browse the repository at this point in the history
  • Loading branch information
yanagi0602 committed Nov 22, 2024
1 parent 276ee27 commit 6f38a85
Showing 1 changed file with 11 additions and 8 deletions.
19 changes: 11 additions & 8 deletions bin/outputExtensionReplace.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,20 +38,23 @@ async function replaceImportsInFile(filePath) {
const importPath = resolve(dirname(filePath), p1 + p2);
const mjsPath = importPath + '.mjs';
const indexPath = join(importPath, 'index.mjs');
try {
await access(mjsPath);
return `from '${p1}${p2}.mjs${p3}`;
} catch {
try {
await access(indexPath);
return await Promise.allSettled([
access(mjsPath),
access(indexPath),
]).then((results) => {
const mjsPathResult = results[0];
const indexPathResult = results[1];
if (mjsPathResult.status === 'fulfilled') {
return `from '${p1}${p2}.mjs${p3}`;
} else if (indexPathResult.status === 'fulfilled') {
return `from '${p1}${p2}/index.mjs${p3}`;
} catch {
} else {
console.error(
`.mjs または /index.mjs が存在しません: ${importPath}`,
);
process.exit(1);
}
}
});
},
);
await writeFile(filePath, result, 'utf8');
Expand Down

0 comments on commit 6f38a85

Please sign in to comment.