Skip to content

Commit

Permalink
Merge pull request #31 from buschtoens/feat/typescript-types
Browse files Browse the repository at this point in the history
feat(build): generate TypeScript declaration file
  • Loading branch information
pzuraq authored Feb 10, 2019
2 parents 0e30fd4 + d7e975c commit 9079c88
Show file tree
Hide file tree
Showing 9 changed files with 75 additions and 25 deletions.
4 changes: 3 additions & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ module.exports = {
'config/**/*.js',
'tests/dummy/config/**/*.js',
'node-tests/**/*.js',
'comparision-plugin.js'
'comparision-plugin.js',
'utils/**/*.js',
'build/**/*.js'
],
excludedFiles: [
'app/**',
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
# compiled output
/dist
/tmp
/index.d.ts

# dependencies
/node_modules
Expand Down
1 change: 1 addition & 0 deletions .npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,4 @@ bower.json.ember-try
package.json.ember-try

/node-tests
/build
12 changes: 12 additions & 0 deletions build/boolean-flags.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
'use strict';

const VersionChecker = require('ember-cli-version-checker');
const getFlags = require('../utils/get-flags');

/**
* Calls the `get-flags` util with mock inputs in order to retrieve the actual
* flag keys.
*/
module.exports = Object.keys(
getFlags('0.0.0', new VersionChecker({ root: __dirname }))
);
14 changes: 14 additions & 0 deletions build/types.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
'use strict';

const booleanFlags = require('./boolean-flags');

/**
* Generates type declarations for the `comparison-plugin` (`gte`, `lte`) and
* and the debug plugin (boolean flags).
*/
module.exports = [
'export function gte(version: string): boolean;',
'export function lte(version: string): boolean;'
]
.concat(booleanFlags.map(b => `export const ${b}: boolean;`))
.join('\n');
7 changes: 7 additions & 0 deletions build/write-declaration-file.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
'use strict';

const fs = require('fs');
const path = require('path');
const types = require('./types');

fs.writeFileSync(path.resolve(__dirname, '../index.d.ts'), types);
25 changes: 2 additions & 23 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@

const VersionChecker = require('ember-cli-version-checker');
const extractTrueVersion = require('./utils/extract-true-version');
const semver = require('semver');
const satisfies = semver.satisfies;
const gte = semver.gte;
const getFlags = require('./utils/get-flags');

module.exports = {
name: 'ember-compatibility-helpers',
Expand Down Expand Up @@ -66,9 +64,6 @@ module.exports = {
},

_getDebugPlugin(emberVersion, parentChecker) {
const trueEmberVersion = extractTrueVersion(emberVersion);
const emberDataVersion = parentChecker.for('ember-data', 'npm').version;

const options = {
debugTools: {
isDebug: process.env.EMBER_ENV !== 'production',
Expand All @@ -79,23 +74,7 @@ module.exports = {
{
name: 'ember-compatibility-helpers',
source: 'ember-compatibility-helpers',
flags: {
HAS_UNDERSCORE_ACTIONS: !gte(trueEmberVersion, '2.0.0'),
HAS_MODERN_FACTORY_INJECTIONS: gte(trueEmberVersion, '2.13.0'),
HAS_DESCRIPTOR_TRAP: satisfies(trueEmberVersion, '~3.0.0'),
HAS_NATIVE_COMPUTED_GETTERS: gte(trueEmberVersion, '3.1.0-beta.1'),

IS_GLIMMER_2: gte(trueEmberVersion, '2.10.0'),
IS_RECORD_DATA: !emberDataVersion ? false : gte(emberDataVersion, '3.5.0'),

SUPPORTS_FACTORY_FOR: gte(trueEmberVersion, '2.12.0') || parentChecker.for('ember-factory-for-polyfill', 'npm').gte('1.0.0'),
SUPPORTS_GET_OWNER: gte(trueEmberVersion, '2.3.0') || parentChecker.for('ember-getowner-polyfill', 'npm').gte('1.1.0'),
SUPPORTS_SET_OWNER: gte(trueEmberVersion, '2.3.0'),
SUPPORTS_NEW_COMPUTED: gte(trueEmberVersion, '1.12.0-beta.1'),
SUPPORTS_INVERSE_BLOCK: gte(trueEmberVersion, '1.13.0'),
SUPPORTS_CLOSURE_ACTIONS: gte(trueEmberVersion, '1.13.0'),
SUPPORTS_UNIQ_BY_COMPUTED: gte(trueEmberVersion, '2.7.0')
}
flags: getFlags(emberVersion, parentChecker)
}
]
};
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
"lint:js": "eslint ./*.js addon addon-test-support app config lib server test-support tests node-tests",
"start": "ember serve",
"test": "mocha node-tests/babel-7",
"test:all": "ember try:each"
"test:all": "ember try:each",
"prepack": "node ./build/write-declaration-file"
},
"dependencies": {
"babel-plugin-debug-macros": "^0.2.0",
Expand Down
33 changes: 33 additions & 0 deletions utils/get-flags.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
'use strict';

const extractTrueVersion = require('./extract-true-version');
const semver = require('semver');
const satisfies = semver.satisfies;
const gte = semver.gte;

module.exports = function(emberVersion, parentChecker) {
const trueEmberVersion = extractTrueVersion(emberVersion);
const emberDataVersion = parentChecker.for('ember-data', 'npm').version;

return {
HAS_UNDERSCORE_ACTIONS: !gte(trueEmberVersion, '2.0.0'),
HAS_MODERN_FACTORY_INJECTIONS: gte(trueEmberVersion, '2.13.0'),
HAS_DESCRIPTOR_TRAP: satisfies(trueEmberVersion, '~3.0.0'),
HAS_NATIVE_COMPUTED_GETTERS: gte(trueEmberVersion, '3.1.0-beta.1'),

IS_GLIMMER_2: gte(trueEmberVersion, '2.10.0'),
IS_RECORD_DATA: !emberDataVersion ? false : gte(emberDataVersion, '3.5.0'),

SUPPORTS_FACTORY_FOR:
gte(trueEmberVersion, '2.12.0') ||
parentChecker.for('ember-factory-for-polyfill', 'npm').gte('1.0.0'),
SUPPORTS_GET_OWNER:
gte(trueEmberVersion, '2.3.0') ||
parentChecker.for('ember-getowner-polyfill', 'npm').gte('1.1.0'),
SUPPORTS_SET_OWNER: gte(trueEmberVersion, '2.3.0'),
SUPPORTS_NEW_COMPUTED: gte(trueEmberVersion, '1.12.0-beta.1'),
SUPPORTS_INVERSE_BLOCK: gte(trueEmberVersion, '1.13.0'),
SUPPORTS_CLOSURE_ACTIONS: gte(trueEmberVersion, '1.13.0'),
SUPPORTS_UNIQ_BY_COMPUTED: gte(trueEmberVersion, '2.7.0')
};
};

0 comments on commit 9079c88

Please sign in to comment.