This repository was archived by the owner on Jan 5, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
147 lines (122 loc) · 4.54 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/* eslint-env node */
'use strict';
const merge = require('deepmerge')
const globby = require('globby')
const fuzzysearch = require('fuzzysearch')
const titleize = require('titleize')
const humanizeString = require('humanize-string')
const detectInstalled = require('detect-installed')
const VersionChecker = require('ember-cli-version-checker')
const typefaceList = require('./lib/typefaces')
const defaultOptions = { typefaces: [] }
module.exports = {
name: 'ember-typeface',
init () {
this._super.init && this._super.init.apply(this, arguments);
let checker = new VersionChecker(this)
let assertMessage = 'To use ember-typeface you must have ember-cli 2.16 or above.'
checker
.for('ember-cli')
.assertAbove('2.16.0', assertMessage);
},
included (app) {
let typefaceOptions = require(`${this.project.root}/config/environment`)(app.env).typefaceOptions || defaultOptions
let typefaces = typefaceOptions.disableAuto ? [] : getTypefacesFromPackage()
this._options = merge.all([{}, {
typefaces
}, typefaceOptions])
if (!this._options.typefaces.length) {
return;
}
this._checkTypefaces()
this._createImports()
},
includedCommands () {
let emberEnv = process.env.EMBER_ENV || 'development'
let typefaceOptions = require(`${this.project.root}/config/environment`)(emberEnv).typefaceOptions || defaultOptions
return {
'typeface:active': {
name: 'typeface:active',
description: 'Display a list of the active typefaces.',
works: 'insideProject',
run () {
let typefaceListFromConfig = typefaceOptions.typefaces
.filter((typeface) => typefaceList.includes(typeface.toLowerCase()))
let typefaceListFromPackages = getTypefacesFromPackage()
.filter((typeface) => typefaceList.includes(typeface.toLowerCase()))
let fullTypefaceList = typefaceOptions.disableAuto ?
typefaceListFromConfig :
typefaceListFromConfig.concat(typefaceListFromPackages).filter(onlyUnique)
if (!(fullTypefaceList.length > 0)) {
this.ui.writeLine('There are no active typefaces.')
}
for (let typeface of fullTypefaceList) {
this.ui.writeLine(`${titleize(humanizeString(typeface))} (${typeface})`)
}
}
},
'typeface:list': {
name: 'typeface:list',
description: 'Display a list of all the available typefaces.',
works: 'insideProject',
run () {
for (let typeface of typefaceList) {
this.ui.writeLine(`${titleize(humanizeString(typeface))} (${typeface})`)
}
}
},
'typeface:search': {
name: 'typeface:search',
description: 'Fuzzy search the list of available typefaces.',
works: 'insideProject',
anonymousOptions: [
'<name>'
],
run (commandOptions, rawArgs) {
let name = rawArgs[0]
let filteredTypefaceList = typefaceList
.filter((typeface) => fuzzysearch(name.toLowerCase(), typeface.toLowerCase()))
for (let typeface of filteredTypefaceList) {
this.ui.writeLine(`${titleize(humanizeString(typeface))} (${typeface})`)
}
}
}
}
},
_getAddonOptions (app) {
return (this.parent && this.parent.options) || (app && app.options) || {}
},
_checkTypefaces () {
this._options.typefaces.forEach((typeface) => {
if (!typefaceList.includes(typeface)) {
throw new Error(`The font '${typeface}' is not supported. Please chose a font from the available list.`)
}
if (!detectInstalled.sync(`typeface-${typeface}`, { local: true })) {
throw new Error(`The font package 'typeface-${typeface}' is not installed. Please add it to your project with NPM.`)
}
})
},
_getTypefaceFiles (typeface) {
return globby.sync(`node_modules/typeface-${typeface}/files`)
},
_createImports () {
this._options.typefaces.filter(onlyUnique).forEach((typeface) => {
this.import(`node_modules/typeface-${typeface}/index.css`, {
destDir: 'assets/files'
});
this._getTypefaceFiles(typeface).forEach((fileName) => {
this.import(fileName, {
destDir: 'assets/files'
});
})
})
},
};
function onlyUnique(value, index, self) {
return self.indexOf(value) === index
}
function getTypefacesFromPackage () {
return globby
.sync('typeface-*', { cwd: 'node_modules', onlyDirectories: true })
.map((fullPath) => fullPath.replace('typeface-', '')
)}