From 0b13b1b50a0552e68b65a800bcaa204c87b1e0f0 Mon Sep 17 00:00:00 2001 From: Josh <37798644+joshuaellis@users.noreply.github.com> Date: Thu, 12 Oct 2023 15:01:25 +0100 Subject: [PATCH] chore: convert other packages to pack-up (#18339) * chore: convert logger to pack-up * chore: convert generators to TS * chore: convert generators/app * chore: convert core/types * chore: preserve modules for `generators/app` --- packages/core/types/index.js | 2 -- packages/core/types/package.json | 13 +++++++---- .../src/types/__tests__/utils/object.test.ts | 6 +++++ packages/generators/app/package.json | 9 +++++--- packages/generators/app/packup.config.ts | 8 +++++++ packages/generators/app/src/create-project.ts | 23 +++++++++---------- packages/generators/app/tsconfig.build.json | 7 ++++++ packages/generators/app/tsconfig.eslint.json | 2 +- packages/generators/app/tsconfig.json | 6 +---- packages/generators/generators/package.json | 9 +++++--- .../generators/generators/packup.config.ts | 14 +++++++++++ .../generators/generators/tsconfig.build.json | 7 ++++++ .../generators/tsconfig.eslint.json | 2 +- packages/generators/generators/tsconfig.json | 5 +--- packages/utils/logger/package.json | 9 +++++--- packages/utils/logger/packup.config.ts | 6 +++++ packages/utils/logger/src/formats/index.ts | 3 ++- packages/utils/logger/tsconfig.build.json | 8 +++++++ packages/utils/logger/tsconfig.eslint.json | 5 +--- packages/utils/logger/tsconfig.json | 7 ++---- test/helpers/test-app.js | 2 +- yarn.lock | 4 ++++ 22 files changed, 107 insertions(+), 50 deletions(-) delete mode 100644 packages/core/types/index.js create mode 100644 packages/generators/app/packup.config.ts create mode 100644 packages/generators/app/tsconfig.build.json create mode 100644 packages/generators/generators/packup.config.ts create mode 100644 packages/generators/generators/tsconfig.build.json create mode 100644 packages/utils/logger/packup.config.ts create mode 100644 packages/utils/logger/tsconfig.build.json diff --git a/packages/core/types/index.js b/packages/core/types/index.js deleted file mode 100644 index db1c359ae9..0000000000 --- a/packages/core/types/index.js +++ /dev/null @@ -1,2 +0,0 @@ -// this is a failsafe file in case the package gets required as code -module.exports = null; diff --git a/packages/core/types/package.json b/packages/core/types/package.json index 89d7be34fe..a8b0110bf5 100644 --- a/packages/core/types/package.json +++ b/packages/core/types/package.json @@ -26,21 +26,23 @@ "url": "https://strapi.io" } ], - "main": "index.js", + "main": "./dist/index.js", + "module": "./dist/index.mjs", + "source": "./src/index.ts", "types": "./dist/index.d.ts", "files": [ "./dist/**/!(*.d.ts.map)", - "index.js" + "./dist" ], "scripts": { - "build": "run -T tsc -p tsconfig.build.json", - "build:ts": "run build", + "build": "pack-up build", "clean": "run -T rimraf ./dist", "lint": "run -T eslint .", "prepublishOnly": "yarn clean && yarn build", + "test:ts": "run -T tsc --noEmit", "test:unit": "run -T jest", "test:unit:watch": "run -T jest --watch", - "watch": "run -T tsc -p tsconfig.build.json -w --preserveWatchOutput" + "watch": "pack-up watch" }, "dependencies": { "@koa/cors": "3.4.3", @@ -56,6 +58,7 @@ "node-schedule": "2.1.0" }, "devDependencies": { + "@strapi/pack-up": "workspace:*", "@strapi/ts-zen": "^0.2.0", "@types/jest": "29.5.2", "@types/koa": "2.13.4", diff --git a/packages/core/types/src/types/__tests__/utils/object.test.ts b/packages/core/types/src/types/__tests__/utils/object.test.ts index afb0ef7999..ee11fb6c8c 100644 --- a/packages/core/types/src/types/__tests__/utils/object.test.ts +++ b/packages/core/types/src/types/__tests__/utils/object.test.ts @@ -39,20 +39,24 @@ describe('Utils.Object', () => { test('PickBy', () => { type('PickByString').isMappedType({ properties: { + // @ts-expect-error - Wants to be string[] foo: t.stringLiteral('bar'), bar: t.stringLiteral('foo'), }, }); type('PickByNumber').isMappedType({ properties: { + // @ts-expect-error - Wants to be string[] foobar: t.numberLiteral(2), }, }); type('PickByNever').isMappedType({ + // @ts-expect-error - Thinks it _must_ have a property properties: {}, }); type('PickByUnknown').isMappedType({ properties: { + // @ts-expect-error - Wants to be string[] foobar: t.numberLiteral(2), foo: t.stringLiteral('bar'), bar: t.stringLiteral('foo'), @@ -60,7 +64,9 @@ describe('Utils.Object', () => { }); type('PickByObj').isMappedType({ properties: { + // @ts-expect-error - Wants to be string[] foo: t.mappedType({ properties: { x: t.stringLiteral('bar') } }), + // @ts-expect-error - Wants to be string[] bar: t.mappedType({ properties: { x: t.stringLiteral('foo') } }), }, }); diff --git a/packages/generators/app/package.json b/packages/generators/app/package.json index 5814a15c93..8ca8ebbeb8 100644 --- a/packages/generators/app/package.json +++ b/packages/generators/app/package.json @@ -29,18 +29,20 @@ } ], "main": "./dist/index.js", + "module": "./dist/index.mjs", + "source": "./src/index.ts", "types": "./dist/index.d.ts", "files": [ "./dist" ], "scripts": { - "build": "run -T tsc && run copy-files", - "build:ts": "run build", + "build": "pack-up build && run copy-files", "clean": "run -T rimraf ./dist", "copy-files": "copyfiles -u 1 -a 'src/resources/files/**/*' 'src/resources/dot-files/**/*' 'src/resources/**/*.template' dist", "lint": "run -T eslint .", "prepublishOnly": "yarn clean && yarn build", - "watch": "run -T tsc -w --preserveWatchOutput" + "test:ts": "run -T tsc --noEmit", + "watch": "pack-up watch" }, "dependencies": { "@sentry/node": "6.19.7", @@ -56,6 +58,7 @@ "tar": "6.1.13" }, "devDependencies": { + "@strapi/pack-up": "workspace:*", "copyfiles": "2.4.1" }, "engines": { diff --git a/packages/generators/app/packup.config.ts b/packages/generators/app/packup.config.ts new file mode 100644 index 0000000000..354dbbc23e --- /dev/null +++ b/packages/generators/app/packup.config.ts @@ -0,0 +1,8 @@ +// eslint-disable-next-line import/no-extraneous-dependencies +import { defineConfig } from '@strapi/pack-up'; + +export default defineConfig({ + externals: ['crypto', 'fs', 'node:fs', 'node:os', 'node:path', 'node:readline', 'os', 'path'], + preserveModules: true, + runtime: 'node', +}); diff --git a/packages/generators/app/src/create-project.ts b/packages/generators/app/src/create-project.ts index bd0e3f35c7..a62290c09a 100644 --- a/packages/generators/app/src/create-project.ts +++ b/packages/generators/app/src/create-project.ts @@ -12,6 +12,9 @@ import mergeTemplate from './utils/merge-template.js'; import tryGitInit from './utils/git'; import packageJSON from './resources/json/common/package.json'; +import jsconfig from './resources/json/js/jsconfig.json'; +import adminTsconfig from './resources/json/ts/tsconfig-admin.json'; +import serverTsconfig from './resources/json/ts/tsconfig-server.json'; import { createDatabaseConfig, generateDbEnvariables } from './resources/templates/database'; import createEnvFile from './resources/templates/env'; import { Configuration, Scope, isStderrError } from './types'; @@ -79,31 +82,27 @@ export default async function createProject( await trackUsage({ event: 'didWritePackageJSON', scope }); if (useTypescript) { - const tsJSONDir = join(__dirname, 'resources/json/ts'); const filesMap = { 'tsconfig-admin.json.js': 'src/admin', 'tsconfig-server.json.js': '.', }; for (const [fileName, path] of Object.entries(filesMap)) { - const srcPath = join(tsJSONDir, fileName); const destPath = join(rootPath, path, 'tsconfig.json'); - const json = require(srcPath).default(); - - await fse.writeJSON(destPath, json, { spaces: 2 }); + if (fileName === 'tsconfig-admin.json.js') { + await fse.writeJSON(destPath, adminTsconfig(), { spaces: 2 }); + } + if (fileName === 'tsconfig-server.json.js') { + await fse.writeJSON(destPath, serverTsconfig(), { spaces: 2 }); + } } } else { - const jsJSONDir = join(__dirname, 'resources/json/js'); const filesMap = { 'jsconfig.json.js': '.' }; - for (const [fileName, path] of Object.entries(filesMap)) { - const srcPath = join(jsJSONDir, fileName); + for (const [, path] of Object.entries(filesMap)) { const destPath = join(rootPath, path, 'jsconfig.json'); - - const json = require(srcPath).default(); - - await fse.writeJSON(destPath, json, { spaces: 2 }); + await fse.writeJSON(destPath, jsconfig(), { spaces: 2 }); } } diff --git a/packages/generators/app/tsconfig.build.json b/packages/generators/app/tsconfig.build.json new file mode 100644 index 0000000000..04e0f5134a --- /dev/null +++ b/packages/generators/app/tsconfig.build.json @@ -0,0 +1,7 @@ +{ + "extends": "./tsconfig.json", + "compilerOptions": { + "outDir": "dist" + }, + "exclude": ["**/__tests__/**", "src/resources/files"] +} diff --git a/packages/generators/app/tsconfig.eslint.json b/packages/generators/app/tsconfig.eslint.json index b531808514..d0192f9ff6 100644 --- a/packages/generators/app/tsconfig.eslint.json +++ b/packages/generators/app/tsconfig.eslint.json @@ -3,6 +3,6 @@ "compilerOptions": { "noEmit": true }, - "include": ["src"], + "include": ["src", "packup.config.ts"], "exclude": ["node_modules"] } diff --git a/packages/generators/app/tsconfig.json b/packages/generators/app/tsconfig.json index 8295d18f69..1972676bb3 100644 --- a/packages/generators/app/tsconfig.json +++ b/packages/generators/app/tsconfig.json @@ -1,9 +1,5 @@ { "extends": "tsconfig/base.json", - "compilerOptions": { - "outDir": "dist", - "target": "ES2021" - }, "include": ["src"], - "exclude": ["node_modules", "**/__tests__/**", "src/resources/files"] + "exclude": ["node_modules", "src/resources/files"] } diff --git a/packages/generators/generators/package.json b/packages/generators/generators/package.json index 153b7c9a36..cd58e62193 100644 --- a/packages/generators/generators/package.json +++ b/packages/generators/generators/package.json @@ -28,20 +28,22 @@ } ], "main": "./dist/index.js", + "module": "./dist/index.mjs", + "source": "./src/index.ts", "types": "./dist/index.d.ts", "files": [ "./dist" ], "scripts": { - "build": "run -T tsc && run copy-files", - "build:ts": "run build", + "build": "pack-up build && run copy-files", "clean": "run -T rimraf ./dist", "copy-files": "copyfiles -u 1 -a 'src/templates/**/*' 'src/files/**/*' dist", "lint": "run -T eslint .", "prepublishOnly": "yarn clean && yarn build", + "test:ts": "run -T tsc --noEmit", "test:unit": "run -T jest", "test:unit:watch": "run -T jest --watch", - "watch": "run -T tsc -w --preserveWatchOutput" + "watch": "pack-up watch" }, "dependencies": { "@sindresorhus/slugify": "1.1.0", @@ -55,6 +57,7 @@ "pluralize": "8.0.0" }, "devDependencies": { + "@strapi/pack-up": "workspace:*", "eslint-config-custom": "4.14.4", "tsconfig": "4.14.4" }, diff --git a/packages/generators/generators/packup.config.ts b/packages/generators/generators/packup.config.ts new file mode 100644 index 0000000000..0454bba8fb --- /dev/null +++ b/packages/generators/generators/packup.config.ts @@ -0,0 +1,14 @@ +// eslint-disable-next-line import/no-extraneous-dependencies +import { defineConfig } from '@strapi/pack-up'; + +export default defineConfig({ + bundles: [ + { + source: './src/plopfile.ts', + require: './dist/plopfile.js', + import: './dist/plopfile.mjs', + }, + ], + externals: ['node:path', 'path'], + runtime: 'node', +}); diff --git a/packages/generators/generators/tsconfig.build.json b/packages/generators/generators/tsconfig.build.json new file mode 100644 index 0000000000..ffaaa42409 --- /dev/null +++ b/packages/generators/generators/tsconfig.build.json @@ -0,0 +1,7 @@ +{ + "extends": "./tsconfig.json", + "compilerOptions": { + "outDir": "dist" + }, + "exclude": ["**/__tests__/**", "src/files", "src/templates"] +} diff --git a/packages/generators/generators/tsconfig.eslint.json b/packages/generators/generators/tsconfig.eslint.json index b531808514..d0192f9ff6 100644 --- a/packages/generators/generators/tsconfig.eslint.json +++ b/packages/generators/generators/tsconfig.eslint.json @@ -3,6 +3,6 @@ "compilerOptions": { "noEmit": true }, - "include": ["src"], + "include": ["src", "packup.config.ts"], "exclude": ["node_modules"] } diff --git a/packages/generators/generators/tsconfig.json b/packages/generators/generators/tsconfig.json index 3296ac4882..3f29a13c9d 100644 --- a/packages/generators/generators/tsconfig.json +++ b/packages/generators/generators/tsconfig.json @@ -1,8 +1,5 @@ { "extends": "tsconfig/base.json", - "compilerOptions": { - "outDir": "dist" - }, "include": ["src"], - "exclude": ["node_modules", "**/__tests__/**", "src/files", "src/templates"] + "exclude": ["node_modules", "src/files", "src/templates"] } diff --git a/packages/utils/logger/package.json b/packages/utils/logger/package.json index 4bfdf58a90..ec2b1d1445 100644 --- a/packages/utils/logger/package.json +++ b/packages/utils/logger/package.json @@ -24,23 +24,26 @@ } ], "main": "./dist/index.js", + "module": "./dist/index.mjs", + "source": "./src/index.ts", "types": "./dist/index.d.ts", "files": [ "./dist" ], "scripts": { - "build": "run -T tsc", - "build:ts": "run -T tsc", + "build": "pack-up build", "clean": "run -T rimraf ./dist", "lint": "run -T eslint .", "prepublishOnly": "yarn clean && yarn build", - "watch": "run -T tsc -w --preserveWatchOutput" + "test:ts": "run -T tsc --noEmit", + "watch": "pack-up watch" }, "dependencies": { "lodash": "4.17.21", "winston": "3.10.0" }, "devDependencies": { + "@strapi/pack-up": "workspace:*", "eslint-config-custom": "4.14.4", "tsconfig": "4.14.4" }, diff --git a/packages/utils/logger/packup.config.ts b/packages/utils/logger/packup.config.ts new file mode 100644 index 0000000000..95e06b2131 --- /dev/null +++ b/packages/utils/logger/packup.config.ts @@ -0,0 +1,6 @@ +// eslint-disable-next-line import/no-extraneous-dependencies +import { defineConfig } from '@strapi/pack-up'; + +export default defineConfig({ + runtime: 'node', +}); diff --git a/packages/utils/logger/src/formats/index.ts b/packages/utils/logger/src/formats/index.ts index 3664e59c5d..616c4fed2b 100644 --- a/packages/utils/logger/src/formats/index.ts +++ b/packages/utils/logger/src/formats/index.ts @@ -1,3 +1,4 @@ -export { default as prettyPrint, PrettyPrintOptions } from './pretty-print'; +export { default as prettyPrint } from './pretty-print'; +export type { PrettyPrintOptions } from './pretty-print'; export { default as levelFilter } from './level-filter'; export { default as excludeColors } from './exclude-colors'; diff --git a/packages/utils/logger/tsconfig.build.json b/packages/utils/logger/tsconfig.build.json new file mode 100644 index 0000000000..f6e9b92e7e --- /dev/null +++ b/packages/utils/logger/tsconfig.build.json @@ -0,0 +1,8 @@ +{ + "extends": "tsconfig/base.json", + "compilerOptions": { + "outDir": "dist" + }, + "include": ["src"], + "exclude": ["node_modules", "**/__tests__/**"] +} diff --git a/packages/utils/logger/tsconfig.eslint.json b/packages/utils/logger/tsconfig.eslint.json index a8e84f79ac..1c66acf6d8 100644 --- a/packages/utils/logger/tsconfig.eslint.json +++ b/packages/utils/logger/tsconfig.eslint.json @@ -1,6 +1,3 @@ { - "extends": "./tsconfig.json", - "compilerOptions": { - "noEmit": true, - }, + "extends": "./tsconfig" } diff --git a/packages/utils/logger/tsconfig.json b/packages/utils/logger/tsconfig.json index f6e9b92e7e..327ffb9a92 100644 --- a/packages/utils/logger/tsconfig.json +++ b/packages/utils/logger/tsconfig.json @@ -1,8 +1,5 @@ { "extends": "tsconfig/base.json", - "compilerOptions": { - "outDir": "dist" - }, - "include": ["src"], - "exclude": ["node_modules", "**/__tests__/**"] + "include": ["src", "packup.config.ts"], + "exclude": ["node_modules"] } diff --git a/test/helpers/test-app.js b/test/helpers/test-app.js index e299806325..211bdd43e2 100644 --- a/test/helpers/test-app.js +++ b/test/helpers/test-app.js @@ -4,7 +4,7 @@ const path = require('path'); const fs = require('fs'); const rimraf = require('rimraf'); const execa = require('execa'); -const generateNew = require('../../packages/generators/app/dist/generate-new').default; +const generateNew = require('../../packages/generators/app/dist/generate-new'); /** * Deletes a test app diff --git a/yarn.lock b/yarn.lock index 4cb28a9605..7daa6b73f3 100644 --- a/yarn.lock +++ b/yarn.lock @@ -7639,6 +7639,7 @@ __metadata: resolution: "@strapi/generate-new@workspace:packages/generators/app" dependencies: "@sentry/node": 6.19.7 + "@strapi/pack-up": "workspace:*" chalk: ^4.1.2 copyfiles: 2.4.1 execa: 5.1.1 @@ -7658,6 +7659,7 @@ __metadata: resolution: "@strapi/generators@workspace:packages/generators/generators" dependencies: "@sindresorhus/slugify": 1.1.0 + "@strapi/pack-up": "workspace:*" "@strapi/typescript-utils": 4.14.4 "@strapi/utils": 4.14.4 chalk: 4.1.2 @@ -7750,6 +7752,7 @@ __metadata: version: 0.0.0-use.local resolution: "@strapi/logger@workspace:packages/utils/logger" dependencies: + "@strapi/pack-up": "workspace:*" eslint-config-custom: 4.14.4 lodash: 4.17.21 tsconfig: 4.14.4 @@ -8347,6 +8350,7 @@ __metadata: "@koa/router": 10.1.1 "@strapi/database": 4.14.4 "@strapi/logger": 4.14.4 + "@strapi/pack-up": "workspace:*" "@strapi/permissions": 4.14.4 "@strapi/ts-zen": ^0.2.0 "@strapi/utils": 4.14.4