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

Check Intl config for inputPath #95

Open
wants to merge 3 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
12 changes: 12 additions & 0 deletions __snapshots__/test.js.snap
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Test Fixtures alternative-path 1`] = `
"[1/4] 🔍 Finding JS and HBS files...
[2/4] 🔍 Searching for translations keys in JS and HBS files...
[3/4] ⚙️ Checking for unused translations...
[4/4] ⚙️ Checking for missing translations...

👏 No unused translations were found!

👏 No missing translations were found!
"
`;

exports[`Test Fixtures decorators 1`] = `
"[1/4] 🔍 Finding JS and HBS files...
[2/4] 🔍 Searching for translations keys in JS and HBS files...
Expand Down
7 changes: 7 additions & 0 deletions fixtures/alternative-path/app/controllers/application.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import Controller from '@ember/controller';

export default Controller.extend({
foo: computed('intl.locale', function() {
return this.intl.t('js-translation');
}),
});
1 change: 1 addition & 0 deletions fixtures/alternative-path/app/templates/application.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{t "hbs-translation"}}
5 changes: 5 additions & 0 deletions fixtures/alternative-path/config/ember-intl.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = function() {
return {
inputPath: './polyglotter/'
};
};
4 changes: 4 additions & 0 deletions fixtures/alternative-path/polyglotter/de.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"hbs-translation": "Lenkstage",
"js-translation": "Affentheater"
}
4 changes: 4 additions & 0 deletions fixtures/alternative-path/polyglotter/en.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"hbs-translation": "HBS!",
"js-translation": "JS!"
}
30 changes: 26 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ async function run(rootDir, options = {}) {
const step = num => chalk.dim(`[${num}/${NUM_STEPS}]`);

let config = readConfig(rootDir);
let intlConfig = readIntlConfig(rootDir);

log(`${step(1)} 🔍 Finding JS and HBS files...`);
let files = await findAppFiles(rootDir);
Expand All @@ -33,7 +34,7 @@ async function run(rootDir, options = {}) {

log(`${step(3)} ⚙️ Checking for unused translations...`);

let translationFiles = await findTranslationFiles(rootDir);
let translationFiles = await findTranslationFiles(rootDir, intlConfig);
let existingTranslationKeys = await analyzeTranslationFiles(rootDir, translationFiles);
let whitelist = config.whitelist || [];

Expand Down Expand Up @@ -89,12 +90,33 @@ function readConfig(cwd) {
return config;
}

function readIntlConfig(cwd) {
let configPath = `${cwd}/config/ember-intl.js`;

let config;
if (fs.existsSync(configPath)) {
let requireESM = require('esm')(module);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the default ember-intl blueprint is using module.exports (not export default), so I'm not sure why we would need to support ES module files here.

also, I would assume that the default export would still be a function, but it seems that the code below does not call the default and assumes that it is an object directly, right?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have ember-intl changed their setup recently because this PR worked when I opened it

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure. I've checked in a few of my projects that use ember-intl and they all use module.exports 😅

config = requireESM(configPath);
if (config) {
if (typeof config === 'function') {
config = config();
} else if (Object.prototype.hasOwnProperty.call(config, 'default')) {
config = config.default;
}
}
}

return config || {};
}

async function findAppFiles(cwd) {
return globby(['app/**/*.js', 'app/**/*.hbs', 'app/**/*.emblem'], { cwd });
}

async function findTranslationFiles(cwd) {
return globby(['translations/**/*.json', 'translations/**/*.yaml', 'translations/**/*.yml'], {
async function findTranslationFiles(cwd, intlConfig) {
let { inputPath = 'translations/' } = intlConfig;

return globby([`${inputPath}**/*.json`, `${inputPath}**/*.yaml`, `${inputPath}**/*.yml`], {
cwd,
});
}
Expand Down Expand Up @@ -139,7 +161,7 @@ async function analyzeJsFile(content) {
// parse the JS file
let ast = BabelParser.parse(content, {
sourceType: 'module',
plugins: ['decorators-legacy', 'dynamicImport'],
plugins: ['decorators-legacy', 'dynamicImport', 'classProperties'],
});

// find translation keys in the syntax tree
Expand Down