diff --git a/.eslintignore b/.eslintignore index 4e2a0d2..b06261c 100644 --- a/.eslintignore +++ b/.eslintignore @@ -1,4 +1,4 @@ -fixtures/*/lib/** +fixtures/*/lib*/** fixtures/*/out/** *.d.ts *.d.mts diff --git a/.prettierignore b/.prettierignore index bef0fa9..7f00852 100644 --- a/.prettierignore +++ b/.prettierignore @@ -9,7 +9,7 @@ coverage cspell.json **/*.schema.json /fixtures/**/out -/fixtures/**/lib +/fixtures/**/lib* temp .release-please-manifest.json diff --git a/fixtures/sample/lib-cjs/app.cjs b/fixtures/sample/lib-cjs/app.cjs new file mode 100644 index 0000000..fe8e7df --- /dev/null +++ b/fixtures/sample/lib-cjs/app.cjs @@ -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 \ No newline at end of file diff --git a/fixtures/sample/lib-cjs/app.d.ts b/fixtures/sample/lib-cjs/app.d.ts new file mode 100644 index 0000000..9b35cb2 --- /dev/null +++ b/fixtures/sample/lib-cjs/app.d.ts @@ -0,0 +1,2 @@ +export declare function run(): Promise; +//# sourceMappingURL=app.d.ts.map \ No newline at end of file diff --git a/fixtures/sample/lib-cjs/app.d.ts.map b/fixtures/sample/lib-cjs/app.d.ts.map new file mode 100644 index 0000000..73d13c7 --- /dev/null +++ b/fixtures/sample/lib-cjs/app.d.ts.map @@ -0,0 +1 @@ +{"version":3,"file":"app.d.ts","sourceRoot":"","sources":["../src/app.ts"],"names":[],"mappings":"AAIA,wBAAsB,GAAG,kBAUxB"} \ No newline at end of file diff --git a/fixtures/sample/lib-cjs/app.js b/fixtures/sample/lib-cjs/app.js new file mode 100644 index 0000000..fe8e7df --- /dev/null +++ b/fixtures/sample/lib-cjs/app.js @@ -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 \ No newline at end of file diff --git a/fixtures/sample/lib-cjs/app.js.map b/fixtures/sample/lib-cjs/app.js.map new file mode 100644 index 0000000..c925511 --- /dev/null +++ b/fixtures/sample/lib-cjs/app.js.map @@ -0,0 +1 @@ +{"version":3,"file":"app.js","sourceRoot":"","sources":["../src/app.ts"],"names":[],"mappings":";;;;;;AAAA,kDAA0B;AAE1B,2CAA2C;AAEpC,KAAK,UAAU,GAAG;IACrB,MAAM,IAAI,GAAG,MAAM,CAAC;IACpB,MAAM,MAAM,GAAG,MAAM,IAAA,wBAAY,EAAC,MAAM,CAAC,CAAC;IAE1C,IAAI,CAAC,MAAM,EAAE;QACT,OAAO,CAAC,GAAG,CAAC,eAAK,CAAC,GAAG,CAAC,UAAU,eAAK,CAAC,MAAM,CAAC,IAAI,CAAC,aAAa,CAAC,CAAC,CAAC;QAClE,OAAO;KACV;IAED,OAAO,CAAC,GAAG,CAAC,UAAU,MAAM,CAAC,IAAI,EAAE,CAAC,CAAC;AACzC,CAAC;AAVD,kBAUC"} \ No newline at end of file diff --git a/fixtures/sample/lib-cjs/database/fetch.d.ts b/fixtures/sample/lib-cjs/database/fetch.d.ts new file mode 100644 index 0000000..979ea89 --- /dev/null +++ b/fixtures/sample/lib-cjs/database/fetch.d.ts @@ -0,0 +1,3 @@ +import { Entity, GUID } from '../types.js'; +export declare function fetchEntity(guid: GUID): Promise; +//# sourceMappingURL=fetch.d.ts.map \ No newline at end of file diff --git a/fixtures/sample/lib-cjs/database/fetch.d.ts.map b/fixtures/sample/lib-cjs/database/fetch.d.ts.map new file mode 100644 index 0000000..25fb6df --- /dev/null +++ b/fixtures/sample/lib-cjs/database/fetch.d.ts.map @@ -0,0 +1 @@ +{"version":3,"file":"fetch.d.ts","sourceRoot":"","sources":["../../src/database/fetch.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,aAAa,CAAC;AAE3C,wBAAsB,WAAW,CAAC,CAAC,SAAS,MAAM,EAAE,IAAI,EAAE,IAAI,GAAG,OAAO,CAAC,CAAC,GAAG,SAAS,CAAC,CAGtF"} \ No newline at end of file diff --git a/fixtures/sample/lib-cjs/database/fetch.js b/fixtures/sample/lib-cjs/database/fetch.js new file mode 100644 index 0000000..643bc01 --- /dev/null +++ b/fixtures/sample/lib-cjs/database/fetch.js @@ -0,0 +1,9 @@ +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +exports.fetchEntity = void 0; +async function fetchEntity(guid) { + console.log(`Fetch Entity ${guid}`); + return undefined; +} +exports.fetchEntity = fetchEntity; +//# sourceMappingURL=fetch.js.map \ No newline at end of file diff --git a/fixtures/sample/lib-cjs/database/fetch.js.map b/fixtures/sample/lib-cjs/database/fetch.js.map new file mode 100644 index 0000000..bc45539 --- /dev/null +++ b/fixtures/sample/lib-cjs/database/fetch.js.map @@ -0,0 +1 @@ +{"version":3,"file":"fetch.js","sourceRoot":"","sources":["../../src/database/fetch.ts"],"names":[],"mappings":";;;AAEO,KAAK,UAAU,WAAW,CAAmB,IAAU;IAC1D,OAAO,CAAC,GAAG,CAAC,gBAAgB,IAAI,EAAE,CAAC,CAAC;IACpC,OAAO,SAAS,CAAC;AACrB,CAAC;AAHD,kCAGC"} \ No newline at end of file diff --git a/fixtures/sample/lib-cjs/database/index.d.ts b/fixtures/sample/lib-cjs/database/index.d.ts new file mode 100644 index 0000000..2f6fd33 --- /dev/null +++ b/fixtures/sample/lib-cjs/database/index.d.ts @@ -0,0 +1,2 @@ +export { fetchEntity } from './fetch.js'; +//# sourceMappingURL=index.d.ts.map \ No newline at end of file diff --git a/fixtures/sample/lib-cjs/database/index.d.ts.map b/fixtures/sample/lib-cjs/database/index.d.ts.map new file mode 100644 index 0000000..0ad7177 --- /dev/null +++ b/fixtures/sample/lib-cjs/database/index.d.ts.map @@ -0,0 +1 @@ +{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/database/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,YAAY,CAAC"} \ No newline at end of file diff --git a/fixtures/sample/lib-cjs/database/index.js b/fixtures/sample/lib-cjs/database/index.js new file mode 100644 index 0000000..fca8299 --- /dev/null +++ b/fixtures/sample/lib-cjs/database/index.js @@ -0,0 +1,6 @@ +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +exports.fetchEntity = void 0; +var fetch_js_1 = require("./fetch.js"); +Object.defineProperty(exports, "fetchEntity", { enumerable: true, get: function () { return fetch_js_1.fetchEntity; } }); +//# sourceMappingURL=index.js.map \ No newline at end of file diff --git a/fixtures/sample/lib-cjs/database/index.js.map b/fixtures/sample/lib-cjs/database/index.js.map new file mode 100644 index 0000000..1d5b1f2 --- /dev/null +++ b/fixtures/sample/lib-cjs/database/index.js.map @@ -0,0 +1 @@ +{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/database/index.ts"],"names":[],"mappings":";;;AAAA,uCAAyC;AAAhC,uGAAA,WAAW,OAAA"} \ No newline at end of file diff --git a/fixtures/sample/lib-cjs/index.d.ts b/fixtures/sample/lib-cjs/index.d.ts new file mode 100644 index 0000000..74db2be --- /dev/null +++ b/fixtures/sample/lib-cjs/index.d.ts @@ -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 \ No newline at end of file diff --git a/fixtures/sample/lib-cjs/index.d.ts.map b/fixtures/sample/lib-cjs/index.d.ts.map new file mode 100644 index 0000000..b59f37d --- /dev/null +++ b/fixtures/sample/lib-cjs/index.d.ts.map @@ -0,0 +1 @@ +{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACH,WAAW,EACX,KAAK,EACL,IAAI,EACJ,WAAW,EACX,OAAO,EACP,eAAe,EACf,WAAW,EACX,UAAU,EACV,MAAM,EACN,UAAU,GACb,MAAM,YAAY,CAAC;AAEpB,OAAO,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAE3C,OAAO,EAAE,WAAW,EAAE,MAAM,qBAAqB,CAAC"} \ No newline at end of file diff --git a/fixtures/sample/lib-cjs/index.js b/fixtures/sample/lib-cjs/index.js new file mode 100644 index 0000000..47ab833 --- /dev/null +++ b/fixtures/sample/lib-cjs/index.js @@ -0,0 +1,8 @@ +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +exports.fetchEntity = exports.lookUpPerson = void 0; +var lookup_js_1 = require("./lookup.js"); +Object.defineProperty(exports, "lookUpPerson", { enumerable: true, get: function () { return lookup_js_1.lookUpPerson; } }); +var index_js_1 = require("./database/index.js"); +Object.defineProperty(exports, "fetchEntity", { enumerable: true, get: function () { return index_js_1.fetchEntity; } }); +//# sourceMappingURL=index.js.map \ No newline at end of file diff --git a/fixtures/sample/lib-cjs/index.js.map b/fixtures/sample/lib-cjs/index.js.map new file mode 100644 index 0000000..c4e740f --- /dev/null +++ b/fixtures/sample/lib-cjs/index.js.map @@ -0,0 +1 @@ +{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;AAaA,yCAA2C;AAAlC,yGAAA,YAAY,OAAA;AAErB,gDAAkD;AAAzC,uGAAA,WAAW,OAAA"} \ No newline at end of file diff --git a/fixtures/sample/lib-cjs/lookup.d.ts b/fixtures/sample/lib-cjs/lookup.d.ts new file mode 100644 index 0000000..3f8157c --- /dev/null +++ b/fixtures/sample/lib-cjs/lookup.d.ts @@ -0,0 +1,3 @@ +import { Person, PersonGuid } from './types.js'; +export declare function lookUpPerson(_guid: PersonGuid): Promise; +//# sourceMappingURL=lookup.d.ts.map \ No newline at end of file diff --git a/fixtures/sample/lib-cjs/lookup.d.ts.map b/fixtures/sample/lib-cjs/lookup.d.ts.map new file mode 100644 index 0000000..cb18342 --- /dev/null +++ b/fixtures/sample/lib-cjs/lookup.d.ts.map @@ -0,0 +1 @@ +{"version":3,"file":"lookup.d.ts","sourceRoot":"","sources":["../src/lookup.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,UAAU,EAAE,MAAM,YAAY,CAAC;AAEhD,wBAAsB,YAAY,CAAC,KAAK,EAAE,UAAU,GAAG,OAAO,CAAC,MAAM,GAAG,SAAS,CAAC,CAEjF"} \ No newline at end of file diff --git a/fixtures/sample/lib-cjs/lookup.js b/fixtures/sample/lib-cjs/lookup.js new file mode 100644 index 0000000..f196700 --- /dev/null +++ b/fixtures/sample/lib-cjs/lookup.js @@ -0,0 +1,8 @@ +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +exports.lookUpPerson = void 0; +async function lookUpPerson(_guid) { + return undefined; +} +exports.lookUpPerson = lookUpPerson; +//# sourceMappingURL=lookup.js.map \ No newline at end of file diff --git a/fixtures/sample/lib-cjs/lookup.js.map b/fixtures/sample/lib-cjs/lookup.js.map new file mode 100644 index 0000000..a91dd78 --- /dev/null +++ b/fixtures/sample/lib-cjs/lookup.js.map @@ -0,0 +1 @@ +{"version":3,"file":"lookup.js","sourceRoot":"","sources":["../src/lookup.ts"],"names":[],"mappings":";;;AAEO,KAAK,UAAU,YAAY,CAAC,KAAiB;IAChD,OAAO,SAAS,CAAC;AACrB,CAAC;AAFD,oCAEC"} \ No newline at end of file diff --git a/fixtures/sample/lib-cjs/types.d.ts b/fixtures/sample/lib-cjs/types.d.ts new file mode 100644 index 0000000..9920397 --- /dev/null +++ b/fixtures/sample/lib-cjs/types.d.ts @@ -0,0 +1,44 @@ +export type PrimeNumber = number; +export type Tuple = [T, U]; +export type GUID = string; +export type AddressGuid = GUID; +export interface EntityBase { + 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 \ No newline at end of file diff --git a/fixtures/sample/lib-cjs/types.d.ts.map b/fixtures/sample/lib-cjs/types.d.ts.map new file mode 100644 index 0000000..18b77df --- /dev/null +++ b/fixtures/sample/lib-cjs/types.d.ts.map @@ -0,0 +1 @@ +{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,WAAW,GAAG,MAAM,CAAC;AACjC,MAAM,MAAM,KAAK,CAAC,CAAC,EAAE,CAAC,IAAI,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC;AAEjC,MAAM,MAAM,IAAI,GAAG,MAAM,CAAC;AAE1B,MAAM,MAAM,WAAW,GAAG,IAAI,CAAC;AAE/B,MAAM,WAAW,UAAU,CAAC,CAAC,SAAS,MAAM;IACxC,IAAI,EAAE,IAAI,CAAC;IACX,IAAI,EAAE,CAAC,CAAC;CACX;AAED,MAAM,WAAW,MAAM;IACnB,IAAI,EAAE,IAAI,CAAC;IACX,IAAI,EAAE,MAAM,CAAC;CAChB;AAED,MAAM,WAAW,OAAQ,SAAQ,UAAU,CAAC,SAAS,CAAC;IAClD,IAAI,EAAE,WAAW,CAAC;IAClB,IAAI,EAAE,MAAM,CAAC;IACb,MAAM,EAAE,MAAM,CAAC;IACf,IAAI,EAAE,MAAM,CAAC;IACb,UAAU,EAAE,MAAM,CAAC;IACnB,OAAO,EAAE,MAAM,CAAC;IAChB,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,WAAW,CAAC,EAAE,UAAU,EAAE,CAAC;CAC9B;AAED,MAAM,MAAM,eAAe,GAAG,IAAI,CAAC;AAEnC,MAAM,WAAW,WAAY,SAAQ,UAAU,CAAC,aAAa,CAAC;IAC1D,IAAI,EAAE,eAAe,CAAC;IACtB,IAAI,EAAE,MAAM,CAAC;IACb,MAAM,EAAE,MAAM,CAAC;IACf,WAAW,CAAC,EAAE,UAAU,EAAE,CAAC;CAC9B;AAED,MAAM,MAAM,UAAU,GAAG,IAAI,CAAC;AAE9B,MAAM,WAAW,MAAO,SAAQ,UAAU,CAAC,QAAQ,CAAC;IAChD,IAAI,EAAE,UAAU,CAAC;IACjB,IAAI,EAAE,MAAM,CAAC;IACb,OAAO,CAAC,EAAE,MAAM,EAAE,CAAC;IACnB,GAAG,CAAC,EAAE,MAAM,CAAC;IACb,SAAS,CAAC,EAAE,OAAO,EAAE,CAAC;IACtB,KAAK,CAAC,EAAE,UAAU,EAAE,CAAC;CACxB;AAED,MAAM,WAAW,UAAW,SAAQ,UAAU,CAAC,YAAY,CAAC;IACxD,IAAI,EAAE,MAAM,CAAC;IACb,MAAM,EAAE,UAAU,CAAC;CACtB"} \ No newline at end of file diff --git a/fixtures/sample/lib-cjs/types.js b/fixtures/sample/lib-cjs/types.js new file mode 100644 index 0000000..11e638d --- /dev/null +++ b/fixtures/sample/lib-cjs/types.js @@ -0,0 +1,3 @@ +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +//# sourceMappingURL=types.js.map \ No newline at end of file diff --git a/fixtures/sample/lib-cjs/types.js.map b/fixtures/sample/lib-cjs/types.js.map new file mode 100644 index 0000000..c768b79 --- /dev/null +++ b/fixtures/sample/lib-cjs/types.js.map @@ -0,0 +1 @@ +{"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":""} \ No newline at end of file diff --git a/fixtures/sample/tsconfig.cjs.json b/fixtures/sample/tsconfig.cjs.json new file mode 100644 index 0000000..f646d4e --- /dev/null +++ b/fixtures/sample/tsconfig.cjs.json @@ -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"] +} diff --git a/fixtures/sample/tsconfig.json b/fixtures/sample/tsconfig.json index 552fe6f..c0068ab 100644 --- a/fixtures/sample/tsconfig.json +++ b/fixtures/sample/tsconfig.json @@ -4,8 +4,8 @@ "compilerOptions": { "declaration": true, "declarationMap": true, - "module": "ES2020", - "moduleResolution": "node16", + "module": "Node16", + "moduleResolution": "Node16", "newLine": "LF", "outDir": "lib", "sourceMap": true, diff --git a/src/__snapshots__/app.test.ts.snap b/src/__snapshots__/app.test.ts.snap index 5ea8459..33f681d 100644 --- a/src/__snapshots__/app.test.ts.snap +++ b/src/__snapshots__/app.test.ts.snap @@ -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 @@ -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]: @@ -289,12 +327,13 @@ exports[`app > run error '--help' 1`] = ` [error]: [stdout]: Usage: ts2mjs [options] [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 The output directory. [stdout]: --cwd The current working directory. [stdout]: --root The root directory. diff --git a/src/__snapshots__/processFile.test.ts.snap b/src/__snapshots__/processFile.test.ts.snap index 614bb82..cbfd724 100644 --- a/src/__snapshots__/processFile.test.ts.snap +++ b/src/__snapshots__/processFile.test.ts.snap @@ -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(guid: GUID): Promise; diff --git a/src/app.ts b/src/app.ts index ded4204..1ce25e6 100644 --- a/src/app.ts +++ b/src/app.ts @@ -53,6 +53,7 @@ interface CliOptions { dryRun?: boolean; verbose?: boolean; enforceRoot?: boolean; + cjs?: boolean; } export interface AppLogger { @@ -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('', 'The files to rename.') + .option('--cjs', 'Files are renamed to .cjs.') .option('-o, --output ', 'The output directory.') .option('--cwd ', 'The current working directory.') .option('--root ', 'The root directory.') @@ -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, diff --git a/src/processFile.test.ts b/src/processFile.test.ts index 4771a9a..d906f89 100644 --- a/src/processFile.test.ts +++ b/src/processFile.test.ts @@ -12,10 +12,12 @@ const sc = (text: string) => expect.stringContaining(text); describe('processFile', () => { test.each` - file | root | target | expected - ${'sample/lib/index.js'} | ${'sample/lib'} | ${'target'} | ${{ filename: 'target/index.mjs', linesChanged: 2 }} - ${'sample/lib/index.d.ts'} | ${'sample/lib'} | ${'target'} | ${{ filename: 'target/index.d.mts', linesChanged: 3 }} - `('processFile $file', async ({ file, root, target, expected }) => { + file | root | target | ext | expected + ${'sample/lib/index.js'} | ${'sample/lib'} | ${'target'} | ${'mjs'} | ${{ filename: 'target/index.mjs', linesChanged: 2 }} + ${'sample/lib/index.d.ts'} | ${'sample/lib'} | ${'target'} | ${'mjs'} | ${{ filename: 'target/index.d.mts', linesChanged: 3 }} + ${'sample/lib/index.js'} | ${'sample/lib'} | ${'target'} | ${'cjs'} | ${{ filename: 'target/index.cjs', linesChanged: 2 }} + ${'sample/lib/index.d.ts'} | ${'sample/lib'} | ${'target'} | ${'cjs'} | ${{ filename: 'target/index.d.cts', linesChanged: 3 }} + `('processFile $file', async ({ file, root, ext, target, expected }) => { const resolveTemp = resolverTemp(); file = ff(file); root = ff(root); @@ -28,21 +30,22 @@ describe('processFile', () => { }); const src = await readSourceFile(file); - const result = processSourceFile(src, { root, target, warning: vi.fn() }); + const result = processSourceFile(src, { root, target, warning: vi.fn(), ext }); expect(result).toEqual(resolvedExpected); expect(result?.content).toMatchSnapshot(); }); test.each` - file | root | target | expected - ${'sample/out/app.mjs'} | ${'sample'} | ${'sample'} | ${undefined} - `('processFile skip $file', async ({ file, root, target, expected }) => { + file | root | target | ext | expected + ${'sample/out/app.mjs'} | ${'sample'} | ${'sample'} | ${'mjs'} | ${undefined} + ${'sample/lib-cjs/app.cjs'} | ${'sample'} | ${'sample'} | ${'cjs'} | ${undefined} + `('processFile skip $file', async ({ file, root, target, ext, expected }) => { file = ff(file); root = ff(root); target = ff(target); const src = await readSourceFile(file); - const result = processSourceFile(src, { root, target, warning: vi.fn() }); + const result = processSourceFile(src, { root, target, ext, warning: vi.fn() }); expect(result).toEqual(expected); }); @@ -63,23 +66,24 @@ describe('processFile', () => { const src = await readSourceFile(file); const warning = vi.fn(); - const result = processSourceFile(src, { root, target, allowJsOutsideOfRoot: true, warning }); + const result = processSourceFile(src, { root, target, ext: 'mjs', allowJsOutsideOfRoot: true, warning }); expect(result).toEqual(resolvedExpected); expect(result?.content).toMatchSnapshot(); expect(warning).toHaveBeenCalledWith(sc('Import of a file outside of the root')); }); test.each` - file | root | expected - ${'sample/lib/image.css'} | ${'sample/lib'} | ${new UsageError('Must be a supported file type (.js, .mjs, .d.ts, .d.mts).')} - ${'sample/src/index.js'} | ${'sample/lib'} | ${new UsageError('Must be under root.')} - `('processFile not processed $file', async ({ file, root, expected }) => { + file | root | ext | expected + ${'sample/lib/image.css'} | ${'sample/lib'} | ${'mjs'} | ${new UsageError('Must be a supported file type (.js, .mjs, .d.ts, .d.mts).')} + ${'sample/lib/image.css'} | ${'sample/lib'} | ${'cjs'} | ${new UsageError('Must be a supported file type (.js, .cjs, .d.ts, .d.cts).')} + ${'sample/src/index.js'} | ${'sample/lib'} | ${'mjs'} | ${new UsageError('Must be under root.')} + `('processFile not processed $file', async ({ file, root, ext, expected }) => { root = ff(root); file = ff(file); const content = ''; const target = ff('../temp/lib'); expect(() => - processSourceFile({ srcFilename: file, content }, { root, target, warning: vi.fn() }), + processSourceFile({ srcFilename: file, content }, { root, target, ext, warning: vi.fn() }), ).toThrowError(expected); }); @@ -91,17 +95,21 @@ describe('processFile', () => { file = ff(file); const source = await readSourceFile(file); const target = ff('../temp/lib'); - expect(() => processSourceFile(source, { root, target, warning: vi.fn() })).toThrowError(expected); + expect(() => processSourceFile(source, { root, target, ext: 'mjs', warning: vi.fn() })).toThrowError(expected); }); test.each` - file | root | target | expected - ${'sample/lib/index.js'} | ${'sample'} | ${'temp'} | ${'temp/lib/index.mjs'} - ${'sample/lib/index.js.map'} | ${'sample'} | ${'temp'} | ${'temp/lib/index.mjs.map'} - ${'sample/lib/index.d.ts.map'} | ${'sample'} | ${'temp'} | ${'temp/lib/index.d.mts.map'} - ${'sample/lib/style.css'} | ${'sample'} | ${'temp'} | ${'temp/lib/style.css'} - `('calcNewFilename $file', ({ file, root, target, expected }) => { - expect(__testing__.calcNewFilename(ff(file), ff(root), ff(target))).toEqual(ff(expected)); + file | root | target | ext | expected + ${'sample/lib/index.js'} | ${'sample'} | ${'temp'} | ${'mjs'} | ${'temp/lib/index.mjs'} + ${'sample/lib/index.js.map'} | ${'sample'} | ${'temp'} | ${'mjs'} | ${'temp/lib/index.mjs.map'} + ${'sample/lib/index.d.ts.map'} | ${'sample'} | ${'temp'} | ${'mjs'} | ${'temp/lib/index.d.mts.map'} + ${'sample/lib/style.css'} | ${'sample'} | ${'temp'} | ${'mjs'} | ${'temp/lib/style.css'} + ${'sample/lib/index.js'} | ${'sample'} | ${'temp'} | ${'cjs'} | ${'temp/lib/index.cjs'} + ${'sample/lib/index.js.map'} | ${'sample'} | ${'temp'} | ${'cjs'} | ${'temp/lib/index.cjs.map'} + ${'sample/lib/index.d.ts.map'} | ${'sample'} | ${'temp'} | ${'cjs'} | ${'temp/lib/index.d.cts.map'} + ${'sample/lib/style.css'} | ${'sample'} | ${'temp'} | ${'cjs'} | ${'temp/lib/style.css'} + `('calcNewFilename $file', ({ file, root, target, ext, expected }) => { + expect(__testing__.calcNewFilename(ff(file), ff(root), ff(target), ext)).toEqual(ff(expected)); }); test.each` diff --git a/src/processFile.ts b/src/processFile.ts index 2487536..4f98ad4 100644 --- a/src/processFile.ts +++ b/src/processFile.ts @@ -8,10 +8,13 @@ import { readSourceFile, SOURCE_MAP_URL_MARKER } from './readSourceFile.js'; import type { SourceFile, SourceMap } from './SourceFile.js'; import { UsageError } from './errors.js'; -const isSupportedFile = /\.(m?js|d\.m?ts)$/; +const isSupportedFileMJS = /\.(m?js|d\.m?ts)$/; +const isSupportedFileCJS = /\.(c?js|d\.c?ts)$/; const regExpImportExport = /(import|export).*? from ('|")(?\..*?)\2;/g; +type ExtensionType = 'cjs' | 'mjs'; + export interface ProcessFileResult { filename: string; oldFilename: string; @@ -29,12 +32,16 @@ export interface ProcessSourceFileResult extends ProcessFileResult { export function processSourceFile(src: SourceFile, options: ProcessFileOptions): ProcessSourceFileResult | undefined { const { srcFilename, content, map: mappings } = src; - const { root: srcRoot, target: targetRoot, allowJsOutsideOfRoot } = options; + const { root: srcRoot, target: targetRoot, allowJsOutsideOfRoot, ext } = options; assert(doesContain(srcRoot, srcFilename), 'Must be under root.'); - assert(isSupportedFile.test(srcFilename), 'Must be a supported file type (.js, .mjs, .d.ts, .d.mts).'); + if (options.ext === 'cjs') { + assert(isSupportedFileCJS.test(srcFilename), 'Must be a supported file type (.js, .cjs, .d.ts, .d.cts).'); + } else { + assert(isSupportedFileMJS.test(srcFilename), 'Must be a supported file type (.js, .mjs, .d.ts, .d.mts).'); + } - const filename = calcNewFilename(srcFilename, srcRoot, targetRoot); + const filename = calcNewFilename(srcFilename, srcRoot, targetRoot, ext); if (pathJoin(targetRoot, filename) === pathJoin(srcRoot, srcFilename)) { // Do not process files in place. return undefined; @@ -92,13 +99,14 @@ export function processSourceFile(src: SourceFile, options: ProcessFileOptions): }; } -export function isSupportedFileType(filename: string): boolean { - return isSupportedFile.test(filename); +export function isSupportedFileType(filename: string, ext: ExtensionType): boolean { + return (ext === 'mjs' ? isSupportedFileMJS : isSupportedFileCJS).test(filename); } export interface ProcessFileOptions { root: string; target: string; + ext: ExtensionType; allowJsOutsideOfRoot?: boolean; warning: (msg: string) => void; } @@ -112,8 +120,11 @@ export async function processFile(filename: string, options: ProcessFileOptions) return [r, processSourceMap(r.mappings)]; } -function calcNewFilename(srcFilename: string, root: string, target: string): string { - const newName = srcFilename.replace(/\.js(\.map)?$/, '.mjs$1').replace(/\.ts(.map)?$/, '.mts$1'); +function calcNewFilename(srcFilename: string, root: string, target: string, ext: ExtensionType): string { + const newName = + ext === 'mjs' + ? srcFilename.replace(/\.js(\.map)?$/, '.mjs$1').replace(/\.ts(.map)?$/, '.mts$1') + : srcFilename.replace(/\.js(\.map)?$/, '.cjs$1').replace(/\.ts(.map)?$/, '.cts$1'); return rebaseFile(newName, root, target); } diff --git a/src/processFiles.ts b/src/processFiles.ts index c4ca172..e987e31 100644 --- a/src/processFiles.ts +++ b/src/processFiles.ts @@ -38,6 +38,10 @@ export interface Options { * The root directory, files outside of the root will NOT be copied. */ root: string | undefined; + /** + * Rename files to `.cjs` instead of `.mjs`. + */ + cjs: boolean | undefined; } export interface ProcessFilesResult { @@ -54,8 +58,11 @@ export async function processFiles(files: string[], options: Options): Promise

>(); const result: ProcessFilesResult = { @@ -107,7 +114,13 @@ export async function processFiles(files: string[], options: Options): Promise

${relName(filename)} ${chalk.green('Generated')}`); @@ -122,7 +135,7 @@ export async function processFiles(files: string[], options: Options): Promise