3

Cannot figure out why the below script won't run. It is likely the script is not going to do what I want but using

node ./contentful/contentful-assets.js

in the terminal, it does nothing - No errors, nothing logged for me to even start debugging. However, if I remove async it will attempt the script and shoot back an error.

./contentful/contentful-assets.js

const contentful = require('contentful-management');
const iterator = require('make-iterator');
const assets = require('./assetObject.js');

async resolve => {
  console.log('Creating Contentful client');
  const client = contentful.createClient({
    accessToken: 'token',
    logHandler: (level, data) => console.log(`${level} | ${data}`)
  });

  const iterableAssets = iterator(assets);
  const space = await client.getSpace('space');
  const environment = await space.getEnvironment('enviroment');

  const cmsAssets = [];

  const assetProcessingTimes = [];
  const inProcess = new Map();
  let processedAssetsCounter = 0;

  const createAndPublishSingleAsset = async ({ asset, done, index }) => {
    if (done) {
      if (inProcess.size > 0) return false;
      return resolve(cmsAssets);
    }

    const start = Date.now();
    const id = '' + start + Math.round(Math.random() * 100);
    inProcess.set(id, true);
    let cmsAsset;

    try {
      cmsAsset = await environment.createAssetWithId(asset.postId, {
        fields: {
          title: {
            'en-US': asset.title
          },
          description: {
            'en-US': asset.description
          },
          file: {
            'en-US': {
              contentType: 'image/jpg',
              fileName: asset.filename,
              upload: asset.link
            }
          }
        }
      });
    } catch (e) {
      console.log(`Asset "${asset.title}" failed to create, retrying...`);
      createAndPublishSingleAsset({
        asset,
        done,
        index
      });
    }
    try {
      const processedCMSAsset = await cmsAsset.processForAllLocales();
      const publishedCMSAsset = await processedCMSAsset.publish();

      cmsAssets.push(publishedCMSAsset);
      assetProcessingTimes.push((Date.now() - start) / 1000);
      inProcess.clear(id);
      const eta = Math.floor(
        assetProcessingTimes.reduce((a, b) => a + b, 0) /
          assetProcessingTimes.length *
          (assets.length - index) /
          60
      );
      processedAssetsCounter += 1;
      console.log(
        `Processed asset ${processedAssetsCounter}/${assets.length} - eta: ${eta}m`
      );
      createAndPublishSingleAsset(iterableAssets.next());
    } catch (e) {
      console.log(`Asset "${asset.title}" failed to process, retrying...`);
      await cmsAsset.delete();
      createAndPublishSingleAsset({
        asset,
        done,
        index
      });
    }
  };
  console.log('Starting to create assets');
  createAndPublishSingleAsset(iterableAssets.next());
  createAndPublishSingleAsset(iterableAssets.next());
  createAndPublishSingleAsset(iterableAssets.next());
};

assetObject.js

[
  {
    link: 'https://example.com/example1.jpg',
    title: 'Example 1',
    description: 'Description of example 1',
    postId: '1234567890',
    filename: 'example1.jpeg'
  }, ... // Many more
]

What have I missed here?

0

2 Answers 2

5

I fear that you are not calling the function, could you try, the following?

const contentful = require('contentful-management');
const iterator = require('make-iterator');
const assets = require('./assetObject.js');

const doWork = async resolve => {
  console.log('Creating Contentful client');
  ...
}

doWork();

You are just declaring a function that is async and does all of the code defined, but you are not actually calling it.

Sign up to request clarification or add additional context in comments.

1 Comment

Haha... yes. Thank you very much.
0

In this code snippet you are declaring a function, but never invoking it:

//declaring an async function, with "resolve" as the argument
async resolve => {
 //function definition
}

In order to be able to later reference the function to invoke you can assign it to const/let/etc.:

const createAssets = async resolve => { }
//now, invoke
createAssets()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.