All files / app factory.ts

100% Statements 216/216
100% Branches 19/19
100% Functions 5/5
100% Lines 216/216

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 2161x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 79x 79x 79x 3x 3x 76x 79x 3x 3x 73x 73x 73x 1x 1x 1x 1x 1x 1x 1x 1x 66x 66x 26x 26x 40x 40x 40x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 5x 5x 5x 5x 2x 2x 3x 3x 5x 1x 1x 2x 2x 2x 2x 2x 2x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 43x 43x 43x 43x 43x 43x 43x 43x 43x 43x 43x 43x 43x 43x 43x 43x 43x 43x 43x 43x 43x 43x 43x 43x 43x 43x 43x 43x 43x 43x 43x 43x 43x 43x 43x 43x 43x 43x 43x 43x 43x 43x 43x 43x 43x 43x 43x 43x 43x 43x 43x 43x 43x 43x 43x 43x 43x 43x 43x 43x 43x 43x 43x 43x 43x 43x 43x 43x 43x 43x 43x 43x 43x 43x 43x 43x 43x 43x 4x 43x 43x 43x 43x 43x 17x 43x 43x 43x 43x 43x 5x 43x 43x 43x 43x 43x 3x 43x 43x 43x 43x 43x 43x 43x 43x
import {Command, Option, InvalidArgumentError} from "commander";
import Cron from "croner";
import * as PACKAGE_INFO from '../lib/package.js';
import {ErrorHandler} from "./event/error-handler.js";
import {TokenApp, SyncApp, ArchiveApp, iCPSApp, DaemonApp} from "./icloud-app.js";
 
/**
 * This function can be used as a commander argParser. It will try to parse the value as a positive integer and throw an invalid argument error in case it fails
 * @param value - The string literal, read from the CLI
 * @param _dummyPrevious - Conforming to the interface - unused
 * @returns The parsed number
 * @throws An InvalidArgumentError in case parsing failed
 */
function commanderParsePositiveInt(value: string, _dummyPrevious?: unknown): number {
    const parsedValue = parseInt(value, 10);
    if (isNaN(parsedValue)) {
        throw new InvalidArgumentError(`Not a number.`);
    }
 
    if (parsedValue < 0) {
        throw new InvalidArgumentError(`Not a positive number.`);
    }
 
    return parsedValue;
}
 
/**
 * This function can be used as a commander argParser. It will try to parse the value as an integer or the string literal 'Infinity' and throw an invalid argument error in case it fails
 * @param value - The string literal, read from the CLI
 * @param _dummyPrevious - Conforming to the interface - unused
 * @returns The parsed number
 * @throws An InvalidArgumentError in case parsing failed
 */
function commanderParsePositiveIntOrInfinity(value: string, _dummyPrevious?: unknown): number {
    if (value === `Infinity`) {
        return Infinity;
    }
 
    return commanderParsePositiveInt(value);
}
 
/**
 * Tries parsing a string as Cron schedule
 * @param value - The string literal, read from the CLI
 * @param _dummyPrevious - Conforming to the interface - unused
 * @returns The original string
 * @throws An InvalidArgumentError in case parsing failed
 */
function commanderParseCron(value: string, _dummyPrevious?: unknown): string {
    try {
        const job = new Cron(value);
        job.stop();
        return value;
    } catch (err) {
        throw new InvalidArgumentError(`Not a valid cron pattern. See https://crontab.guru (or for more information on the underlying implementation https://github.com/hexagon/croner#pattern)`);
    }
}
 
/**
 * This function can be used as a commander argParser. It will try to parse the value as an interval with the format \<numberOfRequests|Infinity\>/\<timeInMs\>.
 * @param value - The string literal, read from the CLI
 * @param _dummyPrevious - Conforming to the interface - unused
 * @returns A tuple consisting of the max number of runs in the given interval of time and the interval in ms.
 * @throws An InvalidArgumentError in case parsing failed
 */
function commanderParseInterval(value: string, _dummyPrevious?: unknown): [number, number] {
    const expectedRegExp = /^([0-9]+|Infinity)\/([0-9]+)$/;
    const match = value.match(expectedRegExp);
    if (!match) {
        throw new InvalidArgumentError(`Not a valid interval pattern. Expects the format '<numberOfRequests|Infinity>/<timeInMs>', e.g. '1/20' to limit requests to one in 20ms.`);
    }
 
    const intervalCap = commanderParsePositiveIntOrInfinity(match[1]);
    if (typeof intervalCap === `number` && intervalCap <= 0) {
        throw new InvalidArgumentError(`Not a valid interval. Number of runs needs to be >0`);
    }
 
    return [
        intervalCap,
        commanderParsePositiveInt(match[2]),
    ];
}
 
export type iCPSAppOptions = {
    username: string,
    password: string,
    trustToken: string,
    dataDir: string,
    port: number,
    maxRetries: number,
    downloadThreads: number,
    schedule: string,
    enableCrashReporting: boolean,
    failOnMfa: boolean,
    force: boolean,
    refreshToken: boolean,
    remoteDelete: boolean,
    logLevel: string,
    silent: boolean,
    logToCli: boolean,
    suppressWarnings: boolean,
    exportMetrics: boolean,
    metadataRate: [number, number]
}
 
/**
 * This function will parse the provided string array and environment variables and return the correct application object.
 * @param argv - The argument vector to be parsed
 * @returns - The appropriate iCloudApp object, ready to run
 */
