Skip to content

Commit

Permalink
Merge pull request #5 from AtidaTech/release-0.1.4
Browse files Browse the repository at this point in the history
Release 0.1.4
  • Loading branch information
fciacchi authored Jun 1, 2023
2 parents e6d1267 + 8b368fb commit e68875a
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 32 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
build/*
node_modules/*
export*
export/
export/*
.env
Expand Down
1 change: 1 addition & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
build/*
node_modules/*
export*
export/
export/*
.env
Expand Down
11 changes: 9 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
![GitHub code size in bytes](https://img.shields.io/github/languages/code-size/AtidaTech/contentful-cli-export)
![Downloads](https://img.shields.io/npm/dw/contentful-cli-export)
![Forks](https://img.shields.io/github/forks/AtidaTech/contentful-cli-export)
[![Bun.sh](https://img.shields.io/badge/bun.sh-compatible-orange)](https://bun.sh)

# Contentful Export Tool

Expand Down Expand Up @@ -42,6 +43,8 @@ Or, if using [yarn 🔗](https://yarnpkg.com/lang/en/):
yarn add contentful-cli-export
```

> Similarly, if you are using [Bun 🔗](https://bun.sh), just run `bun add contentful-cli-export`
### Requirements

* `node` >= 14.0.0
Expand All @@ -58,11 +61,13 @@ yarn add contentful-cli-export
CMS_MANAGEMENT_TOKEN=<management-token>
CMS_SPACE_ID=<space-id>
CMS_MAX_ALLOWED_LIMIT=100
CMS_EXPORT_DIR=export/
```

However, these values could also be passed as parameters during execution.

* Also, it is needed that you create an `export/` folder in the root of your project. It will contain all the exports.
* You will need to create the `CMS_EXPORT_DIR` folder, that will contain all the exports. This folder should stay preferably in the root of your project.
If no folder is specified, a folder `export/` will be created automatically if missing.

## 📟 Example

Expand Down Expand Up @@ -91,12 +96,14 @@ This script can be used from the command line and accepts various arguments for
* `--download-assets`: To include assets in the exported data.
* `--verbose`: Display the progress in new lines, instead of animated UI (useful in CI).
* `--compress`: To compress the result into a ZIP file.
* `--export-dir`: To specify a custom directory for the exported data (default is sub-directory `export/` in your project root).
* `--export-dir`: To specify a custom directory for the exported data (default is sub-directory `CMS_EXPORT_DIR` or `export/` in your project root). The script will exit if this custom folder doesn't exist.
* `--max-allowed-limit`: Number of entries to fetch at each iteration. Max: `1000` - Recommended: `100` (lower values fire more API calls, but avoid 'Response too big' error).
## 📅 Todo
* Add compatibility with official Contentful Export env/settings.
* Improve Logging (+ Colors).
* Add Tests.
## 👾 Contributors
Expand Down
44 changes: 29 additions & 15 deletions contentful-cli-export.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
#! /usr/bin/env node

const DELETE_FOLDER_DELAY = 5000
const PLACEHOLDER_MANAGEMENT_TOKEN = 'placeholder-management-token'
const PLACEHOLDER_SPACE_ID = 'placeholder-space-id'
const DEFAULT_ALLOWED_LIMIT = 100
const DEFAULT_EXPORT_DIR = 'export/'

;(async function main() {
try {
Expand All @@ -10,12 +14,15 @@ const DELETE_FOLDER_DELAY = 5000
const envValues = await getEnvValues(localeWorkingDir, scriptDirectory)

const cmsManagementToken =
envValues?.CMS_MANAGEMENT_TOKEN ?? 'placeholder-management-token'
const cmsSpaceId = envValues?.CMS_SPACE_ID ?? 'placeholder-space-id'
const cmsMaxEntries = parseInt(envValues?.CMS_MAX_ALLOWED_LIMIT) ?? 100
envValues?.CMS_MANAGEMENT_TOKEN ?? PLACEHOLDER_MANAGEMENT_TOKEN
const cmsSpaceId = envValues?.CMS_SPACE_ID ?? PLACEHOLDER_SPACE_ID
const cmsMaxEntries =
parseInt(envValues?.CMS_MAX_ALLOWED_LIMIT) ?? DEFAULT_ALLOWED_LIMIT
const cmsExportDir = envValues?.CMS_EXPORT_DIR ?? DEFAULT_EXPORT_DIR

const initialSettings = await parseArguments(
localeWorkingDir,
cmsExportDir,
cmsManagementToken,
cmsSpaceId,
cmsMaxEntries
Expand All @@ -37,6 +44,7 @@ const DELETE_FOLDER_DELAY = 5000
* @property {string} CMS_MANAGEMENT_TOKEN - The CMA token for Contentful.
* @property {string} CMS_SPACE_ID - The Space ID.
* @property {string|number} CMS_MAX_ALLOWED_LIMIT - The maximum number of entries per query.
* @property {string} CMS_EXPORT_DIR - The default export dir from the working directory.
*
*/
async function getEnvValues(localWorkingDir, scriptDirectory) {
Expand All @@ -61,7 +69,8 @@ async function getEnvValues(localWorkingDir, scriptDirectory) {
/**
* Parses command line arguments and sets default values.
*
* @param {string} dirNamePath - The directory path where the .env files are located.
* @param {string} rootFolder - The directory path where the .env files are located.
* @param {string} cmsExportDir - The CMS Default Export Directory.
* @param {string} cmsManagementToken - The CMS Management Token.
* @param {string} cmsSpaceId - The CMS Space ID.
* @param {number} [cmsMaxEntries=100] - The CMS Max Entries to fetch at each iteration.
Expand All @@ -80,10 +89,11 @@ async function getEnvValues(localWorkingDir, scriptDirectory) {
* @throws {Error} If '--environment-id' or '--from' are not provided or if '--management-token' or '--mt' are duplicated.
*/
async function parseArguments(
dirNamePath,
rootFolder,
cmsExportDir,
cmsManagementToken,
cmsSpaceId,
cmsMaxEntries = 100
cmsMaxEntries = DEFAULT_ALLOWED_LIMIT
) {
const minimist = (await import('minimist')).default
const dateFormat = (await import('dateformat')).default
Expand All @@ -97,7 +107,8 @@ async function parseArguments(
parsedArgs['management-token'] ?? parsedArgs['mt'] ?? cmsManagementToken
const maxEntries = parsedArgs['max-allowed-limit'] ?? cmsMaxEntries
const rootDestinationFolder = await getDestinationFolder(
dirNamePath,
rootFolder,
cmsExportDir,
parsedArgs
)

Expand Down Expand Up @@ -177,31 +188,34 @@ async function checkArgs(parsedArgs) {
/**
* This function gets the destination folder based on whether a custom folder is provided or not.
*
* @param {string} dirNamePath - The directory path where the .env files are located.
* @param {string} rootFolder - The directory path where the script is being executed.
* @param {string} cmsExportDir - The CMS Default Export Directory.
* @param {Object} parsedArgs - The object that contains the parsed command line arguments.
*
* @returns {Promise<object>} An object containing the evaluated destination folder and a flag indicating whether a custom folder was used.
* @property {string} destinationFolder - The destination folder for the export.
*
* @throws {Error} If the destination folder does not exist or is not accessible.
*/
async function getDestinationFolder(dirNamePath, parsedArgs) {
async function getDestinationFolder(rootFolder, cmsExportDir, parsedArgs) {
const fileSystem = await import('fs')

let destinationFolder = parsedArgs['export-dir'] ?? dirNamePath + '/export/'
if (!destinationFolder.endsWith('/')) {
destinationFolder += '/'
}
const defaultExportDirectory = cmsExportDir.startsWith('/')
? cmsExportDir
: `${rootFolder}/${cmsExportDir}`

let destinationFolder = parsedArgs['export-dir'] || defaultExportDirectory
destinationFolder = destinationFolder.replace(/\/$/, '') + '/'

// Create destination folder if not present
const destinationFolderExists = fileSystem.existsSync(destinationFolder)
if (!parsedArgs.hasOwnProperty('export-dir') && !destinationFolderExists) {
if (!parsedArgs['export-dir'] && !destinationFolderExists) {
fileSystem.mkdirSync(destinationFolder)
}

if (!fileSystem.existsSync(destinationFolder) || destinationFolder === '/') {
console.error(
'@@/ERROR: Destination folder does not exist or not accessible!'
'@@/ERROR: Destination folder does not exist or is not accessible!'
)
process.exit(1)
}
Expand Down
28 changes: 14 additions & 14 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "contentful-cli-export",
"version": "0.1.3",
"version": "0.1.4",
"type": "module",
"description": "Contentful CLI Export Environment",
"homepage": "https://github.com/AtidaTech/contentful-cli-export",
Expand Down

0 comments on commit e68875a

Please sign in to comment.