move publish script to its own package

This commit is contained in:
Carl-Gerhard Lindesvärd
2023-11-02 12:46:46 +01:00
parent 493e1b7650
commit d706e89f68
8 changed files with 159 additions and 89 deletions

View File

@@ -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"
}
}

View File

@@ -1,7 +1,6 @@
{
"name": "@mixan/sdk",
"version": "0.0.1",
"type": "module",
"module": "index.ts",
"scripts": {
"lint": "eslint .",

View File

@@ -1,7 +1,6 @@
{
"name": "@mixan/types",
"version": "0.0.1",
"type": "module",
"module": "index.ts",
"scripts": {
"lint": "eslint .",

32
pnpm-lock.yaml generated
View File

@@ -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:

View File

@@ -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<string, any>) {
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()

View File

@@ -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"
}
}

View File

@@ -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<string, any>) {
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();

View File

@@ -0,0 +1,9 @@
{
"extends": "@mixan/tsconfig/base.json",
"compilerOptions": {
"module": "CommonJS",
"tsBuildInfoFile": "node_modules/.cache/tsbuildinfo.json"
},
"include": ["."],
"exclude": ["node_modules"]
}