export function appFactory(argv: string[]): iCPSApp {
    const AppCommands = {
        "archive": `archive`,
        "sync": `sync`,
        "token": `token`,
        "daemon": `daemon`,
    };
 
    const program = new Command();
    let app: iCPSApp;
 
    program.name(PACKAGE_INFO.NAME)
        .description(PACKAGE_INFO.DESC)
        .version(PACKAGE_INFO.VERSION)
        .addOption(new Option(`-u, --username <email>`, `AppleID username.`)
            .env(`APPLE_ID_USER`)
            .makeOptionMandatory(true))
        .addOption(new Option(`-p, --password <password>`, `AppleID password.`)
            .env(`APPLE_ID_PWD`)
            .makeOptionMandatory(true))
        .addOption(new Option(`-T, --trust-token <string>`, `The trust token for authentication. If not provided, '.trust-token.icloud' in data dir is tried to be read. If all fails, a new trust token will be acquired, requiring the input of an MFA code.`)
            .env(`TRUST_TOKEN`))
        .addOption(new Option(`-d, --data-dir <string>`, `Directory to store local copy of library.`)
            .env(`DATA_DIR`)
            .default(`/opt/icloud-photos-library`))
        .addOption(new Option(`-P, --port <number>`, `Port number for MFA server (Awaiting MFA code when necessary).`)
            .env(`PORT`)
            .default(80)
            .argParser(commanderParsePositiveInt))
        .addOption(new Option(`-r, --max-retries <number>`, `Sets the number of maximum retries upon an error ('Infinity' means that it will always retry).`)
            .env(`MAX_RETRIES`)
            .default(Infinity, `Infinity`)
            .argParser(commanderParsePositiveIntOrInfinity))
        .addOption(new Option(`-t, --download-threads <number>`, `Sets the number of download threads ('Infinity' will remove all limitations).`)
            .env(`DOWNLOAD_THREADS`)
            .default(5)
            .argParser(commanderParsePositiveIntOrInfinity))
        .addOption(new Option(`-S, --schedule <cron-string>`, `In case this app is executed in daemon mode, it will use this cron schedule to perform regular syncs.`)
            .env(`SCHEDULE`)
            .default(`0 2 * * *`)
            .argParser(commanderParseCron))
        .addOption(new Option(`--enable-crash-reporting`, `Enables automatic collection of errors and crashes, see https://icloud-photos-sync.steilerdev.de/user-guides/error-reporting/ for more information.`)
            .env(`ENABLE_CRASH_REPORTING`)
            .default(false))
        .addOption(new Option(`--fail-on-mfa`, `If a MFA is necessary, exit the program. (This might be useful in test and scheduled scenarios)`)
            .env(`FAIL_ON_MFA`)
            .default(false))
        .addOption(new Option(`--force`, `Force the execution of the operation, independent of an existing lock on the library. USE WITH CAUTION!`)
            .env(`FORCE`)
            .default(false))
        .addOption(new Option(`--refresh-token`, `Ignore any stored token and always refresh it.`)
            .env(`REFRESH_TOKEN`)
            .default(false))
        .addOption(new Option(`--remote-delete`, `If this flag is set, delete non-favorite photos in the iCloud Photos backend upon archiving.`)
            .env(`REMOTE_DELETE`)
            .default(false))
        .addOption(new Option(`-l, --log-level <level>`, `Set the log level. NOTE: 'trace' might leak sensitive session data.`)
            .env(`LOG_LEVEL`)
            .choices([`trace`, `debug`, `info`, `warn`, `error`])
            .default(`info`))
        .addOption(new Option(`-s, --silent`, `Disables logging to the console and forces logs to go to the log file.`)
            .env(`SILENT`)
            .default(false))
        .addOption(new Option(`--log-to-cli`, `Disables logging to file and logs everything to the console. This will be ignored if '--silent' is set.`)
            .env(`LOG_TO_CLI`)
            .default(false))
        .addOption(new Option(`--suppress-warnings`, `Non critical warnings will not be displayed in the UI. They will still go into the log.`)
            .env(`SUPPRESS_WARNINGS`)
            .default(false))
        .addOption(new Option(`--export-metrics`, `Enables the export of sync metrics to a file using the Influx Line Protocol.`)
            .env(`EXPORT_METRICS`)
            .default(false))
        .addOption(new Option(`--metadata-rate <interval>`, `Limits the rate of metadata fetching in order to avoid getting throttled by the API. Expects the format '<numberOfRequests|Infinity>/<timeInMs>', e.g. '1/20' to limit requests to one request in 20ms.`)
            .env(`METADATA_RATE`)
            .default([Infinity, 0], `Infinity/0`)
            .argParser(commanderParseInterval));
 
    program.command(AppCommands.daemon)
        .action(() => {
            app = new DaemonApp(program.opts() as iCPSAppOptions);
        })
        .description(`Starts the synchronization in scheduled daemon mode - continuously running based on the provided cron schedule.`);
 
    program.command(AppCommands.token)
        .action(() => {
            app = new TokenApp(program.opts() as iCPSAppOptions);
        })
        .description(`Validates the current trust token, fetches a new one (if necessary) and prints it to the CLI.`);
 
    program.command(AppCommands.sync)
        .action(() => {
            app = new SyncApp(program.opts() as iCPSAppOptions);
        })
        .description(`This command will fetch the remote state and persist it to the local disk.`);
 
    program.command(AppCommands.archive)
        .action(archivePath => {
            app = new ArchiveApp(program.opts() as iCPSAppOptions, archivePath);
        })
        .description(`Archives a given folder. Before archiving, it will first perform a sync, to make sure the correct state is archived.`)
        .argument(`<path>`, `Path to the folder that should be archived`);
 
    program.parse(argv);
    ErrorHandler.cleanEnv();
    return app;
}