Skip to content

Commit

Permalink
contentfulのエントリからutc offsetを削除する #5
Browse files Browse the repository at this point in the history
  • Loading branch information
mtane0412 committed Jan 12, 2021
1 parent 47d5d9d commit d781d2c
Showing 1 changed file with 52 additions and 0 deletions.
52 changes: 52 additions & 0 deletions contentful/remove-timezone.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
require("dotenv").config({ path: `.env.development` });
const env = process.env;

process.on("unhandledRejection", console.dir);

const contentful = require("contentful-management");
const client = contentful.createClient({
accessToken: env.CONTENTFUL_PERSONAL_ACCESS_TOKEN,
});

const convertNoUtcOffset = async (entries) => {
for (let item of entries.items) {
try {
if (item.fields.publishDate.ja.split('+')[1]) {
const convertedTime = await item.fields.publishDate.ja.split('+')[0];
console.log(`converted: ${item.fields.publishDate.ja} --> ${convertedTime}`);
item.fields.publishDate.ja = convertedTime;
const updatedEntry = await item.update();
await updatedEntry.publish();
}
} catch (err) {
console.log('skipped for some reason');
}
};
}

const checkPublishDate = (entries) => {
for (let item of entries.items) {
try {
if (item.fields.publishDate.ja.split('+')[1]) {
console.log(`warning: ${item.fields.title.ja}`);
console.log(item.fields.publishDate.ja);
} else {
console.log(item.fields.publishDate.ja)
}
} catch (err) {
throw err;
}
}
}

(async () => {
const space = await client.getSpace(env.CONTENTFUL_SPACE_ID);
const environment = await space.getEnvironment("master");
const entries = await environment.getEntries({
content_type: "blogPost",
limit: 200,
order: "sys.createdAt",
});
convertNoUtcOffset(entries);
checkPublishDate(entries);
})();

0 comments on commit d781d2c

Please sign in to comment.