@nasl/cli 0.3.3 → 0.3.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.mjs CHANGED
@@ -15,6 +15,7 @@ import crypto$1, { createHash } from 'crypto';
15
15
  import http2 from 'http2';
16
16
  import zlib from 'zlib';
17
17
  import { spawn, spawnSync } from 'child_process';
18
+ import * as readline from 'readline';
18
19
  import { realpath as realpath$1, stat as stat$2, lstat as lstat$1, open, readdir as readdir$1 } from 'fs/promises';
19
20
  import { lstat, stat as stat$1, readdir, realpath } from 'node:fs/promises';
20
21
  import { Readable as Readable$1 } from 'node:stream';
@@ -35191,41 +35192,33 @@ async function parseApi(fullNaturalTS, options) {
35191
35192
  }
35192
35193
 
35193
35194
  /**
35194
- * 创建应用同步
35195
+ * 创建应用(POST /nasl/app/create)
35195
35196
  */
35196
- async function createAppSyncApi(fullNaturalTS, options) {
35197
- const url = new URL(options.serverBaseURL);
35198
- const origin = url.origin + '/app-ai-creator';
35199
- const axios = await createAxios({ serverBaseURL: origin, ideVersion: options.ideVersion });
35200
- try {
35201
- const res = await axios.post('/api/createAppSync', {
35202
- fullNaturalTS,
35203
- packageName: options.packageName,
35204
- appName: options.appName,
35205
- hostname: url.hostname,
35206
- enableAuthTemplate: false,
35207
- ideVersion: options.ideVersion,
35208
- fullVersion: `${options.ideVersion}.0`,
35209
- });
35210
- if (res.data.success) {
35211
- return {
35212
- success: true,
35213
- appId: res.data.data?.appId,
35214
- appURL: res.data.data?.appURL,
35215
- message: res.data.data?.message,
35216
- };
35217
- }
35218
- else {
35219
- return {
35220
- success: false,
35221
- error: res.data.message || '应用创建失败',
35222
- };
35223
- }
35224
- }
35225
- catch (error) {
35226
- console.log(error);
35227
- throw error;
35228
- }
35197
+ async function createApp(fullNaturalTS, options, appName, packageName) {
35198
+ const axiosInstance = await createAxios(options);
35199
+ const res = await axiosInstance.post('/app/create', {
35200
+ fullNaturalTS,
35201
+ packageName,
35202
+ appName,
35203
+ ideVersion: options.ideVersion,
35204
+ }, {
35205
+ headers: { ...buildNaslHeaders(options), 'Content-Type': 'application/json' },
35206
+ });
35207
+ return res.data.result;
35208
+ }
35209
+ /**
35210
+ * 更新应用(POST /nasl/app/update)
35211
+ */
35212
+ async function updateApp(fullNaturalTS, options, appId) {
35213
+ const axiosInstance = await createAxios(options);
35214
+ const res = await axiosInstance.post('/app/update', {
35215
+ fullNaturalTS,
35216
+ appId,
35217
+ ideVersion: options.ideVersion,
35218
+ }, {
35219
+ headers: { ...buildNaslHeaders(options), 'Content-Type': 'application/json' },
35220
+ });
35221
+ return res.data.result;
35229
35222
  }
35230
35223
 
35231
35224
  async function installDependenciesApi(dependenciesJSON, options) {
@@ -35632,78 +35625,94 @@ async function build(entry, options) {
35632
35625
  }
35633
35626
 
35634
35627
  /**
35635
- * 获取 specs 下第1个文件夹的名字
35628
+ * specs/ 目录取第一个子文件夹名;若无则退回项目根目录名
35636
35629
  */
35637
- function getFirstSpecFolderName(projectRoot) {
35638
- const dirname = path$1.basename(projectRoot);
35630
+ function getSpecBaseName(projectRoot) {
35639
35631
  const specsDir = path$1.join(projectRoot, 'specs');
35640
- if (!libExports.existsSync(specsDir))
35641
- return dirname;
35642
- const entries = libExports.readdirSync(specsDir, { withFileTypes: true });
35643
- const folders = entries
35644
- .filter(entry => entry.isDirectory())
35645
- .map(entry => entry.name);
35646
- if (folders.length === 0)
35647
- return dirname;
35648
- return folders[0];
35632
+ if (libExports.existsSync(specsDir)) {
35633
+ const folders = libExports.readdirSync(specsDir, { withFileTypes: true })
35634
+ .filter(e => e.isDirectory())
35635
+ .map(e => e.name);
35636
+ if (folders.length > 0)
35637
+ return folders[0];
35638
+ }
35639
+ return path$1.basename(projectRoot);
35640
+ }
35641
+ /**
35642
+ * 从项目目录推导出合法的应用名称和包名
35643
+ */
35644
+ function deriveAppNames(projectRoot) {
35645
+ const baseName = getSpecBaseName(projectRoot);
35646
+ const suffix = dayjs().format('MMDDHHmmss');
35647
+ let cleaned = baseName.replace(/[^a-zA-Z0-9]/g, '').replace(/^\d+/, '').slice(0, 12);
35648
+ if (!cleaned)
35649
+ cleaned = 'app';
35650
+ return {
35651
+ appName: `${cleaned}_${suffix}`,
35652
+ packageName: `${cleaned}${suffix}`,
35653
+ };
35649
35654
  }
35655
+
35650
35656
  /**
35651
- * 创建应用在 IDE
35657
+ * 使用 readline 询问用户确认
35652
35658
  */
35659
+ function askForConfirmation(question) {
35660
+ const rl = readline.createInterface({
35661
+ input: process.stdin,
35662
+ output: process.stdout,
35663
+ });
35664
+ return new Promise((resolve) => {
35665
+ rl.question(question, (answer) => {
35666
+ rl.close();
35667
+ const a = answer.trim().toLowerCase();
35668
+ resolve(!a || a === 'y' || a === 'yes' || a === '是');
35669
+ });
35670
+ });
35671
+ }
35672
+
35653
35673
  async function createAppInIde(entry, options) {
35654
35674
  const logger = options?.logger || defaultLogger;
35655
- logger.info('开始创建应用在 IDE 中...');
35656
- // 收集需要处理的文件
35657
- const { collectedFiles, config } = await resolveNASLFiles(entry, logger, false, options?.verbose);
35658
- if (config.useOPENAPI) {
35659
- logger.error('create-app-in-ide 暂不支持 useOPENAPI 模式');
35660
- logger.exit(1);
35675
+ // ── 扫描 ─────────────────────────────────────────────
35676
+ const { collectedFiles, config, mergedDependencies } = await resolveNASLFiles(entry, logger, false, options?.verbose);
35677
+ // ── 展示参数 ──────────────────────────────────────────
35678
+ const { appName, packageName } = deriveAppNames(getProjectRoot());
35679
+ logger.newLine();
35680
+ logger.info(`应用名称: ${appName}`);
35681
+ logger.info(`包名: ${packageName}`);
35682
+ logger.info(`IDE 版本: ${config.ideVersion}`);
35683
+ logger.info(`租户名称: ${config.tenantName || 'defaulttenant'}`);
35684
+ // ── 确认 ──────────────────────────────────────────────
35685
+ if (!options?.quiet) {
35686
+ logger.newLine();
35687
+ const confirmed = await askForConfirmation('即将在 IDE 中创建新的应用,请确认是否继续?(Y/n): ');
35688
+ if (!confirmed) {
35689
+ logger.info('已取消操作');
35690
+ process.exit(0);
35691
+ }
35661
35692
  }
35662
- // 生成 fullNaturalTS
35693
+ // ── 执行 ──────────────────────────────────────────────
35663
35694
  logger.newLine();
35664
35695
  logger.info('正在生成 NaturalTS 代码...');
35665
- let fullNaturalTS = '';
35696
+ let fullNaturalTS;
35666
35697
  try {
35667
35698
  fullNaturalTS = composeToString(collectedFiles);
35699
+ fullNaturalTS = prependDependencies(fullNaturalTS, mergedDependencies);
35668
35700
  }
35669
35701
  catch (error) {
35670
35702
  logger.error(`生成 NaturalTS 代码失败: ${error.message}`);
35671
35703
  throw error;
35672
35704
  }
35673
- // 获取 specs 下第1个文件夹
35674
- const projectRoot = getProjectRoot();
35675
- const firstSpecFolderName = getFirstSpecFolderName(projectRoot);
35676
- const suffix = dayjs().format('MMDDHHmmss');
35677
- let cleaned = firstSpecFolderName.replace(/[^a-zA-Z0-9]/g, '').replace(/^\d+/, '').slice(0, 12);
35678
- if (!cleaned)
35679
- cleaned = 'app';
35680
- const appName = `${cleaned}_${suffix}`;
35681
- const packageName = `${cleaned}${suffix}`;
35682
- logger.info(`应用名称: ${appName}`);
35683
- logger.info(`包名: ${packageName}`);
35684
- logger.info(`IDE 版本: ${config.ideVersion}`);
35685
- logger.info(`完整版本: ${config.ideVersion}.0`);
35686
- // 调用创建应用 API
35687
- logger.newLine();
35688
35705
  logger.info('正在调用创建应用服务...');
35689
35706
  try {
35690
- const result = await createAppSyncApi(fullNaturalTS, {
35691
- packageName: packageName,
35692
- appName: appName,
35693
- serverBaseURL: config.serverBaseURL,
35694
- ideVersion: config.ideVersion,
35695
- });
35707
+ const result = await createApp(fullNaturalTS, config, appName, packageName);
35696
35708
  if (result.success) {
35697
35709
  logger.success('应用创建成功!');
35698
- if (result.appURL) {
35710
+ if (result.appURL)
35699
35711
  logger.info(`应用 URL: ${result.appURL}`);
35700
- }
35701
- if (result.appId) {
35712
+ if (result.appId)
35702
35713
  logger.info(`应用 ID: ${result.appId}`);
35703
- }
35704
- if (result.message) {
35714
+ if (result.message)
35705
35715
  logger.info(result.message);
35706
- }
35707
35716
  }
35708
35717
  else {
35709
35718
  logger.error(`应用创建失败: ${result.error || '未知错误'}`);
@@ -35716,6 +35725,57 @@ async function createAppInIde(entry, options) {
35716
35725
  }
35717
35726
  }
35718
35727
 
35728
+ async function updateAppInIde(appId, entry, options) {
35729
+ const logger = options?.logger || defaultLogger;
35730
+ // ── 扫描 ─────────────────────────────────────────────
35731
+ const { collectedFiles, config, mergedDependencies } = await resolveNASLFiles(entry, logger, false, options?.verbose);
35732
+ // ── 展示参数 ──────────────────────────────────────────
35733
+ logger.newLine();
35734
+ logger.info(`应用 ID: ${appId}`);
35735
+ logger.info(`IDE 版本: ${config.ideVersion}`);
35736
+ logger.info(`租户名称: ${config.tenantName || 'defaulttenant'}`);
35737
+ // ── 确认 ──────────────────────────────────────────────
35738
+ if (!options?.quiet) {
35739
+ logger.newLine();
35740
+ const confirmed = await askForConfirmation(`即将更新应用 ${appId},请确认是否继续?(Y/n): `);
35741
+ if (!confirmed) {
35742
+ logger.info('已取消操作');
35743
+ process.exit(0);
35744
+ }
35745
+ }
35746
+ // ── 执行 ──────────────────────────────────────────────
35747
+ logger.newLine();
35748
+ logger.info('正在生成 NaturalTS 代码...');
35749
+ let fullNaturalTS;
35750
+ try {
35751
+ fullNaturalTS = composeToString(collectedFiles);
35752
+ fullNaturalTS = prependDependencies(fullNaturalTS, mergedDependencies);
35753
+ }
35754
+ catch (error) {
35755
+ logger.error(`生成 NaturalTS 代码失败: ${error.message}`);
35756
+ throw error;
35757
+ }
35758
+ logger.info('正在调用更新应用服务...');
35759
+ try {
35760
+ const result = await updateApp(fullNaturalTS, config, appId);
35761
+ if (result.success) {
35762
+ logger.success('应用更新成功!');
35763
+ if (result.appURL)
35764
+ logger.info(`应用 URL: ${result.appURL}`);
35765
+ if (result.message)
35766
+ logger.info(result.message);
35767
+ }
35768
+ else {
35769
+ logger.error(`应用更新失败: ${result.error || '未知错误'}`);
35770
+ throw new Error(result.error || '应用更新失败');
35771
+ }
35772
+ }
35773
+ catch (error) {
35774
+ logger.error(`调用更新应用服务失败: ${error.message}`);
35775
+ throw error;
35776
+ }
35777
+ }
35778
+
35719
35779
  /**
35720
35780
  * 编译 NASL 代码
35721
35781
  * TODO: 实现具体的 API 调用逻辑
@@ -37764,5 +37824,5 @@ var index$1 = /*#__PURE__*/Object.freeze({
37764
37824
  watch: watch
37765
37825
  });
37766
37826
 
37767
- export { BaseLogger, CONFIG_FILE_NAME, ConsoleLogger, ConsoleLoggerWithoutExit, DEFAULT_CONFIG, MemoryLogger, build, check, compile, composeToString, createAppInIde, createSorter, defaultLogger, dep, dev, docCheck, extractFullNaturalTS, fastAppendLogToFile, fastLogToFile, init, installAuto, installByJSON, installByKind, loadAppDependencies, loadMergedDependencies, mergeDependenciesIntoApp, prependDependencies, resolveNASLFiles, scanEntryFiles, scanNASLFiles, sorter, transform, tryCompile };
37827
+ export { BaseLogger, CONFIG_FILE_NAME, ConsoleLogger, ConsoleLoggerWithoutExit, DEFAULT_CONFIG, MemoryLogger, build, check, compile, composeToString, createAppInIde, createSorter, defaultLogger, dep, dev, docCheck, extractFullNaturalTS, fastAppendLogToFile, fastLogToFile, init, installAuto, installByJSON, installByKind, loadAppDependencies, loadMergedDependencies, mergeDependenciesIntoApp, prependDependencies, resolveNASLFiles, scanEntryFiles, scanNASLFiles, sorter, transform, tryCompile, updateAppInIde };
37768
37828
  //# sourceMappingURL=index.mjs.map