Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Support generating .cjs files #69

Merged
merged 1 commit into from
Aug 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .eslintignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
fixtures/*/lib/**
fixtures/*/lib*/**
fixtures/*/out/**
*.d.ts
*.d.mts
2 changes: 1 addition & 1 deletion .prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ coverage
cspell.json
**/*.schema.json
/fixtures/**/out
/fixtures/**/lib
/fixtures/**/lib*
temp

.release-please-manifest.json
19 changes: 19 additions & 0 deletions fixtures/sample/lib-cjs/app.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.run = void 0;
const chalk_1 = __importDefault(require("chalk"));
const lookup_js_1 = require("./lookup.js");
async function run() {
const guid = 'GUID';
const person = await (0, lookup_js_1.lookUpPerson)('GUID');
if (!person) {
console.log(chalk_1.default.red(`Person ${chalk_1.default.yellow(guid)} not found.`));
return;
}
console.log(`Found: ${person.name}`);
}
exports.run = run;
//# sourceMappingURL=app.js.map
2 changes: 2 additions & 0 deletions fixtures/sample/lib-cjs/app.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export declare function run(): Promise<void>;
//# sourceMappingURL=app.d.ts.map
1 change: 1 addition & 0 deletions fixtures/sample/lib-cjs/app.d.ts.map

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

19 changes: 19 additions & 0 deletions fixtures/sample/lib-cjs/app.js

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

1 change: 1 addition & 0 deletions fixtures/sample/lib-cjs/app.js.map

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

3 changes: 3 additions & 0 deletions fixtures/sample/lib-cjs/database/fetch.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { Entity, GUID } from '../types.js';
export declare function fetchEntity<T extends Entity>(guid: GUID): Promise<T | undefined>;
//# sourceMappingURL=fetch.d.ts.map
1 change: 1 addition & 0 deletions fixtures/sample/lib-cjs/database/fetch.d.ts.map

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

9 changes: 9 additions & 0 deletions fixtures/sample/lib-cjs/database/fetch.js

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

1 change: 1 addition & 0 deletions fixtures/sample/lib-cjs/database/fetch.js.map

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

2 changes: 2 additions & 0 deletions fixtures/sample/lib-cjs/database/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export { fetchEntity } from './fetch.js';
//# sourceMappingURL=index.d.ts.map
1 change: 1 addition & 0 deletions fixtures/sample/lib-cjs/database/index.d.ts.map

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

6 changes: 6 additions & 0 deletions fixtures/sample/lib-cjs/database/index.js

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

1 change: 1 addition & 0 deletions fixtures/sample/lib-cjs/database/index.js.map

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

4 changes: 4 additions & 0 deletions fixtures/sample/lib-cjs/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export { PrimeNumber, Tuple, GUID, AddressGuid, Address, PhoneNumberGuid, PhoneNumber, PersonGuid, Person, Annotation, } from './types.js';
export { lookUpPerson } from './lookup.js';
export { fetchEntity } from './database/index.js';
//# sourceMappingURL=index.d.ts.map
1 change: 1 addition & 0 deletions fixtures/sample/lib-cjs/index.d.ts.map

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

8 changes: 8 additions & 0 deletions fixtures/sample/lib-cjs/index.js

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

1 change: 1 addition & 0 deletions fixtures/sample/lib-cjs/index.js.map

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

3 changes: 3 additions & 0 deletions fixtures/sample/lib-cjs/lookup.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { Person, PersonGuid } from './types.js';
export declare function lookUpPerson(_guid: PersonGuid): Promise<Person | undefined>;
//# sourceMappingURL=lookup.d.ts.map
1 change: 1 addition & 0 deletions fixtures/sample/lib-cjs/lookup.d.ts.map

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

8 changes: 8 additions & 0 deletions fixtures/sample/lib-cjs/lookup.js

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

1 change: 1 addition & 0 deletions fixtures/sample/lib-cjs/lookup.js.map

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

44 changes: 44 additions & 0 deletions fixtures/sample/lib-cjs/types.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
export type PrimeNumber = number;
export type Tuple<T, U> = [T, U];
export type GUID = string;
export type AddressGuid = GUID;
export interface EntityBase<T extends string> {
guid: GUID;
type: T;
}
export interface Entity {
guid: GUID;
type: string;
}
export interface Address extends EntityBase<'Address'> {
guid: AddressGuid;
name: string;
street: string;
city: string;
postalCode: string;
country: string;
state?: string;
provence?: string;
annotations?: Annotation[];
}
export type PhoneNumberGuid = GUID;
export interface PhoneNumber extends EntityBase<'PhoneNumber'> {
guid: PhoneNumberGuid;
name: string;
number: string;
annotations?: Annotation[];
}
export type PersonGuid = GUID;
export interface Person extends EntityBase<'Person'> {
guid: PersonGuid;
name: string;
aliases?: string[];
dob?: string;
addresses?: Address[];
notes?: Annotation[];
}
export interface Annotation extends EntityBase<'Annotation'> {
note: string;
madeBy: PersonGuid;
}
//# sourceMappingURL=types.d.ts.map
1 change: 1 addition & 0 deletions fixtures/sample/lib-cjs/types.d.ts.map

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

