From 1108623d17bb4c391401b80d138b679804626a2e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matuzal=C3=A9m=20S=2E=20Teles?= Date: Wed, 2 Sep 2020 22:16:36 -0300 Subject: [PATCH] cli: adds support for the yaml file format --- packages/cli/package.json | 5 +- packages/cli/src/commands/deploy/files.js | 82 ++++++++++++++----- packages/cli/src/commands/deploy/index.js | 8 +- .../cli/src/commands/deploy/local-path.js | 5 -- yarn.lock | 4 + 5 files changed, 73 insertions(+), 31 deletions(-) delete mode 100644 packages/cli/src/commands/deploy/local-path.js diff --git a/packages/cli/package.json b/packages/cli/package.json index f38a078..e507cbd 100644 --- a/packages/cli/package.json +++ b/packages/cli/package.json @@ -20,7 +20,6 @@ "@fleetfn/sdk": "^1.0.0-alpha.1", "adm-zip": "^0.4.16", "ansi-escapes": "^4.3.1", - "ora": "^4.0.4", "chalk": "^2.4.2", "commander": "^4.1.1", "console": "^0.7.2", @@ -29,10 +28,12 @@ "load-json-file": "3.0.0", "ms": "^2.1.1", "node-fetch": "^2.3.0", + "ora": "^4.0.4", "progress": "^2.0.3", "prompts": "^2.3.0", "webpack-merge": "^4.2.2", - "write-json-file": "^4.2.1" + "write-json-file": "^4.2.1", + "yaml": "^1.10.0" }, "dependencies": { "fs-extra": "^9.0.0", diff --git a/packages/cli/src/commands/deploy/files.js b/packages/cli/src/commands/deploy/files.js index edfbf63..58e6f6b 100644 --- a/packages/cli/src/commands/deploy/files.js +++ b/packages/cli/src/commands/deploy/files.js @@ -1,32 +1,74 @@ -import {existsSync} from 'fs'; -import getLocalPathConfig from './local-path'; +import {existsSync, readFileSync} from 'fs'; +import {join, extname} from 'path'; import loadJSON from 'load-json-file'; +import YAML from 'yaml'; -export function readLocalConfig({error}, prefix) { - const target = getLocalPathConfig(prefix || process.cwd()); - let localConfigExists; +function getConfigPath(prefix) { + const configYaml = join(prefix, 'fleet.yml'); + const configJson = join(prefix, 'fleet.json'); + if (existsSync(configYaml)) { + return configYaml; + } else if (existsSync(configJson)) { + return configJson; + } + + return null; +} + +function getConfigJson(error, path) { try { - localConfigExists = existsSync(target); + return loadJSON.sync(path); } catch (err) { - error('Failed to check if `fleet.json` exists', null); + if (err.name === 'JSONError') { + error(err.message, null); + } else { + const code = err.code ? `(${err.code})` : ''; + error(`Failed to read the \`fleet.json\` file ${code}`, null); + } + process.exit(1); } +} + +function getConfigYaml(error, path) { + try { + const file = readFileSync(path, 'utf8'); + const content = YAML.parse(file); + const functions = []; - if (localConfigExists) { - try { - return loadJSON.sync(target); - } catch (err) { - if (err.name === 'JSONError') { - error(err.message, null); - } else { - const code = err.code ? `(${err.code})` : ''; - error(`Failed to read the \`fleet.json\` file ${code}`, null); - } - - process.exit(1); + if (content.functions) { + Object.keys(content.functions).forEach((key) => { + functions.push({name: key, ...content.functions[key]}); + }); + + return { + ...content, + functions, + }; } + + return content; + } catch (err) { + const code = err.code ? `(${err.code})` : ''; + error(`Failed to parse the \`fleet.yml\` file ${code}`, null); + + process.exit(1); } +} - return null; +export function getLocalConfig({error}, prefix) { + const path = getConfigPath(prefix); + const ext = extname(path); + + if (path) { + switch (ext) { + case '.json': + return getConfigJson(error, path); + case '.yml': + return getConfigYaml(error, path); + default: + return null; + } + } } diff --git a/packages/cli/src/commands/deploy/index.js b/packages/cli/src/commands/deploy/index.js index 4449f5a..44a4a8d 100644 --- a/packages/cli/src/commands/deploy/index.js +++ b/packages/cli/src/commands/deploy/index.js @@ -9,7 +9,7 @@ import {build} from './build'; import {cmd, createLogger} from '../../shared/logger'; import {getLinkedProject, linkFolderToProject} from './link'; import {readAuthConfigFile} from '../../shared/config'; -import {readLocalConfig} from './files'; +import {getLocalConfig} from './files'; import humanizePath from '../../shared/humanize-path'; import stamp from './stamp'; @@ -46,11 +46,11 @@ export default async (argv) => { const path = process.cwd(); const git = simpleGit(); - const localConfig = readLocalConfig(logger, path); + const localConfig = getLocalConfig(logger, path); if (!localConfig) { warn( - `Your project is missing a ${chalk.bold('fleet.json')} file.`, + `Your project is missing a ${chalk.bold('fleet.yml')} file.`, 'configuration.html' ); return 1; @@ -68,7 +68,7 @@ export default async (argv) => { warn( `Your project is missing the ${chalk.yellow.blue( 'functions' - )} property in the ${chalk.bold('fleet.json')} file.`, + )} property in the ${chalk.bold('fleet.yml')} file.`, 'configuration.html' ); return 1; diff --git a/packages/cli/src/commands/deploy/local-path.js b/packages/cli/src/commands/deploy/local-path.js deleted file mode 100644 index 07f95ac..0000000 --- a/packages/cli/src/commands/deploy/local-path.js +++ /dev/null @@ -1,5 +0,0 @@ -import path from 'path'; - -export default (prefix) => { - return path.join(prefix, 'fleet.json'); -}; diff --git a/yarn.lock b/yarn.lock index 20a4420..6b3f3b6 100644 --- a/yarn.lock +++ b/yarn.lock @@ -3858,3 +3858,7 @@ yallist@^3.0.2: yallist@^4.0.0: version "4.0.0" resolved "https://registry.yarnpkg.com/yallist/-/yallist-4.0.0.tgz#9bb92790d9c0effec63be73519e11a35019a3a72" + +yaml@^1.10.0: + version "1.10.0" + resolved "https://registry.yarnpkg.com/yaml/-/yaml-1.10.0.tgz#3b593add944876077d4d683fee01081bd9fff31e"