@rankwrangler/cli 0.2.3 → 0.4.0

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/README.md CHANGED
@@ -11,16 +11,23 @@ npm install -g @rankwrangler/cli
11
11
  ## Usage
12
12
 
13
13
  ```bash
14
- rw config set api-key rrk_...
14
+ export RR_LICENSE_KEY=rrk_...
15
+ rw config set storage-dir ~/.config/rankwrangler
15
16
  rw products get B0DV53VS61
16
17
  rw products history B0DV53VS61 --metrics bsr,price
17
18
  # `rankwrangler` is also supported as an alias
18
19
  ```
19
20
 
21
+ `RR_LICENSE_KEY` is the API-key source. `rw config set storage-dir <path>` saves the
22
+ active storage directory globally. After that, every CLI command reads and writes its non-secret
23
+ config/data from that directory, while preserving existing config values when switching.
24
+ `RR_STORAGE_DIR`, `RR_API_URL`, and `RR_MARKETPLACE_ID` override saved CLI config when set.
25
+
20
26
  ## Development
21
27
 
22
28
  ```bash
23
29
  bun run cli:build
30
+ bun run cli:test:e2e
24
31
  ```
25
32
 
26
33
  ## Release (Maintainers)
@@ -0,0 +1,31 @@
1
+ export type CliConfig = {
2
+ baseUrl?: string;
3
+ marketplaceId?: string;
4
+ };
5
+ export type CliPaths = {
6
+ defaultStorageDir: string;
7
+ storageDir: string;
8
+ configPath: string;
9
+ globalConfigPath: string;
10
+ };
11
+ export declare class CliStorageError extends Error {
12
+ readonly code: string;
13
+ readonly details?: unknown;
14
+ constructor(code: string, message: string, details?: unknown);
15
+ }
16
+ export declare const getDefaultCliPaths: () => CliPaths;
17
+ export declare const loadCliContext: () => Promise<{
18
+ paths: CliPaths;
19
+ config: CliConfig;
20
+ }>;
21
+ export declare const loadCliPathsOrDefault: () => Promise<CliPaths>;
22
+ export declare const clearConfig: (paths: CliPaths) => Promise<void>;
23
+ export declare const saveConfig: (paths: CliPaths, config: CliConfig) => Promise<void>;
24
+ export declare const switchStorageDir: ({ currentConfig, currentPaths, requestedStorageDir, }: {
25
+ currentConfig: CliConfig;
26
+ currentPaths: CliPaths;
27
+ requestedStorageDir: string;
28
+ }) => Promise<{
29
+ paths: CliPaths;
30
+ config: CliConfig;
31
+ }>;
@@ -0,0 +1,136 @@
1
+ import { mkdir, readFile, rm, writeFile } from 'node:fs/promises';
2
+ import { homedir } from 'node:os';
3
+ import path from 'node:path';
4
+ const DEFAULT_STORAGE_DIR = path.join(homedir(), '.rankwrangler');
5
+ const CONFIG_FILENAME = 'config.json';
6
+ const GLOBAL_CONFIG_FILENAME = 'global.json';
7
+ const STORAGE_DIR_ENV_VAR = 'RR_STORAGE_DIR';
8
+ export class CliStorageError extends Error {
9
+ code;
10
+ details;
11
+ constructor(code, message, details) {
12
+ super(message);
13
+ this.name = 'CliStorageError';
14
+ this.code = code;
15
+ this.details = details;
16
+ }
17
+ }
18
+ export const getDefaultCliPaths = () => {
19
+ return buildCliPaths(DEFAULT_STORAGE_DIR);
20
+ };
21
+ export const loadCliContext = async () => {
22
+ const paths = await resolveCliPaths();
23
+ const config = await loadConfig(paths.configPath);
24
+ return {
25
+ paths,
26
+ config,
27
+ };
28
+ };
29
+ export const loadCliPathsOrDefault = async () => {
30
+ try {
31
+ return await resolveCliPaths();
32
+ }
33
+ catch {
34
+ return getDefaultCliPaths();
35
+ }
36
+ };
37
+ export const clearConfig = async (paths) => {
38
+ await rm(paths.configPath, { force: true });
39
+ };
40
+ export const saveConfig = async (paths, config) => {
41
+ await mkdir(paths.storageDir, { recursive: true });
42
+ await writeFile(paths.configPath, `${JSON.stringify(config, null, 2)}\n`, 'utf8');
43
+ };
44
+ export const switchStorageDir = async ({ currentConfig, currentPaths, requestedStorageDir, }) => {
45
+ const storageDir = normalizeStorageDir(requestedStorageDir);
46
+ const nextPaths = buildCliPaths(storageDir);
47
+ const targetConfig = await loadConfig(nextPaths.configPath);
48
+ const nextConfig = mergeConfigs(currentConfig, targetConfig);
49
+ await saveConfig(nextPaths, nextConfig);
50
+ await saveGlobalConfig(currentPaths.globalConfigPath, {
51
+ storageDir: storageDir === currentPaths.defaultStorageDir ? undefined : storageDir,
52
+ });
53
+ return {
54
+ paths: nextPaths,
55
+ config: nextConfig,
56
+ };
57
+ };
58
+ const resolveCliPaths = async () => {
59
+ const defaults = getDefaultCliPaths();
60
+ const globalConfig = await loadGlobalConfig(defaults.globalConfigPath);
61
+ const storageDir = normalizeStorageDir(process.env[STORAGE_DIR_ENV_VAR] ?? globalConfig.storageDir ?? defaults.storageDir);
62
+ return buildCliPaths(storageDir);
63
+ };
64
+ const buildCliPaths = (storageDir) => {
65
+ return {
66
+ defaultStorageDir: DEFAULT_STORAGE_DIR,
67
+ storageDir,
68
+ configPath: path.join(storageDir, CONFIG_FILENAME),
69
+ globalConfigPath: path.join(DEFAULT_STORAGE_DIR, GLOBAL_CONFIG_FILENAME),
70
+ };
71
+ };
72
+ const loadConfig = async (configPath) => {
73
+ const parsed = await readJsonFile(configPath, 'failed to read config');
74
+ if (!parsed) {
75
+ return {};
76
+ }
77
+ return {
78
+ baseUrl: typeof parsed.baseUrl === 'string' ? parsed.baseUrl : undefined,
79
+ marketplaceId: typeof parsed.marketplaceId === 'string' ? parsed.marketplaceId : undefined,
80
+ };
81
+ };
82
+ const loadGlobalConfig = async (globalConfigPath) => {
83
+ const parsed = await readJsonFile(globalConfigPath, 'failed to read global config');
84
+ if (!parsed) {
85
+ return {};
86
+ }
87
+ return {
88
+ storageDir: typeof parsed.storageDir === 'string' ? parsed.storageDir : undefined,
89
+ };
90
+ };
91
+ const saveGlobalConfig = async (globalConfigPath, config) => {
92
+ if (!config.storageDir) {
93
+ await rm(globalConfigPath, { force: true });
94
+ return;
95
+ }
96
+ await mkdir(path.dirname(globalConfigPath), { recursive: true });
97
+ await writeFile(globalConfigPath, `${JSON.stringify(config, null, 2)}\n`, 'utf8');
98
+ };
99
+ const readJsonFile = async (filePath, errorMessage) => {
100
+ try {
101
+ const raw = await readFile(filePath, 'utf8');
102
+ return JSON.parse(raw);
103
+ }
104
+ catch (error) {
105
+ if (error &&
106
+ typeof error === 'object' &&
107
+ 'code' in error &&
108
+ error.code === 'ENOENT') {
109
+ return null;
110
+ }
111
+ throw new CliStorageError('INVALID_CONFIG', errorMessage, { path: filePath });
112
+ }
113
+ };
114
+ const normalizeStorageDir = (value) => {
115
+ const trimmed = value.trim();
116
+ if (!trimmed) {
117
+ throw new CliStorageError('INVALID_INPUT', 'storage dir cannot be empty');
118
+ }
119
+ const expanded = expandHomeDirectory(trimmed);
120
+ return path.resolve(expanded);
121
+ };
122
+ const expandHomeDirectory = (value) => {
123
+ if (value === '~') {
124
+ return homedir();
125
+ }
126
+ if (value.startsWith('~/')) {
127
+ return path.join(homedir(), value.slice(2));
128
+ }
129
+ return value;
130
+ };
131
+ const mergeConfigs = (currentConfig, targetConfig) => {
132
+ return {
133
+ baseUrl: targetConfig.baseUrl ?? currentConfig.baseUrl,
134
+ marketplaceId: targetConfig.marketplaceId ?? currentConfig.marketplaceId,
135
+ };
136
+ };
@@ -0,0 +1,34 @@
1
+ import type { CliConfig } from './cli-config';
2
+ import { type HistoryMetricAlias } from './history-response';
3
+ type CliFail = (code: string, message: string, details?: unknown) => never;
4
+ export type CliOptionValues = {
5
+ baseUrl?: string;
6
+ marketplace?: string;
7
+ asin?: string[];
8
+ metrics?: string;
9
+ startAt?: string;
10
+ endAt?: string;
11
+ days?: string;
12
+ limit?: string;
13
+ help?: boolean;
14
+ };
15
+ export declare const requireMarketplaceId: (values: CliOptionValues, config: CliConfig) => string;
16
+ export declare const requireAsins: (commandArgs: string[], values: CliOptionValues, fail: CliFail) => string[];
17
+ export declare const requireSingleAsin: (commandArgs: string[], values: CliOptionValues, fail: CliFail) => string;
18
+ export declare const resolveHistoryMetrics: (values: CliOptionValues, fail: CliFail) => HistoryMetricAlias[];
19
+ export declare const resolveHistoryWindow: (values: CliOptionValues, fail: CliFail) => {
20
+ days: number;
21
+ endAt?: string | undefined;
22
+ startAt?: string | undefined;
23
+ };
24
+ export declare const resolveApiKey: () => string | undefined;
25
+ export declare const resolveBaseUrl: (values: CliOptionValues, config: CliConfig, defaultBaseUrl: string, fail: CliFail) => string;
26
+ export declare const normalizeBaseUrl: (value: string, fail: CliFail) => string;
27
+ export declare const parseIntegerOption: ({ value, optionName, min, max, defaultValue, }: {
28
+ value: string | undefined;
29
+ optionName: string;
30
+ min: number;
31
+ max: number;
32
+ defaultValue: number;
33
+ }, fail: CliFail) => number;
34
+ export {};
@@ -0,0 +1,101 @@
1
+ import { HISTORY_METRIC_ALIASES } from './history-response.js';
2
+ const ASIN_REGEX = /^[A-Z0-9]{10}$/i;
3
+ const TRAILING_SLASHES_REGEX = /\/+$/;
4
+ const API_SUFFIX_REGEX = /\/api$/i;
5
+ const DEFAULT_MARKETPLACE_ID = 'ATVPDKIKX0DER';
6
+ export const requireMarketplaceId = (values, config) => {
7
+ return values.marketplace ?? config.marketplaceId ?? process.env.RR_MARKETPLACE_ID ?? DEFAULT_MARKETPLACE_ID;
8
+ };
9
+ export const requireAsins = (commandArgs, values, fail) => {
10
+ const candidates = [...commandArgs, ...(values.asin ?? []), ...resolveEnvAsins()]
11
+ .map(value => value.trim())
12
+ .filter(Boolean);
13
+ if (candidates.length === 0) {
14
+ fail('INVALID_INPUT', 'at least one asin is required');
15
+ }
16
+ return Array.from(new Set(candidates.map(value => normalizeAsin(value, fail))));
17
+ };
18
+ export const requireSingleAsin = (commandArgs, values, fail) => {
19
+ const asins = requireAsins(commandArgs, values, fail);
20
+ if (asins.length !== 1) {
21
+ fail('INVALID_INPUT', 'products history requires exactly one asin');
22
+ }
23
+ return asins[0];
24
+ };
25
+ export const resolveHistoryMetrics = (values, fail) => {
26
+ const requested = (values.metrics ?? process.env.RR_HISTORY_METRICS ?? 'bsr,price')
27
+ .split(',')
28
+ .map(value => value.trim().toLowerCase())
29
+ .filter(Boolean);
30
+ if (requested.length === 0) {
31
+ fail('INVALID_INPUT', 'metrics cannot be empty');
32
+ }
33
+ const invalid = requested.filter(value => !HISTORY_METRIC_ALIASES.includes(value));
34
+ if (invalid.length > 0) {
35
+ fail('INVALID_INPUT', `unsupported history metric: ${invalid[0]}`, {
36
+ supportedMetrics: HISTORY_METRIC_ALIASES,
37
+ });
38
+ }
39
+ return Array.from(new Set(requested));
40
+ };
41
+ export const resolveHistoryWindow = (values, fail) => {
42
+ const startAt = values.startAt ? normalizeDateOption('startAt', values.startAt, fail) : undefined;
43
+ const endAt = values.endAt ? normalizeDateOption('endAt', values.endAt, fail) : undefined;
44
+ if (values.days && (startAt || endAt)) {
45
+ fail('INVALID_INPUT', 'use --days or --startAt/--endAt, not both');
46
+ }
47
+ return {
48
+ ...(startAt ? { startAt } : {}),
49
+ ...(endAt ? { endAt } : {}),
50
+ days: parseIntegerOption({
51
+ value: values.days,
52
+ optionName: 'days',
53
+ min: 30,
54
+ max: 3650,
55
+ defaultValue: 365,
56
+ }, fail),
57
+ };
58
+ };
59
+ export const resolveApiKey = () => {
60
+ return process.env.RR_LICENSE_KEY;
61
+ };
62
+ export const resolveBaseUrl = (values, config, defaultBaseUrl, fail) => {
63
+ return normalizeBaseUrl(values.baseUrl ?? config.baseUrl ?? process.env.RR_API_URL ?? defaultBaseUrl, fail);
64
+ };
65
+ export const normalizeBaseUrl = (value, fail) => {
66
+ const normalized = value.trim().replace(TRAILING_SLASHES_REGEX, '').replace(API_SUFFIX_REGEX, '');
67
+ if (!normalized) {
68
+ fail('INVALID_INPUT', 'base url cannot be empty');
69
+ }
70
+ return normalized;
71
+ };
72
+ export const parseIntegerOption = ({ value, optionName, min, max, defaultValue, }, fail) => {
73
+ if (!value) {
74
+ return defaultValue;
75
+ }
76
+ const parsed = Number.parseInt(value, 10);
77
+ if (!Number.isFinite(parsed) || parsed < min || parsed > max) {
78
+ fail('INVALID_INPUT', `${optionName} must be an integer between ${min} and ${max}`);
79
+ }
80
+ return parsed;
81
+ };
82
+ const normalizeDateOption = (optionName, value, fail) => {
83
+ const parsed = new Date(value);
84
+ if (!Number.isFinite(parsed.getTime())) {
85
+ fail('INVALID_INPUT', `${optionName} must be a valid date`);
86
+ }
87
+ return parsed.toISOString();
88
+ };
89
+ const normalizeAsin = (value, fail) => {
90
+ const normalized = value.trim().toUpperCase();
91
+ if (!ASIN_REGEX.test(normalized)) {
92
+ fail('INVALID_INPUT', `invalid ASIN: ${value}`);
93
+ }
94
+ return normalized;
95
+ };
96
+ const resolveEnvAsins = () => {
97
+ return [
98
+ ...(process.env.RR_ASINS ? process.env.RR_ASINS.split(',') : []),
99
+ ...(process.env.RR_ASIN ? [process.env.RR_ASIN] : []),
100
+ ];
101
+ };
@@ -0,0 +1,11 @@
1
+ export declare const printSuccess: <T>(data: T) => void;
2
+ export declare const fail: (code: string, message: string, details?: unknown) => never;
3
+ export declare const resolveError: (error: unknown) => {
4
+ code: string;
5
+ message: string;
6
+ details: unknown;
7
+ } | {
8
+ code: string;
9
+ message: string;
10
+ details?: undefined;
11
+ };
@@ -0,0 +1,53 @@
1
+ import { CliStorageError } from './cli-config.js';
2
+ const OUTPUT_PRETTY = true;
3
+ export const printSuccess = (data) => {
4
+ const envelope = {
5
+ ok: true,
6
+ data,
7
+ };
8
+ console.log(JSON.stringify(envelope, null, OUTPUT_PRETTY ? 2 : undefined));
9
+ };
10
+ export const fail = (code, message, details) => {
11
+ const envelope = {
12
+ ok: false,
13
+ error: {
14
+ code,
15
+ message,
16
+ ...(details !== undefined ? { details } : {}),
17
+ },
18
+ };
19
+ console.error(JSON.stringify(envelope, null, OUTPUT_PRETTY ? 2 : undefined));
20
+ process.exit(1);
21
+ };
22
+ export const resolveError = (error) => {
23
+ if (error instanceof CliStorageError) {
24
+ return {
25
+ code: error.code,
26
+ message: error.message,
27
+ details: error.details,
28
+ };
29
+ }
30
+ if (error && typeof error === 'object' && 'data' in error) {
31
+ const data = error.data;
32
+ if (data?.code) {
33
+ return {
34
+ code: data.code,
35
+ message: error instanceof Error ? error.message : 'Request failed',
36
+ details: data.httpStatus ? { httpStatus: data.httpStatus } : undefined,
37
+ };
38
+ }
39
+ }
40
+ if (error && typeof error === 'object' && 'message' in error) {
41
+ const message = error.message;
42
+ if (typeof message === 'string') {
43
+ return {
44
+ code: 'REQUEST_FAILED',
45
+ message,
46
+ };
47
+ }
48
+ }
49
+ return {
50
+ code: 'UNKNOWN_ERROR',
51
+ message: 'Unknown error',
52
+ };
53
+ };
@@ -0,0 +1,41 @@
1
+ export declare const HISTORY_METRIC_ALIASES: readonly ["bsr", "price"];
2
+ export type HistoryMetricAlias = (typeof HISTORY_METRIC_ALIASES)[number];
3
+ type HistoryPointTuple = [string, number | null] | [string, null, 1];
4
+ export type AgentHistorySeries = {
5
+ bsr?: {
6
+ unit: 'rank';
7
+ category: {
8
+ id: number;
9
+ name: string | null;
10
+ } | null;
11
+ points: HistoryPointTuple[];
12
+ };
13
+ price?: {
14
+ unit: 'minorCurrency';
15
+ currencyCode: 'USD';
16
+ valueScale: 100;
17
+ points: HistoryPointTuple[];
18
+ };
19
+ };
20
+ export type AgentHistoryResponse = {
21
+ schemaVersion?: number;
22
+ status?: string;
23
+ syncTriggered: boolean;
24
+ latestImportAt: string | null;
25
+ series?: AgentHistorySeries;
26
+ };
27
+ export declare const buildCliHistoryResponse: ({ asin, marketplaceId, metrics, response, }: {
28
+ asin: string;
29
+ marketplaceId: string;
30
+ metrics: HistoryMetricAlias[];
31
+ response: AgentHistoryResponse;
32
+ }) => {
33
+ schemaVersion: number;
34
+ asin: string;
35
+ marketplaceId: string;
36
+ status: string;
37
+ latestImportAt: string | null;
38
+ syncTriggered: boolean;
39
+ series: AgentHistorySeries;
40
+ };
41
+ export {};
@@ -0,0 +1,26 @@
1
+ export const HISTORY_METRIC_ALIASES = ['bsr', 'price'];
2
+ export const buildCliHistoryResponse = ({ asin, marketplaceId, metrics, response, }) => {
3
+ const series = {
4
+ ...(metrics.includes('bsr') && response.series?.bsr ? { bsr: response.series.bsr } : {}),
5
+ ...(metrics.includes('price') && response.series?.price
6
+ ? { price: response.series.price }
7
+ : {}),
8
+ };
9
+ const hasAnyPoints = Object.values(series).some(metricSeries => metricSeries.points.length > 0);
10
+ const status = normalizeHistoryStatus(response.status);
11
+ return {
12
+ schemaVersion: response.schemaVersion ?? 1,
13
+ asin,
14
+ marketplaceId,
15
+ status: status ?? (response.syncTriggered ? 'collecting' : hasAnyPoints ? 'ready' : 'empty'),
16
+ latestImportAt: response.latestImportAt,
17
+ syncTriggered: response.syncTriggered,
18
+ series,
19
+ };
20
+ };
21
+ const normalizeHistoryStatus = (status) => {
22
+ if (status === 'ready' || status === 'collecting' || status === 'empty') {
23
+ return status;
24
+ }
25
+ return null;
26
+ };
package/dist/index.js CHANGED
@@ -1,17 +1,11 @@
1
1
  #!/usr/bin/env node
