Skip to content

Commit

Permalink
replace chalk with picocolors & fix issue with prettier config
Browse files Browse the repository at this point in the history
  • Loading branch information
Ben ZΓΆrb committed Apr 22, 2024
1 parent 77b9daa commit 9ad625b
Show file tree
Hide file tree
Showing 11 changed files with 85 additions and 84 deletions.
21 changes: 6 additions & 15 deletions cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
/* eslint-env node */
const fs = require('fs-extra');
const path = require('path');
const chalk = require('chalk');
const pc = require('picocolors');
const { Command } = require('commander');

const { initializeContentModel, migrateToContentStorage, migrateToTagStorage } = require('./lib/backend');
Expand Down Expand Up @@ -39,9 +39,9 @@ const parseArgs = (cmd) => {
const errorHandler = (error, log) => {
if (log) {
const { errors, message } = error;
console.error(chalk.red('\nError:'), message);
console.error(pc.red('\nError:'), message);
(errors || []).forEach((err) => {
console.error(chalk.red('Error:'), err.message);
console.error(pc.red('Error:'), err.message);
});
}
process.exit(1);
Expand Down Expand Up @@ -144,10 +144,7 @@ program

const { missingStorageModel } = verified;
if (missingStorageModel) {
console.error(
chalk.red('\nError:'),
`Missing migration content type. Run ${chalk.cyan('npx migrations init')}`
);
console.error(pc.red('\nError:'), `Missing migration content type. Run ${pc.cyan('npx migrations init')}`);
process.exit(1);
}

Expand All @@ -170,10 +167,7 @@ program

const { missingStorageModel } = verified;
if (missingStorageModel) {
console.error(
chalk.red('\nError:'),
`Missing migration content type. Run ${chalk.cyan('npx migrations init')}`
);
console.error(pc.red('\nError:'), `Missing migration content type. Run ${pc.cyan('npx migrations init')}`);
process.exit(1);
}

Expand All @@ -198,10 +192,7 @@ program

const { missingStorageModel } = verified;
if (missingStorageModel) {
console.error(
chalk.red('\nError:'),
`Missing migration content type. Run ${chalk.cyan('npx migrations init')}`
);
console.error(pc.red('\nError:'), `Missing migration content type. Run ${pc.cyan('npx migrations init')}`);
process.exit(1);
}

Expand Down
10 changes: 5 additions & 5 deletions lib/backend.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const path = require('path');
const fs = require('fs/promises');
const chalk = require('chalk');
const pc = require('picocolors');
const cliProgress = require('cli-progress');
const { getEnvironment, getDefaultLocale, getMigrationItems } = require('./contentful');
const { STORAGE_TAG, STORAGE_CONTENT, STATE_SUCCESS, STATE_FAILURE } = require('./config');
Expand All @@ -19,7 +19,7 @@ const initializeContentModel = async (config) => {

if (!exists) {
console.log(
`\nCreating content-type: ${chalk.green(migrationContentTypeId)} in environment ${chalk.green(environmentId)}`
`\nCreating content-type: ${pc.green(migrationContentTypeId)} in environment ${pc.green(environmentId)}`
);
const contentType = await client.createContentTypeWithId(migrationContentTypeId, {
name: 'Migrations',
Expand Down Expand Up @@ -204,7 +204,7 @@ const migrateToContentStorage = async (config) => {
});

console.log(

Check failure on line 206 in lib/backend.js

View workflow job for this annotation

GitHub Actions / Node 14 on ubuntu-latest

Replace `⏎····`\nFound·${pc.green(filtered.length)}·executed·migrations·in·environment·${pc.green(environmentId)}`⏎··` with ``\nFound·${pc.green(filtered.length)}·executed·migrations·in·environment·${pc.green(environmentId)}``

Check failure on line 206 in lib/backend.js

View workflow job for this annotation

GitHub Actions / Node 16 on ubuntu-latest

Replace `⏎····`\nFound·${pc.green(filtered.length)}·executed·migrations·in·environment·${pc.green(environmentId)}`⏎··` with ``\nFound·${pc.green(filtered.length)}·executed·migrations·in·environment·${pc.green(environmentId)}``
`\nFound ${chalk.green(filtered.length)} executed migrations in environment ${chalk.green(environmentId)}`
`\nFound ${pc.green(filtered.length)} executed migrations in environment ${pc.green(environmentId)}`
);

const bar = new cliProgress.SingleBar(
Expand Down Expand Up @@ -254,7 +254,7 @@ const migrateToTagStorage = async (config) => {
);

if (version && (oldVersion || 0) < version) {
console.log(`\nAdding ${chalk.green(fieldId)} tag with version ${chalk.green(version)}`);
console.log(`\nAdding ${pc.green(fieldId)} tag with version ${pc.green(version)}`);
await setMigrationTag(version, config);
}

Expand Down Expand Up @@ -393,7 +393,7 @@ const getNewMigrations = async (config) => {
if (initial) {
return migrations;
}
console.error(chalk.red('\nError:'), `Missing migration content type. Run ${chalk.cyan('npx migrations init')}`);
console.error(pc.red('\nError:'), `Missing migration content type. Run ${pc.cyan('npx migrations init')}`);
process.exit(1);
}
}
Expand Down
16 changes: 8 additions & 8 deletions lib/content.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const inquirer = require('inquirer');
const contentfulImport = require('contentful-import');
const chalk = require('chalk');
const pc = require('picocolors');
const { getContent, getContentId, getLinkedAssets, getLinkedEntries } = require('./contentful');
const { diff } = require('./diff');
const { buildTree } = require('./tree');
Expand Down Expand Up @@ -60,14 +60,14 @@ const transferContent = async (config) => {

if (sourceVersion !== destVersion) {
throw new Error(
`Different migration states detected. ${chalk.bold(sourceEnvironmentId)} (${sourceVersion}) !== ${chalk.bold(
`Different migration states detected. ${pc.bold(sourceEnvironmentId)} (${sourceVersion}) !== ${pc.bold(
destEnvironmentId
)} (${destVersion})`
);
}

// Get content from source environment (all + filtered by passed contentType)
console.log(`\nFetching content from ${chalk.green(sourceEnvironmentId)} environment`);
console.log(`\nFetching content from ${pc.green(sourceEnvironmentId)} environment`);
const {
entries: sourceEntries,
assets: sourceAssetsBase,
Expand All @@ -78,7 +78,7 @@ const transferContent = async (config) => {
environmentId: sourceEnvironmentId,
});

console.log(`Fetching content from ${chalk.green(destEnvironmentId)} environment\n`);
console.log(`Fetching content from ${pc.green(destEnvironmentId)} environment\n`);
const {
entries: destEntries,
assets: destAssets,
Expand Down Expand Up @@ -136,7 +136,7 @@ const transferContent = async (config) => {
const br = diffConflicts && Object.keys(entryOverwrites).length ? '\n' : '';

if (assets.length === 0 && entries.length === 0) {
console.log(chalk.green(`${br}All done`), 'πŸš€');
console.log(pc.green(`${br}All done`), 'πŸš€');
return;
}

Expand All @@ -146,9 +146,9 @@ const transferContent = async (config) => {
}

console.log(
`${br}Transfering ${chalk.cyan(`${entries.length} Entries`)} and ${chalk.cyan(
`${br}Transfering ${pc.cyan(`${entries.length} Entries`)} and ${pc.cyan(

Check failure on line 149 in lib/content.js

View workflow job for this annotation

GitHub Actions / Node 14 on ubuntu-latest

Replace `⏎······`${assets.length}·Assets`⏎····)}·from·${pc.cyan(sourceEnvironmentId` with ``${assets.length}·Assets`)}·from·${pc.cyan(⏎······sourceEnvironmentId⏎····`

Check failure on line 149 in lib/content.js

View workflow job for this annotation

GitHub Actions / Node 16 on ubuntu-latest

Replace `⏎······`${assets.length}·Assets`⏎····)}·from·${pc.cyan(sourceEnvironmentId` with ``${assets.length}·Assets`)}·from·${pc.cyan(⏎······sourceEnvironmentId⏎····`
`${assets.length} Assets`
)} from ${chalk.cyan(sourceEnvironmentId)} to ${chalk.cyan(destEnvironmentId)}`
)} from ${pc.cyan(sourceEnvironmentId)} to ${pc.cyan(destEnvironmentId)}`
);

proceed = await confirm(config);
Expand All @@ -168,7 +168,7 @@ const transferContent = async (config) => {
console.log(error.message);
}

console.log(chalk.green('\nAll done'), 'πŸš€');
console.log(pc.green('\nAll done'), 'πŸš€');
};

module.exports.transferContent = transferContent;
36 changes: 18 additions & 18 deletions lib/diff.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const Diff = require('diff');
const { documentToPlainTextString } = require('@contentful/rich-text-plain-text-renderer');
const chalk = require('chalk');
const pc = require('picocolors');
const { diff: deepDiff } = require('deep-diff');
const {
getContentId,
Expand Down Expand Up @@ -28,9 +28,9 @@ const getNodeDate = (node) => {
return date.toLocaleDateString(locale, options);
};

const oldValueColor = (text) => chalk.reset.redBright.dim(text);
const newValueColor = (text) => chalk.reset.green(text);
const unchangedColor = (text) => chalk.reset.grey(text);
const oldValueColor = (text) => pc.reset(pc.red(pc.dim(text)));
const newValueColor = (text) => pc.reset(pc.green(text));
const unchangedColor = (text) => pc.reset(pc.grey(text));

const diffString = (source, dest) => {
const diff = Diff.diffWords(source, dest);
Expand All @@ -45,7 +45,7 @@ const diffString = (source, dest) => {

if (part.value.length > 50) {
const value = `${part.value.slice(0, 15)} [...] ${part.value.slice(-15)}`;
return unchangedColor(chalk.grey(value));
return unchangedColor(pc.grey(value));
}

return unchangedColor(part.value);
Expand Down Expand Up @@ -112,37 +112,37 @@ const diff = (source, dest, contentTypes) => {
const sourceValue = getFieldValue(fieldId, source);
const destValue = getFieldValue(fieldId, dest);
if (type === TYPE_SYMBOL) {
return `${chalk.bold(name)}: ${chalk.reset(diffString(`${destValue || ''}`, `${sourceValue || ''}`))}`;
return `${pc.bold(name)}: ${pc.reset(diffString(`${destValue || ''}`, `${sourceValue || ''}`))}`;
}
if (type === TYPE_RICHTEXT) {
return `${chalk.bold(name)}: ${chalk.reset(
return `${pc.bold(name)}: ${pc.reset(
diffString(documentToPlainTextString(destValue || {}), documentToPlainTextString(sourceValue || {}))
)}`;
}
if (type === TYPE_NUMBER) {
// return `${chalk.bold(name)}: ${chalk.reset(diffString(`${sourceValue || ''}`, `${destValue || ''}`))}`;
return `${chalk.bold(name)}: ${oldValueColor(`${destValue}`)} ${newValueColor(`${sourceValue}`)}`;
// return `${pc.bold(name)}: ${pc.reset(diffString(`${sourceValue || ''}`, `${destValue || ''}`))}`;
return `${pc.bold(name)}: ${oldValueColor(`${destValue}`)} ${newValueColor(`${sourceValue}`)}`;
}
if (type === TYPE_TEXT) {
return `${chalk.bold(name)}: ${chalk.reset(diffString(`${destValue || ''}`, `${sourceValue || ''}`))}`;
return `${pc.bold(name)}: ${pc.reset(diffString(`${destValue || ''}`, `${sourceValue || ''}`))}`;
}
if (type === TYPE_DATE) {
return `${chalk.bold(name)}: ${oldValueColor(`${destValue || ''}`)} ${newValueColor(`${sourceValue || ''}`)}`;
return `${pc.bold(name)}: ${oldValueColor(`${destValue || ''}`)} ${newValueColor(`${sourceValue || ''}`)}`;
}
if (type === TYPE_LOCATION) {
return `${chalk.bold(name)}: ${oldValueColor(`${JSON.stringify(destValue || '')}`)} ${newValueColor(
return `${pc.bold(name)}: ${oldValueColor(`${JSON.stringify(destValue || '')}`)} ${newValueColor(
`${JSON.stringify(sourceValue || '')}`
)}`;
}
if (type === TYPE_BOOLEAN) {
return `${chalk.bold(name)}: ${oldValueColor(`${JSON.stringify(destValue)}`)} ${newValueColor(
return `${pc.bold(name)}: ${oldValueColor(`${JSON.stringify(destValue)}`)} ${newValueColor(
`${JSON.stringify(sourceValue)}`
)}`;
}
if (type === TYPE_LINK) {
const sourceValueId = getContentId(sourceValue || {});
const destValueId = getContentId(destValue || {});
return `${chalk.bold(name)} (${linkType}): ${oldValueColor(destValueId || 'deleted')} ${newValueColor(
return `${pc.bold(name)} (${linkType}): ${oldValueColor(destValueId || 'deleted')} ${newValueColor(
sourceValueId || 'empty'
)}`;
}
Expand Down Expand Up @@ -175,7 +175,7 @@ const diff = (source, dest, contentTypes) => {
return `${oldValueColor(destArray[index] || '')}`;
});

return `${chalk.bold(name)}: [\n ${array.join('\n ')}\n ]`;
return `${pc.bold(name)}: [\n ${array.join('\n ')}\n ]`;
}

return name;
Expand All @@ -186,17 +186,17 @@ const diff = (source, dest, contentTypes) => {

return {
type: 'list',
message: `${chalk.reset('Conflict on')} ${chalk.cyan(name)}\n ${chalk.reset(`${fieldData.join('\n ')}`)}\n`,
message: `${pc.reset('Conflict on')} ${pc.cyan(name)}\n ${pc.reset(`${fieldData.join('\n ')}`)}\n`,

name: id,
choices: [
{
name: `Use ${oldValueColor(destEnv)} - updated on ${getNodeDate(dest)} ${chalk.bold('(skip)')}`,
name: `Use ${oldValueColor(destEnv)} - updated on ${getNodeDate(dest)} ${pc.bold('(skip)')}`,
value: false,
short: 'skip',
},
{
name: `Use ${newValueColor(sourceEnv)} - updated on ${getNodeDate(source)} ${chalk.bold('(overwrite)')}`,
name: `Use ${newValueColor(sourceEnv)} - updated on ${getNodeDate(source)} ${pc.bold('(overwrite)')}`,
value: true,
short: 'overwrite',
},
Expand Down
6 changes: 3 additions & 3 deletions lib/doc.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const path = require('path');
const chalk = require('chalk');
const pc = require('picocolors');
const fs = require('fs-extra');
const Mustache = require('mustache');
const { getEditorInterfaces, getContentTypes, getContentTypeId, getContentId } = require('./contentful');
Expand Down Expand Up @@ -105,11 +105,11 @@ const createOfflineDocs = async (config) => {
const filename = `${getContentId(contentType)}.${extension}`;
const filepath = path.join(directory, filename);
await fs.outputFile(filepath, content);
console.log(` - ${chalk.green(path.relative(process.cwd(), filepath))}`);
console.log(` - ${pc.green(path.relative(process.cwd(), filepath))}`);
}

// await fs.outputFile(filename, content);
// console.log(`Generated new migration file to ${chalk.green(filename)}`);
// console.log(`Generated new migration file to ${pc.green(filename)}`);
};

module.exports.createOfflineDocs = createOfflineDocs;
16 changes: 8 additions & 8 deletions lib/environment.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const chalk = require('chalk');
const pc = require('picocolors');
const { getSpace, getApiKeys } = require('./contentful');

const getActiveApiKey = async (config) => {
Expand All @@ -21,15 +21,15 @@ const getActiveApiKey = async (config) => {
*/
const createEnvironment = async (environmentId, config) => {
const { sourceEnvironmentId = 'master' } = config;
console.log(`\nCreating new environment ${chalk.green(environmentId)} from ${chalk.green(sourceEnvironmentId)}`);
console.log(`\nCreating new environment ${pc.green(environmentId)} from ${pc.green(sourceEnvironmentId)}`);

const space = await getSpace(config);
const environment = await space.createEnvironmentWithId(environmentId, { name: environmentId }, sourceEnvironmentId);

const apiKey = await getActiveApiKey(config);

if (apiKey) {
console.log(`Activating ${chalk.green(environmentId)} for API key ${chalk.green(apiKey.sys.id)}`);
console.log(`Activating ${pc.green(environmentId)} for API key ${pc.green(apiKey.sys.id)}`);
apiKey.environments = [
...apiKey.environments,
{
Expand All @@ -43,7 +43,7 @@ const createEnvironment = async (environmentId, config) => {

await apiKey.update();
}
console.log(chalk.green('\nDone'), 'πŸš€');
console.log(pc.green('\nDone'), 'πŸš€');
};

/**
Expand All @@ -55,20 +55,20 @@ const removeEnvironment = async (environmentId, config) => {
if (['master'].includes(environmentId)) {
throw new Error('Removing the master environment is not supported');
}
console.log(`\nRemoving environment ${chalk.green(environmentId)}`);
console.log(`\nRemoving environment ${pc.green(environmentId)}`);
const space = await getSpace(config);
const environment = await space.getEnvironment(environmentId);
await environment.delete();

const apiKey = await getActiveApiKey(config);
if (apiKey) {
console.log(`Removing ${chalk.green(environmentId)} from API key ${chalk.green(apiKey.sys.id)}`);
console.log(`Removing ${pc.green(environmentId)} from API key ${pc.green(apiKey.sys.id)}`);
const index = (apiKey.environments || []).findIndex((env) => env.sys.id === environment.sys.id);
apiKey.environments = [...apiKey.environments.slice(0, index), ...apiKey.environments.slice(index + 1)];

await apiKey.update();
}
console.log(chalk.green('\nDone'), 'πŸš€');
console.log(pc.green('\nDone'), 'πŸš€');
};

/**
Expand All @@ -80,7 +80,7 @@ const resetEnvironment = async (environmentId, config) => {
if (['master'].includes(environmentId)) {
throw new Error('Removing the master environment is not supported');
}
console.log(`\nRemoving environment ${chalk.green(environmentId)}`);
console.log(`\nRemoving environment ${pc.green(environmentId)}`);
const space = await getSpace(config);
try {
const environment = await space.getEnvironment(environmentId);
Expand Down
Loading

0 comments on commit 9ad625b

Please sign in to comment.