forked from storybookjs/ember-cli-storybook
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
129 lines (97 loc) Β· 3.77 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
'use strict';
const fs = require('fs');
const path = require('path');
const YUIDocsGenerator = require('ember-cli-addon-docs-yuidoc/lib/broccoli/generator');
const Funnel = require('broccoli-funnel');
const mergeTrees = require('broccoli-merge-trees');
const { parse, generatePreviewHead } = require('./lib/util');
module.exports = {
name: require('./package').name,
configKey: 'ember-cli-storybook',
_getOptions() {
let addonOptions = (this.parent && this.parent.options) || (this.app && this.app.options) || {};
return addonOptions[this.configKey] || {};
},
included(app) {
this._super.included.apply(this, arguments);
// see: https://github.com/ember-cli/ember-cli/issues/3718
if (typeof app.import !== 'function' && app.app) {
app = app.app;
}
this.app = app;
},
postprocessTree(type, appTree) {
this._super.postprocessTree.apply(this, arguments);
let options = this._getOptions();
let componentFilePathPatterns = options.componentFilePathPatterns || [
'app/components/*.js',
'lib/**/addon/components/*.js',
'addon/components/*.js',
];
if (type !== 'all' || !options.enableAddonDocsIntegration) {
return appTree;
}
let componentJS = new Funnel('.', {
include: componentFilePathPatterns,
});
let componentDocsTree = new YUIDocsGenerator([componentJS], {
project: this.project,
destDir: 'storybook-docgen',
packages: [ this.project.name() ]
});
return mergeTrees([
appTree,
componentDocsTree,
]);
},
outputReady: function(result) {
if (!this.app) {
// You will need ember-cli >= 1.13 to use ember-cli-deploy's postBuild integration.
// This is because prior to 1.13, `this.app` is not available in the outputReady hook.
this.ui.writeErrorLine('please upgrade to ember-cli >= 1.13')
return;
}
const { name } = this.app;
const { storybook={} } = this.app.project.pkg;
const { ignoreTestFiles=true, config={ 'link': [] } } = storybook;
const distFilePath = path.resolve(result.directory, 'index.html');
const testFilePath = path.resolve(result.directory, 'tests/index.html');
const previewHeadFilePath = path.resolve(process.cwd(), '.storybook/preview-head.html');
const previewHeadDirectory = path.dirname(previewHeadFilePath);
const envFilePath = path.resolve(process.cwd(), '.env');
let fileContents = '';
this.ui.writeDebugLine('Generating files needed by Storybook');
if(fs.existsSync(testFilePath)) {
fileContents = fs.readFileSync(testFilePath);
this.ui.writeDebugLine(`Parsing ${testFilePath}`);
} else {
fileContents = fs.readFileSync(distFilePath);
this.ui.writeDebugLine(`Parsing ${distFilePath}`);
}
const parsedConfig = parse(fileContents, ignoreTestFiles);
this.ui.writeDebugLine('Generating preview-head.html');
if(config) {
this.ui.writeDebugLine('Setting up overrides.');
for(const key in config) {
if(!parsedConfig[key]) {
parsedConfig[key] = []
}
parsedConfig[key] = parsedConfig[key].concat(config[key])
}
}
const previewHead = generatePreviewHead(parsedConfig);
this.ui.writeDebugLine('Generating files needed by Storybook');
fs.mkdirSync(previewHeadDirectory, { recursive: true });
fs.writeFileSync(previewHeadFilePath, previewHead);
this.ui.writeDebugLine('Generating .env');
if(fs.existsSync(path.resolve(process.cwd(), '.env'))) {
let fileContent = fs.readFileSync(envFilePath, 'utf8');
if(fileContent.indexOf('STORYBOOK_NAME') === -1) {
fileContent += `\nSTORYBOOK_NAME=${name}`;
fs.writeFileSync(envFilePath, fileContent)
}
} else {
fs.writeFileSync(envFilePath, `STORYBOOK_NAME=${name}`)
}
}
};