fix: pre-publish and post-publish hooks
This commit is contained in:
3
.gitignore
vendored
3
.gitignore
vendored
@@ -25,6 +25,9 @@ dist-ssr
|
|||||||
*.sw?
|
*.sw?
|
||||||
.qoder
|
.qoder
|
||||||
|
|
||||||
|
# publish backup
|
||||||
|
package.json.bak
|
||||||
|
|
||||||
# env files
|
# env files
|
||||||
.env
|
.env
|
||||||
.env.*
|
.env.*
|
||||||
|
|||||||
@@ -33,6 +33,7 @@
|
|||||||
"build:website": "npm run build:website --workspace=@page-agent/website",
|
"build:website": "npm run build:website --workspace=@page-agent/website",
|
||||||
"build:ext": "npm run build:libs && npm run zip -w @page-agent/ext",
|
"build:ext": "npm run build:libs && npm run zip -w @page-agent/ext",
|
||||||
"version": "node scripts/sync-version.js",
|
"version": "node scripts/sync-version.js",
|
||||||
|
"postpublish": "npm run postpublish --workspaces --if-present",
|
||||||
"typecheck": "tsc --noEmit -p tsconfig.typecheck.json && tsc --noEmit -p packages/extension/tsconfig.json",
|
"typecheck": "tsc --noEmit -p tsconfig.typecheck.json && tsc --noEmit -p packages/extension/tsconfig.json",
|
||||||
"lint": "eslint .",
|
"lint": "eslint .",
|
||||||
"cleanup": "rm -rf packages/*/dist && rm -rf packages/*/.output",
|
"cleanup": "rm -rf packages/*/dist && rm -rf packages/*/.output",
|
||||||
|
|||||||
@@ -49,8 +49,8 @@
|
|||||||
"scripts": {
|
"scripts": {
|
||||||
"build": "vite build",
|
"build": "vite build",
|
||||||
"dev:iife": "concurrently \"vite build --config vite.iife.config.js --watch\" \"npx serve dist/iife -p 5174\"",
|
"dev:iife": "concurrently \"vite build --config vite.iife.config.js --watch\" \"npx serve dist/iife -p 5174\"",
|
||||||
"prepublishOnly": "node ../../scripts/prepare-publish.js",
|
"prepublishOnly": "node ../../scripts/pre-publish.js",
|
||||||
"postpublish": "node ../../scripts/restore-dev.js"
|
"postpublish": "node ../../scripts/post-publish.js"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"chalk": "^5.6.2",
|
"chalk": "^5.6.2",
|
||||||
|
|||||||
@@ -43,8 +43,8 @@
|
|||||||
"homepage": "https://alibaba.github.io/page-agent/",
|
"homepage": "https://alibaba.github.io/page-agent/",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"build": "vite build",
|
"build": "vite build",
|
||||||
"prepublishOnly": "node ../../scripts/prepare-publish.js",
|
"prepublishOnly": "node ../../scripts/pre-publish.js",
|
||||||
"postpublish": "node ../../scripts/restore-dev.js"
|
"postpublish": "node ../../scripts/post-publish.js"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"chalk": "^5.6.2"
|
"chalk": "^5.6.2"
|
||||||
|
|||||||
@@ -50,8 +50,8 @@
|
|||||||
"build": "vite build && npm run build:demo",
|
"build": "vite build && npm run build:demo",
|
||||||
"build:demo": "vite build --config vite.iife.config.js",
|
"build:demo": "vite build --config vite.iife.config.js",
|
||||||
"dev:demo": "concurrently \"vite build --config vite.iife.config.js --watch\" \"npx serve dist/iife -p 5174\"",
|
"dev:demo": "concurrently \"vite build --config vite.iife.config.js --watch\" \"npx serve dist/iife -p 5174\"",
|
||||||
"prepublishOnly": "node ../../scripts/prepare-publish.js",
|
"prepublishOnly": "node ../../scripts/pre-publish.js",
|
||||||
"postpublish": "node ../../scripts/restore-dev.js"
|
"postpublish": "node ../../scripts/post-publish.js"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@page-agent/core": "1.7.1",
|
"@page-agent/core": "1.7.1",
|
||||||
|
|||||||
@@ -42,8 +42,8 @@
|
|||||||
"homepage": "https://alibaba.github.io/page-agent/",
|
"homepage": "https://alibaba.github.io/page-agent/",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"build": "vite build",
|
"build": "vite build",
|
||||||
"prepublishOnly": "node ../../scripts/prepare-publish.js",
|
"prepublishOnly": "node ../../scripts/pre-publish.js",
|
||||||
"postpublish": "node ../../scripts/restore-dev.js"
|
"postpublish": "node ../../scripts/post-publish.js"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"ai-motion": "^0.4.8"
|
"ai-motion": "^0.4.8"
|
||||||
|
|||||||
@@ -42,7 +42,7 @@
|
|||||||
"homepage": "https://alibaba.github.io/page-agent/",
|
"homepage": "https://alibaba.github.io/page-agent/",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"build": "vite build",
|
"build": "vite build",
|
||||||
"prepublishOnly": "node ../../scripts/prepare-publish.js",
|
"prepublishOnly": "node ../../scripts/pre-publish.js",
|
||||||
"postpublish": "node ../../scripts/restore-dev.js"
|
"postpublish": "node ../../scripts/post-publish.js"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
30
scripts/post-publish.js
Normal file
30
scripts/post-publish.js
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
#!/usr/bin/env node
|
||||||
|
/**
|
||||||
|
* Restore package.json from the backup created by pre-publish.js,
|
||||||
|
* then clean up temporary files (backup, LICENSE, README.md).
|
||||||
|
*
|
||||||
|
* Usage: node ../../scripts/post-publish.js (from a package dir)
|
||||||
|
*/
|
||||||
|
import { existsSync, readFileSync, renameSync, rmSync } from 'fs'
|
||||||
|
import { join } from 'path'
|
||||||
|
|
||||||
|
const pkgPath = join(process.cwd(), 'package.json')
|
||||||
|
const bakPath = pkgPath + '.bak'
|
||||||
|
|
||||||
|
if (!existsSync(bakPath)) {
|
||||||
|
console.log(' No backup found, nothing to restore.')
|
||||||
|
process.exit(0)
|
||||||
|
}
|
||||||
|
|
||||||
|
const name = JSON.parse(readFileSync(pkgPath, 'utf-8')).name
|
||||||
|
|
||||||
|
renameSync(bakPath, pkgPath)
|
||||||
|
console.log(' ✓ package.json restored from backup')
|
||||||
|
|
||||||
|
rmSync(join(process.cwd(), 'LICENSE'), { force: true })
|
||||||
|
console.log(' ✓ LICENSE removed')
|
||||||
|
|
||||||
|
if (name === 'page-agent') {
|
||||||
|
rmSync(join(process.cwd(), 'README.md'), { force: true })
|
||||||
|
console.log(' ✓ README.md removed')
|
||||||
|
}
|
||||||
42
scripts/pre-publish.js
Normal file
42
scripts/pre-publish.js
Normal file
@@ -0,0 +1,42 @@
|
|||||||
|
#!/usr/bin/env node
|
||||||
|
/**
|
||||||
|
* Backup package.json, then rewrite it for publish:
|
||||||
|
* - Promote `publishConfig` fields to top level
|
||||||
|
* - Remove `publishConfig` (npm doesn't need the wrapper)
|
||||||
|
* - Copy LICENSE (and README.md for the main package)
|
||||||
|
*
|
||||||
|
* Usage: node ../../scripts/pre-publish.js (from a package dir)
|
||||||
|
*/
|
||||||
|
import { copyFileSync, readFileSync, writeFileSync } from 'fs'
|
||||||
|
import { join } from 'path'
|
||||||
|
|
||||||
|
const pkgPath = join(process.cwd(), 'package.json')
|
||||||
|
const raw = readFileSync(pkgPath, 'utf-8')
|
||||||
|
const pkg = JSON.parse(raw)
|
||||||
|
|
||||||
|
const publishConfig = pkg.publishConfig
|
||||||
|
if (!publishConfig) {
|
||||||
|
console.log(' No publishConfig found, skipping manifest rewrite.')
|
||||||
|
process.exit(0)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Backup the original file byte-for-byte
|
||||||
|
copyFileSync(pkgPath, pkgPath + '.bak')
|
||||||
|
console.log(' ✓ package.json backed up')
|
||||||
|
|
||||||
|
for (const [field, value] of Object.entries(publishConfig)) {
|
||||||
|
pkg[field] = value
|
||||||
|
}
|
||||||
|
delete pkg.publishConfig
|
||||||
|
|
||||||
|
writeFileSync(pkgPath, JSON.stringify(pkg, null, ' ') + '\n')
|
||||||
|
console.log(` ✓ Manifest rewritten for publish (${Object.keys(publishConfig).join(', ')})`)
|
||||||
|
|
||||||
|
const root = join(process.cwd(), '../..')
|
||||||
|
copyFileSync(join(root, 'LICENSE'), join(process.cwd(), 'LICENSE'))
|
||||||
|
console.log(' ✓ LICENSE copied')
|
||||||
|
|
||||||
|
if (pkg.name === 'page-agent') {
|
||||||
|
copyFileSync(join(root, 'README.md'), join(process.cwd(), 'README.md'))
|
||||||
|
console.log(' ✓ README.md copied')
|
||||||
|
}
|
||||||
@@ -1,48 +0,0 @@
|
|||||||
#!/usr/bin/env node
|
|
||||||
/**
|
|
||||||
* Swap source-first dev entrypoints for dist-first publish entrypoints.
|
|
||||||
*
|
|
||||||
* This script is called by `prepublishOnly` in each publishable package.
|
|
||||||
* It reads the `publishConfig` object and promotes its fields to top level,
|
|
||||||
* storing the original dev fields in `_devConfig` so `restore-dev.js` can undo it.
|
|
||||||
*
|
|
||||||
* Usage: node ../../scripts/prepare-publish.js
|
|
||||||
*/
|
|
||||||
import { copyFileSync, readFileSync, writeFileSync } from 'fs'
|
|
||||||
import { join } from 'path'
|
|
||||||
|
|
||||||
const pkgPath = join(process.cwd(), 'package.json')
|
|
||||||
const pkg = JSON.parse(readFileSync(pkgPath, 'utf-8'))
|
|
||||||
|
|
||||||
const publishConfig = pkg.publishConfig
|
|
||||||
if (!publishConfig) {
|
|
||||||
console.log(' No publishConfig found, skipping manifest rewrite.')
|
|
||||||
process.exit(0)
|
|
||||||
}
|
|
||||||
|
|
||||||
const PUBLISH_FIELDS = ['main', 'module', 'types', 'exports']
|
|
||||||
|
|
||||||
const devConfig = {}
|
|
||||||
for (const field of PUBLISH_FIELDS) {
|
|
||||||
if (field in publishConfig) {
|
|
||||||
devConfig[field] = pkg[field]
|
|
||||||
pkg[field] = publishConfig[field]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pkg._devConfig = devConfig
|
|
||||||
delete pkg.publishConfig
|
|
||||||
|
|
||||||
writeFileSync(pkgPath, JSON.stringify(pkg, null, ' ') + '\n')
|
|
||||||
console.log(` ✓ Manifest rewritten for publish (${Object.keys(devConfig).join(', ')})`)
|
|
||||||
|
|
||||||
const root = join(process.cwd(), '../..')
|
|
||||||
copyFileSync(join(root, 'LICENSE'), join(process.cwd(), 'LICENSE'))
|
|
||||||
console.log(' ✓ LICENSE copied')
|
|
||||||
|
|
||||||
const readmeSrc = join(root, 'README.md')
|
|
||||||
const readmeDest = join(process.cwd(), 'README.md')
|
|
||||||
if (pkg.name === 'page-agent') {
|
|
||||||
copyFileSync(readmeSrc, readmeDest)
|
|
||||||
console.log(' ✓ README.md copied')
|
|
||||||
}
|
|
||||||
@@ -1,45 +0,0 @@
|
|||||||
#!/usr/bin/env node
|
|
||||||
/**
|
|
||||||
* Restore source-first dev entrypoints after publish.
|
|
||||||
*
|
|
||||||
* Undoes what `prepare-publish.js` did by reading `_devConfig`
|
|
||||||
* and reinstating the original dev fields + `publishConfig`.
|
|
||||||
*
|
|
||||||
* Usage: node ../../scripts/restore-dev.js
|
|
||||||
*/
|
|
||||||
import { readFileSync, rmSync, writeFileSync } from 'fs'
|
|
||||||
import { join } from 'path'
|
|
||||||
|
|
||||||
const pkgPath = join(process.cwd(), 'package.json')
|
|
||||||
const pkg = JSON.parse(readFileSync(pkgPath, 'utf-8'))
|
|
||||||
|
|
||||||
const devConfig = pkg._devConfig
|
|
||||||
if (!devConfig) {
|
|
||||||
console.log(' No _devConfig found, skipping manifest restore.')
|
|
||||||
process.exit(0)
|
|
||||||
}
|
|
||||||
|
|
||||||
const PUBLISH_FIELDS = ['main', 'module', 'types', 'exports']
|
|
||||||
|
|
||||||
const publishConfig = {}
|
|
||||||
for (const field of PUBLISH_FIELDS) {
|
|
||||||
if (field in devConfig) {
|
|
||||||
publishConfig[field] = pkg[field]
|
|
||||||
pkg[field] = devConfig[field]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pkg.publishConfig = publishConfig
|
|
||||||
delete pkg._devConfig
|
|
||||||
|
|
||||||
writeFileSync(pkgPath, JSON.stringify(pkg, null, ' ') + '\n')
|
|
||||||
console.log(` ✓ Manifest restored for dev (${Object.keys(publishConfig).join(', ')})`)
|
|
||||||
|
|
||||||
const licensePath = join(process.cwd(), 'LICENSE')
|
|
||||||
rmSync(licensePath, { force: true })
|
|
||||||
console.log(' ✓ LICENSE removed')
|
|
||||||
|
|
||||||
if (pkg.name === 'page-agent') {
|
|
||||||
rmSync(join(process.cwd(), 'README.md'), { force: true })
|
|
||||||
console.log(' ✓ README.md removed')
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user