Skip to content

Commit

Permalink
cli: adds support for the yaml file format
Browse files Browse the repository at this point in the history
  • Loading branch information
matuzalemsteles committed Sep 3, 2020
1 parent b6334f1 commit 1108623
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 31 deletions.
5 changes: 3 additions & 2 deletions packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand Down
82 changes: 62 additions & 20 deletions packages/cli/src/commands/deploy/files.js
Original file line number Diff line number Diff line change
@@ -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;
}
}
}
8 changes: 4 additions & 4 deletions packages/cli/src/commands/deploy/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand Down
5 changes: 0 additions & 5 deletions packages/cli/src/commands/deploy/local-path.js

This file was deleted.

4 changes: 4 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"

0 comments on commit 1108623

Please sign in to comment.