Skip to content

Commit

Permalink
Improving message json generation
Browse files Browse the repository at this point in the history
  • Loading branch information
David-Desmaisons committed Apr 8, 2020
1 parent c03ea67 commit 1c32b4d
Show file tree
Hide file tree
Showing 9 changed files with 131 additions and 107 deletions.
15 changes: 4 additions & 11 deletions Neutronium.SPA/Neutronium.SPA.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
<NodePath>$(GlobalNodePath)</NodePath>
<NodeModulePath>$(GlobalNodeModulePath)</NodeModulePath>
</PropertyGroup>
<Target Name="EnsureNode">
<Target Name="EnsureNode" BeforeTargets="BeforeBuild">
<PropertyGroup>
<UnknownNodePath>Global NodeJS installation not found. Please set the 'NODEJS' environment variable for a global installation or set the 'LocalNodePath' property in your project file for a local installation.</UnknownNodePath>
<InvalidGlobalNodePath>Invalid global node path.</InvalidGlobalNodePath>
Expand Down Expand Up @@ -309,11 +309,7 @@
<None Include="View\src\components\sideMenu.vue" />
<None Include="View\src\components\textButton.vue" />
<None Include="View\src\components\topMenu.vue" />
<None Include="View\src\message.json">
<DependentUpon>message.tt</DependentUpon>
<DesignTime>True</DesignTime>
<AutoGen>True</AutoGen>
</None>
<None Include="View\src\message.json" />
<None Include="View\src\pages\about.vue" />
<None Include="View\src\pages\main.vue" />
<None Include="View\src\stylus\main.styl" />
Expand All @@ -337,6 +333,7 @@
<Content Include="View\build-scripts\config.js" />
<Content Include="View\build-scripts\create-hash.js" />
<Content Include="View\build-scripts\hash-builder.js" />
<Content Include="View\build-scripts\message-updater.js" />
<Content Include="View\src\neutronium\entry.js" />
<Content Include="View\src\neutronium\main.js" />
<Content Include="View\src\neutronium\route.js" />
Expand All @@ -346,10 +343,6 @@
<Resource Include="View\public\favicon.ico" />
<Content Include="View\public\index.html" />
<Content Include="View\src\install.js" />
<Content Include="View\src\message.tt">
<Generator>TextTemplatingFileGenerator</Generator>
<LastGenOutput>message.json</LastGenOutput>
</Content>
<Content Include="View\src\routeDefinitions.js" />
<Content Include="View\tests\unit\.eslintrc.js" />
<Content Include="View\tests\unit\example.spec.js" />
Expand Down Expand Up @@ -378,7 +371,7 @@
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>-->
<Target Name="AfterBuild" DependsOnTargets="EnsureNode">
<Target Name="NpmBuild" DependsOnTargets="EnsureNode" BeforeTargets="BeforeBuild">
<PropertyGroup>
<NpmWorkingDirectory>$([System.IO.Path]::GetDirectoryName('$(NpmFile)'))</NpmWorkingDirectory>
<RunNpmCmd1>"$(NodePath)\npm" $(NpmCommandInstall)</RunNpmCmd1>
Expand Down
6 changes: 5 additions & 1 deletion Neutronium.SPA/View/build-scripts/config.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
exports.filePath = "./dist/hash.json";
exports.fileName = "hash.json";
exports.fileName = "hash.json";
exports.messagePath = "src/message.json";
exports.supportedLanguages = [
"en-US"
]
3 changes: 2 additions & 1 deletion Neutronium.SPA/View/build-scripts/hash-builder.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
const { hashElement } = require('folder-hash');
const { fileName } = require('./config');
const { messagePath } = require('./config');

const optionsSource = {
folders: { exclude: ['.*', 'node_modules', 'test_coverage', 'tests', 'data', 'build-scripts', 'dist'] },
files: { exclude: ['README.md'] }
files: { exclude: ['README.md', messagePath] }
};