2
- import { mkdir, readFile, rm, writeFile } from 'node:fs/promises';
3
- import { homedir } from 'node:os';
4
- import path from 'node:path';
5
2
  import { parseArgs } from 'node:util';
6
3
  import { createRankWranglerClient, DEFAULT_API_BASE_URL } from '@rankwrangler/http-client';
7
- const ASIN_REGEX = /^[A-Z0-9]{10}$/i;
8
- const TRAILING_SLASHES_REGEX = /\/+$/;
9
- const API_SUFFIX_REGEX = /\/api$/i;
10
- const DEFAULT_MARKETPLACE_ID = 'ATVPDKIKX0DER';
11
- const HISTORY_METRIC_ALIASES = ['bsr', 'price'];
12
- const CONFIG_DIR = path.join(homedir(), '.rankwrangler');
13
- const CONFIG_PATH = path.join(CONFIG_DIR, 'config.json');
14
- const DEFAULT_OUTPUT_PRETTY = true;
4
+ import { clearConfig, loadCliContext, loadCliPathsOrDefault, saveConfig, switchStorageDir, } from './cli-config.js';
5
+ import { normalizeBaseUrl, parseIntegerOption, requireAsins, requireMarketplaceId, requireSingleAsin, resolveApiKey, resolveBaseUrl, resolveHistoryMetrics, resolveHistoryWindow, } from './cli-options.js';
6
+ import { fail, printSuccess, resolveError } from './cli-output.js';
7
+ import { buildCliHistoryResponse } from './history-response.js';
8
+ import { printUsage } from './usage.js';
15
9
  const SUPPORTED_COMMANDS = new Set([
16
10
  'products:get',
17
11
  'products:history',
@@ -25,7 +19,6 @@ const { positionals, values } = parseArgs({
25
19
  args: process.argv.slice(2),
26
20
  options: {
27
21
  help: { type: 'boolean', short: 'h' },
28
- apiKey: { type: 'string' },
29
22
  baseUrl: { type: 'string' },
30
23
  marketplace: { type: 'string', short: 'm' },
31
24
  asin: { type: 'string', multiple: true },
@@ -37,44 +30,38 @@ const { positionals, values } = parseArgs({
37
30
  },
38
31
  allowPositionals: true,
39
32
  });
40
- const outputPretty = DEFAULT_OUTPUT_PRETTY;
41
33
  const main = async () => {
34
+ const optionValues = values;
42
35
  if (values.help || positionals.length === 0) {
43
- printUsage();
36
+ printUsage(await loadCliPathsOrDefault());
44
37
  process.exit(values.help ? 0 : 1);
45
38
  }
46
- const command = resolveCommand(positionals);
47
- if (!command) {
48
- fail('UNKNOWN_COMMAND', 'Unknown command', { command: positionals.join(' ') });
49
- return;
50
- }
39
+ const command = resolveCommandOrFail(positionals);
51
40
  if (!isSupportedCommand(command)) {
52
- fail('UNKNOWN_COMMAND', 'Unknown command', { command: `${command.resource} ${command.verb}` });
53
- return;
41
+ fail('UNKNOWN_COMMAND', 'Unknown command', {
42
+ command: `${command.resource} ${command.verb}`,
43
+ });
54
44
  }
55
- const config = await loadConfig();
45
+ const { config, paths } = await loadCliContext();
56
46
  if (command.resource === 'config') {
57
- const response = await runConfigCommand(command, config);
58
- printSuccess(response);
47
+ printSuccess(await runConfigCommand(command, config, paths));
59
48
  return;
60
49
  }
61
- const apiKey = resolveApiKey(config);
50
+ const apiKey = resolveApiKey();
62
51
  if (!apiKey) {
63
- fail('MISSING_CONFIG', 'api key is required. set via `config set api-key <value>`');
64
- return;
52
+ fail('MISSING_CONFIG', 'api key is required. set RR_LICENSE_KEY');
65
53
  }
66
- const baseUrl = resolveBaseUrl(config);
67
54
  const client = createRankWranglerClient({
68
- baseUrl,
55
+ baseUrl: resolveBaseUrl(optionValues, config, DEFAULT_API_BASE_URL, fail),
69
56
  apiKey,
70
57
  });
71
- const response = await runApiCommand(command, client, config);
72
- printSuccess(response);
58
+ printSuccess(await runApiCommand(command, client, config));
73
59
  };
74
60
  const runApiCommand = async (command, client, config) => {
75
61
  if (command.resource === 'products' && command.verb === 'get') {
76
- const marketplaceId = requireMarketplaceId(config);
77
- const asins = requireAsins(command.args);
62
+ const optionValues = values;
63
+ const marketplaceId = requireMarketplaceId(optionValues, config);
64
+ const asins = requireAsins(command.args, optionValues, fail);
78
65
  if (asins.length === 1) {
79
66
  return client.getProductInfo.mutate({ marketplaceId, asin: asins[0] });
80
67
  }
@@ -92,20 +79,20 @@ const runApiCommand = async (command, client, config) => {
92
79
  fail('UNKNOWN_COMMAND', 'Unknown command', {
93
80
  command: `${command.resource} ${command.verb}`,
94
81
  });
95
- return null;
96
82
  };
97
83
  const runProductHistoryCommand = async (command, client, config) => {
98
- const marketplaceId = requireMarketplaceId(config);
99
- const asin = requireSingleAsin(command.args);
100
- const metrics = resolveHistoryMetrics();
101
- const historyWindow = resolveHistoryWindow();
84
+ const optionValues = values;
85
+ const asin = requireSingleAsin(command.args, optionValues, fail);
86
+ const marketplaceId = requireMarketplaceId(optionValues, config);
87
+ const metrics = resolveHistoryMetrics(optionValues, fail);
88
+ const historyWindow = resolveHistoryWindow(optionValues, fail);
102
89
  const limit = parseIntegerOption({
103
- value: values.limit,
90
+ value: optionValues.limit,
104
91
  optionName: 'limit',
105
92
  min: 1,
106
93
  max: 10000,
107
94
  defaultValue: 5000,
108
- });
95
+ }, fail);
109
96
  const response = await client.getProductHistory.mutate({
110
97
  marketplaceId,
111
98
  asin,
@@ -118,187 +105,63 @@ const runProductHistoryCommand = async (command, client, config) => {
118
105
  asin,
119
106
  marketplaceId,
120
107
  metrics,
121
- response,
108
+ response: response,
122
109
  });
123
110
  };
124
- const runConfigCommand = async (command, config) => {
111
+ const runConfigCommand = async (command, config, paths) => {
125
112
  if (command.verb === 'show') {
126
- return {
127
- path: CONFIG_PATH,
128
- config,
129
- };
113
+ return buildConfigResponse(paths, config);
130
114
  }
131
115
  if (command.verb === 'clear') {
132
- await rm(CONFIG_PATH, { force: true });
116
+ await clearConfig(paths);
133
117
  return {
134
- path: CONFIG_PATH,
118
+ ...buildConfigResponse(paths, {}),
135
119
  cleared: true,
136
120
  };
137
121
  }
138
- if (command.verb === 'set') {
139
- const [key, ...valueParts] = command.args;
140
- if (!key || valueParts.length === 0) {
141
- fail('INVALID_INPUT', 'config set requires <key> <value>');
142
- return null;
143
- }
144
- const value = valueParts.join(' ').trim();
145
- if (!value) {
146
- fail('INVALID_INPUT', 'config set value cannot be empty');
147
- return null;
148
- }
149
- const nextConfig = { ...config };
150
- if (key === 'api-key') {
151
- nextConfig.apiKey = value;
152
- }
153
- else if (key === 'base-url') {
154
- nextConfig.baseUrl = normalizeBaseUrl(value);
155
- }
156
- else if (key === 'marketplace') {
157
- nextConfig.marketplaceId = value;
158
- }
159
- else {
160
- fail('INVALID_INPUT', 'unsupported config key', {
161
- key,
162
- supportedKeys: ['api-key', 'base-url', 'marketplace'],
163
- });
164
- return null;
165
- }
166
- await saveConfig(nextConfig);
167
- return {
168
- path: CONFIG_PATH,
169
- config: nextConfig,
170
- };
171
- }
172
- fail('UNKNOWN_COMMAND', 'Unknown config command', { verb: command.verb });
173
- return null;
174
- };
175
- const requireMarketplaceId = (config) => {
176
- const marketplaceId = values.marketplace ?? config.marketplaceId ?? process.env.RR_MARKETPLACE_ID ?? DEFAULT_MARKETPLACE_ID;
177
- return marketplaceId;
178
- };
179
- const requireAsins = (commandArgs) => {
180
- const candidates = collectAsinCandidates(commandArgs);
181
- if (candidates.length === 0) {
182
- fail('INVALID_INPUT', 'at least one asin is required');
183
- }
184
- return Array.from(new Set(candidates.map(normalizeAsin)));
185
- };
186
- const requireSingleAsin = (commandArgs) => {
187
- const asins = requireAsins(commandArgs);
188
- if (asins.length !== 1) {
189
- fail('INVALID_INPUT', 'products history requires exactly one asin');
122
+ if (command.verb !== 'set') {
123
+ fail('UNKNOWN_COMMAND', 'Unknown config command', { verb: command.verb });
190
124
  }
191
- return asins[0];
192
- };
193
- const collectAsinCandidates = (commandArgs) => {
194
- const optionAsins = [...(values.asin ?? [])];
195
- const envAsins = process.env.RR_ASINS
196
- ? process.env.RR_ASINS.split(',').map(value => value.trim())
197
- : [];
198
- const envSingleAsin = process.env.RR_ASIN ? [process.env.RR_ASIN] : [];
199
- return [...commandArgs, ...optionAsins, ...envAsins, ...envSingleAsin]
200
- .map(value => value.trim())
201
- .filter(Boolean);
202
- };
203
- const normalizeAsin = (value) => {
204
- const normalized = value.trim().toUpperCase();
205
- if (!ASIN_REGEX.test(normalized)) {
206
- fail('INVALID_INPUT', `invalid ASIN: ${value}`);
125
+ const [key, ...valueParts] = command.args;
126
+ if (!key || valueParts.length === 0) {
127
+ fail('INVALID_INPUT', 'config set requires <key> <value>');
207
128
  }
208
- return normalized;
209
- };
210
- const resolveHistoryMetrics = () => {
211
- const rawMetrics = values.metrics ?? process.env.RR_HISTORY_METRICS ?? 'bsr,price';
212
- const requested = rawMetrics
213
- .split(',')
214
- .map(value => value.trim().toLowerCase())
215
- .filter(Boolean);
216
- if (requested.length === 0) {
217
- fail('INVALID_INPUT', 'metrics cannot be empty');
129
+ const value = valueParts.join(' ').trim();
130
+ if (!value) {
131
+ fail('INVALID_INPUT', 'config set value cannot be empty');
218
132
  }
219
- const invalid = requested.filter(value => !HISTORY_METRIC_ALIASES.includes(value));
220
- if (invalid.length > 0) {
221
- fail('INVALID_INPUT', `unsupported history metric: ${invalid[0]}`, {
222
- supportedMetrics: HISTORY_METRIC_ALIASES,
133
+ if (key === 'storage-dir') {
134
+ const nextState = await switchStorageDir({
135
+ currentConfig: config,
136
+ currentPaths: paths,
137
+ requestedStorageDir: value,
223
138
  });
139
+ return buildConfigResponse(nextState.paths, nextState.config);
224
140
  }
225
- return Array.from(new Set(requested));
226
- };
227
- const resolveHistoryWindow = () => {
228
- const startAt = values.startAt ? normalizeDateOption('startAt', values.startAt) : undefined;
229
- const endAt = values.endAt ? normalizeDateOption('endAt', values.endAt) : undefined;
230
- const days = parseIntegerOption({
231
- value: values.days,
232
- optionName: 'days',
233
- min: 30,
234
- max: 3650,
235
- defaultValue: 365,
236
- });
237
- if (values.days && (startAt || endAt)) {
238
- fail('INVALID_INPUT', 'use --days or --startAt/--endAt, not both');
239
- }
240
- return {
241
- ...(startAt ? { startAt } : {}),
242
- ...(endAt ? { endAt } : {}),
243
- days,
244
- };
245
- };
246
- const parseIntegerOption = ({ value, optionName, min, max, defaultValue, }) => {
247
- if (!value) {
248
- return defaultValue;
141
+ const nextConfig = { ...config };
142
+ if (key === 'base-url') {
143
+ nextConfig.baseUrl = normalizeBaseUrl(value, fail);
249
144
  }
250
- const parsed = Number.parseInt(value, 10);
251
- if (!Number.isFinite(parsed) || parsed < min || parsed > max) {
252
- fail('INVALID_INPUT', `${optionName} must be an integer between ${min} and ${max}`);
145
+ else if (key === 'marketplace') {
146
+ nextConfig.marketplaceId = value;
253
147
  }
254
- return parsed;
255
- };
256
- const normalizeDateOption = (optionName, value) => {
257
- const parsed = new Date(value);
258
- if (!Number.isFinite(parsed.getTime())) {
259
- fail('INVALID_INPUT', `${optionName} must be a valid date`);
148
+ else {
149
+ fail('INVALID_INPUT', 'unsupported config key', {
150
+ key,
151
+ supportedKeys: ['base-url', 'marketplace', 'storage-dir'],
152
+ });
260
153
  }
261
- return parsed.toISOString();
154
+ await saveConfig(paths, nextConfig);
155
+ return buildConfigResponse(paths, nextConfig);
262
156
  };
263
- const buildCliHistoryResponse = ({ asin, marketplaceId, metrics, response, }) => {
264
- const series = {
265
- ...(metrics.includes('bsr') && response.series?.bsr ? { bsr: response.series.bsr } : {}),
266
- ...(metrics.includes('price') && response.series?.price ? { price: response.series.price } : {}),
267
- };
268
- const hasAnyPoints = Object.values(series).some(metricSeries => metricSeries.points.length > 0);
269
- const status = normalizeHistoryStatus(response.status);
157
+ const buildConfigResponse = (paths, config) => {
270
158
  return {
271
- schemaVersion: response.schemaVersion ?? 1,
272
- asin,
273
- marketplaceId,
274
- status: status ?? (response.syncTriggered ? 'collecting' : hasAnyPoints ? 'ready' : 'empty'),
275
- latestImportAt: response.latestImportAt,
276
- syncTriggered: response.syncTriggered,
277
- series,
159
+ storageDir: paths.storageDir,
160
+ path: paths.configPath,
161
+ globalPath: paths.globalConfigPath,
162
+ config,
278
163
  };
279
164
  };
280
- const normalizeHistoryStatus = (status) => {
281
- if (status === 'ready' || status === 'collecting' || status === 'empty') {
282
- return status;
283
- }
284
- return null;
285
- };
286
- const resolveApiKey = (config) => {
287
- return values.apiKey ?? config.apiKey ?? process.env.RR_LICENSE_KEY;
288
- };
289
- const resolveBaseUrl = (config) => {
290
- const configured = values.baseUrl ?? config.baseUrl ?? process.env.RR_API_URL;
291
- return normalizeBaseUrl(configured ?? DEFAULT_API_BASE_URL);
292
- };
293
- const normalizeBaseUrl = (value) => {
294
- const trimmed = value.trim();
295
- const withoutTrailingSlashes = trimmed.replace(TRAILING_SLASHES_REGEX, '');
296
- const withoutApiSuffix = withoutTrailingSlashes.replace(API_SUFFIX_REGEX, '');
297
- if (!withoutApiSuffix) {
298
- fail('INVALID_INPUT', 'base url cannot be empty');
299
- }
300
- return withoutApiSuffix;
301
- };
302
165
  const resolveCommand = (inputPositionals) => {
303
166
  const [first, second, ...rest] = inputPositionals;
304
167
  if (!first || !second) {
@@ -310,130 +173,16 @@ const resolveCommand = (inputPositionals) => {
310
173
  args: rest,
311
174
  };
312
175
  };
313
- const isSupportedCommand = (command) => {
314
- return SUPPORTED_COMMANDS.has(`${command.resource}:${command.verb}`);
315
- };
316
- const loadConfig = async () => {
317
- try {
318
- const raw = await readFile(CONFIG_PATH, 'utf8');
319
- const parsed = JSON.parse(raw);
320
- return {
321
- apiKey: typeof parsed.apiKey === 'string' ? parsed.apiKey : undefined,
322
- baseUrl: typeof parsed.baseUrl === 'string' ? normalizeBaseUrl(parsed.baseUrl) : undefined,
323
- marketplaceId: typeof parsed.marketplaceId === 'string' ? parsed.marketplaceId : undefined,
324
- };
325
- }
326
- catch (error) {
327
- if (error &&
328
- typeof error === 'object' &&
329
- 'code' in error &&
330
- error.code === 'ENOENT') {
331
- return {};
332
- }
333
- fail('INVALID_CONFIG', 'failed to read config', { path: CONFIG_PATH });
334
- return {};
176
+ const resolveCommandOrFail = (inputPositionals) => {
177
+ const command = resolveCommand(inputPositionals);
178
+ if (command) {
179
+ return command;
335
180
  }
181
+ fail('UNKNOWN_COMMAND', 'Unknown command', { command: inputPositionals.join(' ') });
182
+ throw new Error('unreachable');
336
183
  };
337
- const saveConfig = async (config) => {
338
- await mkdir(CONFIG_DIR, { recursive: true });
339
- await writeFile(CONFIG_PATH, `${JSON.stringify(config, null, 2)}\n`, 'utf8');
340
- };
341
- const resolveError = (error) => {
342
- if (error && typeof error === 'object') {
343
- if ('data' in error) {
344
- const data = error.data;
345
- if (data?.code) {
346
- return {
347
- code: data.code,
348
- message: error instanceof Error
349
- ? error.message
350
- : 'Request failed',
351
- details: data.httpStatus ? { httpStatus: data.httpStatus } : undefined,
352
- };
353
- }
354
- }
355
- if ('message' in error && typeof error.message === 'string') {
356
- return {
357
- code: 'REQUEST_FAILED',
358
- message: error.message,
359
- };
360
- }
361
- }
362
- return {
363
- code: 'UNKNOWN_ERROR',
364
- message: 'Unknown error',
365
- };
366
- };
367
- const printSuccess = (data) => {
368
- const envelope = {
369
- ok: true,
370
- data,
371
- };
372
- console.log(JSON.stringify(envelope, null, outputPretty ? 2 : undefined));
373
- };
374
- const fail = (code, message, details) => {
375
- const envelope = {
376
- ok: false,
377
- error: {
378
- code,
379
- message,
380
- ...(details !== undefined ? { details } : {}),
381
- },
382
- };
383
- console.error(JSON.stringify(envelope, null, outputPretty ? 2 : undefined));
384
- process.exit(1);
385
- };
386
- const printUsage = () => {
387
- const usage = [
388
- 'NAME',
389
- ' rw, rankwrangler - RankWrangler command line interface',
390
- '',
391
- 'SYNOPSIS',
392
- ' rw <resource> <verb> [args...] [options]',
393
- ' rankwrangler <resource> <verb> [args...] [options]',
394
- '',
395
- 'COMMANDS',
396
- ' products get <ASIN...>',
397
- ' products history <ASIN>',
398
- ' license status',
399
- ' license validate',
400
- ' config show',
401
- ' config clear',
402
- ' config set api-key <value>',
403
- ' config set base-url <origin>',
404
- ' config set marketplace <marketplaceId>',
405
- '',
406
- 'OPTIONS',
407
- ' -h, --help Show this help message',
408
- ' --apiKey <value> Override API key',
409
- ' --baseUrl <origin> Override API origin',
410
- ` -m, --marketplace <id> Override marketplace (default: ${DEFAULT_MARKETPLACE_ID})`,
411
- ' --asin <ASIN> Add ASIN (repeatable)',
412
- ' --metrics <list> History metrics: bsr,price (default: bsr,price)',
413
- ' --days <N> History lookback window (30-3650, default: 365)',
414
- ' --startAt <ISO> History range start (ISO date/time)',
415
- ' --endAt <ISO> History range end (ISO date/time)',
416
- ' --limit <N> Max points per metric (1-10000, default: 5000)',
417
- '',
418
- 'FILES',
419
- ` ${CONFIG_PATH} Local CLI config`,
420
- '',
421
- 'ENVIRONMENT',
422
- ' RR_LICENSE_KEY API key fallback',
423
- ' RR_API_URL API origin fallback',
424
- ' RR_MARKETPLACE_ID Marketplace fallback',
425
- ' RR_ASIN Single ASIN fallback',
426
- ' RR_ASINS Comma-separated ASIN fallback',
427
- ' RR_HISTORY_METRICS Comma-separated history metrics fallback',
428
- '',
429
- 'EXAMPLES',
430
- ' rw config set api-key rrk_...',
431
- ' rw products get B0DV53VS61',
432
- ' rw products history B0DV53VS61 --metrics bsr,price',
433
- ' rw products history B0DV53VS61 --startAt 2025-01-01 --endAt 2025-12-31',
434
- ' rankwrangler license status',
435
- ];
436
- console.log(usage.join('\n'));
184
+ const isSupportedCommand = (command) => {
185
+ return SUPPORTED_COMMANDS.has(`${command.resource}:${command.verb}`);
437
186
  };
438
187
  await main().catch(error => {
439
188
  const resolved = resolveError(error);
@@ -0,0 +1,2 @@
1
+ import type { CliPaths } from './cli-config';
2
+ export declare const printUsage: (paths: CliPaths) => void;
package/dist/usage.js ADDED
@@ -0,0 +1,55 @@
1
+ const DEFAULT_MARKETPLACE_ID = 'ATVPDKIKX0DER';
2
+ export const printUsage = (paths) => {
3
+ const usage = [
4
+ 'NAME',
5
+ ' rw, rankwrangler - RankWrangler command line interface',
6
+ '',
7
+ 'SYNOPSIS',
8
+ ' rw <resource> <verb> [args...] [options]',
9
+ ' rankwrangler <resource> <verb> [args...] [options]',
10
+ '',
11
+ 'COMMANDS',
12
+ ' products get <ASIN...>',
13
+ ' products history <ASIN>',
14
+ ' license status',
15
+ ' license validate',
16
+ ' config show',
17
+ ' config clear',
18
+ ' config set base-url <origin>',
19
+ ' config set marketplace <marketplaceId>',
20
+ ' config set storage-dir <path>',
21
+ '',
22
+ 'OPTIONS',
23
+ ' -h, --help Show this help message',
24
+ ' --baseUrl <origin> Override API origin',
25
+ ` -m, --marketplace <id> Override marketplace (default: ${DEFAULT_MARKETPLACE_ID})`,
26
+ ' --asin <ASIN> Add ASIN (repeatable)',
27
+ ' --metrics <list> History metrics: bsr,price (default: bsr,price)',
28
+ ' --days <N> History lookback window (30-3650, default: 365)',
29
+ ' --startAt <ISO> History range start (ISO date/time)',
30
+ ' --endAt <ISO> History range end (ISO date/time)',
31
+ ' --limit <N> Max points per metric (1-10000, default: 5000)',
32
+ '',
33
+ 'FILES',
34
+ ` ${paths.configPath} Active CLI config`,
35
+ ` ${paths.globalConfigPath} Global CLI settings`,
36
+ '',
37
+ 'ENVIRONMENT',
38
+ ' RR_LICENSE_KEY API key source',
39
+ ' RR_API_URL API origin fallback',
40
+ ' RR_MARKETPLACE_ID Marketplace fallback',
41
+ ' RR_STORAGE_DIR Storage directory override',
42
+ ' RR_ASIN Single ASIN fallback',
43
+ ' RR_ASINS Comma-separated ASIN fallback',
44
+ ' RR_HISTORY_METRICS Comma-separated history metrics fallback',
45
+ '',
46
+ 'EXAMPLES',
47
+ ' RR_LICENSE_KEY=rrk_... rw products get B0DV53VS61',
48
+ ' RR_STORAGE_DIR=~/.config/rankwrangler rw config show',
49
+ ' rw config set storage-dir ~/.config/rankwrangler',
50
+ ' rw products history B0DV53VS61 --metrics bsr,price',
51
+ ' rw products history B0DV53VS61 --startAt 2025-01-01 --endAt 2025-12-31',
52
+ ' rankwrangler license status',
53
+ ];
54
+ console.log(usage.join('\n'));
55
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rankwrangler/cli",
3
- "version": "0.2.3",
3
+ "version": "0.4.0",
4
4
  "description": "Official RankWrangler command line interface",
5
5
  "type": "module",
6
6
  "bin": {
@@ -14,13 +14,14 @@
14
14
  "README.md"
15
15
  ],
16
16
  "scripts": {
17
- "build": "tsc -p tsconfig.json"
17
+ "build": "tsc -p tsconfig.json && node scripts/fix-dist-imports.mjs",
18
+ "test:e2e": "bun test test/index.e2e.test.ts"
18
19
  },
19
20
  "publishConfig": {
20
21
  "access": "public"
21
22
  },
22
23
  "dependencies": {
23
- "@rankwrangler/http-client": "^0.2.3"
24
+ "@rankwrangler/http-client": "^0.4.0"
24
25
  },
25
26
  "devDependencies": {
26
27
  "@types/node": "^24.3.0",