Skip to content

Commit

Permalink
fix: Do configuration during build
Browse files Browse the repository at this point in the history
Add default options and detect currentVersion during the build step.

BREAKING CHANGE: config options come from a new place, see the readme
  • Loading branch information
jrjohnson authored and knownasilya committed Dec 15, 2021
1 parent 8511ae9 commit 2fe8486
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 49 deletions.
11 changes: 1 addition & 10 deletions addon/services/new-version.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,22 +35,13 @@ export default class NewVersionService extends Service {
* @type Configuration
*/
get _newVersionConfig() {
const defaultConfiguration = {
versionFileName: 'VERSION.txt',
firstCheckInterval: 0,
updateInterval: 60000,
enableInTests: false,
maxCountInTesting: 10,
};

return Object.assign(defaultConfiguration, this._config.newVersion);
return this._config.newVersion;
}

/**
* @type {string}
*/
get currentVersion() {
// Users of the addon must set currentVersion.
return this._newVersionConfig.currentVersion;
}

Expand Down
7 changes: 1 addition & 6 deletions ember-cli-build.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,7 @@
const EmberAddon = require('ember-cli/lib/broccoli/ember-addon');

module.exports = function (defaults) {
let app = new EmberAddon(defaults, {
// Add options here
newVersion: {
enabled: true,
},
});
let app = new EmberAddon(defaults, {});

/*
This build file specifies the options for the dummy test app of this
Expand Down
66 changes: 33 additions & 33 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,49 +7,49 @@ module.exports = {
name: require('./package').name,

/**
* Store `ember-cli-build.js` options
* Setup default configuration options and auto detect the currentVersion if it isn't set manually
*/
included(app /*, parentAddon*/) {
this._super.included.apply(this, arguments);
this._options = app.options.newVersion || {};

if (this._options.enabled === true) {
this._options.fileName = this._options.fileName || 'VERSION.txt';
this._options.prepend = this._options.prepend || '';
this._options.useAppVersion = this._options.useAppVersion || false;
config(env, baseConfig) {
const defaultConfiguration = {
versionFileName: 'VERSION.txt',
firstCheckInterval: 0,
updateInterval: 60000,
enableInTests: false,
maxCountInTesting: 10,
createVersionFileAutomatically: false,
};

baseConfig.newVersion = Object.assign(
defaultConfiguration,
baseConfig.newVersion
);

if (!baseConfig.newVersion.currentVersion) {
if (baseConfig.APP.version) {
//if `ember-cli-app-version` is installed use the detected version from that addon
baseConfig.newVersion.currentVersion = baseConfig.APP.version;
} else {
//otherwise use what is in package.json.
baseConfig.newVersion.currentVersion = this.parent.pkg.version;
}
}
},

/**
* Copy version from `ember-cli-app-version`
*/
config(env, baseConfig) {
this._appVersion = baseConfig.APP.version || null;
this._config = baseConfig.newVersion;

return baseConfig;
},

/**
* Write version file
*
* based on
* - ember-cli-app-version if installed
* - package.json of consuming application or
*/
treeForPublic() {
let detectedVersion;

if (this._options.useAppVersion && this._appVersion) {
detectedVersion = this._appVersion;
}

if (!detectedVersion) {
detectedVersion = this.parent.pkg.version;
}

if (detectedVersion && this._options.enabled) {
const fileName = this._options.fileName;
const { currentVersion, createVersionFileAutomatically, versionFileName } =
this._config;
if (currentVersion && createVersionFileAutomatically) {
const fileName = versionFileName;

this.ui.writeLine(`Created ${fileName} with ${detectedVersion}`);
return writeFile(fileName, detectedVersion);
this.ui.writeLine(`Created ${fileName} with ${currentVersion}`);
return writeFile(fileName, currentVersion);
}
},
};
3 changes: 3 additions & 0 deletions tests/dummy/config/environment.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ module.exports = function (environment) {
'ember-cli-mirage': {
enabled: false,
},
newVersion: {
createVersionFileAutomatically: true,
},
};

if (environment === 'development') {
Expand Down

0 comments on commit 2fe8486

Please sign in to comment.