@sentio/cli 1.37.5-rc.7 → 2.0.0-rc.2
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/lib/build.js +42 -32
- package/lib/build.js.map +1 -1
- package/lib/cli.js +25 -96
- package/lib/cli.js.map +1 -1
- package/lib/commands/login-server.js +41 -65
- package/lib/commands/login-server.js.map +1 -1
- package/lib/commands/run-create.js +35 -30
- package/lib/commands/run-create.js.map +1 -1
- package/lib/commands/run-login.js +28 -58
- package/lib/commands/run-login.js.map +1 -1
- package/lib/commands/run-upload.d.ts +3 -0
- package/lib/commands/run-upload.js +254 -0
- package/lib/commands/run-upload.js.map +1 -0
- package/lib/commands/run-version.js +9 -39
- package/lib/commands/run-version.js.map +1 -1
- package/lib/config.js +4 -11
- package/lib/config.js.map +1 -1
- package/lib/key.js +18 -26
- package/lib/key.js.map +1 -1
- package/lib/utils.js +7 -15
- package/lib/utils.js.map +1 -1
- package/package.json +11 -18
- package/src/build.ts +29 -9
- package/src/cli.ts +7 -72
- package/src/commands/login-server.ts +25 -17
- package/src/commands/run-create.ts +14 -0
- package/src/commands/run-login.ts +4 -4
- package/src/{upload.ts → commands/run-upload.ts} +94 -19
- package/src/commands/run-version.ts +1 -1
- package/templates/aptos/jest.config.ts +8 -0
- package/templates/aptos/package.json +3 -2
- package/templates/aptos/src/processor.ts +3 -3
- package/templates/aptos/tsconfig.json +4 -3
- package/templates/evm/jest.config.ts +8 -0
- package/templates/evm/package.json +2 -1
- package/templates/evm/src/processor.ts +3 -3
- package/templates/evm/tsconfig.json +4 -3
- package/templates/raw/jest.config.ts +8 -0
- package/templates/raw/package.json +2 -1
- package/templates/raw/tsconfig.json +4 -3
- package/templates/solana/jest.config.ts +8 -0
- package/templates/solana/package.json +3 -2
- package/templates/solana/src/processor.ts +1 -1
- package/templates/solana/tsconfig.json +4 -3
- package/LICENSE +0 -55
- package/lib/upload.d.ts +0 -2
- package/lib/upload.js +0 -189
- package/lib/upload.js.map +0 -1
- package/lib/webpack.config.js +0 -50
- package/src/webpack.config.js +0 -50
- package/templates/aptos/jest.config.js +0 -7
- package/templates/evm/jest.config.js +0 -7
- package/templates/raw/jest.config.js +0 -7
- package/templates/raw/yarn.lock +0 -4095
- package/templates/solana/jest.config.js +0 -7
- package/templates/solana/yarn.lock +0 -4918
package/lib/build.js
CHANGED
|
@@ -1,14 +1,19 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
};
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
const
|
|
8
|
-
|
|
9
|
-
const
|
|
10
|
-
|
|
11
|
-
|
|
1
|
+
import chalk from 'chalk';
|
|
2
|
+
import path from 'path';
|
|
3
|
+
import fs from 'fs';
|
|
4
|
+
import { exec } from 'child_process';
|
|
5
|
+
import * as process from 'process';
|
|
6
|
+
import { createRequire } from 'module';
|
|
7
|
+
const require = createRequire(import.meta.url);
|
|
8
|
+
function getPackageRoot(pkgId) {
|
|
9
|
+
const m = require.resolve(pkgId);
|
|
10
|
+
let dir = path.dirname(m);
|
|
11
|
+
while (!fs.existsSync(path.join(dir, 'package.json'))) {
|
|
12
|
+
dir = path.dirname(dir);
|
|
13
|
+
}
|
|
14
|
+
return dir;
|
|
15
|
+
}
|
|
16
|
+
export async function buildProcessor(onlyGen) {
|
|
12
17
|
if (!onlyGen) {
|
|
13
18
|
await installDeps();
|
|
14
19
|
}
|
|
@@ -17,24 +22,30 @@ async function buildProcessor(onlyGen) {
|
|
|
17
22
|
await buildProcessorForTarget(onlyGen);
|
|
18
23
|
// }
|
|
19
24
|
if (!onlyGen) {
|
|
20
|
-
|
|
25
|
+
let WEBPACK_CONFIG;
|
|
26
|
+
try {
|
|
27
|
+
WEBPACK_CONFIG = path.resolve(getPackageRoot('@sentio/sdk'), 'lib/tsup.config.ts');
|
|
28
|
+
}
|
|
29
|
+
catch (e) {
|
|
30
|
+
console.error(chalk.red("Wrong CLI version for sdk, can't find tsup.config.ts"));
|
|
31
|
+
process.exit(1);
|
|
32
|
+
}
|
|
21
33
|
await execStep('yarn tsc -p .', 'Compile');
|
|
22
|
-
await execStep('yarn
|
|
34
|
+
await execStep('yarn tsup --config=' + WEBPACK_CONFIG, 'Packaging');
|
|
23
35
|
}
|
|
24
36
|
}
|
|
25
|
-
exports.buildProcessor = buildProcessor;
|
|
26
37
|
async function buildProcessorForTarget(onlyGen) {
|
|
27
|
-
await codeGenEthersProcessor(
|
|
38
|
+
await codeGenEthersProcessor(path.join('abis', 'evm'));
|
|
28
39
|
try {
|
|
29
|
-
//
|
|
30
|
-
const
|
|
31
|
-
|
|
40
|
+
// @ts-ignore dynamic import
|
|
41
|
+
const codegen = await import('@sentio/sdk-solana/codegen');
|
|
42
|
+
codegen.codeGenSolanaProcessor(path.join('abis', 'solana'));
|
|
32
43
|
}
|
|
33
44
|
catch (e) { }
|
|
34
45
|
try {
|
|
35
|
-
//
|
|
36
|
-
const
|
|
37
|
-
|
|
46
|
+
// @ts-ignore dynamic import
|
|
47
|
+
const codegen = await import('@sentio/sdk-aptos/codegen');
|
|
48
|
+
codegen.codeGenAptosProcessor(path.join('abis', 'aptos'));
|
|
38
49
|
}
|
|
39
50
|
catch (e) { }
|
|
40
51
|
if (onlyGen) {
|
|
@@ -44,12 +55,12 @@ async function buildProcessorForTarget(onlyGen) {
|
|
|
44
55
|
async function installDeps() {
|
|
45
56
|
await execStep('yarn install --ignore-scripts', 'Yarn Install');
|
|
46
57
|
}
|
|
47
|
-
async function codeGenEthersProcessor(abisDir, ETHERS_TARGET =
|
|
48
|
-
if (!
|
|
58
|
+
export async function codeGenEthersProcessor(abisDir, ETHERS_TARGET = path.resolve(getPackageRoot('@sentio/sdk'), 'lib/target-ethers-sentio/index.cjs'), outDir = 'src/types/internal') {
|
|
59
|
+
if (!fs.existsSync(abisDir)) {
|
|
49
60
|
return;
|
|
50
61
|
}
|
|
51
62
|
let haveJson = false;
|
|
52
|
-
const files =
|
|
63
|
+
const files = fs.readdirSync(abisDir);
|
|
53
64
|
for (const file of files) {
|
|
54
65
|
if (file.toLowerCase().endsWith('.json')) {
|
|
55
66
|
haveJson = true;
|
|
@@ -59,16 +70,15 @@ async function codeGenEthersProcessor(abisDir, ETHERS_TARGET = path_1.default.di
|
|
|
59
70
|
if (!haveJson) {
|
|
60
71
|
return;
|
|
61
72
|
}
|
|
62
|
-
console.log(
|
|
73
|
+
console.log(chalk.green('Generated Types for EVM'));
|
|
63
74
|
// TODO this will fail during postinstall, need to locate real typechain path
|
|
64
|
-
await execStep('yarn typechain --target ' + ETHERS_TARGET + ` --out-dir ${outDir} ${
|
|
75
|
+
await execStep('yarn typechain --target ' + ETHERS_TARGET + ` --out-dir ${outDir} ${path.join(abisDir, '*.json')}`, 'Type definitions gen');
|
|
65
76
|
}
|
|
66
|
-
exports.codeGenEthersProcessor = codeGenEthersProcessor;
|
|
67
77
|
async function execStep(cmd, stepName) {
|
|
68
|
-
const child =
|
|
69
|
-
console.log(
|
|
78
|
+
const child = exec(cmd);
|
|
79
|
+
console.log(chalk.blue(stepName + ' begin'));
|
|
70
80
|
if (!child.stdout || !child.stderr) {
|
|
71
|
-
console.error(
|
|
81
|
+
console.error(chalk.red(stepName + ' failed'));
|
|
72
82
|
process.exit(1);
|
|
73
83
|
}
|
|
74
84
|
child.stdout.pipe(process.stdout);
|
|
@@ -77,10 +87,10 @@ async function execStep(cmd, stepName) {
|
|
|
77
87
|
child.on('close', resolve);
|
|
78
88
|
});
|
|
79
89
|
if (child.exitCode) {
|
|
80
|
-
console.error(
|
|
90
|
+
console.error(chalk.red(stepName + ' failed'));
|
|
81
91
|
process.exit(child.exitCode);
|
|
82
92
|
}
|
|
83
|
-
console.log(
|
|
93
|
+
console.log(chalk.blue(stepName + ' success'));
|
|
84
94
|
console.log();
|
|
85
95
|
}
|
|
86
96
|
//# sourceMappingURL=build.js.map
|
package/lib/build.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"build.js","sourceRoot":"","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,KAAK,CAAC,QAAQ,EAAE;QAClB,OAAO,CAAC,KAAK,CAAC,KAAK,CAAC,GAAG,CAAC,QAAQ,GAAG,SAAS,CAAC,CAAC,CAAA;QAC9C,OAAO,CAAC,IAAI,CAAC,KAAK,CAAC,QAAQ,CAAC,CAAA;KAC7B;IACD,OAAO,CAAC,GAAG,CAAC,KAAK,CAAC,IAAI,CAAC,QAAQ,GAAG,UAAU,CAAC,CAAC,CAAA;IAC9C,OAAO,CAAC,GAAG,EAAE,CAAA;AACf,CAAC","sourcesContent":["import chalk from 'chalk'\nimport path from 'path'\nimport fs from 'fs'\nimport { exec } from 'child_process'\nimport * as process from 'process'\nimport { createRequire } from 'module'\nconst require = createRequire(import.meta.url)\n\nfunction getPackageRoot(pkgId: string): string {\n const m = require.resolve(pkgId)\n\n let dir = path.dirname(m)\n while (!fs.existsSync(path.join(dir, 'package.json'))) {\n dir = path.dirname(dir)\n }\n\n return dir\n}\n\nexport async function buildProcessor(onlyGen: boolean) {\n if (!onlyGen) {\n await installDeps()\n }\n\n // targets.forEach(async (target) => await buildProcessorForTarget(onlyGen, target))\n // for (const target) {\n await buildProcessorForTarget(onlyGen)\n // }\n\n if (!onlyGen) {\n let WEBPACK_CONFIG: string\n try {\n WEBPACK_CONFIG = path.resolve(getPackageRoot('@sentio/sdk'), 'lib/tsup.config.ts')\n } catch (e) {\n console.error(chalk.red(\"Wrong CLI version for sdk, can't find tsup.config.ts\"))\n process.exit(1)\n }\n await execStep('yarn tsc -p .', 'Compile')\n await execStep('yarn tsup --config=' + WEBPACK_CONFIG, 'Packaging')\n }\n}\n\nasync function buildProcessorForTarget(onlyGen: boolean) {\n await codeGenEthersProcessor(path.join('abis', 'evm'))\n\n try {\n // @ts-ignore dynamic import\n const codegen = await import('@sentio/sdk-solana/codegen')\n codegen.codeGenSolanaProcessor(path.join('abis', 'solana'))\n } catch (e) {}\n\n try {\n // @ts-ignore dynamic import\n const codegen = await import('@sentio/sdk-aptos/codegen')\n codegen.codeGenAptosProcessor(path.join('abis', 'aptos'))\n } catch (e) {}\n\n if (onlyGen) {\n return\n }\n}\n\nasync function installDeps() {\n await execStep('yarn install --ignore-scripts', 'Yarn Install')\n}\n\nexport async function codeGenEthersProcessor(\n abisDir: string,\n ETHERS_TARGET = path.resolve(getPackageRoot('@sentio/sdk'), 'lib/target-ethers-sentio/index.cjs'),\n outDir = 'src/types/internal'\n) {\n if (!fs.existsSync(abisDir)) {\n return\n }\n\n let haveJson = false\n const files = fs.readdirSync(abisDir)\n for (const file of files) {\n if (file.toLowerCase().endsWith('.json')) {\n haveJson = true\n break\n }\n }\n if (!haveJson) {\n return\n }\n\n console.log(chalk.green('Generated Types for EVM'))\n\n // TODO this will fail during postinstall, need to locate real typechain path\n await execStep(\n 'yarn typechain --target ' + ETHERS_TARGET + ` --out-dir ${outDir} ${path.join(abisDir, '*.json')}`,\n 'Type definitions gen'\n )\n}\n\nasync function execStep(cmd: string, stepName: string) {\n const child = exec(cmd)\n console.log(chalk.blue(stepName + ' begin'))\n\n if (!child.stdout || !child.stderr) {\n console.error(chalk.red(stepName + ' failed'))\n process.exit(1)\n }\n\n child.stdout.pipe(process.stdout)\n child.stderr.pipe(process.stderr)\n\n await new Promise((resolve) => {\n child.on('close', resolve)\n })\n\n if (child.exitCode) {\n console.error(chalk.red(stepName + ' failed'))\n process.exit(child.exitCode)\n }\n console.log(chalk.blue(stepName + ' success'))\n console.log()\n}\n"]}
|
package/lib/cli.js
CHANGED
|
@@ -1,23 +1,17 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
const upload_1 = require("./upload");
|
|
14
|
-
const chalk_1 = __importDefault(require("chalk"));
|
|
15
|
-
const build_1 = require("./build");
|
|
16
|
-
const run_create_1 = require("./commands/run-create");
|
|
17
|
-
const run_version_1 = require("./commands/run-version");
|
|
18
|
-
const run_login_1 = require("./commands/run-login");
|
|
2
|
+
import commandLineArgs from 'command-line-args';
|
|
3
|
+
import commandLineUsage from 'command-line-usage';
|
|
4
|
+
import fs from 'fs';
|
|
5
|
+
import path from 'path';
|
|
6
|
+
import yaml from 'js-yaml';
|
|
7
|
+
import chalk from 'chalk';
|
|
8
|
+
import { buildProcessor } from './build.js';
|
|
9
|
+
import { runCreate } from './commands/run-create.js';
|
|
10
|
+
import { runVersion } from './commands/run-version.js';
|
|
11
|
+
import { runLogin } from './commands/run-login.js';
|
|
12
|
+
import { runUpload } from './commands/run-upload.js';
|
|
19
13
|
const mainDefinitions = [{ name: 'command', defaultOption: true }];
|
|
20
|
-
const mainOptions = (
|
|
14
|
+
const mainOptions = commandLineArgs(mainDefinitions, {
|
|
21
15
|
stopAtFirstUnknown: true,
|
|
22
16
|
});
|
|
23
17
|
const argv = mainOptions._unknown || [];
|
|
@@ -25,13 +19,13 @@ if (!mainOptions.command) {
|
|
|
25
19
|
usage();
|
|
26
20
|
}
|
|
27
21
|
if (mainOptions.command === 'login') {
|
|
28
|
-
|
|
22
|
+
runLogin(argv);
|
|
29
23
|
}
|
|
30
24
|
else if (mainOptions.command === 'create') {
|
|
31
|
-
|
|
25
|
+
runCreate(argv);
|
|
32
26
|
}
|
|
33
27
|
else if (mainOptions.command === 'version') {
|
|
34
|
-
|
|
28
|
+
runVersion(argv);
|
|
35
29
|
}
|
|
36
30
|
else {
|
|
37
31
|
// For all the commands that need read project configs
|
|
@@ -40,20 +34,20 @@ else {
|
|
|
40
34
|
let processorConfig = { host: '', project: '', build: true, debug: false };
|
|
41
35
|
// Fist step, read from project yaml file
|
|
42
36
|
try {
|
|
43
|
-
console.log(
|
|
37
|
+
console.log(chalk.blue('Loading Process config'));
|
|
44
38
|
// TODO correctly located sentio.yaml
|
|
45
39
|
const pwd = process.cwd();
|
|
46
|
-
const packageJson =
|
|
47
|
-
if (!
|
|
40
|
+
const packageJson = path.join(pwd, 'package.json');
|
|
41
|
+
if (!fs.existsSync(packageJson)) {
|
|
48
42
|
console.error('package.json not found, please run this command in the root of your project');
|
|
49
43
|
process.exit(1);
|
|
50
44
|
}
|
|
51
|
-
const yamlPath =
|
|
52
|
-
if (!
|
|
45
|
+
const yamlPath = path.join(pwd, 'sentio.yaml');
|
|
46
|
+
if (!fs.existsSync(yamlPath)) {
|
|
53
47
|
console.error('sentio.yaml not found, please create one according to: TODO docs');
|
|
54
48
|
process.exit(1);
|
|
55
49
|
}
|
|
56
|
-
processorConfig =
|
|
50
|
+
processorConfig = yaml.load(fs.readFileSync('sentio.yaml', 'utf8'));
|
|
57
51
|
if (!processorConfig.project === undefined) {
|
|
58
52
|
console.error('Config yaml must have contain a valid project identifier');
|
|
59
53
|
process.exit(1);
|
|
@@ -84,85 +78,20 @@ else {
|
|
|
84
78
|
process.exit(1);
|
|
85
79
|
}
|
|
86
80
|
if (mainOptions.command === 'upload') {
|
|
87
|
-
|
|
88
|
-
{
|
|
89
|
-
name: 'help',
|
|
90
|
-
alias: 'h',
|
|
91
|
-
type: Boolean,
|
|
92
|
-
description: 'Display this usage guide.',
|
|
93
|
-
},
|
|
94
|
-
{
|
|
95
|
-
name: 'api-key',
|
|
96
|
-
type: String,
|
|
97
|
-
description: '(Optional) Manually provide API key rather than use saved credential',
|
|
98
|
-
},
|
|
99
|
-
{
|
|
100
|
-
name: 'host',
|
|
101
|
-
description: '(Optional) Override Sentio Host name',
|
|
102
|
-
type: String,
|
|
103
|
-
},
|
|
104
|
-
{
|
|
105
|
-
name: 'owner',
|
|
106
|
-
description: '(Optional) Override Project owner',
|
|
107
|
-
type: String,
|
|
108
|
-
},
|
|
109
|
-
{
|
|
110
|
-
name: 'nobuild',
|
|
111
|
-
description: '(Optional) Skip build & pack file before uploading, default false',
|
|
112
|
-
type: Boolean,
|
|
113
|
-
},
|
|
114
|
-
{
|
|
115
|
-
name: 'debug',
|
|
116
|
-
description: '(Optional) Set driver logging level to debug',
|
|
117
|
-
type: Boolean,
|
|
118
|
-
},
|
|
119
|
-
];
|
|
120
|
-
const options = (0, command_line_args_1.default)(optionDefinitions, { argv });
|
|
121
|
-
if (options.help) {
|
|
122
|
-
const usage = (0, command_line_usage_1.default)([
|
|
123
|
-
{
|
|
124
|
-
header: 'Sentio upload',
|
|
125
|
-
content: 'sentio upload',
|
|
126
|
-
},
|
|
127
|
-
{
|
|
128
|
-
header: 'Options',
|
|
129
|
-
optionList: optionDefinitions,
|
|
130
|
-
},
|
|
131
|
-
]);
|
|
132
|
-
console.log(usage);
|
|
133
|
-
}
|
|
134
|
-
else {
|
|
135
|
-
if (options.host) {
|
|
136
|
-
processorConfig.host = options.host;
|
|
137
|
-
}
|
|
138
|
-
if (options.nobuild) {
|
|
139
|
-
processorConfig.build = false;
|
|
140
|
-
}
|
|
141
|
-
if (options.debug) {
|
|
142
|
-
processorConfig.debug = true;
|
|
143
|
-
}
|
|
144
|
-
(0, config_1.finalizeHost)(processorConfig);
|
|
145
|
-
(0, config_1.FinalizeProjectName)(processorConfig, options.owner);
|
|
146
|
-
console.log(processorConfig);
|
|
147
|
-
let apiOverride = undefined;
|
|
148
|
-
if (options['api-key']) {
|
|
149
|
-
apiOverride = options['api-key'];
|
|
150
|
-
}
|
|
151
|
-
(0, upload_1.uploadFile)(processorConfig, apiOverride);
|
|
152
|
-
}
|
|
81
|
+
runUpload(processorConfig, argv);
|
|
153
82
|
}
|
|
154
83
|
else if (mainOptions.command === 'build') {
|
|
155
|
-
|
|
84
|
+
buildProcessor(false);
|
|
156
85
|
}
|
|
157
86
|
else if (mainOptions.command === 'gen') {
|
|
158
|
-
|
|
87
|
+
buildProcessor(true);
|
|
159
88
|
}
|
|
160
89
|
else {
|
|
161
90
|
usage();
|
|
162
91
|
}
|
|
163
92
|
}
|
|
164
93
|
function usage() {
|
|
165
|
-
const usage = (
|
|
94
|
+
const usage = commandLineUsage([
|
|
166
95
|
{
|
|
167
96
|
header: 'Sentio',
|
|
168
97
|
content: 'Login & Manage your project files to Sentio.',
|
package/lib/cli.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"cli.js","sourceRoot":"","sources":["../src/cli.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"cli.js","sourceRoot":"","sources":["../src/cli.ts"],"names":[],"mappings":";AAEA,OAAO,eAAe,MAAM,mBAAmB,CAAA;AAC/C,OAAO,gBAAgB,MAAM,oBAAoB,CAAA;AACjD,OAAO,EAAE,MAAM,IAAI,CAAA;AACnB,OAAO,IAAI,MAAM,MAAM,CAAA;AAEvB,OAAO,IAAI,MAAM,SAAS,CAAA;AAE1B,OAAO,KAAK,MAAM,OAAO,CAAA;AACzB,OAAO,EAAE,cAAc,EAAE,MAAM,YAAY,CAAA;AAC3C,OAAO,EAAE,SAAS,EAAE,MAAM,0BAA0B,CAAA;AACpD,OAAO,EAAE,UAAU,EAAE,MAAM,2BAA2B,CAAA;AACtD,OAAO,EAAE,QAAQ,EAAE,MAAM,yBAAyB,CAAA;AAClD,OAAO,EAAE,SAAS,EAAE,MAAM,0BAA0B,CAAA;AAEpD,MAAM,eAAe,GAAG,CAAC,EAAE,IAAI,EAAE,SAAS,EAAE,aAAa,EAAE,IAAI,EAAE,CAAC,CAAA;AAClE,MAAM,WAAW,GAAG,eAAe,CAAC,eAAe,EAAE;IACnD,kBAAkB,EAAE,IAAI;CACzB,CAAC,CAAA;AACF,MAAM,IAAI,GAAG,WAAW,CAAC,QAAQ,IAAI,EAAE,CAAA;AAEvC,IAAI,CAAC,WAAW,CAAC,OAAO,EAAE;IACxB,KAAK,EAAE,CAAA;CACR;AAED,IAAI,WAAW,CAAC,OAAO,KAAK,OAAO,EAAE;IACnC,QAAQ,CAAC,IAAI,CAAC,CAAA;CACf;KAAM,IAAI,WAAW,CAAC,OAAO,KAAK,QAAQ,EAAE;IAC3C,SAAS,CAAC,IAAI,CAAC,CAAA;CAChB;KAAM,IAAI,WAAW,CAAC,OAAO,KAAK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AAC","sourcesContent":["#!/usr/bin/env node\n\nimport commandLineArgs from 'command-line-args'\nimport commandLineUsage from 'command-line-usage'\nimport fs from 'fs'\nimport path from 'path'\n\nimport yaml from 'js-yaml'\nimport { SentioProjectConfig } from './config.js'\nimport chalk from 'chalk'\nimport { buildProcessor } from './build.js'\nimport { runCreate } from './commands/run-create.js'\nimport { runVersion } from './commands/run-version.js'\nimport { runLogin } from './commands/run-login.js'\nimport { runUpload } from './commands/run-upload.js'\n\nconst mainDefinitions = [{ name: 'command', defaultOption: true }]\nconst mainOptions = commandLineArgs(mainDefinitions, {\n stopAtFirstUnknown: true,\n})\nconst argv = mainOptions._unknown || []\n\nif (!mainOptions.command) {\n usage()\n}\n\nif (mainOptions.command === 'login') {\n runLogin(argv)\n} else if (mainOptions.command === 'create') {\n runCreate(argv)\n} else if (mainOptions.command === 'version') {\n runVersion(argv)\n} else {\n // For all the commands that need read project configs\n // TODO move them to their own modules\n\n // Process configs\n let processorConfig: SentioProjectConfig = { host: '', project: '', build: true, debug: false }\n // Fist step, read from project yaml file\n try {\n console.log(chalk.blue('Loading Process config'))\n // TODO correctly located sentio.yaml\n const pwd = process.cwd()\n const packageJson = path.join(pwd, 'package.json')\n if (!fs.existsSync(packageJson)) {\n console.error('package.json not found, please run this command in the root of your project')\n process.exit(1)\n }\n\n const yamlPath = path.join(pwd, 'sentio.yaml')\n if (!fs.existsSync(yamlPath)) {\n console.error('sentio.yaml not found, please create one according to: TODO docs')\n process.exit(1)\n }\n\n processorConfig = yaml.load(fs.readFileSync('sentio.yaml', 'utf8')) as SentioProjectConfig\n if (!processorConfig.project === undefined) {\n console.error('Config yaml must have contain a valid project identifier')\n process.exit(1)\n }\n if (processorConfig.build === undefined) {\n processorConfig.build = true\n }\n if (!processorConfig.host) {\n processorConfig.host = 'prod'\n }\n if (processorConfig.debug === undefined) {\n processorConfig.debug = false\n }\n\n // if (!processorConfig.source) {\n // processorConfig.source = 'src/processor.ts'\n // }\n // if (!processorConfig.targets) {\n // console.warn('targets is not defined, use EVM as the default target')\n // processorConfig.targets = []\n // }\n // if (processorConfig.targets.length === 0) {\n // // By default evm\n // processorConfig.targets.push({ chain: EVM })\n // }\n } catch (e) {\n console.error(e)\n process.exit(1)\n }\n\n if (mainOptions.command === 'upload') {\n runUpload(processorConfig, argv)\n } else if (mainOptions.command === 'build') {\n buildProcessor(false)\n } else if (mainOptions.command === 'gen') {\n buildProcessor(true)\n } else {\n usage()\n }\n}\n\nfunction usage() {\n const usage = commandLineUsage([\n {\n header: 'Sentio',\n content: 'Login & Manage your project files to Sentio.',\n },\n {\n header: 'Usage',\n content: [\n 'sentio <command> --help\\t\\tshow detail usage of specific command',\n 'sentio login\\t\\t\\t\\tlogin to sentio',\n 'sentio create\\t\\t\\t\\tcreate a template project',\n 'sentio upload\\t\\t\\t\\tbuild and upload processor to sentio',\n 'sentio gen\\t\\t\\t\\tgenerate abi',\n 'sentio build\\t\\t\\t\\tgenerate abi and build',\n 'sentio version\\t\\t\\tcurrent cli version',\n ],\n },\n ])\n console.log(usage)\n process.exit(1)\n}\n"]}
|
|
@@ -1,103 +1,79 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
o[k2] = m[k];
|
|
12
|
-
}));
|
|
13
|
-
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
-
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
-
}) : function(o, v) {
|
|
16
|
-
o["default"] = v;
|
|
17
|
-
});
|
|
18
|
-
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
-
if (mod && mod.__esModule) return mod;
|
|
20
|
-
var result = {};
|
|
21
|
-
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
-
__setModuleDefault(result, mod);
|
|
23
|
-
return result;
|
|
24
|
-
};
|
|
25
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
26
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
27
|
-
};
|
|
28
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
29
|
-
exports.startServer = void 0;
|
|
30
|
-
const express_1 = __importDefault(require("express"));
|
|
31
|
-
const config_1 = require("../config");
|
|
32
|
-
const url_1 = __importStar(require("url"));
|
|
33
|
-
const node_fetch_1 = __importDefault(require("node-fetch"));
|
|
34
|
-
const utils_1 = require("../utils");
|
|
35
|
-
const key_1 = require("../key");
|
|
36
|
-
const chalk_1 = __importDefault(require("chalk"));
|
|
37
|
-
const os_1 = __importDefault(require("os"));
|
|
38
|
-
const crypto = __importStar(require("crypto"));
|
|
39
|
-
const app = (0, express_1.default)();
|
|
1
|
+
import express from 'express';
|
|
2
|
+
import { getAuthConfig, getFinalizedHost } from '../config.js';
|
|
3
|
+
import url, { URL } from 'url';
|
|
4
|
+
import fetch from 'node-fetch';
|
|
5
|
+
import { getCliVersion } from '../utils.js';
|
|
6
|
+
import { WriteKey } from '../key.js';
|
|
7
|
+
import chalk from 'chalk';
|
|
8
|
+
import os from 'os';
|
|
9
|
+
import * as crypto from 'crypto';
|
|
10
|
+
const app = express();
|
|
40
11
|
let server;
|
|
41
12
|
let authParams;
|
|
42
|
-
function startServer(params) {
|
|
13
|
+
export function startServer(params) {
|
|
43
14
|
authParams = params;
|
|
44
15
|
server = app.listen(params.serverPort);
|
|
45
16
|
}
|
|
46
|
-
exports.startServer = startServer;
|
|
47
17
|
app.get('/callback', async (req, res) => {
|
|
48
|
-
|
|
49
|
-
|
|
18
|
+
const fail = function (...args) {
|
|
19
|
+
console.error(chalk.red(args));
|
|
20
|
+
res.end(args.toString());
|
|
21
|
+
server.close();
|
|
22
|
+
};
|
|
23
|
+
const host = getFinalizedHost(authParams.sentioHost);
|
|
50
24
|
const code = req.query.code;
|
|
51
25
|
if (!code || code.length == 0) {
|
|
26
|
+
fail('Failed to get authorization code');
|
|
52
27
|
return;
|
|
53
28
|
}
|
|
54
29
|
// exchange token
|
|
55
30
|
const tokenResRaw = await getToken(host, code);
|
|
56
31
|
if (!tokenResRaw.ok) {
|
|
57
|
-
|
|
32
|
+
fail(`Failed to get access token: ${tokenResRaw.status} ${tokenResRaw.statusText}`);
|
|
58
33
|
return;
|
|
59
34
|
}
|
|
60
|
-
const tokenRes = await tokenResRaw.json();
|
|
61
|
-
const accessToken = tokenRes
|
|
35
|
+
const tokenRes = (await tokenResRaw.json());
|
|
36
|
+
const accessToken = tokenRes.access_token;
|
|
62
37
|
// check if the account is ready
|
|
63
38
|
const userResRaw = await getUser(host, accessToken);
|
|
64
39
|
if (!userResRaw.ok) {
|
|
65
40
|
if (userResRaw.status == 401) {
|
|
66
|
-
|
|
41
|
+
fail('The account does not exist, please sign up on sentio first');
|
|
67
42
|
}
|
|
68
43
|
else {
|
|
69
|
-
|
|
44
|
+
fail(`Failed to get user info: ${userResRaw.status} ${userResRaw.statusText}`);
|
|
70
45
|
}
|
|
71
46
|
return;
|
|
72
47
|
}
|
|
73
|
-
const userRes = await userResRaw.json();
|
|
48
|
+
const userRes = (await userResRaw.json());
|
|
74
49
|
if (!userRes.emailVerified) {
|
|
75
|
-
|
|
50
|
+
fail('Your account is not verified, please verify your email first');
|
|
76
51
|
return;
|
|
77
52
|
}
|
|
78
53
|
// create API key
|
|
79
|
-
const apiKeyName = `${
|
|
54
|
+
const apiKeyName = `${os.hostname()}-${crypto.randomBytes(4).toString('hex')}`;
|
|
80
55
|
const createApiKeyResRaw = await createApiKey(host, apiKeyName, 'sdk_generated', accessToken);
|
|
81
56
|
if (!createApiKeyResRaw.ok) {
|
|
82
|
-
|
|
57
|
+
fail(`Failed to create API key: ${createApiKeyResRaw.status} ${createApiKeyResRaw.statusText}`);
|
|
83
58
|
return;
|
|
84
59
|
}
|
|
85
|
-
const createApiKeyRes = await createApiKeyResRaw.json();
|
|
86
|
-
const apiKey = createApiKeyRes
|
|
87
|
-
|
|
88
|
-
|
|
60
|
+
const createApiKeyRes = (await createApiKeyResRaw.json());
|
|
61
|
+
const apiKey = createApiKeyRes.key;
|
|
62
|
+
WriteKey(host, apiKey);
|
|
63
|
+
res.end('Login success, please go back to CLI to continue');
|
|
64
|
+
console.log(chalk.green('Login success, new API key: ' + apiKey));
|
|
89
65
|
server.close();
|
|
90
66
|
});
|
|
91
67
|
async function getToken(host, code) {
|
|
92
|
-
const authConf =
|
|
93
|
-
const params = new
|
|
68
|
+
const authConf = getAuthConfig(host);
|
|
69
|
+
const params = new url.URLSearchParams({
|
|
94
70
|
grant_type: 'authorization_code',
|
|
95
71
|
client_id: authConf.clientId,
|
|
96
72
|
code_verifier: authParams.codeVerifier,
|
|
97
73
|
code: code,
|
|
98
74
|
redirect_uri: `http://localhost:${authParams.serverPort}/callback`,
|
|
99
75
|
});
|
|
100
|
-
return (
|
|
76
|
+
return fetch(`${authConf.domain}/oauth/token`, {
|
|
101
77
|
method: 'POST',
|
|
102
78
|
headers: {
|
|
103
79
|
'Content-Type': 'application/x-www-form-urlencoded',
|
|
@@ -106,12 +82,12 @@ async function getToken(host, code) {
|
|
|
106
82
|
});
|
|
107
83
|
}
|
|
108
84
|
async function createApiKey(host, name, source, accessToken) {
|
|
109
|
-
const createApiKeyUrl = new
|
|
110
|
-
return (
|
|
85
|
+
const createApiKeyUrl = new URL('/api/v1/keys', host);
|
|
86
|
+
return fetch(createApiKeyUrl.href, {
|
|
111
87
|
method: 'POST',
|
|
112
88
|
headers: {
|
|
113
89
|
Authorization: 'Bearer ' + accessToken,
|
|
114
|
-
version:
|
|
90
|
+
version: getCliVersion(),
|
|
115
91
|
},
|
|
116
92
|
body: JSON.stringify({
|
|
117
93
|
name: name,
|
|
@@ -121,12 +97,12 @@ async function createApiKey(host, name, source, accessToken) {
|
|
|
121
97
|
});
|
|
122
98
|
}
|
|
123
99
|
async function getUser(host, accessToken) {
|
|
124
|
-
const getUserUrl = new
|
|
125
|
-
return (
|
|
100
|
+
const getUserUrl = new URL('/api/v1/users', host);
|
|
101
|
+
return fetch(getUserUrl.href, {
|
|
126
102
|
method: 'GET',
|
|
127
103
|
headers: {
|
|
128
104
|
Authorization: 'Bearer ' + accessToken,
|
|
129
|
-
version:
|
|
105
|
+
version: getCliVersion(),
|
|
130
106
|
},
|
|
131
107
|
});
|
|
132
108
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"login-server.js","sourceRoot":"","sources":["../../src/commands/login-server.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"login-server.js","sourceRoot":"","sources":["../../src/commands/login-server.ts"],"names":[],"mappings":"AAAA,OAAO,OAAO,MAAM,SAAS,CAAA;AAC7B,OAAO,EAAE,aAAa,EAAE,gBAAgB,EAAE,MAAM,cAAc,CAAA;AAC9D,OAAO,GAAG,EAAE,EAAE,GAAG,EAAE,MAAM,KAAK,CAAA;AAC9B,OAAO,KAAK,MAAM,YAAY,CAAA;AAC9B,OAAO,EAAE,aAAa,EAAE,MAAM,aAAa,CAAA;AAC3C,OAAO,EAAE,QAAQ,EAAE,MAAM,WAAW,CAAA;AACpC,OAAO,KAAK,MAAM,OAAO,CAAA;AAEzB,OAAO,EAAE,MAAM,IAAI,CAAA;AACnB,OAAO,KAAK,MAAM,MAAM,QAAQ,CAAA;AAQhC,MAAM,GAAG,GAAG,OAAO,EAAE,CAAA;AAErB,IAAI,MAAmB,CAAA;AACvB,IAAI,UAAsB,CAAA;AAE1B,MAAM,UAAU,WAAW,CAAC,MAAkB;IAC5C,UAAU,GAAG,MAAM,CAAA;IACnB,MAAM,GAAG,GAAG,CAAC,MAAM,CAAC,MAAM,CAAC,UAAU,CAAC,CAAA;AACxC,CAAC;AAED,GAAG,CAAC,GAAG,CAAC,WAAW,EAAE,KAAK,EAAE,GAAG,EAAE,GAAG,EAAE,EAAE;IACtC,MAAM,IAAI,GAAG,UAAU,GAAG,IAAW;QACnC,OAA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,CAAC,GAAG,QAAQ,CAAC,MAAM,cAAc,EAAE;QAC7C,MAAM,EAAE,MAAM;QACd,OAAO,EAAE;YACP,cAAc,EAAE,mCAAmC;SACpD;QACD,IAAI,EAAE,MAAM,CAAC,QAAQ,EAAE;KACxB,CAAC,CAAA;AACJ,CAAC;AAED,KAAK,UAAU,YAAY,CAAC,IAAY,EAAE,IAAY,EAAE,MAAc,EAAE,WAAmB;IACzF,MAAM,eAAe,GAAG,IAAI,GAAG,CAAC,cAAc,EAAE,IAAI,CAAC,CAAA;IACrD,OAAO,KAAK,CAAC,eAAe,CAAC,IAAI,EAAE;QACjC,MAAM,EAAE,MAAM;QACd,OAAO,EAAE;YACP,aAAa,EAAE,SAAS,GAAG,WAAW;YACtC,OAAO,EAAE,aAAa,EAAE;SACzB;QACD,IAAI,EAAE,IAAI,CAAC,SAAS,CAAC;YACnB,IAAI,EAAE,IAAI;YACV,MAAM,EAAE,CAAC,eAAe,CAAC;YACzB,MAAM,EAAE,MAAM;SACf,CAAC;KACH,CAAC,CAAA;AACJ,CAAC;AAED,KAAK,UAAU,OAAO,CAAC,IAAY,EAAE,WAAmB;IACtD,MAAM,UAAU,GAAG,IAAI,GAAG,CAAC,eAAe,EAAE,IAAI,CAAC,CAAA;IACjD,OAAO,KAAK,CAAC,UAAU,CAAC,IAAI,EAAE;QAC5B,MAAM,EAAE,KAAK;QACb,OAAO,EAAE;YACP,aAAa,EAAE,SAAS,GAAG,WAAW;YACtC,OAAO,EAAE,aAAa,EAAE;SACzB;KACF,CAAC,CAAA;AACJ,CAAC","sourcesContent":["import express from 'express'\nimport { getAuthConfig, getFinalizedHost } from '../config.js'\nimport url, { URL } from 'url'\nimport fetch from 'node-fetch'\nimport { getCliVersion } from '../utils.js'\nimport { WriteKey } from '../key.js'\nimport chalk from 'chalk'\nimport http from 'http'\nimport os from 'os'\nimport * as crypto from 'crypto'\n\ninterface AuthParams {\n serverPort: number\n sentioHost: string\n codeVerifier: string\n}\n\nconst app = express()\n\nlet server: http.Server\nlet authParams: AuthParams\n\nexport function startServer(params: AuthParams) {\n authParams = params\n server = app.listen(params.serverPort)\n}\n\napp.get('/callback', async (req, res) => {\n const fail = function (...args: any[]) {\n console.error(chalk.red(args))\n res.end(args.toString())\n server.close()\n }\n\n const host = getFinalizedHost(authParams.sentioHost)\n const code = req.query.code\n if (!code || (code as string).length == 0) {\n fail('Failed to get authorization code')\n return\n }\n\n // exchange token\n const tokenResRaw = await getToken(host, code as string)\n if (!tokenResRaw.ok) {\n fail(`Failed to get access token: ${tokenResRaw.status} ${tokenResRaw.statusText}`)\n return\n }\n const tokenRes = (await tokenResRaw.json()) as { access_token: string }\n const accessToken = tokenRes.access_token\n\n // check if the account is ready\n const userResRaw = await getUser(host, accessToken)\n if (!userResRaw.ok) {\n if (userResRaw.status == 401) {\n fail('The account does not exist, please sign up on sentio first')\n } else {\n fail(`Failed to get user info: ${userResRaw.status} ${userResRaw.statusText}`)\n }\n return\n }\n const userRes = (await userResRaw.json()) as { emailVerified: boolean }\n if (!userRes.emailVerified) {\n fail('Your account is not verified, please verify your email first')\n return\n }\n\n // create API key\n const apiKeyName = `${os.hostname()}-${crypto.randomBytes(4).toString('hex')}`\n const createApiKeyResRaw = await createApiKey(host, apiKeyName, 'sdk_generated', accessToken)\n if (!createApiKeyResRaw.ok) {\n fail(`Failed to create API key: ${createApiKeyResRaw.status} ${createApiKeyResRaw.statusText}`)\n return\n }\n const createApiKeyRes = (await createApiKeyResRaw.json()) as { key: string }\n const apiKey = createApiKeyRes.key\n WriteKey(host, apiKey)\n\n res.end('Login success, please go back to CLI to continue')\n console.log(chalk.green('Login success, new API key: ' + apiKey))\n\n server.close()\n})\n\nasync function getToken(host: string, code: string) {\n const authConf = getAuthConfig(host)\n const params = new url.URLSearchParams({\n grant_type: 'authorization_code',\n client_id: authConf.clientId,\n code_verifier: authParams.codeVerifier,\n code: code,\n redirect_uri: `http://localhost:${authParams.serverPort}/callback`,\n })\n return fetch(`${authConf.domain}/oauth/token`, {\n method: 'POST',\n headers: {\n 'Content-Type': 'application/x-www-form-urlencoded',\n },\n body: params.toString(),\n })\n}\n\nasync function createApiKey(host: string, name: string, source: string, accessToken: string) {\n const createApiKeyUrl = new URL('/api/v1/keys', host)\n return fetch(createApiKeyUrl.href, {\n method: 'POST',\n headers: {\n Authorization: 'Bearer ' + accessToken,\n version: getCliVersion(),\n },\n body: JSON.stringify({\n name: name,\n scopes: ['write:project'],\n source: source,\n }),\n })\n}\n\nasync function getUser(host: string, accessToken: string) {\n const getUserUrl = new URL('/api/v1/users', host)\n return fetch(getUserUrl.href, {\n method: 'GET',\n headers: {\n Authorization: 'Bearer ' + accessToken,\n version: getCliVersion(),\n },\n })\n}\n"]}
|