-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
new import/export comand #92
Open
KarolNet
wants to merge
14
commits into
master
Choose a base branch
from
new-importer-exporter
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+7,499
−15,920
Open
Changes from 11 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
3794938
new import/export command
KarolNet 2bb113f
new import/export command - test
KarolNet bd0dd9c
new import/export command - test
KarolNet 188ebc2
new import/export command - remove unused dep
KarolNet 1faa8dd
new import/export command - import simple test
KarolNet 4a21ea3
new import/export command - refactor
KarolNet f9540aa
new import/export command - fix disable webhook option bug
KarolNet 2374acf
new import/export command - disable webhooks optimization
KarolNet 3a9d63e
new import/export command - refactor
KarolNet aa74106
new import/export command - update readme
KarolNet a88e3a3
new import/export command - version up
KarolNet 0cc555d
new import/export command - fix --only-definitions option
KarolNet 27c476e
new import/export command - skip import featuredImage for ctd, disabl…
KarolNet 0100247
new import/export command -
KarolNet File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,15 @@ | ||
name: Tests | ||
|
||
on: | ||
push: | ||
branches: | ||
- main | ||
- feature/export | ||
on: [push] | ||
|
||
jobs: | ||
run-tests: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- name: Checkout | ||
uses: actions/checkout@v2 | ||
- name: Install deps | ||
run: npm install | ||
- name: Jast run | ||
run: npm test | ||
- uses: actions/checkout@v4 | ||
- name: Use Node.js | ||
uses: actions/setup-node@v4 | ||
with: | ||
node-version: '20.x' | ||
- run: yarn install | ||
- run: yarn test |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,177 @@ | ||
#!/usr/bin/env node | ||
|
||
const fs = require("fs/promises"); | ||
const path = require("path"); | ||
const fetch = require("node-fetch"); | ||
const logger = require("./../src/logger"); | ||
const { camelize } = require("./../src/util"); | ||
const FlotiqApi = require('./../src/flotiq-api') | ||
const config = require("../src/configuration/config"); | ||
|
||
exports.command = "export"; | ||
exports.description = "Export flotiq entities to JSON structure"; | ||
exports.builder = { | ||
target: { | ||
description: "Export directory", | ||
alias: "directory", | ||
type: "string", | ||
demand: true, | ||
}, | ||
ctd: { | ||
description: "Coma-delimited list of CTD to export", | ||
type: "string", | ||
}, | ||
skipContent: { | ||
description: "Dump only CTD" | ||
} | ||
}; | ||
|
||
async function exporter(directory, flotiqApiUrl, flotiqApiKey, skipContent, ctd) { | ||
try { | ||
const files = await fs.readdir(directory); | ||
|
||
if (files.length > 0) { | ||
logger.error(`${directory} exists, but isn't empty`); | ||
return false; | ||
} | ||
} catch (e) { | ||
// Skip | ||
} | ||
|
||
await fs.mkdir(directory, { recursive: true }); | ||
|
||
const flotiqApi = new FlotiqApi(flotiqApiUrl, flotiqApiKey); | ||
|
||
let ContentTypeDefinitions = await flotiqApi.fetchContentTypeDefs(); | ||
|
||
if (ctd) { | ||
ctd.split(",").forEach((c) => { | ||
if (!ContentTypeDefinitions.map((d) => d.name).includes(c)) { | ||
throw new Error(`Invalid ctd "${c}"`); | ||
} | ||
}); | ||
ContentTypeDefinitions = ContentTypeDefinitions.filter((def) => | ||
ctd.split(",").includes(def.name) | ||
); | ||
} | ||
|
||
if (ContentTypeDefinitions.length === 0) { | ||
logger.info("Nothing to do"); | ||
return true; | ||
} | ||
|
||
for (const contentTypeDefinition of ContentTypeDefinitions) { | ||
logger.info(`Saving CTD for ${contentTypeDefinition.label}`); | ||
|
||
const ctdPath = path.join( | ||
directory, | ||
`${contentTypeDefinition.internal ? 'Internal' : ''}ContentType${camelize(contentTypeDefinition.name)}` | ||
); | ||
|
||
await fs.mkdir(ctdPath, { recursive: true }); | ||
|
||
const contentTypeDefinitionToPersist = Object.fromEntries( | ||
Object.entries(contentTypeDefinition).filter(([key]) => { | ||
return ![ | ||
"id", | ||
// "internal", | ||
"deletedAt", | ||
"createdAt", | ||
"updatedAt", | ||
].includes(key); | ||
}) | ||
); | ||
|
||
await fs.writeFile( | ||
path.join(ctdPath, "ContentTypeDefinition.json"), | ||
JSON.stringify(contentTypeDefinitionToPersist, null, 2) | ||
); | ||
|
||
if (!skipContent) { | ||
|
||
const ContentObjects = await flotiqApi.fetchContentObjects(contentTypeDefinition.name); | ||
|
||
if (ContentObjects.length === 0) { | ||
logger.info(`No content to save for ${contentTypeDefinition.label}`); | ||
continue; | ||
} | ||
|
||
logger.info(`Saving content for ${contentTypeDefinition.label}`); | ||
|
||
await fs.writeFile( | ||
path.join( | ||
ctdPath, | ||
`contentObject${camelize(contentTypeDefinition.name)}.json` | ||
), | ||
ContentObjects | ||
.map((obj) => ({ ...obj, internal: undefined })) | ||
.sort((a, b) => a.id < b.id ? -1 : 1) | ||
.map(JSON.stringify).join("\n") | ||
); | ||
|
||
if (contentTypeDefinition.name === '_media') { | ||
for (const mediaFile of ContentObjects) { | ||
const outputPath = path.join( | ||
ctdPath, | ||
`${mediaFile.id}.${mediaFile.extension}` | ||
); | ||
|
||
const url = new URL(flotiqApiUrl); | ||
|
||
await fetch(`${url.origin}${mediaFile.url}`) | ||
.then(x => x.arrayBuffer()) | ||
.then(x => fs.writeFile(outputPath, Buffer.from(x))); | ||
} | ||
} | ||
} | ||
} | ||
return true; | ||
} | ||
|
||
async function handler(argv) { | ||
|
||
const dirStat = await fs.lstat(argv.directory); | ||
|
||
if (!dirStat.isDirectory()) { | ||
logger.error(`${argv.directory} exists, but isn't directory`); | ||
return false; | ||
} | ||
|
||
await exporter( | ||
argv.directory, | ||
`${config.apiUrl}/api/v1`, | ||
argv.flotiqApiKey, | ||
false | ||
) | ||
} | ||
|
||
module.exports = { | ||
command: 'export [directory] [flotiqApiKey]', | ||
describe: 'Export objects from Flotiq to directory', | ||
builder: (yargs) => { | ||
return yargs | ||
.option("directory", { | ||
description: "Directory path to import data.", | ||
alias: "", | ||
type: "string", | ||
default: "", | ||
demandOption: false, | ||
}) | ||
.option("flotiqApiKey", { | ||
description: "Flotiq Read and write API KEY.", | ||
alias: "", | ||
type: "string", | ||
default: false, | ||
demandOption: false, | ||
}) | ||
.option("only-definitions", { | ||
description: "Export only content type definitions, ignore content objects", | ||
alias: "", | ||
type: "boolean", | ||
default: false, | ||
demandOption: false, | ||
}) | ||
}, | ||
handler, | ||
exporter | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
only-definitions
flag is not working anymore. We should either add suport for it in new exporter, or remove it from here & from readme