Skip to content

Commit

Permalink
Merge pull request #37 from nlibjs/d-ts
Browse files Browse the repository at this point in the history
feat: rename .d.ts files
  • Loading branch information
gjbkz committed Jul 28, 2022
2 parents 9bd1e5b + e8b1d2f commit 0a0c8d1
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/esmify.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,23 @@ test('rename .js files', async (t) => {
});
});

test('rename .d.ts files', async (t) => {
const directory = await createTestDirectory();
await deployFiles(directory, {
'foo.js': 'console.info(123)',
'foo.d.ts': 'console.info(123)',
'bar.js': 'console.info(456)',
'bar.d.ts': 'console.info(456)',
});
await execute(directory, path.join(directory, '**'));
t.deepEqual(await readFiles(directory), {
'foo.mjs': 'console.info(123)',
'foo.d.mts': 'console.info(123)',
'bar.mjs': 'console.info(456)',
'bar.d.mts': 'console.info(456)',
});
});

test('sync import/export sources', async (t) => {
const directory = await createTestDirectory();
await deployFiles(directory, {
Expand Down
13 changes: 13 additions & 0 deletions src/esmify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ export const esmify = async (
}
console.info(`esmify:writing ${renames.size} files`);
for (const [absoluteFilePath, renamed] of renames) {
if (absoluteFilePath.endsWith('.js')) {
await renameTypeFile(absoluteFilePath);
}
await fs.writeFile(renamed.path, renamed.code);
if (absoluteFilePath !== renamed.path) {
await fs.unlink(absoluteFilePath);
Expand All @@ -55,6 +58,16 @@ export const esmify = async (
}
};

const renameTypeFile = async (absoluteFilePath: string) => {
const typeFilePath = `${absoluteFilePath.slice(0, -3)}.d.ts`;
const renamedTypeFilePath = `${absoluteFilePath.slice(0, -3)}.d.mts`;
await fs.rename(typeFilePath, renamedTypeFilePath).catch((error) => {
if (!isRecord(error) || error.code !== 'ENOENT') {
throw error;
}
});
};

const getRenameMapping = async (patterns: Array<string>, cwd: string) => {
const renames = new Map<string, {path: string, code: string}>();
const targetExtensions = ['.js', '.mjs', '.cjs'];
Expand Down

0 comments on commit 0a0c8d1

Please sign in to comment.