@faable/faable 1.3.7 → 1.3.9
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.
- package/dist/api/context.js +5 -3
- package/dist/api/strategies/{apikey.js → apikey.strategy.js} +2 -2
- package/dist/commands/configure/index.js +6 -18
- package/dist/commands/init/ConfigurationHelper.js +113 -0
- package/dist/commands/init/index.js +23 -0
- package/dist/index.js +3 -0
- package/dist/lib/ConfigStore.js +36 -0
- package/package.json +2 -1
- package/dist/api/userdir_config.js +0 -20
package/dist/api/context.js
CHANGED
|
@@ -1,10 +1,12 @@
|
|
|
1
|
+
import { ConfigStore } from '../lib/ConfigStore.js';
|
|
1
2
|
import { FaableApi } from './FaableApi.js';
|
|
2
|
-
import {
|
|
3
|
+
import { apikey_strategy } from './strategies/apikey.strategy.js';
|
|
3
4
|
|
|
4
5
|
const context = async () => {
|
|
5
|
-
const
|
|
6
|
+
const store = new ConfigStore();
|
|
7
|
+
const apikey = process.env.FAABLE_APIKEY || (await store.loadCredentials())?.apikey;
|
|
6
8
|
return {
|
|
7
|
-
api: FaableApi.create({ authStrategy:
|
|
9
|
+
api: FaableApi.create({ authStrategy: apikey_strategy, auth: { apikey } }),
|
|
8
10
|
};
|
|
9
11
|
};
|
|
10
12
|
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
const
|
|
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 {
|
|
15
|
+
export { apikey_strategy };
|
|
@@ -1,7 +1,5 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
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
|
|
19
|
+
const store = new ConfigStore();
|
|
20
|
+
const helper = new ConfigurationHelper();
|
|
22
21
|
if (remove) {
|
|
23
|
-
await
|
|
24
|
-
console.log("Deleted faable config");
|
|
25
|
-
return;
|
|
22
|
+
await store.deleteCredentials();
|
|
26
23
|
}
|
|
27
|
-
|
|
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,113 @@
|
|
|
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 checkPackageManager() {
|
|
44
|
+
if (fs.existsSync(path__default.join(process.cwd(), "package-lock.json"))) {
|
|
45
|
+
return "npm";
|
|
46
|
+
}
|
|
47
|
+
if (fs.existsSync(path__default.join(process.cwd(), "yarn.lock"))) {
|
|
48
|
+
return "yarn";
|
|
49
|
+
}
|
|
50
|
+
throw new Error("No package-lock.json or yarn.lock file found");
|
|
51
|
+
}
|
|
52
|
+
async initializeGithubAction(force = false) {
|
|
53
|
+
await this.demandConfig();
|
|
54
|
+
// Already configured
|
|
55
|
+
if (!force && fs.pathExistsSync(this.default_action)) {
|
|
56
|
+
log.info(`Github action is already configured.`);
|
|
57
|
+
return;
|
|
58
|
+
}
|
|
59
|
+
const onCancel = (prompt) => {
|
|
60
|
+
log.info("Cancel");
|
|
61
|
+
process.exit(0);
|
|
62
|
+
};
|
|
63
|
+
const { app_name } = await prompts([
|
|
64
|
+
{
|
|
65
|
+
type: "text",
|
|
66
|
+
name: "app_name",
|
|
67
|
+
initial: await this.tentativeName(),
|
|
68
|
+
message: "Which app are you deploying",
|
|
69
|
+
},
|
|
70
|
+
], { onCancel });
|
|
71
|
+
const manager = await this.checkPackageManager();
|
|
72
|
+
const action = {
|
|
73
|
+
name: "Deploy to Faable",
|
|
74
|
+
on: {
|
|
75
|
+
push: {
|
|
76
|
+
branches: ["main"],
|
|
77
|
+
},
|
|
78
|
+
},
|
|
79
|
+
env: {
|
|
80
|
+
FAABLE_APIKEY: "${{ secrets.FAABLE_APIKEY }}",
|
|
81
|
+
},
|
|
82
|
+
jobs: {
|
|
83
|
+
deploy: {
|
|
84
|
+
"runs-on": "ubuntu-latest",
|
|
85
|
+
steps: [
|
|
86
|
+
{ uses: "actions/checkout@v2" },
|
|
87
|
+
{ name: "Prepare CLI", run: "npm i -g @faable/faable" },
|
|
88
|
+
{
|
|
89
|
+
uses: "actions/setup-node@v3",
|
|
90
|
+
with: {
|
|
91
|
+
"node-version": "16",
|
|
92
|
+
cache: "yarn",
|
|
93
|
+
},
|
|
94
|
+
},
|
|
95
|
+
...(manager == "npm" && [{ run: "npm ci" }]),
|
|
96
|
+
...(manager == "yarn" && [
|
|
97
|
+
{ run: "yarn install --frozen-lockfile" },
|
|
98
|
+
]),
|
|
99
|
+
{
|
|
100
|
+
name: "Deploy to Faable",
|
|
101
|
+
run: `faable deploy ${app_name}`,
|
|
102
|
+
},
|
|
103
|
+
],
|
|
104
|
+
},
|
|
105
|
+
},
|
|
106
|
+
};
|
|
107
|
+
await fs.writeFile(this.default_action, yaml.stringify(action));
|
|
108
|
+
log.info(`Written ${this.default_action}`);
|
|
109
|
+
log.info(`Remember to create FAABLE_APIKEY on Github repo secrets`);
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
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.
|
|
30
|
+
"version": "1.3.9"
|
|
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 };
|