Skip to content

Commit

Permalink
build script
Browse files Browse the repository at this point in the history
  • Loading branch information
saqqdy committed Apr 28, 2024
1 parent 9ecd980 commit 165ea0c
Show file tree
Hide file tree
Showing 17 changed files with 103 additions and 66 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@gitmars/monorepo",
"description": "这是一个git工作流工具",
"version": "7.0.0-beta.19",
"version": "7.0.0-beta.20",
"packageManager": "[email protected]",
"main": "index.js",
"files": [
Expand Down
2 changes: 1 addition & 1 deletion packages/api/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@gitmars/api",
"description": "gitmars api",
"version": "7.0.0-beta.19",
"version": "7.0.0-beta.20",
"private": false,
"main": "./dist/index.cjs.js",
"module": "./dist/index.esm-bundler.js",
Expand Down
2 changes: 1 addition & 1 deletion packages/build/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@gitmars/build",
"description": "gitmars build",
"version": "7.0.0-beta.19",
"version": "7.0.0-beta.20",
"private": false,
"main": "./dist/index.cjs.js",
"module": "./dist/index.esm-bundler.js",
Expand Down
34 changes: 30 additions & 4 deletions packages/build/src/buildConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,17 @@ import apollo from 'node-apollo'
import sh from 'shelljs'
import request from '@jssj/request'
import chalk from 'chalk'
import to from 'await-to-done'
import { CACHE_PATH, isCacheExpired, updateCacheTime } from '@gitmars/cache'
import { debug, isFileExist, removeFile, writeFile } from '@gitmars/utils'
import { getConfig } from '@gitmars/git'
import type { ApolloConfigType, GitmarsConfigApisBuildConfigType, GitmarsConfigType } from './types'
import type {
ApolloBranchList,
ApolloConfigProjectType,
ApolloConfigType,
GitmarsConfigApisBuildConfigType,
GitmarsConfigType
} from './types'
import lang from './lang'

const { t } = lang
Expand Down Expand Up @@ -35,7 +42,7 @@ function getNamespace(params: GitmarsConfigApisBuildConfigType['params'] = {}):
*
* @returns buildConfig - Return the configuration object
*/
export async function getBuildConfig(): Promise<ApolloConfigType | null> {
export async function getBuildConfig(): Promise<ApolloConfigType | undefined> {
const config = getConfig() as GitmarsConfigType
const { apis = {} } = config
let NS, _buildConfig
Expand All @@ -49,7 +56,7 @@ export async function getBuildConfig(): Promise<ApolloConfigType | null> {
} else {
sh.echo(chalk.red(t('Please configure apollo or buildConfigApi')))
process.exit(0)
return null
return
}

const BUILD_CONFIG_TIME_NAME = `buildConfigTime-${NS}`
Expand All @@ -76,7 +83,7 @@ export async function getBuildConfig(): Promise<ApolloConfigType | null> {
try {
apolloConfig = JSON.parse(config.apolloConfig)
} catch {
return null
return
}
} else {
apolloConfig = config.apolloConfig
Expand All @@ -89,6 +96,25 @@ export async function getBuildConfig(): Promise<ApolloConfigType | null> {
return _buildConfig
}

/**
* 读取项目配置
*
* @param projectName - project name
* @param env - env type, ApolloBranchList
* @param config - build config, not required
* @returns buildConfig - Return the configuration object
*/
export async function getProjectOption(
projectName: string,
env: ApolloBranchList,
config?: ApolloConfigType
): Promise<ApolloConfigProjectType | undefined> {
if (!config) {
;[, config] = await to(getBuildConfig().then(res => res || undefined))
}
return config?.[env]?.list?.find(item => item.name === projectName)
}

export function cleanBuildConfig() {
removeFile([
{
Expand Down
3 changes: 2 additions & 1 deletion packages/build/src/index.default.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import runJenkins from './runJenkins'
import { cleanBuildConfig, getBuildConfig } from './buildConfig'
import { cleanBuildConfig, getBuildConfig, getProjectOption } from './buildConfig'

export type { RunJenkinsOptionType } from './runJenkins'
export type * from './types'
export default {
version: '__VERSION__',
getBuildConfig,
getProjectOption,
cleanBuildConfig,
runJenkins
}
2 changes: 1 addition & 1 deletion packages/build/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export { default as runJenkins } from './runJenkins'
export { cleanBuildConfig, getBuildConfig } from './buildConfig'
export { cleanBuildConfig, getBuildConfig, getProjectOption } from './buildConfig'

export { default } from './index.default'
export type * from './index.default'
Expand Down
2 changes: 1 addition & 1 deletion packages/cache/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@gitmars/cache",
"description": "gitmars cache",
"version": "7.0.0-beta.19",
"version": "7.0.0-beta.20",
"private": false,
"main": "./dist/index.cjs.js",
"module": "./dist/index.esm-bundler.js",
Expand Down
2 changes: 1 addition & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@gitmars/core",
"description": "gitmars核心程序",
"version": "7.0.0-beta.19",
"version": "7.0.0-beta.20",
"private": false,
"main": "./dist/index.cjs.js",
"module": "./dist/index.esm-bundler.js",
Expand Down
2 changes: 1 addition & 1 deletion packages/docs/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@gitmars/docs",
"description": "gitmars文档库",
"version": "7.0.0-beta.19",
"version": "7.0.0-beta.20",
"private": false,
"files": [
"dist",
Expand Down
2 changes: 1 addition & 1 deletion packages/git/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@gitmars/git",
"description": "gitmars git",
"version": "7.0.0-beta.19",
"version": "7.0.0-beta.20",
"private": false,
"main": "./dist/index.cjs.js",
"module": "./dist/index.esm-bundler.js",
Expand Down
2 changes: 1 addition & 1 deletion packages/gitmars/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "gitmars",
"description": "这是一个git工作流工具",
"type": "module",
"version": "7.0.0-beta.19",
"version": "7.0.0-beta.20",
"private": false,
"bin": {
"gitm": "lib/gitm.mjs"
Expand Down
13 changes: 5 additions & 8 deletions packages/gitmars/src/gitm-build-mp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import chalk from 'chalk'
import to from 'await-to-done'
import { createArgs } from '@gitmars/utils'
import { getGitConfig, getIsGitProject } from '@gitmars/git'
import { getBuildConfig, runJenkins } from '@gitmars/build'
import type { ApolloBranchList, ApolloConfigProjectType } from '@gitmars/build'
import { getBuildConfig, getProjectOption, runJenkins } from '@gitmars/build'
import type { ApolloBranchList } from '@gitmars/build'
import type { GitmarsOptionOptionsType } from './types'
import lang from './common/local'
import buildMpConfig from './conf/build-mp'
Expand Down Expand Up @@ -54,8 +54,6 @@ program.action(async (project: string, opt: GitmBuildMpOption): Promise<void> =>
mini_program = opt.miniprogram || data.mini_program,
description = opt.description || data.description

const buildConfig = await getBuildConfig()

if (!project) {
if (getIsGitProject()) project = getGitConfig().appName
else
Expand Down Expand Up @@ -90,18 +88,17 @@ program.action(async (project: string, opt: GitmBuildMpOption): Promise<void> =>
)
}

const [, buildConfig] = await to(getBuildConfig())
const projectOption = await getProjectOption(project, env!, buildConfig)
if (!app) {
const projectOption = buildConfig?.[env!].list.find(
(item: ApolloConfigProjectType) => item.name === project
)
if (!buildConfig) {
;[, app] = await to(
input({
message: t('Enter the miniprogram to build'),
transformer: val => val.trim()
})
)
} else if (projectOption && projectOption.apps) {
} else if (projectOption?.apps) {
;[, app] = await to(
select<string>({
message: t('Select the miniprogram to build'),
Expand Down
13 changes: 5 additions & 8 deletions packages/gitmars/src/gitm-build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import chalk from 'chalk'
import to from 'await-to-done'
import { createArgs } from '@gitmars/utils'
import { getGitConfig, getIsGitProject } from '@gitmars/git'
import { getBuildConfig, runJenkins } from '@gitmars/build'
import type { ApolloBranchList, ApolloConfigProjectType } from '@gitmars/build'
import { getBuildConfig, getProjectOption, runJenkins } from '@gitmars/build'
import type { ApolloBranchList } from '@gitmars/build'
import type { GitmarsOptionOptionsType } from './types'
import lang from './common/local'
import buildConfig from './conf/build'
Expand Down Expand Up @@ -43,8 +43,6 @@ program.action(async (project: string, opt: GitmBuildOption): Promise<void> => {
app = opt.app,
_confirm = opt.confirm

const buildConfig = await getBuildConfig()

if (!project) {
if (getIsGitProject()) project = getGitConfig().appName
else
Expand Down Expand Up @@ -79,18 +77,17 @@ program.action(async (project: string, opt: GitmBuildOption): Promise<void> => {
)
}

const [, buildConfig] = await to(getBuildConfig())
const projectOption = await getProjectOption(project, env!, buildConfig)
if (!app) {
const projectOption = buildConfig?.[env!].list.find(
(item: ApolloConfigProjectType) => item.name === project
)
if (!buildConfig) {
;[, app] = await to(
input({
message: t('Enter the miniprogram to build'),
transformer: val => val.trim()
})
)
} else if (projectOption && projectOption.apps) {
} else if (projectOption?.apps) {
;[, app] = await to(
select<string>({
message: t('Select the miniprogram to build'),
Expand Down
2 changes: 1 addition & 1 deletion packages/go/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@gitmars/go",
"description": "gitmars go",
"version": "7.0.0-beta.19",
"version": "7.0.0-beta.20",
"private": false,
"main": "./dist/index.cjs.js",
"module": "./dist/index.esm-bundler.js",
Expand Down
2 changes: 1 addition & 1 deletion packages/hook/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@gitmars/hook",
"description": "gitmars hook",
"version": "7.0.0-beta.19",
"version": "7.0.0-beta.20",
"private": false,
"main": "./dist/index.cjs.js",
"module": "./dist/index.esm-bundler.js",
Expand Down
2 changes: 1 addition & 1 deletion packages/utils/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@gitmars/utils",
"description": "gitmars utils",
"version": "7.0.0-beta.19",
"version": "7.0.0-beta.20",
"private": false,
"main": "./dist/index.cjs.js",
"module": "./dist/index.esm-bundler.js",
Expand Down
82 changes: 49 additions & 33 deletions scripts/publish.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { execSync } from 'child_process'
import { join, sep } from 'path'
import consola from 'consola'
import { clone } from 'js-cool'
import { readJSONSync, writeJSONSync } from '@node-kit/extra.fs'
import { version } from '../package.json'
import { packages } from '../build/packages'
Expand All @@ -12,6 +13,7 @@ export const ROOT = join(__dirname, '..')
export const PACKAGE = join(ROOT, 'packages')

const REGISTRY_URL = 'https://registry.npmjs.org'
const jsonMap: Record<string, any> = {}
let command = `npm --registry=${REGISTRY_URL} publish --access public`
if (IS_DRY_RUN) command += ' --dry-run'

Expand All @@ -23,45 +25,59 @@ else if (IS_TEST) {
process.exit(0)
}

transformPkgJson()

for (const { name, pkgName } of packages) {
const dirName = name.replace(/\./g, sep)
const cwd = name === 'monorepo' ? ROOT : join(PACKAGE, dirName)
const PKG_FILE = join(cwd, 'package.json')
const pkgJson = readJSONSync(PKG_FILE)!
const newPkgJson = JSON.parse(JSON.stringify(pkgJson))
for (const { pkgName: pkg } of packages) {
if (
pkg in ((newPkgJson.dependencies as Record<string, unknown>) || {}) &&
newPkgJson.dependencies[pkg].includes('workspace')
) {
newPkgJson.dependencies[pkg] = version
}
if (
pkg in ((newPkgJson.devDependencies as Record<string, unknown>) || {}) &&
newPkgJson.devDependencies[pkg].includes('workspace')
) {
newPkgJson.devDependencies[pkg] = version
}
if (
pkg in ((newPkgJson.peerDependencies as Record<string, unknown>) || {}) &&
newPkgJson.peerDependencies[pkg].includes('workspace')
) {
newPkgJson.peerDependencies[pkg] = version
}
}
writeJSONSync(PKG_FILE, newPkgJson, {
encoding: 'utf8'
})
execSync(command, {
stdio: 'inherit',
cwd
})
writeJSONSync(PKG_FILE, pkgJson, {
encoding: 'utf8'
})
execSync(`npx prettier --write ${PKG_FILE}`, {
stdio: 'inherit',
cwd: ROOT
})
consola.success(`Published ${pkgName}`)
}

transformPkgJson(true)

function transformPkgJson(isFallback = false) {
for (const { name } of packages) {
const dirName = name.replace(/\./g, sep)
const cwd = name === 'monorepo' ? ROOT : join(PACKAGE, dirName)
const PKG_FILE = join(cwd, 'package.json')
if (!isFallback) {
jsonMap[name] = readJSONSync(PKG_FILE)!
const newPkgJson = clone(jsonMap[name])
for (const { pkgName: pkg } of packages) {
if (
pkg in ((newPkgJson.dependencies as Record<string, unknown>) || {}) &&
newPkgJson.dependencies[pkg].includes('workspace')
) {
newPkgJson.dependencies[pkg] = version
}
if (
pkg in ((newPkgJson.devDependencies as Record<string, unknown>) || {}) &&
newPkgJson.devDependencies[pkg].includes('workspace')
) {
newPkgJson.devDependencies[pkg] = version
}
if (
pkg in ((newPkgJson.peerDependencies as Record<string, unknown>) || {}) &&
newPkgJson.peerDependencies[pkg].includes('workspace')
) {
newPkgJson.peerDependencies[pkg] = version
}
}
writeJSONSync(PKG_FILE, newPkgJson, {
encoding: 'utf8'
})
} else {
writeJSONSync(PKG_FILE, jsonMap[name], {
encoding: 'utf8'
})
}
execSync(`npx prettier --write ${PKG_FILE}`, {
stdio: 'inherit',
cwd: ROOT
})
}
}

0 comments on commit 165ea0c

Please sign in to comment.