const optionsDist = {
Expand Down
78 changes: 78 additions & 0 deletions Neutronium.SPA/View/build-scripts/message-updater.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
const xml2js = require('xml2js');
const fs = require('fs');
const { messagePath, supportedLanguages } = require('./config');

const parser = new xml2js.Parser({ attrkey: "ATTR" });

function existsPath(path) {
return new Promise((resolve) => {
fs.exists(path, resolve);
});
}

function parseFile(result) {
const { root: { data } } = result;
const Resource = data.reduce((curr, { ATTR: { name }, value }) => {
curr[name] = value[0];
return curr;
}, {});

return {
Resource
};
}

function getLanguageFromPath(path) {
return new Promise((resolve, reject) => {
fs.readFile(path, { encoding: 'utf-8' }, (error, file) => {
if (error !== null) {
reject(error);
return;
}

parser.parseString(file, function (parseError, file) {
if (parseError !== null) {
reject(parseError);
return;
}
const data = parseFile(file);
resolve(data);
});
});
});
}

async function getLanguage(language, defaultValue) {
const path = `../Resource.${language}.resx`;
const exists = await existsPath(path);
if (!exists) {
return { ...defaultValue };
}
const data = await getLanguageFromPath(path);
const result = Object.assign({}, defaultValue, data);
return result;
}

async function createMessage() {
const messages = {};
const defaultValue = await getLanguageFromPath("../Resource.resx");

for (const language of supportedLanguages) {
const result = await getLanguage(language, defaultValue);
messages[language] = result;
}
return messages;
}

async function updateMessage() {
const file = await createMessage();
fs.writeFile(messagePath, JSON.stringify(file, null, 2), function (err) {
if (err) {
console.log(err);
return;
}
console.log("updated message JSON file");
});
}

updateMessage();
2 changes: 1 addition & 1 deletion Neutronium.SPA/View/dist/js/app.js.map

Large diffs are not rendered by default.

16 changes: 16 additions & 0 deletions Neutronium.SPA/View/package-lock.json

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

8 changes: 5 additions & 3 deletions Neutronium.SPA/View/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
"version": "0.1.0",
"private": true,
"scripts": {
"serve": "vue-cli-service serve ./src/neutronium/main.js --open --port 9000",
"serve": "npm run build:resource && vue-cli-service serve ./src/neutronium/main.js --open --port 9000",
"build": "vue-cli-service build --entry ./src/neutronium/entry.js --modern && npm run create-hash",
"build:cached": "npm run check-hash || npm run build",
"build:cached": "npm run build:resource && npm run check-hash || npm run build",
"build:resource": "node ./build-scripts/message-updater.js",
"test:unit": "vue-cli-service test:unit",
"lint": "vue-cli-service lint",
"live": "vue-cli-service serve ./src/neutronium/entry.js --port 8080 --mode integrated",
Expand Down Expand Up @@ -46,6 +47,7 @@
"neutronium-vm-loader": "^1.3.0",
"sass-loader": "^8.0.2",
"vue-cli-plugin-neutronium": "~0.4.0",
"vue-template-compiler": "^2.6.11"
"vue-template-compiler": "^2.6.11",
"xml2js": "^0.4.23"
}
}
40 changes: 20 additions & 20 deletions Neutronium.SPA/View/src/message.json
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
{
"en-US":{
"Resource":{
"About1":"Complete Neutronium Vue Demo",
"About2":"Including Ioc, navigation (vue-router) internalization (vue-i18n) and chromeless window",
"About3":"Made with vuetify framework",
"Cancel":"Cancel",
"Completed":"Completed",
"ConfirmationNeeded":"Confirmation needed",
"DoYouWantToCloseApplication":"Do you want to close application?",
"DoYouWantToRestartApplication":"Do you want to restart application?",
"GoToAboutSection":"Go To About Section",
"MadeBy":"made by",
"Menu_about":"About",
"Menu_main":"Main",
"Ok":"Ok",
"ProjectDescription":"Neutronium Vue SPA Template including IoC, routing and internalization",
"Restart":"Restart"
}
{
"en-US": {
"Resource": {
"About1": "Complete Neutronium Vue Demo",
"About2": "Including Ioc, navigation (vue-router) internalization (vue-i18n) and chromeless window",
"About3": "Made with vuetify framework",
"Cancel": "Cancel",
"Completed": "Completed",
"ConfirmationNeeded": "Confirmation needed",
"DoYouWantToCloseApplication": "Do you want to close application?",
"DoYouWantToRestartApplication": "Do you want to restart application?",
"GoToAboutSection": "Go To About Section",
"MadeBy": "made by",
"Menu_about": "About",
"Menu_main": "Main",
"Ok": "Ok",
"ProjectDescription": "Neutronium Vue SPA Template including IoC, routing and internalization",
"Restart": "Restart"
}
}
}
}
70 changes: 0 additions & 70 deletions Neutronium.SPA/View/src/message.tt

This file was deleted.

0 comments on commit 1c32b4d

Please sign in to comment.