This repository has been archived by the owner on Aug 28, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 152
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: 修复多个项目同时运行时循环询问, 并重新优化scripts模块
- Loading branch information
Showing
10 changed files
with
163 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
import { execa } from 'execa' | ||
import { | ||
commandArgv, | ||
error, | ||
filterWorkspace, | ||
getWorkspacePackages, | ||
} from './helper' | ||
import { isArray, isString } from '@vben/utils' | ||
import { DEFAULT_SELECT_TYPE } from './constant' | ||
import ora from 'ora' | ||
const spinner = ora({ | ||
text: 'Loading...', | ||
color: 'yellow', | ||
}) | ||
const prompts = require('prompts') | ||
|
||
async function runScript(argv: string[], script: string) { | ||
spinner.stop() | ||
execa('pnpm', ['-w', 'run', `turbo:${script}`].concat(argv), { | ||
stdio: 'inherit', | ||
preferLocal: true, | ||
}) | ||
} | ||
|
||
async function baseScript(command: string, isFilterWorkspace: boolean) { | ||
const argv = commandArgv('filter') | ||
let filterArgv: string[] = [] | ||
try { | ||
if (isArray(argv)) { | ||
filterArgv = argv | ||
.map((argvItem) => ['--filter', `../${argvItem}`]) | ||
.flatMap((argvItem) => argvItem) | ||
} else if (isString(argv)) { | ||
filterArgv = ['--filter', argv] | ||
} else { | ||
filterArgv = isFilterWorkspace ? await filterWorkspace() : [] | ||
} | ||
const workspacePackages = await getWorkspacePackages(filterArgv) | ||
if (!workspacePackages.length) { | ||
throw new Error('No items meet the requirements!') | ||
} | ||
if (workspacePackages.length === 1) { | ||
await runScript(['--filter', workspacePackages[0].name], command) | ||
return | ||
} | ||
const choices = workspacePackages.map((item) => ({ | ||
title: item.name, | ||
value: item.name, | ||
})) | ||
const answer = await prompts([ | ||
{ | ||
type: DEFAULT_SELECT_TYPE, | ||
message: `Choose the package to run ${command} script: `, | ||
name: 'packages', | ||
choices, | ||
validate: function (val) { | ||
if (val && val.length) return true | ||
return 'Please choose at least one: ' | ||
}, | ||
}, | ||
]) | ||
|
||
const scriptArgv = isArray(answer.packages) | ||
? answer.packages | ||
.map((argvItem) => ['--filter', argvItem]) | ||
.flatMap((argvItem) => argvItem) | ||
: ['--filter', answer.packages || ''] | ||
await runScript(scriptArgv, command) | ||
} catch (e) { | ||
throw e | ||
} | ||
} | ||
|
||
export function run(command: string, isFilterWorkspace = false) { | ||
spinner.start() | ||
baseScript(command, isFilterWorkspace).catch((err) => { | ||
error(err) | ||
process.exit(1) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
import { run } from './helper' | ||
import { run } from './base'; | ||
|
||
run('build') | ||
run('build', true); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
export const IGNORE_WORKSPACE = ['packages/*', 'configs/*', 'scripts'] | ||
|
||
export enum SelectTypeEnum { | ||
SINGLE = 'select', | ||
MULTI = 'multiselect', | ||
} | ||
|
||
export const DEFAULT_SELECT_TYPE = SelectTypeEnum.MULTI |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
import { run } from './helper' | ||
import { run } from './base'; | ||
|
||
run('dev') | ||
run('dev', true); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,76 +1,62 @@ | ||
import { green, red } from 'picocolors' | ||
import { load as yamlLoad } from 'js-yaml' | ||
import { readFile } from 'fs-extra' | ||
import { IGNORE_WORKSPACE } from './constant' | ||
import minimist = require('minimist') | ||
import { execa } from 'execa' | ||
import prompts from 'prompts' | ||
import scriptPkg from '../package.json' | ||
import rootPkg from '../../package.json' | ||
|
||
type WorkspacePackage = { name: string; version?: string; path: string } | ||
export type WorkspacePackage = { name: string; version?: string; path: string } | ||
|
||
async function getPackages() { | ||
const { stdout } = await execa('pnpm', [ | ||
'ls', | ||
'-r', | ||
'--depth', | ||
'-1', | ||
'--json', | ||
]) | ||
|
||
return (JSON.parse(stdout) as WorkspacePackage[]).filter( | ||
(p) => | ||
p.name !== scriptPkg.name && | ||
p.name !== rootPkg.name && | ||
p.name.startsWith('@apps') && | ||
p.name !== '@apps/api-server', | ||
) | ||
export function error(err: any) { | ||
console.log(red(err)) | ||
} | ||
|
||
async function runScript(pkg: WorkspacePackage, script: string) { | ||
execa('pnpm', ['run', script, '--filter', `${pkg.name}...`, '--parallel'], { | ||
stdio: 'inherit', | ||
preferLocal: true, | ||
}) | ||
export function succeed(msg: any) { | ||
console.log(green(msg)) | ||
} | ||
|
||
async function runSingleScript(pkg: WorkspacePackage, script: string) { | ||
execa('pnpm', ['--filter', `${pkg.name}`, script], { | ||
stdio: 'inherit', | ||
preferLocal: true, | ||
}) | ||
/** | ||
* Get command line parameters | ||
* @param argvName | ||
*/ | ||
export function commandArgv(argvName: string | undefined = undefined) { | ||
const argv = minimist(process.argv.slice(2)) | ||
return argvName ? argv[argvName] || undefined : argv | ||
} | ||
|
||
export async function run(command: string) { | ||
const main = async () => { | ||
const packages = await getPackages() | ||
if (!packages.length) { | ||
return | ||
} | ||
|
||
if (packages.length === 1) { | ||
runSingleScript(packages[0], command) | ||
return | ||
} | ||
|
||
const { name } = await prompts([ | ||
{ | ||
name: 'name', | ||
message: `Choose the package to run ${command} script`, | ||
type: 'select', | ||
choices: packages.map((p) => { | ||
return { | ||
title: p.name, | ||
value: p.name, | ||
} | ||
}), | ||
}, | ||
]) | ||
/** | ||
* Read Workspace | ||
*/ | ||
export async function readWorkspace() { | ||
const path = '../pnpm-workspace.yaml' | ||
try { | ||
const workspace = yamlLoad(await readFile(path, { encoding: 'utf8' }), { | ||
json: true, | ||
}) as any | ||
return workspace.packages as string[] | ||
} catch (e) { | ||
throw e | ||
} | ||
} | ||
|
||
runScript( | ||
packages.find((p) => p.name === name), | ||
command, | ||
export async function filterWorkspace() { | ||
try { | ||
const filterArgv = (await readWorkspace()).filter( | ||
(wr) => !IGNORE_WORKSPACE.includes(wr), | ||
) | ||
return filterArgv | ||
.map((argv) => ['--filter', '../' + argv]) | ||
.flatMap((argv) => argv) | ||
} catch (e) { | ||
throw e | ||
} | ||
} | ||
|
||
main().catch((error) => { | ||
console.error(error) | ||
process.exit(1) | ||
}) | ||
export async function getWorkspacePackages(filterArgv = []) { | ||
const { stdout } = await execa( | ||
'pnpm', | ||
['ls', '-r', '--depth', '-1', '--json'].concat(filterArgv), | ||
) | ||
if (!stdout) return [] | ||
return JSON.parse(stdout) as WorkspacePackage[] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import { run } from './base'; | ||
|
||
run('preview', true); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,6 +14,9 @@ | |
}, | ||
"dev": { | ||
"cache": false | ||
}, | ||
"preview": { | ||
"cache": false | ||
} | ||
} | ||
} |