UNPKG

6.75 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3var Fs = require("fs-extra");
4var Minimist = require("minimist");
5var Path = require("path");
6var config_1 = require("./config");
7var git_1 = require("./git");
8var paths_1 = require("./paths");
9var renderer_1 = require("./renderer");
10var step_1 = require("./step");
11var translator_1 = require("./translator");
12var utils_1 = require("./utils");
13// register custom transforations from ./tortilla/config.js
14config_1.Config.registerCustomTransformations();
15/**
16 Contains manual related utilities.
17 */
18function init() {
19 if (require.main !== module) {
20 return;
21 }
22 var argv = Minimist(process.argv.slice(2), {
23 string: ['_'],
24 boolean: ['all', 'root'],
25 });
26 var method = argv._[0];
27 var step = argv._[1];
28 var all = argv.all;
29 var root = argv.root;
30 if (!step && all) {
31 step = 'all';
32 }
33 if (!step && root) {
34 step = 'root';
35 }
36 switch (method) {
37 case 'render':
38 return renderManual(step);
39 }
40}
41init();
42// Converts manual into the opposite format
43function renderManual(step) {
44 if (typeof step === 'string') {
45 var isSuperStep = !step.split('.')[1];
46 if (!isSuperStep) {
47 throw TypeError('Provided step must be a super step');
48 }
49 }
50 else {
51 var superMessage = step_1.Step.recentSuperCommit('%s') || '';
52 var stepDescriptor = step_1.Step.descriptor(superMessage) || {};
53 step = stepDescriptor.number || 'root';
54 }
55 // Convert all manuals since the beginning of history
56 if (step === 'all') {
57 return git_1.Git.print(['rebase', '-i', '--root', '--keep-empty'], {
58 env: {
59 GIT_SEQUENCE_EDITOR: "node " + paths_1.Paths.tortilla.editor + " render",
60 },
61 });
62 }
63 // Indicates whether we should continue rebasing at the end of the invocation.
64 // If this script is not run by the git editor we should continue rebasing
65 var shouldContinue = !git_1.Git.rebasing();
66 // Enter rebase, after all this is what rebase-continue is all about
67 if (shouldContinue) {
68 utils_1.Utils.scopeEnv(step_1.Step.edit.bind(step_1.Step, step), {
69 TORTILLA_STDIO: 'ignore',
70 });
71 }
72 var localesDir = paths_1.Paths.manuals.templates + "/locales";
73 var locales = [''];
74 if (utils_1.Utils.exists(localesDir)) {
75 locales = locales.concat(Fs.readdirSync(localesDir));
76 }
77 // Render manual for each locale
78 locales.forEach(function (locale) {
79 // Fetch the current manual and other useful models
80 var manualTemplatePath = getManualTemplatePath(step, locale);
81 var manualTemplate = Fs.readFileSync(manualTemplatePath, 'utf8');
82 var manualViewPath = getManualViewPath(step, locale);
83 var commitMessage = getStepCommitMessage(step);
84 var manualView = renderManualView(manualTemplate, {
85 step: step,
86 commitMessage: commitMessage,
87 templatePath: manualTemplatePath,
88 viewPath: manualViewPath,
89 language: locale,
90 });
91 // Rewrite manual
92 Fs.ensureDir(paths_1.Paths.manuals.views);
93 // In case a custom render target is specified, ensure its dir exists
94 var target = process.env.TORTILLA_RENDER_TARGET;
95 if (target) {
96 var customTargetDir = Path.resolve(paths_1.Paths.manuals.views, target);
97 Fs.ensureDir(customTargetDir);
98 }
99 Fs.ensureDirSync(Path.dirname(manualViewPath));
100 Fs.writeFileSync(manualViewPath, manualView);
101 // Amend changes
102 git_1.Git(['add', manualViewPath]);
103 // The following code is dedicated for locale-free manuals
104 if (locale) {
105 return;
106 }
107 var symlinkPath = Path.resolve(paths_1.Paths.manuals.views, 'root.md');
108 // If this is the root step, create a symlink to README.md if not yet exists
109 if (step !== 'root' || utils_1.Utils.exists(symlinkPath)) {
110 return;
111 }
112 var relativeSymlink = Path.relative(Path.dirname(symlinkPath), manualViewPath);
113 Fs.symlinkSync(relativeSymlink, symlinkPath);
114 git_1.Git(['add', symlinkPath]);
115 });
116 if (shouldContinue || git_1.Git.stagedFiles().length) {
117 git_1.Git.print(['commit', '--amend'], {
118 env: {
119 GIT_EDITOR: true,
120 },
121 });
122 }
123 if (shouldContinue) {
124 git_1.Git.print(['rebase', '--continue'], {
125 env: {
126 GIT_EDITOR: true,
127 },
128 });
129 }
130}
131// Renders manual template into informative view
132function renderManualView(manual, scope) {
133 var header;
134 var body;
135 var footer;
136 translator_1.Translator.scopeLanguage(scope.language, function () {
137 header = renderer_1.Renderer.renderTemplateFile('header', scope);
138 body = renderer_1.Renderer.renderTemplate(manual, scope);
139 footer = renderer_1.Renderer.renderTemplateFile('footer', scope);
140 });
141 return [header, body, footer].join('\n');
142}
143// Gets the manual template belonging to the given step
144function getManualTemplatePath(step, locale) {
145 locale = locale ? ("locales/" + locale) : '';
146 var baseDir = Path.resolve(paths_1.Paths.manuals.templates, locale);
147 var fileName = step === 'root' ? 'root.tmpl' : ("step" + step + ".tmpl");
148 return Path.resolve(baseDir, fileName);
149}
150// Gets the manual view belonging to the given step
151function getManualViewPath(step, locale) {
152 locale = locale ? ("locales/" + locale) : '';
153 // The sub-dir of our views in case a custom render target is specified
154 var subDir = process.env.TORTILLA_RENDER_TARGET || '';
155 var fileName = step === 'root' ? 'root.md' : ("step" + step + ".md");
156 // If sub-dir exists, return its path e.g. manuals/view/medium
157 if (subDir) {
158 return Path.resolve(paths_1.Paths.manuals.views, subDir, locale, fileName);
159 }
160 // If we're trying to render root step, return README.md
161 if (step === 'root' && !locale) {
162 return paths_1.Paths.readme;
163 }
164 // Resolve normally e.g. manuals/views/step1.md
165 return Path.resolve(paths_1.Paths.manuals.views, locale, fileName);
166}
167// Gets the commit message belonging to the given step
168function getStepCommitMessage(step) {
169 if (step === 'root') {
170 var rootHash = git_1.Git.rootHash();
171 return git_1.Git(['log', '-1', rootHash, '--format=%s']);
172 }
173 return git_1.Git(['log', '-1', '--grep', "^Step " + step + ":", '--format=%s']);
174}
175exports.Manual = {
176 render: renderManual,
177 manualTemplatePath: getManualTemplatePath,
178 manualViewPath: getManualViewPath,
179};
180//# sourceMappingURL=manual.js.map
\No newline at end of file