3 changes: 3 additions & 0 deletions fixtures/sample/lib-cjs/types.js

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

1 change: 1 addition & 0 deletions fixtures/sample/lib-cjs/types.js.map

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

15 changes: 15 additions & 0 deletions fixtures/sample/tsconfig.cjs.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"$schema": "https://json.schemastore.org/tsconfig",
"extends": "@tsconfig/node16/tsconfig.json",
"compilerOptions": {
"declaration": true,
"declarationMap": true,
"module": "CommonJS",
"moduleResolution": "Node",
"newLine": "LF",
"outDir": "lib-cjs",
"sourceMap": true,
"types": ["node"]
},
"include": ["src"]
}
4 changes: 2 additions & 2 deletions fixtures/sample/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
"compilerOptions": {
"declaration": true,
"declarationMap": true,
"module": "ES2020",
"moduleResolution": "node16",
"module": "Node16",
"moduleResolution": "Node16",
"newLine": "LF",
"outDir": "lib",
"sourceMap": true,
Expand Down
41 changes: 40 additions & 1 deletion src/__snapshots__/app.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,43 @@ exports[`app > run (--dry-run) [ Array(3) ] 2`] = `
exports[`app > run (actual) [ '.', '--root=fixtures/sample' ] 1`] = `
"Console Output:
[log]: done.
[log]: fixtures/sample/lib-cjs/app.cjs - copy
[log]: fixtures/sample/lib-cjs/app.d.ts -> temp/lib-cjs/app.d.mts Generated
[log]: fixtures/sample/lib-cjs/app.d.ts.map - copy
[log]: fixtures/sample/lib-cjs/app.d.ts.map -> temp/lib-cjs/app.d.mts.map Generated
[log]: fixtures/sample/lib-cjs/app.js -> temp/lib-cjs/app.mjs Generated
[log]: fixtures/sample/lib-cjs/app.js.map - copy
[log]: fixtures/sample/lib-cjs/app.js.map -> temp/lib-cjs/app.mjs.map Generated
[log]: fixtures/sample/lib-cjs/database/fetch.d.ts -> temp/lib-cjs/database/fetch.d.mts Generated
[log]: fixtures/sample/lib-cjs/database/fetch.d.ts.map - copy
[log]: fixtures/sample/lib-cjs/database/fetch.d.ts.map -> temp/lib-cjs/database/fetch.d.mts.map Generated
[log]: fixtures/sample/lib-cjs/database/fetch.js -> temp/lib-cjs/database/fetch.mjs Generated
[log]: fixtures/sample/lib-cjs/database/fetch.js.map - copy
[log]: fixtures/sample/lib-cjs/database/fetch.js.map -> temp/lib-cjs/database/fetch.mjs.map Generated
[log]: fixtures/sample/lib-cjs/database/index.d.ts -> temp/lib-cjs/database/index.d.mts Generated
[log]: fixtures/sample/lib-cjs/database/index.d.ts.map - copy
[log]: fixtures/sample/lib-cjs/database/index.d.ts.map -> temp/lib-cjs/database/index.d.mts.map Generated
[log]: fixtures/sample/lib-cjs/database/index.js -> temp/lib-cjs/database/index.mjs Generated
[log]: fixtures/sample/lib-cjs/database/index.js.map - copy
[log]: fixtures/sample/lib-cjs/database/index.js.map -> temp/lib-cjs/database/index.mjs.map Generated
[log]: fixtures/sample/lib-cjs/index.d.ts -> temp/lib-cjs/index.d.mts Generated
[log]: fixtures/sample/lib-cjs/index.d.ts.map - copy
[log]: fixtures/sample/lib-cjs/index.d.ts.map -> temp/lib-cjs/index.d.mts.map Generated
[log]: fixtures/sample/lib-cjs/index.js -> temp/lib-cjs/index.mjs Generated
[log]: fixtures/sample/lib-cjs/index.js.map - copy
[log]: fixtures/sample/lib-cjs/index.js.map -> temp/lib-cjs/index.mjs.map Generated
[log]: fixtures/sample/lib-cjs/lookup.d.ts -> temp/lib-cjs/lookup.d.mts Generated
[log]: fixtures/sample/lib-cjs/lookup.d.ts.map - copy
[log]: fixtures/sample/lib-cjs/lookup.d.ts.map -> temp/lib-cjs/lookup.d.mts.map Generated
[log]: fixtures/sample/lib-cjs/lookup.js -> temp/lib-cjs/lookup.mjs Generated
[log]: fixtures/sample/lib-cjs/lookup.js.map - copy
[log]: fixtures/sample/lib-cjs/lookup.js.map -> temp/lib-cjs/lookup.mjs.map Generated
[log]: fixtures/sample/lib-cjs/types.d.ts -> temp/lib-cjs/types.d.mts Generated
[log]: fixtures/sample/lib-cjs/types.d.ts.map - copy
[log]: fixtures/sample/lib-cjs/types.d.ts.map -> temp/lib-cjs/types.d.mts.map Generated
[log]: fixtures/sample/lib-cjs/types.js -> temp/lib-cjs/types.mjs Generated
[log]: fixtures/sample/lib-cjs/types.js.map - copy
[log]: fixtures/sample/lib-cjs/types.js.map -> temp/lib-cjs/types.mjs.map Generated
[log]: fixtures/sample/lib/app.d.ts -> temp/lib/app.d.mts Generated
[log]: fixtures/sample/lib/app.d.ts.map - copy
[log]: fixtures/sample/lib/app.d.ts.map -> temp/lib/app.d.mts.map Generated
Expand Down Expand Up @@ -263,6 +300,7 @@ exports[`app > run (actual) [ '.', '--root=fixtures/sample' ] 1`] = `
[log]: fixtures/sample/src/index.ts - copy
[log]: fixtures/sample/src/lookup.ts - copy
[log]: fixtures/sample/src/types.ts - copy
[log]: fixtures/sample/tsconfig.cjs.json - copy
[log]: fixtures/sample/tsconfig.json - copy
[warn]:
[error]:
Expand All @@ -289,12 +327,13 @@ exports[`app > run error '--help' 1`] = `
[error]:
[stdout]: Usage: ts2mjs [options] <files...>
[stdout]:
[stdout]: Rename ESM .js files to .mjs
[stdout]: Rename ESM .js files to .mjs (or .cjs if --cjs options is used)
[stdout]:
[stdout]: Arguments:
[stdout]: files The files to rename.
[stdout]:
[stdout]: Options:
[stdout]: --cjs Files are renamed to .cjs.
[stdout]: -o, --output <dir> The output directory.
[stdout]: --cwd <dir> The current working directory.
[stdout]: --root <dir> The root directory.
Expand Down
13 changes: 13 additions & 0 deletions src/__snapshots__/processFile.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,25 @@ export { fetchEntity } from './database/index.mjs';
//# sourceMappingURL=index.d.mts.map"
`;

exports[`processFile > processFile 'sample/lib/index.d.ts' 2`] = `
"export { PrimeNumber, Tuple, GUID, AddressGuid, Address, PhoneNumberGuid, PhoneNumber, PersonGuid, Person, Annotation, } from './types.mjs';
export { lookUpPerson } from './lookup.mjs';
export { fetchEntity } from './database/index.mjs';
//# sourceMappingURL=index.d.cts.map"
`;

exports[`processFile > processFile 'sample/lib/index.js' 1`] = `
"export { lookUpPerson } from './lookup.mjs';
export { fetchEntity } from './database/index.mjs';
//# sourceMappingURL=index.mjs.map"
`;

exports[`processFile > processFile 'sample/lib/index.js' 2`] = `
"export { lookUpPerson } from './lookup.mjs';
export { fetchEntity } from './database/index.mjs';
//# sourceMappingURL=index.cjs.map"
`;

exports[`processFile > processFile allowJsOutsideOfRoot 'sample/lib/database/fetch.d.ts' 1`] = `
"import { Entity, GUID } from '../../../../../fixtures/sample/lib/types.js';
export declare function fetchEntity<T extends Entity>(guid: GUID): Promise<T | undefined>;
Expand Down
5 changes: 4 additions & 1 deletion src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ interface CliOptions {
dryRun?: boolean;
verbose?: boolean;
enforceRoot?: boolean;
cjs?: boolean;
}

export interface AppLogger {
Expand All @@ -66,8 +67,9 @@ export async function app(program = defaultCommand, logger?: AppLogger): Promise

program
.name((await getName()) || 'ts2mjs')
.description('Rename ESM .js files to .mjs')
.description('Rename ESM .js files to .mjs (or .cjs if --cjs options is used)')
.argument('<files...>', 'The files to rename.')
.option('--cjs', 'Files are renamed to .cjs.')
.option('-o, --output <dir>', 'The output directory.')
.option('--cwd <dir>', 'The current working directory.')
.option('--root <dir>', 'The root directory.')
Expand Down Expand Up @@ -101,6 +103,7 @@ export async function app(program = defaultCommand, logger?: AppLogger): Promise
cwd: optionsCli.cwd,
dryRun: optionsCli.dryRun || false,
output: optionsCli.output,
cjs: optionsCli.cjs,
progress: log,
warning,
root: optionsCli.root,
Expand Down
Loading
Loading