@faable/faable 1.3.7 → 1.3.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,10 +1,12 @@
1
+ import { ConfigStore } from '../lib/ConfigStore.js';
1
2
  import { FaableApi } from './FaableApi.js';
2
- import { apikey } from './strategies/apikey.js';
3
+ import { apikey_strategy } from './strategies/apikey.strategy.js';
3
4
 
4
5
  const context = async () => {
5
- const { config } = await import('./userdir_config.js');
6
+ const store = new ConfigStore();
7
+ const apikey = process.env.FAABLE_APIKEY || (await store.loadCredentials())?.apikey;
6
8
  return {
7
- api: FaableApi.create({ authStrategy: apikey, auth: config }),
9
+ api: FaableApi.create({ authStrategy: apikey_strategy, auth: { apikey } }),
8
10
  };
9
11
  };
10
12
 
@@ -1,4 +1,4 @@
1
- const apikey = (config) => {
1
+ const apikey_strategy = (config) => {
2
2
  const { apikey } = config;
3
3
  if (!apikey) {
4
4
  throw new Error("Missing apikey.");
@@ -12,4 +12,4 @@ const apikey = (config) => {
12
12
  };
13
13
  };
14
14
 
15
- export { apikey };
15
+ export { apikey_strategy };
@@ -1,7 +1,5 @@
1
- import prompts from 'prompts';
2
- import fs from 'fs-extra';
3
- import os from 'os';
4
- import path__default from 'path';
1
+ import { ConfigStore } from '../../lib/ConfigStore.js';
2
+ import { ConfigurationHelper } from '../init/ConfigurationHelper.js';
5
3
 
6
4
  const configure = {
7
5
  command: "configure",
@@ -18,22 +16,12 @@ const configure = {
18
16
  },
19
17
  handler: async (args) => {
20
18
  const { app_name, workdir, api, remove } = args;
21
- const faable_home = path__default.join(os.homedir(), ".faable");
19
+ const store = new ConfigStore();
20
+ const helper = new ConfigurationHelper();
22
21
  if (remove) {
23
- await fs.remove(faable_home);
24
- console.log("Deleted faable config");
25
- return;
22
+ await store.deleteCredentials();
26
23
  }
27
- const response = await prompts([
28
- {
29
- type: "text",
30
- name: "apikey",
31
- message: "What is your Faable ApiKey?",
32
- },
33
- ]);
34
- await fs.ensureDir(faable_home);
35
- await fs.writeJSON(path__default.join(faable_home, "credentials"), response);
36
- console.log("Stored credentials");
24
+ await helper.demandConfig();
37
25
  },
38
26
  };
39
27
 
@@ -0,0 +1,100 @@
1
+ import path__default from 'path';
2
+ import fs from 'fs-extra';
3
+ import { log } from '../../log.js';
4
+ import prompts from 'prompts';
5
+ import { ConfigStore } from '../../lib/ConfigStore.js';
6
+ import yaml from 'yaml';
7
+
8
+ class ConfigurationHelper {
9
+ store;
10
+ constructor() {
11
+ this.store = new ConfigStore();
12
+ }
13
+ get workflows_dir() {
14
+ return path__default.join(process.cwd(), ".github", "workflows");
15
+ }
16
+ get default_action() {
17
+ return path__default.join(this.workflows_dir, "deploy.yml");
18
+ }
19
+ async demandConfig() {
20
+ const creds = await this.store.loadCredentials();
21
+ if (creds.apikey) {
22
+ return;
23
+ }
24
+ const apikey = await prompts([
25
+ {
26
+ type: "text",
27
+ name: "apikey",
28
+ message: "What is your Faable ApiKey?",
29
+ },
30
+ ]);
31
+ await this.store.saveApiKey({ apikey });
32
+ }
33
+ async tentativeName() {
34
+ try {
35
+ const pkg = path__default.join(process.cwd(), "package.json");
36
+ const { name } = await fs.readJSON(pkg);
37
+ return name;
38
+ }
39
+ catch (error) {
40
+ return;
41
+ }
42
+ }
43
+ async initializeGithubAction(force = false) {
44
+ await this.demandConfig();
45
+ // Already configured
46
+ if (!force && fs.pathExistsSync(this.default_action)) {
47
+ log.info(`Github action is already configured.`);
48
+ return;
49
+ }
50
+ const onCancel = (prompt) => {
51
+ log.info("Cancel");
52
+ process.exit(0);
53
+ };
54
+ const { app_name } = await prompts([
55
+ {
56
+ type: "text",
57
+ name: "app_name",
58
+ initial: await this.tentativeName(),
59
+ message: "Which app are you deploying",
60
+ },
61
+ ], { onCancel });
62
+ const action = {
63
+ name: "Deploy to Faable",
64
+ on: {
65
+ push: {
66
+ branches: ["main"],
67
+ },
68
+ },
69
+ env: {
70
+ FAABLE_APIKEY: "${{ secrets.FAABLE_APIKEY }}",
71
+ },
72
+ jobs: {
73
+ deploy: {
74
+ "runs-on": "ubuntu-latest",
75
+ steps: [
76
+ { uses: "actions/checkout@v2" },
77
+ { name: "Prepare CLI", run: "npm i -g @faable/faable" },
78
+ {
79
+ uses: "actions/setup-node@v3",
80
+ with: {
81
+ "node-version": "16",
82
+ cache: "yarn",
83
+ },
84
+ },
85
+ { run: "npm ci" },
86
+ {
87
+ name: "Deploy to Faable",
88
+ run: `faable deploy ${app_name}`,
89
+ },
90
+ ],
91
+ },
92
+ },
93
+ };
94
+ await fs.writeFile(this.default_action, yaml.stringify(action));
95
+ log.info(`Written ${this.default_action}`);
96
+ log.info(`Remember to create FAABLE_APIKEY on Github repo secrets`);
97
+ }
98
+ }
99
+
100
+ export { ConfigurationHelper };
@@ -0,0 +1,23 @@
1
+ import { ConfigurationHelper } from './ConfigurationHelper.js';
2
+
3
+ const init = {
4
+ command: ["initialize", "$0"],
5
+ describe: "Initialize Faable",
6
+ builder: (yargs) => {
7
+ return yargs
8
+ .option("force", {
9
+ alias: "f",
10
+ type: "boolean",
11
+ description: "Force initialization",
12
+ default: false,
13
+ })
14
+ .showHelpOnFail(false);
15
+ },
16
+ handler: async (args) => {
17
+ const { force } = args;
18
+ const helper = new ConfigurationHelper();
19
+ await helper.initializeGithubAction(force);
20
+ },
21
+ };
22
+
23
+ export { init };
package/dist/index.js CHANGED
@@ -4,13 +4,16 @@ import { apps } from './commands/apps/index.js';
4
4
  import { configure } from './commands/configure/index.js';
5
5
  import { deploy } from './commands/deploy/index.js';
6
6
  import { log } from './log.js';
7
+ import { init } from './commands/init/index.js';
7
8
 
8
9
  const yg = yargs();
9
10
  yg.scriptName("faable")
10
11
  .command(deploy)
11
12
  .command(apps)
12
13
  .command(configure)
14
+ .command(init)
13
15
  .demandCommand(1)
16
+ .help()
14
17
  .fail(function (msg, err) {
15
18
  if (err) {
16
19
  log.error(`❌ ${err.message}`);
@@ -0,0 +1,36 @@
1
+ import path__default from 'path';
2
+ import os from 'os';
3
+ import fs from 'fs-extra';
4
+ import { log } from '../log.js';
5
+
6
+ class ConfigStore {
7
+ log;
8
+ faable_home;
9
+ constructor(log$1 = log) {
10
+ this.log = log$1;
11
+ this.faable_home = path__default.join(os.homedir(), ".faable");
12
+ }
13
+ async deleteCredentials() {
14
+ await fs.remove(this.faable_home);
15
+ this.log.info(`Deleted credentials`);
16
+ }
17
+ get credentials_path() {
18
+ return path__default.join(this.faable_home, "credentials");
19
+ }
20
+ async saveApiKey(config) {
21
+ await fs.ensureDir(this.faable_home);
22
+ await fs.writeJSON(this.credentials_path, config);
23
+ this.log.info(`Stored apikey`);
24
+ }
25
+ async loadCredentials() {
26
+ if (!fs.existsSync(this.credentials_path)) {
27
+ // No credentials found
28
+ return;
29
+ }
30
+ // Return credentials
31
+ const config = await fs.readJSON(this.credentials_path);
32
+ return config;
33
+ }
34
+ }
35
+
36
+ export { ConfigStore };
package/package.json CHANGED
@@ -10,6 +10,7 @@
10
10
  "promisify-child-process": "^4.1.1",
11
11
  "prompts": "^2.4.2",
12
12
  "tslib": "^2.4.1",
13
+ "yaml": "^2.2.2",
13
14
  "yargs": "^17.6.2"
14
15
  },
15
16
  "bin": {
@@ -26,5 +27,5 @@
26
27
  "access": "public"
27
28
  },
28
29
  "homepage": "https://github.com/faablecloud/faable#readme",
29
- "version": "1.3.7"
30
+ "version": "1.3.8"
30
31
  }
@@ -1,20 +0,0 @@
1
- import fs from 'fs-extra';
2
- import path__default from 'path';
3
- import os from 'os';
4
-
5
- const faable_home = path__default.join(os.homedir(), ".faable");
6
- const config = {};
7
- const credentials_path = path__default.join(faable_home, "credentials");
8
- if (fs.existsSync(credentials_path)) {
9
- // console.log("Loaded stored config");
10
- const creds = fs.readJSONSync(credentials_path);
11
- // console.log(creds);
12
- config.clientId = creds.clientId;
13
- config.clientSecret = creds.clientSecret;
14
- config.apikey = creds.apikey;
15
- }
16
- config.clientId = process.env.FAABLE_CLIENT_ID || config.clientId;
17
- config.clientSecret = process.env.FAABLE_CLIENT_SECRET || config.clientSecret;
18
- config.apikey = process.env.FAABLE_APIKEY || config.apikey;
19
-
20
- export { config };