UNPKG

@wishcbg/bobcode

Version:

基於店家同步功能的需求,讓店家能夠在不同的店家之間同步各項功能設定,並且能夠檢視各項功能的異動情況。 規劃出一套店家設定即程式碼的機制,讓店家能夠以 config file (.sac) 來定義各項功能設定,並且能夠還原店家各項功能設定。 暫且稱這個機制為「Settings as Code」「店家設定即程式碼」。

63 lines (62 loc) 3.18 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.createIntrospectTemplateCommand = createIntrospectTemplateCommand; exports.getIntrospectStatusCommand = getIntrospectStatusCommand; const commander_1 = require("commander"); const file_js_1 = require("../utils/file.js"); const introspectHelper_js_1 = require("../utils/introspectHelper.js"); // npx bobcode introspect --host http://localhost:3000 --token 1234567890 --shop-id 1234567890 --features feature1,feature2 --output config_file.sac // 產出目標template function createIntrospectTemplateCommand() { const cmd = new commander_1.Command('introspectTemplate'); cmd .description('根據前綴生成店家配置檔') .requiredOption('--host <url>', 'API 主機 URL') .requiredOption('--token <token>', 'API 認證 Token') .requiredOption('--shop-id <id>', '目標店家 ID') .requiredOption('--features <features>', '要生成的功能列表,以逗號分隔', '') .requiredOption('--output <path>', '輸出配置檔 (.sac.yaml) 檔案路徑') .option('--prefix <prefix>', 'template 前綴', 'templateA') .action(async (opts) => { const { host, token, shopId, features, output, prefix } = opts; try { const bobIntrospect = await (0, introspectHelper_js_1.createIntrospectTemplate)(host, token, shopId, features.split(','), prefix); // 寫入配置檔 (0, file_js_1.writeConfigFile)(output, bobIntrospect); console.log(`已生成配置檔 ${output}`); } catch (error) { console.error('生成配置檔失敗:', error); process.exit(1); } }); return cmd; } // 根據指定前綴,生成對應的配置檔 function getIntrospectStatusCommand() { const cmd = new commander_1.Command('introspectSpecificPrefix'); cmd .description('查看指定前綴的店家配置檔') .requiredOption('--host <url>', 'API 主機 URL') .requiredOption('--token <token>', 'API 認證 Token') .requiredOption('--shop-id <id>', '目標店家 ID') .requiredOption('--features <features>', '要生成的功能列表,以逗號分隔', '') .requiredOption('--prefix <prefix>', 'template 前綴', 'templateA') .requiredOption('--output <path>', '輸出配置檔 (.sac.yaml) 檔案路徑') .action(async (opts) => { const { host, token, shopId, features, output, prefix } = opts; try { const bobIntrospect = await (0, introspectHelper_js_1.createIntrospectTemplate)(host, token, shopId, features.split(','), 'default'); // filter logicId 開頭為 `${prefix}-` 的資源 bobIntrospect.resources = bobIntrospect.resources.filter(resource => resource.logicId.startsWith(`${prefix}-`)); // 寫入配置檔 (0, file_js_1.writeConfigFile)(output, bobIntrospect); console.log(`已生成配置檔 ${output}`); } catch (error) { console.error('生成配置檔失敗:', error); process.exit(1); } }); return cmd; }