Skip to content
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: language file to store languages and migrate each language separately (fixes #5) #6

Merged
merged 2 commits into from
Feb 28, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 40 additions & 25 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,16 +126,18 @@ module.exports = function(grunt) {
(async function() {
const migrations = await import('adapt-migrations');

const cwd = process.cwd();
const outputPath = path.join(cwd, './migrations/');
const cache = new migrations.CacheManager();
const cachePath = await cache.getCachePath({
outputPath: buildConfig.outputdir
outputPath: buildConfig.outputdir,
tempPath: outputPath
});

const framework = Helpers.getFramework();
grunt.log.ok(`Using ${framework.useOutputData ? framework.outputPath : framework.sourcePath} folder for course data...`);

const plugins = framework.getPlugins().getAllPackageJSONFileItems().map(fileItem => fileItem.item);
const cwd = process.cwd();
const migrationScripts = Array.from(await new Promise(resolve => {
globs([
'*/*/migrations/**/*.js',
Expand All @@ -153,35 +155,47 @@ module.exports = function(grunt) {

if (mode === 'capture') {
// TODO: capture all languages and not just the first
const data = framework.getData().languages[0].getAllFileItems().map(fileItem => fileItem.item);
const captured = await migrations.capture({ data, fromPlugins: plugins });
const outputPath = path.join(cwd, './migrations/');
if (!fs.existsSync(outputPath)) fs.mkdirSync(outputPath);
const outputFile = path.join(outputPath, 'capture.json');
fs.writeJSONSync(outputFile, captured);
const languages = framework.getData().languages.map((language) => language.name);
const languageFile = path.join(outputPath, 'captureLanguages.json');
fs.writeJSONSync(languageFile, languages);

languages.forEach(async (language, index) => {
const data = framework.getData().languages[index].getAllFileItems().map(fileItem => fileItem.item);
const captured = await migrations.capture({ data, fromPlugins: plugins });
const outputFile = path.join(outputPath, `capture_${language}.json`);
fs.writeJSONSync(outputFile, captured);
});

return next();
}

if (mode === 'migrate') {
const Journal = migrations.Journal;
const outputPath = path.join(cwd, './migrations/');
if (!fs.existsSync(outputPath)) fs.mkdirSync(outputPath);
const outputFile = path.join(outputPath, 'capture.json');
const { data, fromPlugins } = fs.readJSONSync(outputFile);
const journal = new Journal({
data,
supplementEntry: (entry, data) => {
entry._id = data[entry.keys[0]]?._id ?? '';
entry._type = data[entry.keys[0]]?._type ?? '';
if (entry._type && data[entry.keys[0]]?.[`_${entry._type}`]) {
entry[`_${entry._type}`] = data[entry.keys[0]]?.[`_${entry._type}`] ?? '';

const languagesFile = path.join(outputPath, 'captureLanguages.json');
const languages = fs.readJSONSync(languagesFile);

for (let i = 0; i < languages.length; i++) {
const Journal = migrations.Journal;
if (!fs.existsSync(outputPath)) fs.mkdirSync(outputPath);
const outputFile = path.join(outputPath, `capture_${languages[i]}.json`);
joe-allen-89 marked this conversation as resolved.
Show resolved Hide resolved
const { data, fromPlugins, originalFromPlugins } = fs.readJSONSync(outputFile);
const journal = new Journal({
data,
supplementEntry: (entry, data) => {
entry._id = data[entry.keys[0]]?._id ?? '';
entry._type = data[entry.keys[0]]?._type ?? '';
if (entry._type && data[entry.keys[0]]?.[`_${entry._type}`]) {
entry[`_${entry._type}`] = data[entry.keys[0]]?.[`_${entry._type}`] ?? '';
}
return entry;
}
return entry;
}
});
await migrations.migrate({ journal, fromPlugins, toPlugins: plugins });
// TODO: add options to rollback on any error, to default fail silently or to default terminate
console.log(journal.entries);
});
await migrations.migrate({ journal, fromPlugins, originalFromPlugins, toPlugins: plugins });
// TODO: add options to rollback on any error, to default fail silently or to default terminate
console.log(journal.entries);
}

return next();
}

Expand All @@ -196,6 +210,7 @@ module.exports = function(grunt) {

};


```
```sh
grunt migration:capture # captures current plugins and data
Expand Down