Skip to content

Commit

Permalink
feat: esmify
Browse files Browse the repository at this point in the history
  • Loading branch information
gjbkz committed May 5, 2022
1 parent dcc39d2 commit 0429e51
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 53 deletions.
22 changes: 13 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
# @nlib/ts-to-esm
# @nlib/esmify

[![Test](https://github.com/nlibjs/ts-to-esm/actions/workflows/test.yml/badge.svg)](https://github.com/nlibjs/ts-to-esm/actions/workflows/test.yml)
[![codecov](https://codecov.io/gh/nlibjs/ts-to-esm/branch/master/graph/badge.svg)](https://codecov.io/gh/nlibjs/ts-to-esm)
[![Test](https://github.com/nlibjs/esmify/actions/workflows/test.yml/badge.svg)](https://github.com/nlibjs/esmify/actions/workflows/test.yml)
[![codecov](https://codecov.io/gh/nlibjs/esmify/branch/master/graph/badge.svg)](https://codecov.io/gh/nlibjs/esmify)

A command line tool to change file extensions.
A command line tool converts tsc output to ESM modules.

## Usage

```
npx @nlib/ts-to-esm --directory path/to/dir --mapping js/cjs [--mapping mjs/js]
Usage: @nlib/esmify [options] <patterns...>
--directory, -d [string] A directory replaceExt processes
--mapping, -m [string] Specify a mapping in the format from/to (e.g. js/mjs).
--help, -h Show help
--version, -v Output the version number
Arguments:
patterns File patterns passed to fast-glob
Options:
--cwd <cwd> A path to the directory passed to fast-glob.
--keepSourceMap If it exists, esmify won't remove sourcemaps.
-V, --version output the version number
-h, --help display help for command
```
41 changes: 41 additions & 0 deletions bin/esmify.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#!/usr/bin/env node
import {Command} from 'commander';
import * as console from 'console';
import * as fs from 'fs';

/** @type {{name: string, version: string, description: string}} */
const packageJson = JSON.parse(fs.readFileSync(new URL('../package.json', import.meta.url), 'utf-8'));
const program = new Command();
program.name(packageJson.name);
program.description(packageJson.description);
program.option('--cwd <cwd>', 'A path to the directory passed to fast-glob.');
program.option('--keepSourceMap', 'If it exists, esmify won\'t remove sourcemaps.');
program.argument('<patterns...>', 'File patterns passed to fast-glob');
program.version(packageJson.version);
program.action(
/** @param {Array<string>} patterns */
async (patterns, options) => {
const extensions = ['.mjs', '.js'];
while (0 < extensions.length) {
const extension = extensions.shift();
const file = new URL(`../lib/esmify${extension}`, import.meta.url);
const stats = await fs.promises.stat(file).catch((error) => {
if (error && error.code === 'ENOENT') {
return null;
}
throw error;
});
if ((stats && stats.isFile()) || extensions.length === 0) {
/** @type {{esmify: (...patterns: Array<string>) => Promise<void>}} */
const {esmify} = await import(file);
await esmify(patterns, options);
return;
}
}
},
);
program.parseAsync()
.catch((error) => {
console.error(error);
process.exit(1);
});
27 changes: 0 additions & 27 deletions bin/tsToEsm.mjs

This file was deleted.

8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 8 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "@nlib/ts-to-esm",
"name": "@nlib/esmify",
"publishConfig": {
"access": "public"
},
Expand All @@ -10,29 +10,29 @@
"email": "kei.itof@gmail.com",
"url": "https://github.com/kei-ito"
},
"homepage": "https://github.com/nlibjs/ts-to-esm",
"repository": "https://github.com/nlibjs/ts-to-esm",
"homepage": "https://github.com/nlibjs/esmify",
"repository": "https://github.com/nlibjs/esmify",
"engines": {
"node": ">=14"
},
"type": "module",
"main": "./lib/tsToEsm.mjs",
"main": "./lib/esmify.mjs",
"files": [
"bin",
"lib"
],
"bin": {
"ts-to-esm": "./bin/tsToEsm.mjs",
"nlib-ts-to-esm": "./bin/tsToEsm.mjs"
"esmify": "./bin/esmify.mjs",
"nlib-esmify": "./bin/esmify.mjs"
},
"scripts": {
"build": "run-s build:*",
"build:tsc": "tsc",
"build:tsToEsm": "node bin/tsToEsm.mjs 'lib/**'",
"build:esmify": "node bin/esmify.mjs 'lib/**'",
"test": "run-s test:*",
"test:build": "run-s test:build:*",
"test:build:tsc": "tsc",
"test:build:tsToEsm": "node bin/tsToEsm.mjs --keepSourceMap 'lib/**'",
"test:build:esmify": "node bin/esmify.mjs --keepSourceMap 'lib/**'",
"test:unit": "ava",
"lint": "eslint --ext .ts src",
"version": "run-s version:changelog version:add",
Expand Down
4 changes: 2 additions & 2 deletions src/tsToEsm.test.ts → src/esmify.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ import * as os from 'os';
import * as path from 'path';

const require = createRequire(import.meta.url);
const cliFilePath = require.resolve('../bin/tsToEsm.mjs');
const cliFilePath = require.resolve('../bin/esmify.mjs');

type Files = Record<string, string>;

const createTestDirectory = async () => await fs.mkdtemp(path.join(os.tmpdir(), 'tsToEsm-'));
const createTestDirectory = async () => await fs.mkdtemp(path.join(os.tmpdir(), 'esmify-'));
const deployFiles = async (directory: string, files: Files) => {
for (const [relativePath, body] of Object.entries(files)) {
const dest = path.join(directory, relativePath);
Expand Down
6 changes: 3 additions & 3 deletions src/tsToEsm.ts → src/esmify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,19 @@ import * as path from 'path';
interface Options {
/** (default: `process.cwd()`) A path to the directory passed to fast-glob. */
cwd?: string,
/** (default: `false`) If true, tsToEsm won't remove sourcemaps. */
/** (default: `false`) If true, esmify won't remove sourcemaps. */
keepSourceMap?: boolean,
}

export const tsToEsm = async (
export const esmify = async (
patterns: Array<string>,
{cwd = process.cwd(), keepSourceMap = false}: Options = {},
) => {
const renames = await getRenameMapping(patterns, cwd);
const sourceMapFiles = new Set<string>();
for (const [absoluteFilePath, renamed] of renames) {
const baseDir = path.dirname(absoluteFilePath);
console.info(`tsToEsm:parsing:${absoluteFilePath}`);
console.info(`esmify:parsing:${absoluteFilePath}`);
for (const node of parseCode(renamed.code)) {
if ('comment' in node) {
if (!keepSourceMap) {
Expand Down

0 comments on commit 0429e51

Please sign in to comment.