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

added cmd for migrating resouces from core #7

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
4 changes: 3 additions & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
"no-console": 0,
"no-use-before-define": ["error", { "variables": false }],
"max-len": ["error", { "code": 160 }],
"import/prefer-default-export": 0
"import/prefer-default-export": 0,
"class-methods-use-this": "off",
"arrow-body-style": "off"
}
}
85 changes: 54 additions & 31 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ const SrcGenerator = require('./generators/srcGenerator');
const PathUtility = require('./utils/pathUtility');
const LogUtility = require('./utils/logUtility');
const Markup = require('./utils/markupUtility');
const migrateResxFromCoreCmd = require('./migrateFromCoreCmd');

inquirer.registerPrompt('autocomplete', require('inquirer-autocomplete-prompt'));

const initModule = ({
tabSize,
Expand Down Expand Up @@ -66,12 +69,14 @@ const initModule = ({
create: 'create',
add: 'add',
regenerateAll: 'regenerateAll',
migrateResxFromCore: 'migrateResxFromCore',
};

const actonsList = [
{ name: 'Do everything GOOD', value: actions.regenerateAll },
{ name: 'Create new resx', value: actions.create },
{ name: 'Add keys to existing one', value: actions.add },
{ name: 'Migrate resx from core', value: actions.migrateResxFromCore },
];

const startupQuestions = [
Expand All @@ -96,6 +101,17 @@ const initModule = ({

const defaultSelectedLangs = [defaultLang, 'ru'];
const langList = languages.map(l => ({ name: l }));
const langsQuestion = {
type: 'checkbox',
name: 'keyLangs',
message: 'Select languages:',
choices: langList,
default: defaultSelectedLangs,
validate: list => {
const isDefaultLangSelected = list.includes(defaultLang);
return isDefaultLangSelected ? true : `Default language (${defaultLang}) must be selected`;
},
};

const doLangKeyValQuestions = (lang, keyName) => ({
type: 'input',
Expand All @@ -121,23 +137,17 @@ const initModule = ({
return true;
},
},
{
type: 'checkbox',
name: 'keyLangs',
message: 'Select languages:',
choices: langList,
default: defaultSelectedLangs,
validate: list => {
const isDefaultLangSelected = list.includes(defaultLang);
return isDefaultLangSelected ? true : `Default language (${defaultLang}) must be selected`;
},
},
langsQuestion,
];

const doAdd = (chunkName, keyName, langValPairs) => {
SrcGenerator.addKey(chunkName, keyName, langValPairs)
const addKeyToChunk = (chunkName, keyName, langValPairs) => {
return SrcGenerator.addKey(chunkName, keyName, langValPairs)
.then(() => srcGenerator.processChunk(chunkName))
.then(() => distGenerator.generateChunk(chunkName, 'updated'))
.then(() => distGenerator.generateChunk(chunkName, 'updated'));
};

const doAdd = (chunkName, keyName, langValPairs) => {
addKeyToChunk(chunkName, keyName, langValPairs)
.then(() => {
inquirer
.prompt({
Expand Down Expand Up @@ -225,33 +235,43 @@ const initModule = ({
};

const createSelectChunkQuestion = chunkNames => {
const chunkList = chunkNames.map(chunkName => ({ name: chunkName }));
return {
type: 'list',
type: 'autocomplete',
name: 'addKey',
message: 'Select resource: ',
choices: chunkList,
source: async (answers, input) => {
return chunkNames.filter(x => !input || x.indexOf(input) >= 0);
},
};
};

const readChunksAndAsk = () => {
pathUtility.readChunksNames()
.then(chunkNames => {
if (!chunkNames.length) {
LogUtility.logErr(`NO RESOURCES FOUND IN ${srcFolder}`);
askForRecursiveActions();
return;
}
const question = createSelectChunkQuestion(chunkNames);
inquirer
.prompt(question)
.then(a => {
addScenario(a.addKey);
});
})
selectTargetResource()
.then(targetResource => addScenario(targetResource))
.catch(LogUtility.logErr);
};

const selectTargetResource = async () => {
const chunkNames = await pathUtility.readChunksNames();
if (!chunkNames.length) {
LogUtility.logErr(`NO RESOURCES FOUND IN ${srcFolder}`);
askForRecursiveActions();
return null;
}
const question = createSelectChunkQuestion(chunkNames);
const result = await inquirer.prompt(question);
return result.addKey;
};

const migrateResxFromCore = () => migrateResxFromCoreCmd({
addKeyToChunk,
selectTargetResource,
langsQuestion,
yesNo,
yesNoList,
askForRecursiveActions,
});

inquirer
.prompt(startupQuestions)
.then(a => {
Expand All @@ -264,6 +284,9 @@ const initModule = ({
if (a.action === actions.regenerateAll) {
generateAll();
}
if (a.action === actions.migrateResxFromCore) {
migrateResxFromCore();
}
});
};

Expand Down
84 changes: 84 additions & 0 deletions migrateFromCoreCmd.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/* eslint-disable no-await-in-loop */
const inquirer = require('inquirer');
const CoreResxProvider = require('./utils/coreResxProvider');

const migrateResxFromCoreCmd = async ({
addKeyToChunk,
selectTargetResource,
langsQuestion,
yesNo,
yesNoList,
askForRecursiveActions,
}) => {
const { corePath } = (await inquirer.prompt({
type: 'input',
message: 'Enter core project sources path',
default: 'c:/projects/ep',
name: 'corePath',
}));
const coreResxProvider = new CoreResxProvider(corePath);

const coreResxFileName = (await inquirer.prompt({
type: 'autocomplete',
message: 'Select source (core) resx file: ',
name: 'coreResxFile',
source: async (answers, input) => {
const resxFiles = await coreResxProvider.getResxFiles();
return resxFiles.filter(x => !input || x.name.indexOf(input) >= 0)
.map(x => x.name);
},
})).coreResxFile;


const targetResource = await selectTargetResource();
const langs = (await inquirer.prompt(langsQuestion)).keyLangs;
const coreResxFile = await coreResxProvider.getFile(coreResxFileName);
const sourceKeys = await coreResxFile.getKeys();

let migrateOneMoreKey = yesNo.yes;
while (migrateOneMoreKey === yesNo.yes) {
const { sourceKey } = (await inquirer.prompt({
type: 'autocomplete',
message: 'Select source key: ',
name: 'sourceKey',
source: async (answers, input) => {
return sourceKeys.filter(x => !input || x.toLowerCase().indexOf(input) >= 0);
},
}));

const { targetKey } = await inquirer.prompt({
type: 'input',
message: 'Enter target key',
default: sourceKey,
name: 'targetKey',
});

// migrating keys
const values = {};
await Promise.all(langs.map(async lang => {
const coreFile = await coreResxProvider.getFile(coreResxFileName, lang);
if (!coreFile) {
return;
}
const keyValue = await coreFile.getKeyValue(sourceKey);
if (keyValue) {
values[lang] = keyValue;
}
}));

await addKeyToChunk(targetResource, targetKey, values);

// ask for migrating one more key
// eslint-disable-next-line prefer-destructuring
migrateOneMoreKey = (await inquirer.prompt({
type: 'list',
name: 'migrateOneMoreKey',
message: 'migrate one more key?',
choices: yesNoList,
})).migrateOneMoreKey;
}

askForRecursiveActions();
};

module.exports = migrateResxFromCoreCmd;
Loading