From d706e89f68ec4301c2f77d1bc1bbf3d58a15defd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carl-Gerhard=20Lindesva=CC=88rd?= Date: Thu, 2 Nov 2023 12:46:46 +0100 Subject: [PATCH] move publish script to its own package --- package.json | 7 --- packages/sdk/package.json | 1 - packages/types/package.json | 1 - pnpm-lock.yaml | 32 ++++++++---- publish.ts | 71 ------------------------- tooling/publish/package.json | 29 +++++++++++ tooling/publish/publish.ts | 98 +++++++++++++++++++++++++++++++++++ tooling/publish/tsconfig.json | 9 ++++ 8 files changed, 159 insertions(+), 89 deletions(-) delete mode 100644 publish.ts create mode 100644 tooling/publish/package.json create mode 100644 tooling/publish/publish.ts create mode 100644 tooling/publish/tsconfig.json diff --git a/package.json b/package.json index cae12299..84ca93c7 100644 --- a/package.json +++ b/package.json @@ -6,7 +6,6 @@ "author": "Carl-Gerhard Lindesvärd", "packageManager": "pnpm@8.7.6", "module": "index.ts", - "type": "module", "scripts": { "dev": "pnpm -r dev", "format": "pnpm -r format --cache --cache-location=\"node_modules/.cache/.prettiercache\"", @@ -15,11 +14,5 @@ "lint:fix": "pnpm -r lint --fix", "lint:workspace": "pnpm dlx sherif@latest", "typecheck": "pnpm -r typecheck" - }, - "devDependencies": { - "semver": "^7.5.4" - }, - "peerDependencies": { - "typescript": "^5.2.0" } } diff --git a/packages/sdk/package.json b/packages/sdk/package.json index ae2d0050..f90cacfc 100644 --- a/packages/sdk/package.json +++ b/packages/sdk/package.json @@ -1,7 +1,6 @@ { "name": "@mixan/sdk", "version": "0.0.1", - "type": "module", "module": "index.ts", "scripts": { "lint": "eslint .", diff --git a/packages/types/package.json b/packages/types/package.json index cea1460c..10e68f4e 100644 --- a/packages/types/package.json +++ b/packages/types/package.json @@ -1,7 +1,6 @@ { "name": "@mixan/types", "version": "0.0.1", - "type": "module", "module": "index.ts", "scripts": { "lint": "eslint .", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index dd6e1dba..29577168 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -6,15 +6,7 @@ settings: importers: - .: - dependencies: - typescript: - specifier: ^5.2.0 - version: 5.2.2 - devDependencies: - semver: - specifier: ^7.5.4 - version: 7.5.4 + .: {} apps/web: dependencies: @@ -299,6 +291,28 @@ importers: specifier: ^5.2.0 version: 5.2.2 + tooling/publish: + dependencies: + semver: + specifier: ^7.5.4 + version: 7.5.4 + devDependencies: + '@mixan/prettier-config': + specifier: workspace:* + version: link:../prettier + '@mixan/tsconfig': + specifier: workspace:* + version: link:../typescript + '@types/eslint': + specifier: ^8.44.2 + version: 8.44.6 + eslint: + specifier: ^8.48.0 + version: 8.52.0 + typescript: + specifier: ^5.2.0 + version: 5.2.2 + tooling/typescript: {} packages: diff --git a/publish.ts b/publish.ts deleted file mode 100644 index dd0fb8ba..00000000 --- a/publish.ts +++ /dev/null @@ -1,71 +0,0 @@ -import sdkPkg from './packages/sdk/package.json' -import typesPkg from './packages/types/package.json' -import fs from 'node:fs' -import { execSync } from 'node:child_process' -import semver from 'semver' - -function savePackageJson(path: string, data: Record) { - fs.writeFileSync(path, JSON.stringify(data, null, 2), 'utf-8') -} - -function main() { - const [version] = process.argv.slice(2) - - if (!version) { - return console.error('Missing version') - } - - if (!semver.valid(version)) { - return console.error('Version is not valid') - } - - const properties = { - private: false, - version, - type: 'module', - main: './dist/index.js', - module: './dist/index.mjs', - types: './dist/index.d.ts', - files: ['dist'], - } - - savePackageJson('./packages/sdk/package.json', { - ...sdkPkg, - ...properties, - dependencies: Object.entries(sdkPkg.dependencies).reduce( - (acc, [depName, depVersion]) => ({ - ...acc, - [depName]: depName.startsWith('@mixan') ? version : depVersion, - }), - {} - ), - }) - - savePackageJson('./packages/types/package.json', { - ...typesPkg, - ...properties, - }) - - try { - execSync('pnpm dlx tsup', { - cwd: './packages/sdk', - }) - execSync('pnpm dlx tsup', { - cwd: './packages/types', - }) - } catch (error) { - console.log('Build failed') - console.log(error) - process.exit(1) - } - - execSync('npm publish --access=public', { - cwd: './packages/sdk', - }) - - execSync('npm publish --access=public', { - cwd: './packages/types', - }) -} - -main() diff --git a/tooling/publish/package.json b/tooling/publish/package.json new file mode 100644 index 00000000..e8ac0f61 --- /dev/null +++ b/tooling/publish/package.json @@ -0,0 +1,29 @@ +{ + "name": "@mixan/publish", + "version": "0.1.0", + "private": true, + "license": "MIT", + "scripts": { + "publish": "pnpm dlx ts-node publish.ts", + "lint": "eslint .", + "format": "prettier --check \"**/*.{mjs,ts,md,json}\"", + "typecheck": "tsc --noEmit" + }, + "devDependencies": { + "@mixan/prettier-config": "workspace:*", + "@mixan/tsconfig": "workspace:*", + "@types/eslint": "^8.44.2", + "eslint": "^8.48.0", + "typescript": "^5.2.0" + }, + "eslintConfig": { + "root": true, + "extends": [ + "./base.js" + ] + }, + "prettier": "@mixan/prettier-config", + "dependencies": { + "semver": "^7.5.4" + } +} diff --git a/tooling/publish/publish.ts b/tooling/publish/publish.ts new file mode 100644 index 00000000..6b92adb0 --- /dev/null +++ b/tooling/publish/publish.ts @@ -0,0 +1,98 @@ +import { execSync } from 'node:child_process'; +import fs from 'node:fs'; +import path from 'node:path'; +import semver from 'semver'; + +import sdkPkg from '../../packages/sdk/package.json'; +import typesPkg from '../../packages/types/package.json'; + +const workspacePath = (relativePath: string) => + path.resolve(__dirname, '../../', relativePath); + +function savePackageJson(absPath: string, data: Record) { + fs.writeFileSync(absPath, JSON.stringify(data, null, 2), 'utf-8'); +} + +function exit(message: string, error?: unknown) { + console.log(`❌ ${message}`); + if (error instanceof Error) { + console.log(`Error: ${error.message}`); + } else if (typeof error === 'string') { + console.log(`Error: ${error}`); + } + process.exit(1); +} + +function main() { + const [version] = process.argv.slice(2); + + if (!version) { + return console.error('Missing version'); + } + + if (!semver.valid(version)) { + return console.error('Version is not valid'); + } + + const properties = { + private: false, + version, + type: 'module', + main: './dist/index.js', + module: './dist/index.mjs', + types: './dist/index.d.ts', + files: ['dist'], + }; + + try { + savePackageJson(workspacePath('./packages/sdk/package.json'), { + ...sdkPkg, + ...properties, + dependencies: Object.entries(sdkPkg.dependencies).reduce( + (acc, [depName, depVersion]) => ({ + ...acc, + [depName]: depName.startsWith('@mixan') ? version : depVersion, + }), + {} + ), + }); + + savePackageJson(workspacePath('./packages/types/package.json'), { + ...typesPkg, + ...properties, + }); + } catch (error) { + exit('Update JSON files', error); + } + + console.log('✅ Update JSON files'); + + try { + execSync('pnpm dlx tsup', { + cwd: workspacePath('./packages/sdk'), + }); + execSync('pnpm dlx tsup', { + cwd: workspacePath('./packages/types'), + }); + } catch (error) { + exit('Failed build packages', error); + } + + console.log('✅ Built packages'); + + try { + execSync('npm publish --access=public', { + cwd: './packages/sdk', + }); + + execSync('npm publish --access=public', { + cwd: './packages/types', + }); + } catch (error) { + exit('Failed publish packages', error); + } + + console.log('✅ All done!'); +} + +main(); diff --git a/tooling/publish/tsconfig.json b/tooling/publish/tsconfig.json new file mode 100644 index 00000000..75ade3d6 --- /dev/null +++ b/tooling/publish/tsconfig.json @@ -0,0 +1,9 @@ +{ + "extends": "@mixan/tsconfig/base.json", + "compilerOptions": { + "module": "CommonJS", + "tsBuildInfoFile": "node_modules/.cache/tsbuildinfo.json" + }, + "include": ["."], + "exclude": ["node_modules"] +}