1 | 'use strict';
|
2 | var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
3 | return new (P || (P = Promise))(function (resolve, reject) {
|
4 | function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
5 | function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
6 | function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
|
7 | step((generator = generator.apply(thisArg, _arguments || [])).next());
|
8 | });
|
9 | };
|
10 | const fs = require('fs-extra');
|
11 | const path = require('path');
|
12 | const debug = require('debug')('fun:tpl');
|
13 | const { red, yellow } = require('colors');
|
14 | const DEFAULT_BUILD_ARTIFACTS_PATH_SUFFIX = path.join('.fun', 'build', 'artifacts');
|
15 | const DEFAULT_NAS_PATH_SUFFIX = path.join('.fun', 'nas');
|
16 | const DEFAULT_LOCAL_TMP_PATH_SUFFIX = path.join('.fun', 'tmp', 'local');
|
17 | const validate = require('../lib/validate/validate');
|
18 | const { mergeTpl } = require('./utils/tpl');
|
19 | let hasShownTip = false;
|
20 | function asyncFind(pathArrays, filter) {
|
21 | return __awaiter(this, void 0, void 0, function* () {
|
22 | for (let path of pathArrays) {
|
23 | if (yield filter(path)) {
|
24 | return path;
|
25 | }
|
26 | }
|
27 | return null;
|
28 | });
|
29 | }
|
30 | function detectTplPath(preferBuildTpl = true, customTemplateLocations = [], showTip = true) {
|
31 | return __awaiter(this, void 0, void 0, function* () {
|
32 | let buildTemplate = [];
|
33 | if (preferBuildTpl) {
|
34 | buildTemplate = ['template.yml', 'template.yaml'].map(f => {
|
35 | return path.join(process.cwd(), '.fun', 'build', 'artifacts', f);
|
36 | });
|
37 | }
|
38 | const defaultTemplate = ['template.yml', 'template.yaml', 'faas.yml', 'faas.yaml']
|
39 | .map((f) => path.join(process.cwd(), f));
|
40 | const tplPath = yield asyncFind([...customTemplateLocations, ...buildTemplate, ...defaultTemplate], (path) => __awaiter(this, void 0, void 0, function* () {
|
41 | return yield fs.pathExists(path);
|
42 | }));
|
43 | if (tplPath && showTip && !hasShownTip) {
|
44 | console.log(yellow(`using template: ${path.relative(process.cwd(), tplPath)}`));
|
45 | hasShownTip = false;
|
46 | }
|
47 | return tplPath;
|
48 | });
|
49 | }
|
50 | function generateMergedTpl(templates = [], preferBuildTpl = true, customTemplateLocations = [], showTip = true) {
|
51 | return __awaiter(this, void 0, void 0, function* () {
|
52 | let tplPath;
|
53 | if (templates.length > 0) {
|
54 | tplPath = templates[0];
|
55 | }
|
56 | if (!tplPath) {
|
57 | tplPath = yield detectTplPath(preferBuildTpl, customTemplateLocations, showTip);
|
58 | let overrideTplPath;
|
59 | if (tplPath) {
|
60 | templates.push(tplPath);
|
61 | overrideTplPath = yield detectOverrideTplPath(tplPath);
|
62 | }
|
63 | if (overrideTplPath) {
|
64 | templates.push(overrideTplPath);
|
65 | }
|
66 | }
|
67 | if (!tplPath) {
|
68 | throw new Error(red('Current folder not a fun project\nThe folder must contains template.[yml|yaml] or faas.[yml|yaml] .'));
|
69 | }
|
70 | validateTplName(...templates);
|
71 | yield validate(...templates);
|
72 | const tpl = yield getTpl(...templates);
|
73 | return {
|
74 | tpl,
|
75 | tplPath
|
76 | };
|
77 | });
|
78 | }
|
79 | function detectOverrideTplPath(tplPath) {
|
80 | return __awaiter(this, void 0, void 0, function* () {
|
81 | if (!tplPath) {
|
82 | return;
|
83 | }
|
84 | const overrideTplPath = path.resolve(path.dirname(tplPath), 'template.override.yml');
|
85 | if (yield fs.pathExists(overrideTplPath)) {
|
86 | return overrideTplPath;
|
87 | }
|
88 | return;
|
89 | });
|
90 | }
|
91 | function getTpl(...tplPaths) {
|
92 | return __awaiter(this, void 0, void 0, function* () {
|
93 | const tpl = mergeTpl(...tplPaths);
|
94 | debug('exist tpl: %j', tpl);
|
95 | return tpl;
|
96 | });
|
97 | }
|
98 | function validateTplName(...tplPaths) {
|
99 | for (const tplPath of tplPaths) {
|
100 | if (!(path.basename(tplPath).endsWith('.yml') || path.basename(tplPath).endsWith('.yaml'))) {
|
101 | throw new Error(red(`The template file name must end with yml or yaml.`));
|
102 | }
|
103 | }
|
104 | }
|
105 | function getBaseDir(tplPath) {
|
106 | const idx = tplPath.indexOf(DEFAULT_BUILD_ARTIFACTS_PATH_SUFFIX);
|
107 | if (idx !== -1) {
|
108 | const baseDir = tplPath.substring(0, idx);
|
109 | if (!baseDir) {
|
110 | return process.cwd();
|
111 | }
|
112 | return baseDir;
|
113 | }
|
114 | return path.resolve(path.dirname(tplPath));
|
115 | }
|
116 | function getRootBaseDir(baseDir) {
|
117 | const idx = baseDir.indexOf(DEFAULT_BUILD_ARTIFACTS_PATH_SUFFIX);
|
118 | if (idx !== -1) {
|
119 | return baseDir.substring(0, idx);
|
120 | }
|
121 | return baseDir;
|
122 | }
|
123 | function getRootTplPath(tplPath) {
|
124 | const baseDir = getBaseDir(tplPath);
|
125 | return path.join(baseDir, path.basename(tplPath));
|
126 | }
|
127 | function getProjectTpl(tplPath) {
|
128 | return __awaiter(this, void 0, void 0, function* () {
|
129 | const projectBaseDir = getBaseDir(tplPath);
|
130 | const projectTplPath = path.resolve(projectBaseDir, path.basename(tplPath));
|
131 | const projectTpl = yield getTpl(projectTplPath);
|
132 | return {
|
133 | projectTpl,
|
134 | projectTplPath
|
135 | };
|
136 | });
|
137 | }
|
138 | function getNasYmlPath(tplPath) {
|
139 | const baseDir = getBaseDir(tplPath);
|
140 | return path.join(baseDir, '.nas.yml');
|
141 | }
|
142 | function detectTmpDir(tplPath, tmpDir) {
|
143 | if (tmpDir) {
|
144 | return tmpDir;
|
145 | }
|
146 | const baseDir = getBaseDir(tplPath);
|
147 | return path.join(baseDir, DEFAULT_LOCAL_TMP_PATH_SUFFIX);
|
148 | }
|
149 | function detectNasBaseDir(tplPath) {
|
150 | const baseDir = getBaseDir(tplPath);
|
151 | return path.join(baseDir, DEFAULT_NAS_PATH_SUFFIX);
|
152 | }
|
153 | module.exports = {
|
154 | getTpl, detectTplPath, validateTplName,
|
155 | detectNasBaseDir, DEFAULT_BUILD_ARTIFACTS_PATH_SUFFIX, DEFAULT_NAS_PATH_SUFFIX,
|
156 | detectTmpDir, DEFAULT_LOCAL_TMP_PATH_SUFFIX, getBaseDir, getNasYmlPath, getRootBaseDir,
|
157 | getRootTplPath, detectOverrideTplPath, generateMergedTpl, getProjectTpl
|
158 | };
|