diff --git a/cli.json b/cli.json index 5888a38..9ede2ee 100644 --- a/cli.json +++ b/cli.json @@ -299,15 +299,6 @@ } } }, - "hdiffFromPPK": { - "description": "Create hdiff patch from a Prepare package(.ppk)", - "options": { - "output": { - "default": "${tempDir}/output/hdiff-${time}.ppk-patch", - "hasValue": true - } - } - }, "hdiffFromApp": { "description": "Create hdiff patch from a Harmony package(.app)", "options": { @@ -343,6 +334,10 @@ "hasValue": true } } + }, + "install": { + "description": "Install optional dependencies to the CLI", + "options": {} } }, "globalOptions": { diff --git a/src/bundle.ts b/src/bundle.ts index 8cf954e..6c17cc9 100644 --- a/src/bundle.ts +++ b/src/bundle.ts @@ -16,7 +16,7 @@ import os from 'os'; const properties = require('properties'); import { addGitIgnore } from './utils/add-gitignore'; import { checkLockFiles } from './utils/check-lockfile'; -import { tempDir } from './utils/constants'; +import { scriptName, tempDir } from './utils/constants'; import { depVersions } from './utils/dep-versions'; import { t } from './utils/i18n'; import { versionCommands } from './versions'; @@ -548,9 +548,7 @@ async function diffFromPPK(origin: string, next: string, output: string) { }); if (!originSource) { - throw new Error( - 'Bundle file not found! Please use default bundle file name and path.', - ); + throw new Error(t('bundleFileNotFound')); } const copies = {}; @@ -700,9 +698,7 @@ async function diffFromPackage( }); if (!originSource) { - throw new Error( - 'Bundle file not found! Please use default bundle file name and path.', - ); + throw new Error(t('bundleFileNotFound')); } const copies = {}; @@ -856,24 +852,21 @@ function diffArgsCheck(args: string[], options: any, diffFn: string) { if (diffFn.startsWith('hdiff')) { if (!hdiff) { - console.error( - `This function needs "node-hdiffpatch". - Please run "npm i node-hdiffpatch" to install`, - ); + console.error(t('nodeHdiffpatchRequired', { scriptName })); process.exit(1); } diff = hdiff; } else { if (!bsdiff) { - console.error( - `This function needs "node-bsdiff". - Please run "npm i node-bsdiff" to install`, - ); + console.error(t('nodeBsdiffRequired', { scriptName })); process.exit(1); } diff = bsdiff; } - const { output } = options; + const { output } = translateOptions({ + ...options, + tempDir, + }); return { origin, @@ -1004,14 +997,14 @@ export const bundleCommands = { const { origin, next, realOutput } = diffArgsCheck(args, options, 'diff'); await diffFromPPK(origin, next, realOutput); - console.log(`${realOutput} generated.`); + console.log(t('diffPackageGenerated', { output: realOutput })); }, async hdiff({ args, options }) { const { origin, next, realOutput } = diffArgsCheck(args, options, 'hdiff'); await diffFromPPK(origin, next, realOutput); - console.log(`${realOutput} generated.`); + console.log(t('diffPackageGenerated', { output: realOutput })); }, async diffFromApk({ args, options }) { @@ -1027,7 +1020,7 @@ export const bundleCommands = { realOutput, 'assets/index.android.bundle', ); - console.log(`${realOutput} generated.`); + console.log(t('diffPackageGenerated', { output: realOutput })); }, async hdiffFromApk({ args, options }) { @@ -1043,7 +1036,7 @@ export const bundleCommands = { realOutput, 'assets/index.android.bundle', ); - console.log(`${realOutput} generated.`); + console.log(t('diffPackageGenerated', { output: realOutput })); }, async diffFromApp({ args, options }) { @@ -1058,7 +1051,7 @@ export const bundleCommands = { realOutput, 'resources/rawfile/bundle.harmony.js', ); - console.log(`${realOutput} generated.`); + console.log(t('diffPackageGenerated', { output: realOutput })); }, async hdiffFromApp({ args, options }) { @@ -1073,7 +1066,7 @@ export const bundleCommands = { realOutput, 'resources/rawfile/bundle.harmony.js', ); - console.log(`${realOutput} generated.`); + console.log(t('diffPackageGenerated', { output: realOutput })); }, async diffFromIpa({ args, options }) { @@ -1088,7 +1081,7 @@ export const bundleCommands = { return m?.[1]; }); - console.log(`${realOutput} generated.`); + console.log(t('diffPackageGenerated', { output: realOutput })); }, async hdiffFromIpa({ args, options }) { @@ -1103,6 +1096,6 @@ export const bundleCommands = { return m?.[1]; }); - console.log(`${realOutput} generated.`); + console.log(t('diffPackageGenerated', { output: realOutput })); }, }; diff --git a/src/index.ts b/src/index.ts index cdc60c0..54a0703 100644 --- a/src/index.ts +++ b/src/index.ts @@ -3,6 +3,7 @@ import { loadSession } from './api'; import { appCommands } from './app'; import { bundleCommands } from './bundle'; +import { installCommands } from './install'; import { moduleManager } from './module-manager'; import { builtinModules } from './modules'; import { packageCommands } from './package'; @@ -26,15 +27,16 @@ function printUsage() { console.log('React Native Update CLI'); console.log(''); console.log('Traditional commands:'); - + const legacyCommands = { ...userCommands, ...bundleCommands, ...appCommands, ...packageCommands, ...versionCommands, + ...installCommands, }; - + for (const [name, handler] of Object.entries(legacyCommands)) { console.log(` ${name}: Legacy command`); } @@ -62,7 +64,7 @@ function printUsage() { console.log(' list: List all available commands and workflows'); console.log(' workflow : Execute a specific workflow'); console.log(' help: Show this help message'); - + console.log(''); console.log( 'Visit `https://github.com/reactnativecn/react-native-update` for document.', @@ -76,6 +78,7 @@ const legacyCommands = { ...appCommands, ...packageCommands, ...versionCommands, + ...installCommands, help: printUsage, }; @@ -118,7 +121,7 @@ async function run() { process.exit(1); } console.log('Workflow completed successfully:', result.data); - } + } // Try legacy commands first for backward compatibility else if (legacyCommands[argv.command]) { await legacyCommands[argv.command](argv); diff --git a/src/install.ts b/src/install.ts new file mode 100644 index 0000000..28a2086 --- /dev/null +++ b/src/install.ts @@ -0,0 +1,19 @@ +import { spawnSync } from 'child_process'; +import path from 'path'; +import type { CommandContext } from './types'; + +export const installCommands = { + install: async ({ args }: CommandContext) => { + if (args.length === 0) { + return; + } + + const cliDir = path.resolve(__dirname, '..'); + + spawnSync('npm', ['install', ...args], { + cwd: cliDir, + stdio: 'inherit', + shell: true, + }); + }, +}; diff --git a/src/locales/en.ts b/src/locales/en.ts index 994edc1..1a45a11 100644 --- a/src/locales/en.ts +++ b/src/locales/en.ts @@ -137,4 +137,11 @@ This can reduce the risk of inconsistent dependencies and supply chain attacks. deletePackageError: 'Failed to delete native package {{packageId}}: {{error}}', usageDeletePackage: 'Usage: cresc deletePackage [packageId] --appId [appId]', + bundleFileNotFound: + 'Bundle file not found! Please use default bundle file name and path.', + diffPackageGenerated: '{{- output}} generated.', + nodeBsdiffRequired: + 'This function needs "node-bsdiff". Please run "{{scriptName}} install node-bsdiff" to install', + nodeHdiffpatchRequired: + 'This function needs "node-hdiffpatch". Please run "{{scriptName}} install node-hdiffpatch" to install', }; diff --git a/src/locales/zh.ts b/src/locales/zh.ts index d8e00be..ae705a2 100644 --- a/src/locales/zh.ts +++ b/src/locales/zh.ts @@ -129,4 +129,10 @@ export default { deletePackageError: '删除原生包 {{packageId}} 失败: {{error}}', usageDeletePackage: '使用方法: pushy deletePackage [packageId] --appId [appId]', + bundleFileNotFound: '未找到 bundle 文件!请使用默认的 bundle 文件名和路径。', + diffPackageGenerated: '{{- output}} 已生成。', + nodeBsdiffRequired: + '此功能需要 "node-bsdiff"。请运行 "{{scriptName}} install node-bsdiff" 来安装', + nodeHdiffpatchRequired: + '此功能需要 "node-hdiffpatch"。请运行 "{{scriptName}} install node-hdiffpatch" 来安装', }; diff --git a/src/utils/constants.ts b/src/utils/constants.ts index a412fa6..05e9dbe 100644 --- a/src/utils/constants.ts +++ b/src/utils/constants.ts @@ -1,6 +1,6 @@ import path from 'path'; -const scriptName = path.basename(process.argv[1]) as 'cresc' | 'pushy'; +export const scriptName = path.basename(process.argv[1]) as 'cresc' | 'pushy'; export const IS_CRESC = scriptName === 'cresc'; export const credentialFile = IS_CRESC ? '.cresc.token' : '.update';