From 22a85c9384a48a7a17fa9e736771d8fe54b3fc47 Mon Sep 17 00:00:00 2001 From: Nazmus Sayad <87106526+NazmusSayad@users.noreply.github.com> Date: Wed, 21 Aug 2024 00:47:43 +0600 Subject: [PATCH] code compiles to cjs and mjs --- package-lock.json | 351 +++++++++++++++++++++++++++++++++++- package.json | 8 +- src/__lab.ts | 4 +- src/main.ts | 6 + src/program/build.ts | 13 +- src/program/dev.ts | 52 +++++- src/scripts/pushNodeCode.ts | 4 +- src/scripts/tsc.ts | 4 +- src/updateImports/index.ts | 63 ++++--- src/updateImports/utils.ts | 7 +- src/utils.ts | 30 +-- 11 files changed, 476 insertions(+), 66 deletions(-) diff --git a/package-lock.json b/package-lock.json index 147199b..120019c 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,24 +1,28 @@ { "name": "npmize", - "version": "0.0.10", + "version": "0.0.11", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "npmize", - "version": "0.0.10", + "version": "0.0.11", "license": "ISC", "dependencies": { "@babel/parser": "^7.20.7", "ansi-colors": "^4.1.3", + "cross-spawn": "^7.0.3", + "execa": "^9.3.1", "lskit": "^1.0.0", "noarg": "^1.0.3", - "shelljs": "^0.8.5" + "shelljs": "^0.8.5", + "zx": "^8.1.4" }, "bin": { "npmize": "dist/index.js" }, "devDependencies": { + "@types/cross-spawn": "^6.0.6", "@types/node": "^18.11.9", "@types/shelljs": "^0.8.15", "typescript": "^5.3.3" @@ -44,6 +48,45 @@ "node": ">=0.1.90" } }, + "node_modules/@sec-ant/readable-stream": { + "version": "0.4.1", + "resolved": "https://registry.npmjs.org/@sec-ant/readable-stream/-/readable-stream-0.4.1.tgz", + "integrity": "sha512-831qok9r2t8AlxLko40y2ebgSDhenenCatLVeW/uBtnHPyhHOvG0C7TvfgecV+wHzIm5KUICgzmVpWS+IMEAeg==", + "license": "MIT" + }, + "node_modules/@sindresorhus/merge-streams": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/@sindresorhus/merge-streams/-/merge-streams-4.0.0.tgz", + "integrity": "sha512-tlqY9xq5ukxTUZBmoOp+m61cqwQD5pHJtFY3Mn8CA8ps6yghLH/Hw8UPdqg4OLmFW3IFlcXnQNmo/dh8HzXYIQ==", + "license": "MIT", + "engines": { + "node": ">=18" + }, + "funding": { + "url": "https://github.com/sponsors/sindresorhus" + } + }, + "node_modules/@types/cross-spawn": { + "version": "6.0.6", + "resolved": "https://registry.npmjs.org/@types/cross-spawn/-/cross-spawn-6.0.6.tgz", + "integrity": "sha512-fXRhhUkG4H3TQk5dBhQ7m/JDdSNHKwR2BBia62lhwEIq9xGiQKLxd6LymNhn47SjXhsUEPmxi+PKw2OkW4LLjA==", + "dev": true, + "license": "MIT", + "dependencies": { + "@types/node": "*" + } + }, + "node_modules/@types/fs-extra": { + "version": "11.0.4", + "resolved": "https://registry.npmjs.org/@types/fs-extra/-/fs-extra-11.0.4.tgz", + "integrity": "sha512-yTbItCNreRooED33qjunPthRcSjERP1r4MqCZc7wv0u2sUkzTFp45tgUfS5+r7FrZPdmCCNflLhVSP/o+SemsQ==", + "license": "MIT", + "optional": true, + "dependencies": { + "@types/jsonfile": "*", + "@types/node": "*" + } + }, "node_modules/@types/glob": { "version": "7.2.0", "resolved": "https://registry.npmjs.org/@types/glob/-/glob-7.2.0.tgz", @@ -54,6 +97,16 @@ "@types/node": "*" } }, + "node_modules/@types/jsonfile": { + "version": "6.1.4", + "resolved": "https://registry.npmjs.org/@types/jsonfile/-/jsonfile-6.1.4.tgz", + "integrity": "sha512-D5qGUYwjvnNNextdU59/+fI+spnwtTFmyQP0h+PfIOSkNfpU6AOICUOkm4i0OnSk+NyjdPJrxCDro0sJsWlRpQ==", + "license": "MIT", + "optional": true, + "dependencies": { + "@types/node": "*" + } + }, "node_modules/@types/minimatch": { "version": "5.1.2", "resolved": "https://registry.npmjs.org/@types/minimatch/-/minimatch-5.1.2.tgz", @@ -64,7 +117,7 @@ "version": "18.11.9", "resolved": "https://registry.npmjs.org/@types/node/-/node-18.11.9.tgz", "integrity": "sha512-CRpX21/kGdzjOpFsZSkcrXMGIBWMGNIHXXBVFSH+ggkftxg+XYP20TESbh+zFvFj3EQOl5byk0HTRn1IL6hbqg==", - "dev": true + "devOptional": true }, "node_modules/@types/shelljs": { "version": "0.8.15", @@ -125,11 +178,66 @@ "resolved": "https://registry.npmjs.org/concat-map/-/concat-map-0.0.1.tgz", "integrity": "sha512-/Srv4dswyQNBfohGpz9o6Yb3Gz3SrUDqBH5rTuhGR7ahtlbYKnVxw2bCFMRljaA7EXHaXZ8wsHdodFvbkhKmqg==" }, + "node_modules/cross-spawn": { + "version": "7.0.3", + "resolved": "https://registry.npmjs.org/cross-spawn/-/cross-spawn-7.0.3.tgz", + "integrity": "sha512-iRDPJKUPVEND7dHPO8rkbOnPpyDygcDFtWjpeWNCgy8WP2rXcxXL8TskReQl6OrB2G7+UJrags1q15Fudc7G6w==", + "license": "MIT", + "dependencies": { + "path-key": "^3.1.0", + "shebang-command": "^2.0.0", + "which": "^2.0.1" + }, + "engines": { + "node": ">= 8" + } + }, "node_modules/emoji-regex": { "version": "8.0.0", "resolved": "https://registry.npmjs.org/emoji-regex/-/emoji-regex-8.0.0.tgz", "integrity": "sha512-MSjYzcWNOA0ewAHpz0MxpYFvwg6yjy1NG3xteoqz644VCo/RPgnr1/GGt+ic3iJTzQ8Eu3TdM14SawnVUmGE6A==" }, + "node_modules/execa": { + "version": "9.3.1", + "resolved": "https://registry.npmjs.org/execa/-/execa-9.3.1.tgz", + "integrity": "sha512-gdhefCCNy/8tpH/2+ajP9IQc14vXchNdd0weyzSJEFURhRMGncQ+zKFxwjAufIewPEJm9BPOaJnvg2UtlH2gPQ==", + "license": "MIT", + "dependencies": { + "@sindresorhus/merge-streams": "^4.0.0", + "cross-spawn": "^7.0.3", + "figures": "^6.1.0", + "get-stream": "^9.0.0", + "human-signals": "^8.0.0", + "is-plain-obj": "^4.1.0", + "is-stream": "^4.0.1", + "npm-run-path": "^5.2.0", + "pretty-ms": "^9.0.0", + "signal-exit": "^4.1.0", + "strip-final-newline": "^4.0.0", + "yoctocolors": "^2.0.0" + }, + "engines": { + "node": "^18.19.0 || >=20.5.0" + }, + "funding": { + "url": "https://github.com/sindresorhus/execa?sponsor=1" + } + }, + "node_modules/figures": { + "version": "6.1.0", + "resolved": "https://registry.npmjs.org/figures/-/figures-6.1.0.tgz", + "integrity": "sha512-d+l3qxjSesT4V7v2fh+QnmFnUWv9lSpjarhShNTgBOfA0ttejbQUAlHLitbjkoRiDulW0OPoQPYIGhIC8ohejg==", + "license": "MIT", + "dependencies": { + "is-unicode-supported": "^2.0.0" + }, + "engines": { + "node": ">=18" + }, + "funding": { + "url": "https://github.com/sponsors/sindresorhus" + } + }, "node_modules/fs.realpath": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/fs.realpath/-/fs.realpath-1.0.0.tgz", @@ -140,6 +248,22 @@ "resolved": "https://registry.npmjs.org/function-bind/-/function-bind-1.1.1.tgz", "integrity": "sha512-yIovAzMX49sF8Yl58fSCWJ5svSLuaibPxXQJFLmBObTuCr0Mf1KiPopGM9NiFjiYBCbfaa2Fh6breQ6ANVTI0A==" }, + "node_modules/get-stream": { + "version": "9.0.1", + "resolved": "https://registry.npmjs.org/get-stream/-/get-stream-9.0.1.tgz", + "integrity": "sha512-kVCxPF3vQM/N0B1PmoqVUqgHP+EeVjmZSQn+1oCRPxd2P21P2F19lIgbR3HBosbB1PUhOAoctJnfEn2GbN2eZA==", + "license": "MIT", + "dependencies": { + "@sec-ant/readable-stream": "^0.4.1", + "is-stream": "^4.0.1" + }, + "engines": { + "node": ">=18" + }, + "funding": { + "url": "https://github.com/sponsors/sindresorhus" + } + }, "node_modules/glob": { "version": "7.2.3", "resolved": "https://registry.npmjs.org/glob/-/glob-7.2.3.tgz", @@ -170,6 +294,15 @@ "node": ">= 0.4.0" } }, + "node_modules/human-signals": { + "version": "8.0.0", + "resolved": "https://registry.npmjs.org/human-signals/-/human-signals-8.0.0.tgz", + "integrity": "sha512-/1/GPCpDUCCYwlERiYjxoczfP0zfvZMU/OWgQPMya9AbAE24vseigFdhAMObpc8Q4lc/kjutPfUddDYyAmejnA==", + "license": "Apache-2.0", + "engines": { + "node": ">=18.18.0" + } + }, "node_modules/inflight": { "version": "1.0.6", "resolved": "https://registry.npmjs.org/inflight/-/inflight-1.0.6.tgz", @@ -211,6 +344,48 @@ "node": ">=8" } }, + "node_modules/is-plain-obj": { + "version": "4.1.0", + "resolved": "https://registry.npmjs.org/is-plain-obj/-/is-plain-obj-4.1.0.tgz", + "integrity": "sha512-+Pgi+vMuUNkJyExiMBt5IlFoMyKnr5zhJ4Uspz58WOhBF5QoIZkFyNHIbBAtHwzVAgk5RtndVNsDRN61/mmDqg==", + "license": "MIT", + "engines": { + "node": ">=12" + }, + "funding": { + "url": "https://github.com/sponsors/sindresorhus" + } + }, + "node_modules/is-stream": { + "version": "4.0.1", + "resolved": "https://registry.npmjs.org/is-stream/-/is-stream-4.0.1.tgz", + "integrity": "sha512-Dnz92NInDqYckGEUJv689RbRiTSEHCQ7wOVeALbkOz999YpqT46yMRIGtSNl2iCL1waAZSx40+h59NV/EwzV/A==", + "license": "MIT", + "engines": { + "node": ">=18" + }, + "funding": { + "url": "https://github.com/sponsors/sindresorhus" + } + }, + "node_modules/is-unicode-supported": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/is-unicode-supported/-/is-unicode-supported-2.0.0.tgz", + "integrity": "sha512-FRdAyx5lusK1iHG0TWpVtk9+1i+GjrzRffhDg4ovQ7mcidMQ6mj+MhKPmvh7Xwyv5gIS06ns49CA7Sqg7lC22Q==", + "license": "MIT", + "engines": { + "node": ">=18" + }, + "funding": { + "url": "https://github.com/sponsors/sindresorhus" + } + }, + "node_modules/isexe": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/isexe/-/isexe-2.0.0.tgz", + "integrity": "sha512-RHxMLp9lnKHGHRng9QFhRCMbYAcVpn69smSGcq3f36xjgVVWThj4qqLbTLlq7Ssj8B+fIQ1EuCEGI2lKsyQeIw==", + "license": "ISC" + }, "node_modules/lskit": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/lskit/-/lskit-1.0.0.tgz", @@ -236,6 +411,33 @@ "cli-table3": "^0.6.3" } }, + "node_modules/npm-run-path": { + "version": "5.3.0", + "resolved": "https://registry.npmjs.org/npm-run-path/-/npm-run-path-5.3.0.tgz", + "integrity": "sha512-ppwTtiJZq0O/ai0z7yfudtBpWIoxM8yE6nHi1X47eFR2EWORqfbu6CnPlNsjeN683eT0qG6H/Pyf9fCcvjnnnQ==", + "license": "MIT", + "dependencies": { + "path-key": "^4.0.0" + }, + "engines": { + "node": "^12.20.0 || ^14.13.1 || >=16.0.0" + }, + "funding": { + "url": "https://github.com/sponsors/sindresorhus" + } + }, + "node_modules/npm-run-path/node_modules/path-key": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/path-key/-/path-key-4.0.0.tgz", + "integrity": "sha512-haREypq7xkM7ErfgIyA0z+Bj4AGKlMSdlQE2jvJo6huWD1EdkKYV+G/T4nq0YEF2vgTT8kqMFKo1uHn950r4SQ==", + "license": "MIT", + "engines": { + "node": ">=12" + }, + "funding": { + "url": "https://github.com/sponsors/sindresorhus" + } + }, "node_modules/once": { "version": "1.4.0", "resolved": "https://registry.npmjs.org/once/-/once-1.4.0.tgz", @@ -244,6 +446,18 @@ "wrappy": "1" } }, + "node_modules/parse-ms": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/parse-ms/-/parse-ms-4.0.0.tgz", + "integrity": "sha512-TXfryirbmq34y8QBwgqCVLi+8oA3oWx2eAnSn62ITyEhEYaWRlVZ2DvMM9eZbMs/RfxPu/PK/aBLyGj4IrqMHw==", + "license": "MIT", + "engines": { + "node": ">=18" + }, + "funding": { + "url": "https://github.com/sponsors/sindresorhus" + } + }, "node_modules/path-is-absolute": { "version": "1.0.1", "resolved": "https://registry.npmjs.org/path-is-absolute/-/path-is-absolute-1.0.1.tgz", @@ -252,11 +466,35 @@ "node": ">=0.10.0" } }, + "node_modules/path-key": { + "version": "3.1.1", + "resolved": "https://registry.npmjs.org/path-key/-/path-key-3.1.1.tgz", + "integrity": "sha512-ojmeN0qd+y0jszEtoY48r0Peq5dwMEkIlCOu6Q5f41lfkswXuKtYrhgoTpLnyIcHm24Uhqx+5Tqm2InSwLhE6Q==", + "license": "MIT", + "engines": { + "node": ">=8" + } + }, "node_modules/path-parse": { "version": "1.0.7", "resolved": "https://registry.npmjs.org/path-parse/-/path-parse-1.0.7.tgz", "integrity": "sha512-LDJzPVEEEPR+y48z93A0Ed0yXb8pAByGWo/k5YYdYgpY2/2EsOsksJrq7lOHxryrVOn1ejG6oAp8ahvOIQD8sw==" }, + "node_modules/pretty-ms": { + "version": "9.1.0", + "resolved": "https://registry.npmjs.org/pretty-ms/-/pretty-ms-9.1.0.tgz", + "integrity": "sha512-o1piW0n3tgKIKCwk2vpM/vOV13zjJzvP37Ioze54YlTHE06m4tjEbzg9WsKkvTuyYln2DHjo5pY4qrZGI0otpw==", + "license": "MIT", + "dependencies": { + "parse-ms": "^4.0.0" + }, + "engines": { + "node": ">=18" + }, + "funding": { + "url": "https://github.com/sponsors/sindresorhus" + } + }, "node_modules/rechoir": { "version": "0.6.2", "resolved": "https://registry.npmjs.org/rechoir/-/rechoir-0.6.2.tgz", @@ -284,6 +522,27 @@ "url": "https://github.com/sponsors/ljharb" } }, + "node_modules/shebang-command": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/shebang-command/-/shebang-command-2.0.0.tgz", + "integrity": "sha512-kHxr2zZpYtdmrN1qDjrrX/Z1rR1kG8Dx+gkpK1G4eXmvXswmcE1hTWBWYUzlraYw1/yZp6YuDY77YtvbN0dmDA==", + "license": "MIT", + "dependencies": { + "shebang-regex": "^3.0.0" + }, + "engines": { + "node": ">=8" + } + }, + "node_modules/shebang-regex": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/shebang-regex/-/shebang-regex-3.0.0.tgz", + "integrity": "sha512-7++dFhtcx3353uBaq8DDR4NuxBetBzC7ZQOhmTQInHEd6bSrXdiEyzCvG07Z44UYdLShWUyXt5M/yhz8ekcb1A==", + "license": "MIT", + "engines": { + "node": ">=8" + } + }, "node_modules/shelljs": { "version": "0.8.5", "resolved": "https://registry.npmjs.org/shelljs/-/shelljs-0.8.5.tgz", @@ -300,6 +559,18 @@ "node": ">=4" } }, + "node_modules/signal-exit": { + "version": "4.1.0", + "resolved": "https://registry.npmjs.org/signal-exit/-/signal-exit-4.1.0.tgz", + "integrity": "sha512-bzyZ1e88w9O1iNJbKnOlvYTrWPDl46O1bG0D3XInv+9tkPrxrN8jUUTiFlDkkmKWgn1M6CfIA13SuGqOa9Korw==", + "license": "ISC", + "engines": { + "node": ">=14" + }, + "funding": { + "url": "https://github.com/sponsors/isaacs" + } + }, "node_modules/string-width": { "version": "4.2.3", "resolved": "https://registry.npmjs.org/string-width/-/string-width-4.2.3.tgz", @@ -324,6 +595,18 @@ "node": ">=8" } }, + "node_modules/strip-final-newline": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/strip-final-newline/-/strip-final-newline-4.0.0.tgz", + "integrity": "sha512-aulFJcD6YK8V1G7iRB5tigAP4TsHBZZrOV8pjV++zdUwmeV8uzbY7yn6h9MswN62adStNZFuCIx4haBnRuMDaw==", + "license": "MIT", + "engines": { + "node": ">=18" + }, + "funding": { + "url": "https://github.com/sponsors/sindresorhus" + } + }, "node_modules/supports-preserve-symlinks-flag": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/supports-preserve-symlinks-flag/-/supports-preserve-symlinks-flag-1.0.0.tgz", @@ -348,10 +631,70 @@ "node": ">=14.17" } }, + "node_modules/undici-types": { + "version": "6.19.8", + "resolved": "https://registry.npmjs.org/undici-types/-/undici-types-6.19.8.tgz", + "integrity": "sha512-ve2KP6f/JnbPBFyobGHuerC9g1FYGn/F8n1LWTwNxCEzd6IfqTwUQcNXgEtmmQ6DlRrC1hrSrBnCZPokRrDHjw==", + "license": "MIT", + "optional": true + }, + "node_modules/which": { + "version": "2.0.2", + "resolved": "https://registry.npmjs.org/which/-/which-2.0.2.tgz", + "integrity": "sha512-BLI3Tl1TW3Pvl70l3yq3Y64i+awpwXqsGBYWkkqMtnbXgrMD+yj7rhW0kuEDxzJaYXGjEW5ogapKNMEKNMjibA==", + "license": "ISC", + "dependencies": { + "isexe": "^2.0.0" + }, + "bin": { + "node-which": "bin/node-which" + }, + "engines": { + "node": ">= 8" + } + }, "node_modules/wrappy": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/wrappy/-/wrappy-1.0.2.tgz", "integrity": "sha512-l4Sp/DRseor9wL6EvV2+TuQn63dMkPjZ/sp9XkghTEbV9KlPS1xUsZ3u7/IQO4wxtcFB4bgpQPRcR3QCvezPcQ==" + }, + "node_modules/yoctocolors": { + "version": "2.1.1", + "resolved": "https://registry.npmjs.org/yoctocolors/-/yoctocolors-2.1.1.tgz", + "integrity": "sha512-GQHQqAopRhwU8Kt1DDM8NjibDXHC8eoh1erhGAJPEyveY9qqVeXvVikNKrDz69sHowPMorbPUrH/mx8c50eiBQ==", + "license": "MIT", + "engines": { + "node": ">=18" + }, + "funding": { + "url": "https://github.com/sponsors/sindresorhus" + } + }, + "node_modules/zx": { + "version": "8.1.4", + "resolved": "https://registry.npmjs.org/zx/-/zx-8.1.4.tgz", + "integrity": "sha512-QFDYYpnzdpRiJ3dL2102Cw26FpXpWshW4QLTGxiYfIcwdAqg084jRCkK/kuP/NOSkxOjydRwNFG81qzA5r1a6w==", + "license": "Apache-2.0", + "bin": { + "zx": "build/cli.js" + }, + "engines": { + "node": ">= 12.17.0" + }, + "optionalDependencies": { + "@types/fs-extra": ">=11", + "@types/node": ">=20" + } + }, + "node_modules/zx/node_modules/@types/node": { + "version": "22.4.1", + "resolved": "https://registry.npmjs.org/@types/node/-/node-22.4.1.tgz", + "integrity": "sha512-1tbpb9325+gPnKK0dMm+/LMriX0vKxf6RnB0SZUqfyVkQ4fMgUSySqhxE/y8Jvs4NyF1yHzTfG9KlnkIODxPKg==", + "license": "MIT", + "optional": true, + "dependencies": { + "undici-types": "~6.19.2" + } } } } diff --git a/package.json b/package.json index 17346cd..f518bc8 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "npmize", "description": "Let's create an npm package without worrying about anything.", - "version": "0.0.11", + "version": "1.0.0", "bin": "./dist/index.js", "scripts": { "lab": "nodemon ./src/__lab.ts", @@ -12,11 +12,15 @@ "dependencies": { "@babel/parser": "^7.20.7", "ansi-colors": "^4.1.3", + "cross-spawn": "^7.0.3", + "execa": "^9.3.1", "lskit": "^1.0.0", "noarg": "^1.0.3", - "shelljs": "^0.8.5" + "shelljs": "^0.8.5", + "zx": "^8.1.4" }, "devDependencies": { + "@types/cross-spawn": "^6.0.6", "@types/node": "^18.11.9", "@types/shelljs": "^0.8.15", "typescript": "^5.3.3" diff --git a/src/__lab.ts b/src/__lab.ts index aa9f507..e0f6d0f 100644 --- a/src/__lab.ts +++ b/src/__lab.ts @@ -2,4 +2,6 @@ console.clear() import app from './main' -app.run(['init', '--name', '../npmize-test']) +// app.run(['init', '--name', '../npmize-test']) +app.run(['dev', '--root', '../npmize-test', '--node']) +// app.run(['build', '--root', '../npmize-test']) diff --git a/src/main.ts b/src/main.ts index 47e3bcf..db2565a 100644 --- a/src/main.ts +++ b/src/main.ts @@ -66,6 +66,12 @@ app.create( .aliases('t') .default([]) .description("TypeScript's options"), + + node: t + .boolean() + .aliases('n') + .default(false) + .description('Enable __dirname and __filename in ES modules'), }, }, diff --git a/src/program/build.ts b/src/program/build.ts index 5ae8155..767439d 100644 --- a/src/program/build.ts +++ b/src/program/build.ts @@ -6,9 +6,14 @@ import tsc from '../scripts/tsc' import { cleanDir, moveFiles } from '../utils' import updateImports from '../updateImports' import pushNodeCode from '../scripts/pushNodeCode' +import packageJSON from '../scripts/packageJSON' export default function (basePath: string, options: Options) { - cleanDir(options.outDir) + const data = packageJSON.read(basePath) + if (data.type) { + options.module ??= data.type === 'module' ? 'mjs' : 'cjs' + } + console.log(`Build started at ${basePath}`) console.log('') @@ -29,6 +34,8 @@ function runBuild( const tempDir = path.join(basePath, config.tempBuildDir) cleanDir(tempDir) + cleanDir(options.outDir) + tsc(basePath, [ ...options.tsc.map((tsc) => `--${tsc}`), `--outDir ${tempDir}`, @@ -36,8 +43,8 @@ function runBuild( ]) const files = lskit.sync(tempDir) - const renamedFiles = updateImports(moduleType, files) - const movedFiles = moveFiles(tempDir, options.outDir, renamedFiles) + const updatedImports = updateImports(moduleType, files) + const movedFiles = moveFiles(tempDir, options.outDir, updatedImports) if (moduleType === 'mjs' && options.node && movedFiles.length) { console.log('Enabling Node.js __dirname and __filename...') diff --git a/src/program/dev.ts b/src/program/dev.ts index f07d539..2ea5076 100644 --- a/src/program/dev.ts +++ b/src/program/dev.ts @@ -1,22 +1,58 @@ +import fs from 'fs' +import path from 'path' +import config from '../config' import tsc from '../scripts/tsc' +import updateImports from '../updateImports' +import { cleanDir, moveFiles } from '../utils' import packageJSON from '../scripts/packageJSON' -import { cleanDir } from '../utils' +import pushNodeCode from '../scripts/pushNodeCode' export default function (basePath: string, options: DevOptions) { const data = packageJSON.read(basePath) - options.module ??= data.type === 'module' ? 'mjs' : 'cjs' + if (data.type) { + options.module ??= data.type === 'module' ? 'mjs' : 'cjs' + } + const tempDir = path.join(basePath, config.tempBuildDir) + cleanDir(tempDir) cleanDir(options.outDir) - tsc(basePath, [ - ...options.tsc.map((tsc) => `--${tsc}`), - '--watch', - `--outDir ${options.outDir}`, - `--module ${options.module === 'cjs' ? 'commonjs' : 'esnext'}`, - ]) + + function makeOutFiles(filename: string, outModule: 'cjs' | 'mjs') { + const fullPath = path.join(tempDir, filename) + if (fs.existsSync(fullPath) && fs.statSync(fullPath).isFile()) { + const updatedImports = updateImports(outModule, [fullPath]) + const [movedFile] = moveFiles(tempDir, options.outDir, updatedImports) + if (outModule === 'mjs' && options.node) pushNodeCode(movedFile) + } + } + + fs.watch(tempDir, { recursive: true }, (event, filename) => { + if (event !== 'change') return + if (!(filename.endsWith('.js') || filename.endsWith('.ts'))) return + + if (options.module) { + makeOutFiles(filename, options.module) + } else { + makeOutFiles(filename, 'cjs') + makeOutFiles(filename, 'mjs') + } + }) + + tsc( + basePath, + [ + ...options.tsc.map((tsc) => `--${tsc}`), + `--outDir ${tempDir}`, + `--module ${options.module === 'cjs' ? 'commonjs' : 'esnext'}`, + '--watch', + ], + true + ) } type DevOptions = { module?: 'cjs' | 'mjs' + node?: boolean outDir: string tsc: string[] } diff --git a/src/scripts/pushNodeCode.ts b/src/scripts/pushNodeCode.ts index c00ebe9..14f88f9 100644 --- a/src/scripts/pushNodeCode.ts +++ b/src/scripts/pushNodeCode.ts @@ -20,8 +20,8 @@ ${urlModule}${filename}${dirname} export default (...files: string[]) => { files.forEach((file) => { if (file.endsWith('ts')) return - - let data: string = fs.readFileSync(file, 'utf-8') + let data = fs.readFileSync(file, 'utf-8') + if (!(data.includes('__dirname') || data.includes('__filename'))) return if (data.startsWith('#!')) { const dataLines = data.split('\n') diff --git a/src/scripts/tsc.ts b/src/scripts/tsc.ts index b52fac0..ea96c19 100644 --- a/src/scripts/tsc.ts +++ b/src/scripts/tsc.ts @@ -1,5 +1,5 @@ import shelljs from 'shelljs' -export default function (cwd: string, args: string[]) { - shelljs.cd(cwd).exec(['npx tsc', ...args].join(' ')) +export default function (cwd: string, args: string[], async = false) { + shelljs.exec(['npx', 'tsc', ...args].join(' '), { cwd, async }) } diff --git a/src/updateImports/index.ts b/src/updateImports/index.ts index 5e571c0..99d55d5 100644 --- a/src/updateImports/index.ts +++ b/src/updateImports/index.ts @@ -3,7 +3,6 @@ import path from 'path' import * as babel from '@babel/parser' import * as utils from './utils' import * as node from './node' -import { writeFileSync } from '../utils' const getImports = (parsed: any) => { return [ @@ -22,35 +21,35 @@ const getRequires = (parsed: any) => { export default (type: 'cjs' | 'mjs', files: any[]) => { const ext = '.' + type - return files.map((filePath) => { - const dirPath = path.dirname(filePath) - const data = fs.readFileSync(filePath, 'utf-8') - const parsedBody = babel.parse(data, { - sourceType: 'module', - plugins: ['typescript'], - sourceFilename: filePath, - }).program.body - - const foundFilePaths = - filePath.endsWith('.ts') || type === 'mjs' - ? getImports(parsedBody) - : getRequires(parsedBody) - - const updatedData = utils.getUpdatedData( - data, - foundFilePaths, - (node: any) => { - const shortPath = node.value.replace(/\.js$/i, '') - const fullPath = path.join(dirPath, node.value) - - const isExists = utils.isFileExists(files, fullPath) - return isExists ? shortPath + ext : shortPath + '/index' + ext - } - ) - - fs.rmSync(filePath) - const newFilePath = utils.getNewFilePath(filePath, type) - writeFileSync(newFilePath, updatedData) - return newFilePath - }) + return Object.fromEntries( + files.map((filePath) => { + const dirPath = path.dirname(filePath) + const data = fs.readFileSync(filePath, 'utf-8') + const parsedBody = babel.parse(data, { + sourceType: 'module', + plugins: ['typescript'], + sourceFilename: filePath, + }).program.body + + const foundFilePaths = + filePath.endsWith('.ts') || type === 'mjs' + ? getImports(parsedBody) + : getRequires(parsedBody) + + const updatedData = utils.getUpdatedData( + data, + foundFilePaths, + (node: any) => { + const shortPath = node.value.replace(/\.js$/i, '') + const fullPath = path.join(dirPath, node.value) + + const isExists = utils.isFileExists(files, fullPath) + return isExists ? shortPath + ext : shortPath + '/index' + ext + } + ) + + const newFilePath = utils.getNewFilePath(filePath, type) + return [newFilePath, updatedData] as const + }) + ) } diff --git a/src/updateImports/utils.ts b/src/updateImports/utils.ts index 21337f3..e06f91a 100644 --- a/src/updateImports/utils.ts +++ b/src/updateImports/utils.ts @@ -1,3 +1,5 @@ +import * as fs from 'fs' + export const getUpdatedData = (fileData: any, found: any[], cb: any) => { const newEntries = [ { start: 0, end: 0, value: '', rawValue: '', filename: '' }, @@ -33,7 +35,10 @@ export const isFileExists = (files: string[], target: string) => { return ( files.includes(target) || files.includes(target + '.js') || - files.includes(target + '.ts') + files.includes(target + '.ts') || + (fs.existsSync(target) && fs.statSync(target).isFile()) || + (fs.existsSync(target + '.js') && fs.statSync(target + '.js').isFile()) || + (fs.existsSync(target + '.ts') && fs.statSync(target + '.ts').isFile()) ) } diff --git a/src/utils.ts b/src/utils.ts index 895d20e..ce98c7c 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -2,28 +2,36 @@ import fs from 'fs' import path from 'path' import shelljs from 'shelljs' -export function cleanDir(dir: string, create = true) { +export function cleanDir(dir: string, createDir = true) { if (fs.existsSync(dir)) { fs.rmSync(dir, { recursive: true, force: true }) } - if (create) { + if (createDir) { fs.mkdirSync(dir, { recursive: true }) } } -export function moveFiles(baseDir: string, outDir: string, files: string[]) { - return files.map((filePath) => { - const relativePath = path.relative(baseDir, filePath) - const outDirFilePath = path.join(outDir, '/' + relativePath) +export function moveFiles( + baseDir: string, + outDir: string, + data: Record +) { + const newPaths = [] as string[] - if (!fs.existsSync(outDirFilePath)) { - fs.mkdirSync(path.dirname(outDirFilePath), { recursive: true }) + for (const key in data) { + const oldRelative = path.relative(baseDir, key) + const outPath = path.join(outDir, oldRelative) + + if (!fs.existsSync(outPath)) { + fs.mkdirSync(path.dirname(outPath), { recursive: true }) } - fs.renameSync(filePath, outDirFilePath) - return outDirFilePath - }) + fs.writeFileSync(outPath, data[key]) + newPaths.push(outPath) + } + + return newPaths } export function writeFileSync(filePath: string, data: string) {