1 |
|
2 | 'use strict';
|
3 |
|
4 | const path = require('path');
|
5 | const semver = require('semver');
|
6 | const funnel = require('broccoli-funnel');
|
7 | const stringReplace = require('broccoli-string-replace');
|
8 | const UnwatchedDir = require('broccoli-source').UnwatchedDir;
|
9 |
|
10 | module.exports = {
|
11 | name: require('./package').name,
|
12 |
|
13 | included() {
|
14 | this._super.included.apply(this, arguments);
|
15 |
|
16 | this.import('vendor/showdown.js', {
|
17 | using: [{ transformation: 'amd', as: 'showdown' }]
|
18 | });
|
19 | },
|
20 |
|
21 | findModulePath(basedir) {
|
22 | try {
|
23 | let resolve = require('resolve');
|
24 |
|
25 | return path.dirname(resolve.sync('showdown', { basedir: basedir }));
|
26 | } catch (_) {
|
27 | try {
|
28 | return path.dirname(require.resolve('showdown'));
|
29 | } catch (e) {
|
30 | if (e.code === 'MODULE_NOT_FOUND') {
|
31 | this.ui.writeLine(
|
32 | `ember-cli-showdown: showdown not installed, be sure you have showdown installed via npm/yarn.`
|
33 | );
|
34 | return;
|
35 | }
|
36 |
|
37 | throw e;
|
38 | }
|
39 | }
|
40 | },
|
41 |
|
42 | removeSourcemapAnnotation(node) {
|
43 | return stringReplace(node, {
|
44 | files: ['showdown.js'],
|
45 | annotation: 'Remove sourcemap annotation (showdown)',
|
46 | patterns: [
|
47 | {
|
48 | match: /\/\/# sourceMappingURL=showdown.js.map/g,
|
49 | replacement: ''
|
50 | }
|
51 | ]
|
52 | });
|
53 | },
|
54 |
|
55 | treeForVendor() {
|
56 | let modulePath = this.findModulePath(this.project.root);
|
57 |
|
58 | if (modulePath) {
|
59 | let showdownTree = funnel(new UnwatchedDir(modulePath), {
|
60 | include: ['showdown.js']
|
61 | });
|
62 |
|
63 | let pkg = require(path.join(modulePath, '..', 'package.json'));
|
64 |
|
65 | if (pkg.version && semver.gt(pkg.version, '1.7.4')) {
|
66 | return this.removeSourcemapAnnotation(showdownTree);
|
67 | }
|
68 |
|
69 | |
70 |
|
71 |
|
72 |
|
73 |
|
74 | return this.removeSourcemapAnnotation(
|
75 | stringReplace(showdownTree, {
|
76 | files: ['showdown.js'],
|
77 | patterns: [
|
78 | {
|
79 | match: /typeof module !== 'undefined'/g,
|
80 | replacement: 'false'
|
81 | }
|
82 | ]
|
83 | })
|
84 | );
|
85 | }
|
86 | }
|
87 | };
|