storybook-chrome-screenshot
Version:
A Storybook addon, Save the screenshot image of your stories! via puppeteer.
228 lines • 11.9 kB
JavaScript
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __generator = (this && this.__generator) || function (thisArg, body) {
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
function verb(n) { return function (v) { return step([n, v]); }; }
function step(op) {
if (f) throw new TypeError("Generator is already executing.");
while (_) try {
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
if (y = 0, t) op = [op[0] & 2, t.value];
switch (op[0]) {
case 0: case 1: t = op; break;
case 4: _.label++; return { value: op[1], done: false };
case 5: _.label++; y = op[1]; op = [0]; continue;
case 7: op = _.ops.pop(); _.trys.pop(); continue;
default:
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
if (t[2]) _.ops.pop();
_.trys.pop(); continue;
}
op = body.call(thisArg, _);
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
}
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
result["default"] = mod;
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
var chalk_1 = __importDefault(require("chalk"));
var fs = __importStar(require("fs"));
var _ = __importStar(require("lodash"));
var logSymbols = __importStar(require("log-symbols"));
var mkdirp = __importStar(require("mkdirp"));
var node_emoji_1 = require("node-emoji");
var path = __importStar(require("path"));
var util_inspect_1 = __importDefault(require("util-inspect"));
var constants_1 = require("../constants");
var utils_1 = require("../utils");
var App = /** @class */ (function () {
function App(options, store, terminal, server, browserFactory) {
this.options = options;
this.store = store;
this.terminal = terminal;
this.server = server;
this.browsers = _.range(this.options.parallel).map(browserFactory);
this.pages = [];
this.startTime = Date.now();
}
App.prototype.validate = function () {
return __awaiter(this, void 0, void 0, function () {
var _a, cmd, cwd, configDir;
return __generator(this, function (_b) {
_a = this.options, cmd = _a.cmd, cwd = _a.cwd, configDir = _a.configDir;
this.terminal.log('CLI Options', util_inspect_1.default(this.options));
if (!fs.existsSync(cmd)) {
this.terminal.error("Storybook does not exists. First, let's setup a Storybook!\n See: https://storybook.js.org/basics/quick-start-guide/");
return [2 /*return*/];
}
if (!fs.existsSync(path.resolve(cwd, configDir, 'config.js'))) {
this.terminal.error("\"" + configDir + "/config.js\" does not exists.");
return [2 /*return*/];
}
return [2 /*return*/];
});
});
};
App.prototype.launch = function () {
return __awaiter(this, void 0, void 0, function () {
var _a;
var _this = this;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
this.terminal.section('green', constants_1.PhaseTypes.LAUNCH, 'Launching storybook server ...').blank();
return [4 /*yield*/, Promise.all([this.server.start(), Promise.all(this.browsers.map(function (b) { return b.launch(); }))])];
case 1:
_b.sent();
_a = this;
return [4 /*yield*/, Promise.all(this.browsers.map(function (b) {
return b.createPage(_this.server.getURL(), function (type, text) {
_this.terminal.log('BROWSER', type + ": " + text.trim());
});
}))];
case 2:
_a.pages = _b.sent();
return [2 /*return*/];
}
});
});
};
App.prototype.prepare = function () {
var _this = this;
this.terminal.section('cyan', constants_1.PhaseTypes.PREPARE, 'Fetching the target components ...').blank();
mkdirp.sync(this.options.outputDir);
return Promise.all(this.pages.map(function (p) {
return new Promise(function (resolve, reject) { return __awaiter(_this, void 0, void 0, function () {
var e_1;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
_a.trys.push([0, 3, , 4]);
p.waitScreenshotStories()
.then(resolve)
.catch(reject);
return [4 /*yield*/, p.exposeSetScreenshotStories()];
case 1:
_a.sent();
return [4 /*yield*/, p.goto(constants_1.PhaseTypes.PREPARE)];
case 2:
_a.sent();
return [3 /*break*/, 4];
case 3:
e_1 = _a.sent();
reject(e_1);
return [3 /*break*/, 4];
case 4: return [2 /*return*/];
}
});
}); });
})).then(function (storiesList) {
_this.store.set(storiesList.reduce(function (acc, cur) { return acc.concat(cur); }, []));
});
};
App.prototype.capture = function () {
return __awaiter(this, void 0, void 0, function () {
var stories, parallel;
var _this = this;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
stories = this.store.get();
parallel = Math.min(stories.length, this.options.parallel);
this.terminal
.section('yellow', constants_1.PhaseTypes.CAPTURE, 'Capturing component screenshots ...')
.blank()
.progressStart(node_emoji_1.emojify(':camera: [:bar] :current/:total'), stories.length);
return [4 /*yield*/, utils_1.execParalell(stories.map(function (story) { return function (workerIndex) { return __awaiter(_this, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.pages[workerIndex].screenshot(story)];
case 1:
_a.sent();
this.terminal.progressTick();
return [2 /*return*/];
}
});
}); }; }), parallel)];
case 1:
_a.sent();
fs.writeFileSync(path.join(this.options.outputDir, 'stories.json'), JSON.stringify(stories));
this.terminal.progressStop();
return [2 /*return*/];
}
});
});
};
App.prototype.teardown = function () {
return __awaiter(this, void 0, void 0, function () {
var outputDir, takedStories, skippedStories;
var _this = this;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
this.terminal.section('cyan', constants_1.PhaseTypes.DONE, 'Screenshot image saving is completed!').blank();
outputDir = this.options.outputDir;
takedStories = this.store.get(false);
skippedStories = this.store.get(true);
takedStories.forEach(function (_a) {
var filename = _a.filename;
_this.terminal.echo(" " + logSymbols.success + " " + outputDir + "/" + chalk_1.default.bold(filename));
});
skippedStories.forEach(function (_a) {
var filename = _a.filename;
_this.terminal.echo(" " + logSymbols.warning + " " + outputDir + "/" + chalk_1.default.bold(filename) + " " + chalk_1.default.yellow('(skipped)'));
});
this.terminal
.blank(1)
.echo(chalk_1.default.bold('Time') + ": " + utils_1.humanizeDuration(Date.now() - this.startTime))
.echo(chalk_1.default.bold('Screenshots') + ": " + takedStories.length + " total (" + skippedStories.length + " skipped)")
.blank(2);
return [4 /*yield*/, this.terminate()];
case 1:
_a.sent();
return [2 /*return*/];
}
});
});
};
App.prototype.terminate = function (e) {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
if (e != null) {
this.terminal.error("An unexpected error occurred, Please make sure message below\n" + e);
}
this.server.stop();
return [4 /*yield*/, Promise.all(this.browsers.map(function (b) { return b.close(); }))];
case 1:
_a.sent();
return [2 /*return*/];
}
});
});
};
return App;
}());
exports.App = App;
//# sourceMappingURL=App.js.map