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