1 | 'use strict';
|
2 |
|
3 | const fs = require('fs-extra');
|
4 | const path = require('path');
|
5 |
|
6 | const yaml = require('js-yaml');
|
7 | const debug = require('debug')('fun:tpl');
|
8 | const { red, yellow } = require('colors');
|
9 |
|
10 | const DEFAULT_BUILD_ARTIFACTS_PATH_SUFFIX = path.join('.fun', 'build', 'artifacts');
|
11 | const DEFAULT_NAS_PATH_SUFFIX = path.join('.fun', 'nas');
|
12 | const DEFAULT_TMP_INVOKE_PATH_SUFFIX = path.join('.fun', 'tmp', 'invoke');
|
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 getTpl(tplPath) {
|
52 |
|
53 | const tplContent = await fs.readFile(tplPath, 'utf8');
|
54 | const tpl = yaml.safeLoad(tplContent);
|
55 |
|
56 | debug('exist tpl: %j', tpl);
|
57 |
|
58 | return tpl;
|
59 | }
|
60 |
|
61 | function validateYmlName(tplPath) {
|
62 | if (!(path.basename(tplPath).endsWith('.yml') || path.basename(tplPath).endsWith('.yaml'))) {
|
63 | throw new Error(red(`The template file name must end with yml or yaml.`));
|
64 | }
|
65 | }
|
66 |
|
67 | function getBaseDir(tplPath) {
|
68 | const idx = tplPath.indexOf(DEFAULT_BUILD_ARTIFACTS_PATH_SUFFIX);
|
69 |
|
70 | if (idx !== -1) {
|
71 | const baseDir = tplPath.substring(0, idx);
|
72 | if (!baseDir) {
|
73 | return process.cwd();
|
74 | }
|
75 | return baseDir;
|
76 | }
|
77 | return path.resolve(path.dirname(tplPath));
|
78 |
|
79 | }
|
80 |
|
81 | function detectTmpDir(tplPath, tmpDir) {
|
82 | if (tmpDir) { return tmpDir; }
|
83 |
|
84 | const baseDir = getBaseDir(tplPath);
|
85 | return path.join(baseDir, DEFAULT_TMP_INVOKE_PATH_SUFFIX);
|
86 | }
|
87 |
|
88 | function detectNasBaseDir(tplPath) {
|
89 | const baseDir = getBaseDir(tplPath);
|
90 |
|
91 | return path.join(baseDir, DEFAULT_NAS_PATH_SUFFIX);
|
92 | }
|
93 |
|
94 | module.exports = {
|
95 | getTpl, detectTplPath, validateYmlName,
|
96 | detectNasBaseDir, DEFAULT_BUILD_ARTIFACTS_PATH_SUFFIX, DEFAULT_NAS_PATH_SUFFIX,
|
97 | detectTmpDir, DEFAULT_TMP_INVOKE_PATH_SUFFIX, getBaseDir
|
98 | }; |
\ | No newline at end of file |