diff --git a/README.MD b/README.MD index 49fcf45d..ce0e7757 100644 --- a/README.MD +++ b/README.MD @@ -49,18 +49,19 @@ The following commands are available in the terminal: - `typeorm-extension db:drop` to drop the database - `typeorm-extension seed` seed the database -Alternatively, the full command path can be set in the package.json file to run it e.g. with ts-node. - ``` "scripts": { ... - "db:create": "ts-node ./node_modules/typeorm-extension/dist/cli/index.js db:create", - "db:drop": "ts-node ./node_modules/typeorm-extension/dist/cli/index.js db:drop", - "seed": "ts-node ./node_modules/typeorm-extension/dist/cli/index.js seed" + "db:create": "typeorm-extension db:create", + "db:drop": "typeorm-extension db:drop", + "seed": "typeorm-extension seed" ... } ``` +Ts-node is no longer needed due to the fact that TypeScript files are transpiled on the file as needed. +The same applies to files from another module system (CJS / ESM) 🧙‍. + > Read the [Seeding Configuration](#configuration) section to find out how to specify the path, > for the seeder- & factory-location. diff --git a/package-lock.json b/package-lock.json index 88e63117..0ed9b16f 100644 --- a/package-lock.json +++ b/package-lock.json @@ -10,7 +10,7 @@ "license": "MIT", "dependencies": { "@faker-js/faker": "^7.6.0", - "locter": "^0.7.0", + "locter": "^1.0.3", "rapiq": "^0.6.3", "reflect-metadata": "^0.1.13", "yargs": "^17.6.2" @@ -5118,13 +5118,18 @@ } }, "node_modules/ebec": { - "version": "0.1.0", - "resolved": "https://registry.npmjs.org/ebec/-/ebec-0.1.0.tgz", - "integrity": "sha512-QP7lg0ZqM266C//l53J2ywUPSi+rsZx8/uNxkBMUPL+GpGyeEy0V5opqc1fKS6fcV/vLNnCBe2z2kNxDCQMVqg==", + "version": "0.2.1", + "resolved": "https://registry.npmjs.org/ebec/-/ebec-0.2.1.tgz", + "integrity": "sha512-JFYoMVXROnfl0k53pir1/I1OyEtObxObz5og62aIiiCzlIK82lQLq80fJS8hHyFr0Xh5p4ZsBBn+iyk8Tt0H1A==", "dependencies": { - "smob": "^0.0.6" + "smob": "^0.1.0" } }, + "node_modules/ebec/node_modules/smob": { + "version": "0.1.0", + "resolved": "https://registry.npmjs.org/smob/-/smob-0.1.0.tgz", + "integrity": "sha512-u6ezVF7hN3AxA1onkbMsl46XZr1HYrtMksmckSVpkLI8bYJ5I34kHMvDZk8qNCfA0y54VnhWqCoU6DQMMStzpw==" + }, "node_modules/ecc-jsbn": { "version": "0.1.2", "resolved": "https://registry.npmjs.org/ecc-jsbn/-/ecc-jsbn-0.1.2.tgz", @@ -6677,9 +6682,9 @@ "dev": true }, "node_modules/glob": { - "version": "8.0.3", - "resolved": "https://registry.npmjs.org/glob/-/glob-8.0.3.tgz", - "integrity": "sha512-ull455NHSHI/Y1FqGaaYFaLGkNMMJbavMrEGFXG/PGrg6y7sutWHUHrz6gy6WEBH6akM1M414dWKCNs+IhKdiQ==", + "version": "8.1.0", + "resolved": "https://registry.npmjs.org/glob/-/glob-8.1.0.tgz", + "integrity": "sha512-r8hpEjiQEYlF2QU0df3dS+nxxSIreXQS1qRhMJM0Q5NDdR386C7jb7Hwwod8Fgiuex+k0GFjgft18yvxm5XoCQ==", "dependencies": { "fs.realpath": "^1.0.0", "inflight": "^1.0.4", @@ -6715,9 +6720,9 @@ } }, "node_modules/glob/node_modules/minimatch": { - "version": "5.0.1", - "resolved": "https://registry.npmjs.org/minimatch/-/minimatch-5.0.1.tgz", - "integrity": "sha512-nLDxIFRyhDblz3qMuq+SoRZED4+miJ/G+tdDrjkkkRnjAsBexeGpgjLEQ0blJy7rHhR2b93rhQY4SvyWu9v03g==", + "version": "5.1.6", + "resolved": "https://registry.npmjs.org/minimatch/-/minimatch-5.1.6.tgz", + "integrity": "sha512-lKwV/1brpG6mBUFHtb7NUmtABCb2WZZmm2wNiOA5hAb8VdCS4B3dtMWyvcoViccwAW/COERjXLt0zP1zXUN26g==", "dependencies": { "brace-expansion": "^2.0.1" }, @@ -8518,6 +8523,14 @@ "url": "https://github.com/chalk/supports-color?sponsor=1" } }, + "node_modules/jiti": { + "version": "1.17.0", + "resolved": "https://registry.npmjs.org/jiti/-/jiti-1.17.0.tgz", + "integrity": "sha512-CByzPgFqYoB9odEeef7GNmQ3S5THIBOtzRYoSCya2Sv27AuQxy2jgoFjQ6VTF53xsq1MXRm+YWNvOoDHUAteOw==", + "bin": { + "jiti": "bin/jiti.js" + } + }, "node_modules/js-sdsl": { "version": "4.1.4", "resolved": "https://registry.npmjs.org/js-sdsl/-/js-sdsl-4.1.4.tgz", @@ -8830,12 +8843,13 @@ } }, "node_modules/locter": { - "version": "0.7.0", - "resolved": "https://registry.npmjs.org/locter/-/locter-0.7.0.tgz", - "integrity": "sha512-eI1ACD1GSbV42QwoVNutpD8u7I8dGmjqVZs9EevAC2Gum46tipFpIJmIdPPzPm5kDfO9qRuiY9W2r6xi01xNJQ==", + "version": "1.0.3", + "resolved": "https://registry.npmjs.org/locter/-/locter-1.0.3.tgz", + "integrity": "sha512-q/mI6BrWzGDYxT5AL7ydSM5jIVbzckV6HDRpDFQ0qSRHrOLtfwPwjKnQmbuKX+i0SQH0UHH23KopzsEWGE85QQ==", "dependencies": { - "ebec": "^0.1.0", - "glob": "^8.0.3" + "ebec": "^0.2.1", + "glob": "^8.1.0", + "jiti": "^1.17.0" } }, "node_modules/lodash": { @@ -19204,11 +19218,18 @@ } }, "ebec": { - "version": "0.1.0", - "resolved": "https://registry.npmjs.org/ebec/-/ebec-0.1.0.tgz", - "integrity": "sha512-QP7lg0ZqM266C//l53J2ywUPSi+rsZx8/uNxkBMUPL+GpGyeEy0V5opqc1fKS6fcV/vLNnCBe2z2kNxDCQMVqg==", + "version": "0.2.1", + "resolved": "https://registry.npmjs.org/ebec/-/ebec-0.2.1.tgz", + "integrity": "sha512-JFYoMVXROnfl0k53pir1/I1OyEtObxObz5og62aIiiCzlIK82lQLq80fJS8hHyFr0Xh5p4ZsBBn+iyk8Tt0H1A==", "requires": { - "smob": "^0.0.6" + "smob": "^0.1.0" + }, + "dependencies": { + "smob": { + "version": "0.1.0", + "resolved": "https://registry.npmjs.org/smob/-/smob-0.1.0.tgz", + "integrity": "sha512-u6ezVF7hN3AxA1onkbMsl46XZr1HYrtMksmckSVpkLI8bYJ5I34kHMvDZk8qNCfA0y54VnhWqCoU6DQMMStzpw==" + } } }, "ecc-jsbn": { @@ -20385,9 +20406,9 @@ "dev": true }, "glob": { - "version": "8.0.3", - "resolved": "https://registry.npmjs.org/glob/-/glob-8.0.3.tgz", - "integrity": "sha512-ull455NHSHI/Y1FqGaaYFaLGkNMMJbavMrEGFXG/PGrg6y7sutWHUHrz6gy6WEBH6akM1M414dWKCNs+IhKdiQ==", + "version": "8.1.0", + "resolved": "https://registry.npmjs.org/glob/-/glob-8.1.0.tgz", + "integrity": "sha512-r8hpEjiQEYlF2QU0df3dS+nxxSIreXQS1qRhMJM0Q5NDdR386C7jb7Hwwod8Fgiuex+k0GFjgft18yvxm5XoCQ==", "requires": { "fs.realpath": "^1.0.0", "inflight": "^1.0.4", @@ -20405,9 +20426,9 @@ } }, "minimatch": { - "version": "5.0.1", - "resolved": "https://registry.npmjs.org/minimatch/-/minimatch-5.0.1.tgz", - "integrity": "sha512-nLDxIFRyhDblz3qMuq+SoRZED4+miJ/G+tdDrjkkkRnjAsBexeGpgjLEQ0blJy7rHhR2b93rhQY4SvyWu9v03g==", + "version": "5.1.6", + "resolved": "https://registry.npmjs.org/minimatch/-/minimatch-5.1.6.tgz", + "integrity": "sha512-lKwV/1brpG6mBUFHtb7NUmtABCb2WZZmm2wNiOA5hAb8VdCS4B3dtMWyvcoViccwAW/COERjXLt0zP1zXUN26g==", "requires": { "brace-expansion": "^2.0.1" } @@ -21745,6 +21766,11 @@ } } }, + "jiti": { + "version": "1.17.0", + "resolved": "https://registry.npmjs.org/jiti/-/jiti-1.17.0.tgz", + "integrity": "sha512-CByzPgFqYoB9odEeef7GNmQ3S5THIBOtzRYoSCya2Sv27AuQxy2jgoFjQ6VTF53xsq1MXRm+YWNvOoDHUAteOw==" + }, "js-sdsl": { "version": "4.1.4", "resolved": "https://registry.npmjs.org/js-sdsl/-/js-sdsl-4.1.4.tgz", @@ -21991,12 +22017,13 @@ } }, "locter": { - "version": "0.7.0", - "resolved": "https://registry.npmjs.org/locter/-/locter-0.7.0.tgz", - "integrity": "sha512-eI1ACD1GSbV42QwoVNutpD8u7I8dGmjqVZs9EevAC2Gum46tipFpIJmIdPPzPm5kDfO9qRuiY9W2r6xi01xNJQ==", + "version": "1.0.3", + "resolved": "https://registry.npmjs.org/locter/-/locter-1.0.3.tgz", + "integrity": "sha512-q/mI6BrWzGDYxT5AL7ydSM5jIVbzckV6HDRpDFQ0qSRHrOLtfwPwjKnQmbuKX+i0SQH0UHH23KopzsEWGE85QQ==", "requires": { - "ebec": "^0.1.0", - "glob": "^8.0.3" + "ebec": "^0.2.1", + "glob": "^8.1.0", + "jiti": "^1.17.0" } }, "lodash": { diff --git a/package.json b/package.json index 2a733400..06580bd3 100644 --- a/package.json +++ b/package.json @@ -20,7 +20,7 @@ "build": "rm -rf ./dist && tsc", "build:watch": "npm run build -- --watch", "commit": "npx git-cz", - "test": "cross-env NODE_ENV=test jest --config ./test/jest.config.js", + "test": "jest --config ./test/jest.config.js", "test:coverage": "cross-env NODE_ENV=test jest --config ./test/jest.config.js --coverage", "lint": "eslint --ext .js,.vue,.ts ./src", "lint:fix": "npm run lint -- --fix", @@ -48,7 +48,7 @@ "license": "MIT", "dependencies": { "@faker-js/faker": "^7.6.0", - "locter": "^0.7.0", + "locter": "^1.0.3", "rapiq": "^0.6.3", "reflect-metadata": "^0.1.13", "yargs": "^17.6.2" diff --git a/src/data-source/find/module.ts b/src/data-source/find/module.ts index 776984c4..167fdd5b 100644 --- a/src/data-source/find/module.ts +++ b/src/data-source/find/module.ts @@ -1,6 +1,6 @@ import { - loadScriptFileSync, - locateFile, + load, + locate, removeFileNameExtension, } from 'locter'; import path from 'path'; @@ -77,13 +77,13 @@ export async function findDataSource( } for (let i = 0; i < fileNames.length; i++) { - const info = await locateFile(`${fileNames[i]}.{cjs,js,mjs,ts}`, { + const info = await locate(`${fileNames[i]}.{cjs,js,mjs,ts}`, { path: paths, ignore: ['**/*.d.ts'], }); if (info) { - const fileExports = loadScriptFileSync(info); + const fileExports = await load(info); if (InstanceChecker.isDataSource(fileExports)) { return fileExports; } diff --git a/src/seeder/module.ts b/src/seeder/module.ts index 4d6e09a8..0c36f520 100644 --- a/src/seeder/module.ts +++ b/src/seeder/module.ts @@ -1,8 +1,6 @@ import { DataSource, DataSourceOptions } from 'typeorm'; -import { - loadScriptFileExportSync, - loadScriptFileSync, -} from 'locter'; +import { load } from 'locter'; +import { hasOwnProperty } from '../utils'; import { SeederConstructor, SeederOptions } from './type'; import { resolveFilePaths, resolveFilePatterns, setDefaultSeederOptions } from './utils'; import { modifyDataSourceOptionForRuntimeEnvironment, setDataSource } from '../data-source'; @@ -35,7 +33,7 @@ async function prepareSeeder( factoryFiles = resolveFilePaths(factoryFiles); for (let i = 0; i < factoryFiles.length; i++) { - loadScriptFileSync(factoryFiles[i]); + await load(factoryFiles[i]); } } @@ -71,9 +69,13 @@ async function prepareSeeder( seedFiles = resolveFilePaths(seedFiles); for (let i = 0; i < seedFiles.length; i++) { - const fileExport = loadScriptFileExportSync(seedFiles[i]); + let fileExport = await load(seedFiles[i]); + if (hasOwnProperty(fileExport, 'default')) { + fileExport = fileExport.default; + } + if (fileExport) { - const item = fileExport.value as SeederConstructor; + const item = fileExport as SeederConstructor; if (!options.seedName || options.seedName === item.name) { items.push(item); @@ -99,6 +101,10 @@ export async function runSeeder( seeder: SeederConstructor, options?: SeederOptions, ) : Promise { + if (hasOwnProperty(seeder, 'default')) { + seeder = seeder.default as SeederConstructor; + } + options = options || {}; options.seeds = [seeder]; options.factoriesLoad = options.factoriesLoad ?? true; diff --git a/src/seeder/utils/file-path.ts b/src/seeder/utils/file-path.ts index 9f3fff82..d7304081 100644 --- a/src/seeder/utils/file-path.ts +++ b/src/seeder/utils/file-path.ts @@ -1,12 +1,12 @@ /* istanbul ignore next */ import path from 'path'; -import { locateFiles } from 'locter'; +import { locateMany } from 'locter'; export async function resolveFilePatterns( filesPattern: string[], root?: string, ) : Promise { - return locateFiles( + return locateMany( filesPattern, { ...(root ? { path: root } : {}),