plugins/importers/iCloud/contacts.js

const cliProgress = require('cli-progress');
const promptiCloud = require('./prompt-credentials');
const podClient = require('../../../pod/client');

/** The Person Item class. We create a Person for every iCloud contact. */
class Person {
  constructor(externalId, firstName, lastName, role, birthDate) {
    this._type = 'Person';
    this.deleted = false;
    this.externalId = externalId;
    this.firstName = firstName;
    this.lastName = lastName;
    this.role = role;
    this.birthDate = birthDate;
  }
}

/** Contact Importer */
(async () => {
  const client = await new podClient.PodClient();
  await client.testConnection();

  console.log('Connect to iCloud...');
  const myCloud = await promptiCloud();

  // download iCould contacts
  console.log('Fetch contacts...');
  const contactsData = await myCloud.Contacts.list();
  const contacts = contactsData['contacts'];
  // console.log(contactsData) // TODO add groups from contactsData

  console.log('Download contacts...');
  const progressBar = new cliProgress.SingleBar({}, cliProgress.Presets.shades_classic);
  progressBar.start(contacts.length, 0, {});

  for (const contact of contacts) {
    progressBar.increment();
    // console.log(contact['prefix']) // TODO add to schema
    // console.log(contact['companyName']) // TODO add company
    // console.log(contact['profiles'] // TODO add to schema
    // console.log(contact['streetAddress']) // TODO add to schema
    // console.log(contact['notes']) // TODO add to schema
    // console.log(contact['IMs']) // TODO iterate and add item per instance
    // console.log(contact['phones']) // TODO iterate and add item per instance

    let person = new Person(
      contact['contactId'],
      contact['firstName'],
      contact['lastName'],
      contact['jobTitle'],
      contact['birthDate']
    );

    // send to Pod
    await client.createItem(person);
  }
  progressBar.stop();
  myCloud.saveSession();

  // let allItems = await client.getAllItems();
  // console.log(allItems);

})();