forked from storybookjs/ember-cli-storybook
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
66 lines (46 loc) · 1.97 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
'use strict';
const fs = require('fs');
const path = require('path');
const { parse, generatePreviewHead } = require('./util');
module.exports = {
name: 'ember-cli-storybook',
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.writeLine('please upgrade to ember-cli >= 1.13')
return;
}
const { name } = this.app;
const { storybook={} } = this.app.project.pkg;
const { ignoreTestFiles=true } = 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 envFilePath = path.resolve(process.cwd(), '.env');
let fileContents = '';
this.ui.writeLine('Generating files needed by Storybook');
if(fs.existsSync(testFilePath)) {
fileContents = fs.readFileSync(testFilePath);
this.ui.writeLine(`Parsing ${testFilePath}`);
} else {
fileContents = fs.readFileSync(distFilePath);
this.ui.writeLine(`Parsing ${distFilePath}`);
}
const parsedConfig = parse(fileContents, ignoreTestFiles);
this.ui.writeLine('Generating preview-head.html');
const previewHead = generatePreviewHead(parsedConfig);
this.ui.writeLine('Generating files needed by Storybook');
fs.writeFileSync(previewHeadFilePath, previewHead)
this.ui.writeLine('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}`)
}
}
};